author | Florian Pose <fp@igh.de> |
Mon, 04 Sep 2017 17:12:59 +0200 | |
branch | stable-1.5 |
changeset 2681 | 9953c0d372ab |
parent 2421 | bc2d4bf9cbe5 |
child 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> |
|
1414 | 41 |
|
42 |
/****************************************************************************/ |
|
43 |
||
44 |
#include "ecrt.h" |
|
45 |
||
46 |
/****************************************************************************/ |
|
47 |
||
48 |
// Application parameters |
|
1970 | 49 |
#define FREQUENCY 1000 |
50 |
#define CLOCK_TO_USE CLOCK_REALTIME |
|
51 |
#define MEASURE_TIMING |
|
52 |
||
53 |
/****************************************************************************/ |
|
54 |
||
55 |
#define NSEC_PER_SEC (1000000000L) |
|
56 |
#define PERIOD_NS (NSEC_PER_SEC / FREQUENCY) |
|
57 |
||
58 |
#define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + \ |
|
59 |
(B).tv_nsec - (A).tv_nsec) |
|
60 |
||
61 |
#define TIMESPEC2NS(T) ((uint64_t) (T).tv_sec * NSEC_PER_SEC + (T).tv_nsec) |
|
2421 | 62 |
|
1414 | 63 |
/****************************************************************************/ |
64 |
||
65 |
// EtherCAT |
|
66 |
static ec_master_t *master = NULL; |
|
67 |
static ec_master_state_t master_state = {}; |
|
68 |
||
69 |
static ec_domain_t *domain1 = NULL; |
|
70 |
static ec_domain_state_t domain1_state = {}; |
|
71 |
||
72 |
/****************************************************************************/ |
|
73 |
||
74 |
// process data |
|
75 |
static uint8_t *domain1_pd = NULL; |
|
76 |
||
77 |
#define BusCouplerPos 0, 0 |
|
78 |
#define DigOutSlavePos 0, 1 |
|
79 |
#define CounterSlavePos 0, 2 |
|
80 |
||
81 |
#define Beckhoff_EK1100 0x00000002, 0x044c2c52 |
|
82 |
#define Beckhoff_EL2008 0x00000002, 0x07d83052 |
|
83 |
#define IDS_Counter 0x000012ad, 0x05de3052 |
|
84 |
||
85 |
// offsets for PDO entries |
|
86 |
static int off_dig_out; |
|
87 |
static int off_counter_in; |
|
88 |
static int off_counter_out; |
|
89 |
||
90 |
static unsigned int counter = 0; |
|
91 |
static unsigned int blink = 0; |
|
92 |
static unsigned int sync_ref_counter = 0; |
|
1970 | 93 |
const struct timespec cycletime = {0, PERIOD_NS}; |
94 |
||
95 |
/*****************************************************************************/ |
|
96 |
||
97 |
struct timespec timespec_add(struct timespec time1, struct timespec time2) |
|
98 |
{ |
|
99 |
struct timespec result; |
|
100 |
||
101 |
if ((time1.tv_nsec + time2.tv_nsec) >= NSEC_PER_SEC) { |
|
102 |
result.tv_sec = time1.tv_sec + time2.tv_sec + 1; |
|
103 |
result.tv_nsec = time1.tv_nsec + time2.tv_nsec - NSEC_PER_SEC; |
|
104 |
} else { |
|
105 |
result.tv_sec = time1.tv_sec + time2.tv_sec; |
|
106 |
result.tv_nsec = time1.tv_nsec + time2.tv_nsec; |
|
107 |
} |
|
108 |
||
109 |
return result; |
|
110 |
} |
|
1414 | 111 |
|
112 |
/*****************************************************************************/ |
|
113 |
||
114 |
void check_domain1_state(void) |
|
115 |
{ |
|
116 |
ec_domain_state_t ds; |
|
117 |
||
118 |
ecrt_domain_state(domain1, &ds); |
|
119 |
||
120 |
if (ds.working_counter != domain1_state.working_counter) |
|
121 |
printf("Domain1: WC %u.\n", ds.working_counter); |
|
122 |
if (ds.wc_state != domain1_state.wc_state) |
|
123 |
printf("Domain1: State %u.\n", ds.wc_state); |
|
124 |
||
125 |
domain1_state = ds; |
|
126 |
} |
|
127 |
||
128 |
/*****************************************************************************/ |
|
129 |
||
130 |
void check_master_state(void) |
|
131 |
{ |
|
132 |
ec_master_state_t ms; |
|
133 |
||
134 |
ecrt_master_state(master, &ms); |
|
135 |
||
1970 | 136 |
if (ms.slaves_responding != master_state.slaves_responding) |
1414 | 137 |
printf("%u slave(s).\n", ms.slaves_responding); |
138 |
if (ms.al_states != master_state.al_states) |
|
139 |
printf("AL states: 0x%02X.\n", ms.al_states); |
|
140 |
if (ms.link_up != master_state.link_up) |
|
141 |
printf("Link is %s.\n", ms.link_up ? "up" : "down"); |
|
142 |
||
143 |
master_state = ms; |
|
144 |
} |
|
145 |
||
146 |
/****************************************************************************/ |
|
147 |
||
148 |
void cyclic_task() |
|
149 |
{ |
|
1970 | 150 |
struct timespec wakeupTime, time; |
151 |
#ifdef MEASURE_TIMING |
|
152 |
struct timespec startTime, endTime, lastStartTime = {}; |
|
153 |
uint32_t period_ns = 0, exec_ns = 0, latency_ns = 0, |
|
154 |
latency_min_ns = 0, latency_max_ns = 0, |
|
155 |
period_min_ns = 0, period_max_ns = 0, |
|
156 |
exec_min_ns = 0, exec_max_ns = 0; |
|
157 |
#endif |
|
158 |
||
159 |
// get current time |
|
160 |
clock_gettime(CLOCK_TO_USE, &wakeupTime); |
|
161 |
||
162 |
while(1) { |
|
163 |
wakeupTime = timespec_add(wakeupTime, cycletime); |
|
164 |
clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL); |
|
165 |
||
166 |
#ifdef MEASURE_TIMING |
|
167 |
clock_gettime(CLOCK_TO_USE, &startTime); |
|
168 |
latency_ns = DIFF_NS(wakeupTime, startTime); |
|
169 |
period_ns = DIFF_NS(lastStartTime, startTime); |
|
170 |
exec_ns = DIFF_NS(lastStartTime, endTime); |
|
171 |
lastStartTime = startTime; |
|
172 |
||
173 |
if (latency_ns > latency_max_ns) { |
|
174 |
latency_max_ns = latency_ns; |
|
175 |
} |
|
176 |
if (latency_ns < latency_min_ns) { |
|
177 |
latency_min_ns = latency_ns; |
|
178 |
} |
|
179 |
if (period_ns > period_max_ns) { |
|
180 |
period_max_ns = period_ns; |
|
181 |
} |
|
182 |
if (period_ns < period_min_ns) { |
|
183 |
period_min_ns = period_ns; |
|
184 |
} |
|
185 |
if (exec_ns > exec_max_ns) { |
|
186 |
exec_max_ns = exec_ns; |
|
187 |
} |
|
188 |
if (exec_ns < exec_min_ns) { |
|
189 |
exec_min_ns = exec_ns; |
|
190 |
} |
|
191 |
#endif |
|
192 |
||
193 |
// receive process data |
|
194 |
ecrt_master_receive(master); |
|
195 |
ecrt_domain_process(domain1); |
|
196 |
||
197 |
// check process data state (optional) |
|
198 |
check_domain1_state(); |
|
199 |
||
200 |
if (counter) { |
|
201 |
counter--; |
|
202 |
} else { // do this at 1 Hz |
|
203 |
counter = FREQUENCY; |
|
204 |
||
205 |
// check for master state (optional) |
|
206 |
check_master_state(); |
|
207 |
||
208 |
#ifdef MEASURE_TIMING |
|
209 |
// output timing stats |
|
210 |
printf("period %10u ... %10u\n", |
|
2421 | 211 |
period_min_ns, period_max_ns); |
1970 | 212 |
printf("exec %10u ... %10u\n", |
2421 | 213 |
exec_min_ns, exec_max_ns); |
1970 | 214 |
printf("latency %10u ... %10u\n", |
2421 | 215 |
latency_min_ns, latency_max_ns); |
1970 | 216 |
period_max_ns = 0; |
217 |
period_min_ns = 0xffffffff; |
|
218 |
exec_max_ns = 0; |
|
219 |
exec_min_ns = 0xffffffff; |
|
220 |
latency_max_ns = 0; |
|
221 |
latency_min_ns = 0xffffffff; |
|
222 |
#endif |
|
223 |
||
224 |
// calculate new process data |
|
225 |
blink = !blink; |
|
226 |
} |
|
227 |
||
228 |
// write process data |
|
229 |
EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99); |
|
230 |
EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02); |
|
231 |
||
232 |
// write application time to master |
|
233 |
clock_gettime(CLOCK_TO_USE, &time); |
|
1971
ba8a75cb1c98
Fixed TIMESPEC2NS() macro use.
Florian Pose <fp@igh-essen.com>
parents:
1970
diff
changeset
|
234 |
ecrt_master_application_time(master, TIMESPEC2NS(time)); |
1970 | 235 |
|
236 |
if (sync_ref_counter) { |
|
237 |
sync_ref_counter--; |
|
238 |
} else { |
|
239 |
sync_ref_counter = 1; // sync every cycle |
|
240 |
ecrt_master_sync_reference_clock(master); |
|
241 |
} |
|
242 |
ecrt_master_sync_slave_clocks(master); |
|
243 |
||
244 |
// send process data |
|
245 |
ecrt_domain_queue(domain1); |
|
246 |
ecrt_master_send(master); |
|
247 |
||
248 |
#ifdef MEASURE_TIMING |
|
249 |
clock_gettime(CLOCK_TO_USE, &endTime); |
|
250 |
#endif |
|
251 |
} |
|
1414 | 252 |
} |
253 |
||
254 |
/****************************************************************************/ |
|
255 |
||
256 |
int main(int argc, char **argv) |
|
257 |
{ |
|
1804 | 258 |
ec_slave_config_t *sc; |
1970 | 259 |
|
260 |
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { |
|
261 |
perror("mlockall failed"); |
|
262 |
return -1; |
|
263 |
} |
|
2421 | 264 |
|
1414 | 265 |
master = ecrt_request_master(0); |
1804 | 266 |
if (!master) |
267 |
return -1; |
|
1414 | 268 |
|
269 |
domain1 = ecrt_master_create_domain(master); |
|
270 |
if (!domain1) |
|
271 |
return -1; |
|
272 |
||
273 |
// Create configuration for bus coupler |
|
274 |
sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); |
|
275 |
if (!sc) |
|
276 |
return -1; |
|
277 |
||
278 |
if (!(sc = ecrt_master_slave_config(master, |
|
279 |
DigOutSlavePos, Beckhoff_EL2008))) { |
|
280 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
281 |
return -1; |
|
282 |
} |
|
283 |
||
284 |
off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
285 |
0x7000, 1, domain1, NULL); |
|
286 |
if (off_dig_out < 0) |
|
287 |
return -1; |
|
288 |
||
289 |
if (!(sc = ecrt_master_slave_config(master, |
|
290 |
CounterSlavePos, IDS_Counter))) { |
|
291 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
292 |
return -1; |
|
293 |
} |
|
294 |
||
295 |
off_counter_in = ecrt_slave_config_reg_pdo_entry(sc, |
|
296 |
0x6020, 0x11, domain1, NULL); |
|
297 |
if (off_counter_in < 0) |
|
298 |
return -1; |
|
299 |
||
300 |
off_counter_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
301 |
0x7020, 1, domain1, NULL); |
|
302 |
if (off_counter_out < 0) |
|
303 |
return -1; |
|
304 |
||
305 |
// configure SYNC signals for this slave |
|
1970 | 306 |
ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0); |
1414 | 307 |
|
308 |
printf("Activating master...\n"); |
|
309 |
if (ecrt_master_activate(master)) |
|
310 |
return -1; |
|
311 |
||
312 |
if (!(domain1_pd = ecrt_domain_data(domain1))) { |
|
313 |
return -1; |
|
314 |
} |
|
315 |
||
316 |
pid_t pid = getpid(); |
|
317 |
if (setpriority(PRIO_PROCESS, pid, -19)) |
|
318 |
fprintf(stderr, "Warning: Failed to set priority: %s\n", |
|
319 |
strerror(errno)); |
|
1970 | 320 |
|
321 |
printf("Starting cyclic function.\n"); |
|
322 |
cyclic_task(); |
|
2421 | 323 |
|
1970 | 324 |
return 0; |
325 |
} |
|
326 |
||
327 |
/****************************************************************************/ |