author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 05 Oct 2018 23:47:07 +0200 | |
branch | stable-1.5 |
changeset 2722 | 5e5bec5ee9c1 |
parent 2698 | 9e65f782e8a1 |
permissions | -rw-r--r-- |
1414 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
* Copyright (C) 2007-2009 Florian Pose, Ingenieurgemeinschaft IgH |
|
6 |
* |
|
7 |
* This file is part of the IgH EtherCAT Master. |
|
8 |
* |
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it and/or |
|
10 |
* modify it under the terms of the GNU General Public License version 2, as |
|
11 |
* published by the Free Software Foundation. |
|
12 |
* |
|
13 |
* The IgH EtherCAT Master is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
16 |
* Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License along |
|
19 |
* with the IgH EtherCAT Master; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 |
* |
|
22 |
* --- |
|
23 |
* |
|
24 |
* The license mentioned above concerns the source code only. Using the |
|
25 |
* EtherCAT technology and brand is only permitted in compliance with the |
|
26 |
* industrial property and similar rights of Beckhoff Automation GmbH. |
|
27 |
* |
|
28 |
****************************************************************************/ |
|
29 |
||
30 |
#include <errno.h> |
|
31 |
#include <signal.h> |
|
32 |
#include <stdio.h> |
|
33 |
#include <string.h> |
|
34 |
#include <sys/resource.h> |
|
35 |
#include <sys/time.h> |
|
36 |
#include <sys/types.h> |
|
37 |
#include <unistd.h> |
|
1970 | 38 |
#include <time.h> |
39 |
#include <sys/mman.h> |
|
40 |
#include <malloc.h> |
|
2698
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
41 |
#include <sched.h> /* sched_setscheduler() */ |
1414 | 42 |
|
43 |
/****************************************************************************/ |
|
44 |
||
45 |
#include "ecrt.h" |
|
46 |
||
47 |
/****************************************************************************/ |
|
48 |
||
49 |
// Application parameters |
|
1970 | 50 |
#define FREQUENCY 1000 |
2698
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
51 |
#define CLOCK_TO_USE CLOCK_MONOTONIC |
1970 | 52 |
#define MEASURE_TIMING |
53 |
||
54 |
/****************************************************************************/ |
|
55 |
||
56 |
#define NSEC_PER_SEC (1000000000L) |
|
57 |
#define PERIOD_NS (NSEC_PER_SEC / FREQUENCY) |
|
58 |
||
59 |
#define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + \ |
|
60 |
(B).tv_nsec - (A).tv_nsec) |
|
61 |
||
62 |
#define TIMESPEC2NS(T) ((uint64_t) (T).tv_sec * NSEC_PER_SEC + (T).tv_nsec) |
|
2421 | 63 |
|
1414 | 64 |
/****************************************************************************/ |
65 |
||
66 |
// EtherCAT |
|
67 |
static ec_master_t *master = NULL; |
|
68 |
static ec_master_state_t master_state = {}; |
|
69 |
||
70 |
static ec_domain_t *domain1 = NULL; |
|
71 |
static ec_domain_state_t domain1_state = {}; |
|
72 |
||
73 |
/****************************************************************************/ |
|
74 |
||
75 |
// process data |
|
76 |
static uint8_t *domain1_pd = NULL; |
|
77 |
||
78 |
#define BusCouplerPos 0, 0 |
|
79 |
#define DigOutSlavePos 0, 1 |
|
80 |
#define CounterSlavePos 0, 2 |
|
81 |
||
82 |
#define Beckhoff_EK1100 0x00000002, 0x044c2c52 |
|
83 |
#define Beckhoff_EL2008 0x00000002, 0x07d83052 |
|
84 |
#define IDS_Counter 0x000012ad, 0x05de3052 |
|
85 |
||
86 |
// offsets for PDO entries |
|
87 |
static int off_dig_out; |
|
88 |
static int off_counter_in; |
|
89 |
static int off_counter_out; |
|
90 |
||
91 |
static unsigned int counter = 0; |
|
92 |
static unsigned int blink = 0; |
|
93 |
static unsigned int sync_ref_counter = 0; |
|
1970 | 94 |
const struct timespec cycletime = {0, PERIOD_NS}; |
95 |
||
96 |
/*****************************************************************************/ |
|
97 |
||
98 |
struct timespec timespec_add(struct timespec time1, struct timespec time2) |
|
99 |
{ |
|
100 |
struct timespec result; |
|
101 |
||
102 |
if ((time1.tv_nsec + time2.tv_nsec) >= NSEC_PER_SEC) { |
|
103 |
result.tv_sec = time1.tv_sec + time2.tv_sec + 1; |
|
104 |
result.tv_nsec = time1.tv_nsec + time2.tv_nsec - NSEC_PER_SEC; |
|
105 |
} else { |
|
106 |
result.tv_sec = time1.tv_sec + time2.tv_sec; |
|
107 |
result.tv_nsec = time1.tv_nsec + time2.tv_nsec; |
|
108 |
} |
|
109 |
||
110 |
return result; |
|
111 |
} |
|
1414 | 112 |
|
113 |
/*****************************************************************************/ |
|
114 |
||
115 |
void check_domain1_state(void) |
|
116 |
{ |
|
117 |
ec_domain_state_t ds; |
|
118 |
||
119 |
ecrt_domain_state(domain1, &ds); |
|
120 |
||
121 |
if (ds.working_counter != domain1_state.working_counter) |
|
122 |
printf("Domain1: WC %u.\n", ds.working_counter); |
|
123 |
if (ds.wc_state != domain1_state.wc_state) |
|
124 |
printf("Domain1: State %u.\n", ds.wc_state); |
|
125 |
||
126 |
domain1_state = ds; |
|
127 |
} |
|
128 |
||
129 |
/*****************************************************************************/ |
|
130 |
||
131 |
void check_master_state(void) |
|
132 |
{ |
|
133 |
ec_master_state_t ms; |
|
134 |
||
135 |
ecrt_master_state(master, &ms); |
|
136 |
||
1970 | 137 |
if (ms.slaves_responding != master_state.slaves_responding) |
1414 | 138 |
printf("%u slave(s).\n", ms.slaves_responding); |
139 |
if (ms.al_states != master_state.al_states) |
|
140 |
printf("AL states: 0x%02X.\n", ms.al_states); |
|
141 |
if (ms.link_up != master_state.link_up) |
|
142 |
printf("Link is %s.\n", ms.link_up ? "up" : "down"); |
|
143 |
||
144 |
master_state = ms; |
|
145 |
} |
|
146 |
||
147 |
/****************************************************************************/ |
|
148 |
||
149 |
void cyclic_task() |
|
150 |
{ |
|
1970 | 151 |
struct timespec wakeupTime, time; |
152 |
#ifdef MEASURE_TIMING |
|
153 |
struct timespec startTime, endTime, lastStartTime = {}; |
|
154 |
uint32_t period_ns = 0, exec_ns = 0, latency_ns = 0, |
|
155 |
latency_min_ns = 0, latency_max_ns = 0, |
|
156 |
period_min_ns = 0, period_max_ns = 0, |
|
157 |
exec_min_ns = 0, exec_max_ns = 0; |
|
158 |
#endif |
|
159 |
||
160 |
// get current time |
|
161 |
clock_gettime(CLOCK_TO_USE, &wakeupTime); |
|
162 |
||
163 |
while(1) { |
|
164 |
wakeupTime = timespec_add(wakeupTime, cycletime); |
|
165 |
clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL); |
|
166 |
||
167 |
#ifdef MEASURE_TIMING |
|
168 |
clock_gettime(CLOCK_TO_USE, &startTime); |
|
169 |
latency_ns = DIFF_NS(wakeupTime, startTime); |
|
170 |
period_ns = DIFF_NS(lastStartTime, startTime); |
|
171 |
exec_ns = DIFF_NS(lastStartTime, endTime); |
|
172 |
lastStartTime = startTime; |
|
173 |
||
174 |
if (latency_ns > latency_max_ns) { |
|
175 |
latency_max_ns = latency_ns; |
|
176 |
} |
|
177 |
if (latency_ns < latency_min_ns) { |
|
178 |
latency_min_ns = latency_ns; |
|
179 |
} |
|
180 |
if (period_ns > period_max_ns) { |
|
181 |
period_max_ns = period_ns; |
|
182 |
} |
|
183 |
if (period_ns < period_min_ns) { |
|
184 |
period_min_ns = period_ns; |
|
185 |
} |
|
186 |
if (exec_ns > exec_max_ns) { |
|
187 |
exec_max_ns = exec_ns; |
|
188 |
} |
|
189 |
if (exec_ns < exec_min_ns) { |
|
190 |
exec_min_ns = exec_ns; |
|
191 |
} |
|
192 |
#endif |
|
193 |
||
194 |
// receive process data |
|
195 |
ecrt_master_receive(master); |
|
196 |
ecrt_domain_process(domain1); |
|
197 |
||
198 |
// check process data state (optional) |
|
199 |
check_domain1_state(); |
|
200 |
||
201 |
if (counter) { |
|
202 |
counter--; |
|
203 |
} else { // do this at 1 Hz |
|
204 |
counter = FREQUENCY; |
|
205 |
||
206 |
// check for master state (optional) |
|
207 |
check_master_state(); |
|
208 |
||
209 |
#ifdef MEASURE_TIMING |
|
210 |
// output timing stats |
|
211 |
printf("period %10u ... %10u\n", |
|
2421 | 212 |
period_min_ns, period_max_ns); |
1970 | 213 |
printf("exec %10u ... %10u\n", |
2421 | 214 |
exec_min_ns, exec_max_ns); |
1970 | 215 |
printf("latency %10u ... %10u\n", |
2421 | 216 |
latency_min_ns, latency_max_ns); |
1970 | 217 |
period_max_ns = 0; |
218 |
period_min_ns = 0xffffffff; |
|
219 |
exec_max_ns = 0; |
|
220 |
exec_min_ns = 0xffffffff; |
|
221 |
latency_max_ns = 0; |
|
222 |
latency_min_ns = 0xffffffff; |
|
223 |
#endif |
|
224 |
||
225 |
// calculate new process data |
|
226 |
blink = !blink; |
|
227 |
} |
|
228 |
||
229 |
// write process data |
|
230 |
EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99); |
|
231 |
EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02); |
|
232 |
||
233 |
// write application time to master |
|
234 |
clock_gettime(CLOCK_TO_USE, &time); |
|
1971
ba8a75cb1c98
Fixed TIMESPEC2NS() macro use.
Florian Pose <fp@igh-essen.com>
parents:
1970
diff
changeset
|
235 |
ecrt_master_application_time(master, TIMESPEC2NS(time)); |
1970 | 236 |
|
237 |
if (sync_ref_counter) { |
|
238 |
sync_ref_counter--; |
|
239 |
} else { |
|
240 |
sync_ref_counter = 1; // sync every cycle |
|
241 |
ecrt_master_sync_reference_clock(master); |
|
242 |
} |
|
243 |
ecrt_master_sync_slave_clocks(master); |
|
244 |
||
245 |
// send process data |
|
246 |
ecrt_domain_queue(domain1); |
|
247 |
ecrt_master_send(master); |
|
248 |
||
249 |
#ifdef MEASURE_TIMING |
|
250 |
clock_gettime(CLOCK_TO_USE, &endTime); |
|
251 |
#endif |
|
252 |
} |
|
1414 | 253 |
} |
254 |
||
255 |
/****************************************************************************/ |
|
256 |
||
257 |
int main(int argc, char **argv) |
|
258 |
{ |
|
1804 | 259 |
ec_slave_config_t *sc; |
1970 | 260 |
|
261 |
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { |
|
262 |
perror("mlockall failed"); |
|
263 |
return -1; |
|
264 |
} |
|
2421 | 265 |
|
1414 | 266 |
master = ecrt_request_master(0); |
1804 | 267 |
if (!master) |
268 |
return -1; |
|
1414 | 269 |
|
270 |
domain1 = ecrt_master_create_domain(master); |
|
271 |
if (!domain1) |
|
272 |
return -1; |
|
273 |
||
274 |
// Create configuration for bus coupler |
|
275 |
sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); |
|
276 |
if (!sc) |
|
277 |
return -1; |
|
278 |
||
279 |
if (!(sc = ecrt_master_slave_config(master, |
|
280 |
DigOutSlavePos, Beckhoff_EL2008))) { |
|
281 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
282 |
return -1; |
|
283 |
} |
|
284 |
||
285 |
off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
286 |
0x7000, 1, domain1, NULL); |
|
287 |
if (off_dig_out < 0) |
|
288 |
return -1; |
|
289 |
||
290 |
if (!(sc = ecrt_master_slave_config(master, |
|
291 |
CounterSlavePos, IDS_Counter))) { |
|
292 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
293 |
return -1; |
|
294 |
} |
|
295 |
||
296 |
off_counter_in = ecrt_slave_config_reg_pdo_entry(sc, |
|
297 |
0x6020, 0x11, domain1, NULL); |
|
298 |
if (off_counter_in < 0) |
|
299 |
return -1; |
|
300 |
||
301 |
off_counter_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
302 |
0x7020, 1, domain1, NULL); |
|
303 |
if (off_counter_out < 0) |
|
304 |
return -1; |
|
305 |
||
306 |
// configure SYNC signals for this slave |
|
1970 | 307 |
ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0); |
1414 | 308 |
|
309 |
printf("Activating master...\n"); |
|
310 |
if (ecrt_master_activate(master)) |
|
311 |
return -1; |
|
312 |
||
313 |
if (!(domain1_pd = ecrt_domain_data(domain1))) { |
|
314 |
return -1; |
|
315 |
} |
|
316 |
||
2698
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
317 |
/* Set priority */ |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
318 |
|
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
319 |
struct sched_param param = {}; |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
320 |
param.sched_priority = sched_get_priority_max(SCHED_FIFO); |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
321 |
|
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
322 |
printf("Using priority %i.", param.sched_priority); |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
323 |
if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
324 |
perror("sched_setscheduler failed"); |
9e65f782e8a1
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents:
2421
diff
changeset
|
325 |
} |
1970 | 326 |
|
327 |
printf("Starting cyclic function.\n"); |
|
328 |
cyclic_task(); |
|
2421 | 329 |
|
1970 | 330 |
return 0; |
331 |
} |
|
332 |
||
333 |
/****************************************************************************/ |