author | Florian Pose <fp@igh-essen.com> |
Mon, 15 Jun 2009 15:39:26 +0000 | |
changeset 1470 | cb424feef942 |
parent 1466 | 362147819ca1 |
child 1804 | 742607c464c4 |
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> |
|
38 |
||
39 |
/****************************************************************************/ |
|
40 |
||
41 |
#include "ecrt.h" |
|
42 |
||
43 |
/****************************************************************************/ |
|
44 |
||
45 |
// Application parameters |
|
46 |
#define FREQUENCY 100 |
|
47 |
#define PRIORITY 1 |
|
48 |
||
49 |
// Optional features |
|
50 |
#define CONFIGURE_PDOS 1 |
|
51 |
||
52 |
/****************************************************************************/ |
|
53 |
||
54 |
// EtherCAT |
|
55 |
static ec_master_t *master = NULL; |
|
56 |
static ec_master_state_t master_state = {}; |
|
57 |
||
58 |
static ec_domain_t *domain1 = NULL; |
|
59 |
static ec_domain_state_t domain1_state = {}; |
|
60 |
||
61 |
// Timer |
|
62 |
static unsigned int sig_alarms = 0; |
|
63 |
static unsigned int user_alarms = 0; |
|
64 |
||
65 |
/****************************************************************************/ |
|
66 |
||
67 |
// process data |
|
68 |
static uint8_t *domain1_pd = NULL; |
|
69 |
||
70 |
#define BusCouplerPos 0, 0 |
|
71 |
#define DigOutSlavePos 0, 1 |
|
72 |
#define CounterSlavePos 0, 2 |
|
73 |
||
74 |
#define Beckhoff_EK1100 0x00000002, 0x044c2c52 |
|
75 |
#define Beckhoff_EL2008 0x00000002, 0x07d83052 |
|
76 |
#define IDS_Counter 0x000012ad, 0x05de3052 |
|
77 |
||
78 |
// offsets for PDO entries |
|
79 |
static int off_dig_out; |
|
80 |
static int off_counter_in; |
|
81 |
static int off_counter_out; |
|
82 |
||
83 |
static unsigned int counter = 0; |
|
84 |
static unsigned int blink_counter = 0; |
|
85 |
static unsigned int blink = 0; |
|
86 |
static unsigned int sync_ref_counter = 0; |
|
87 |
struct timeval app_time; |
|
88 |
||
89 |
/*****************************************************************************/ |
|
90 |
||
91 |
void check_domain1_state(void) |
|
92 |
{ |
|
93 |
ec_domain_state_t ds; |
|
94 |
||
95 |
ecrt_domain_state(domain1, &ds); |
|
96 |
||
97 |
if (ds.working_counter != domain1_state.working_counter) |
|
98 |
printf("Domain1: WC %u.\n", ds.working_counter); |
|
99 |
if (ds.wc_state != domain1_state.wc_state) |
|
100 |
printf("Domain1: State %u.\n", ds.wc_state); |
|
101 |
||
102 |
domain1_state = ds; |
|
103 |
} |
|
104 |
||
105 |
/*****************************************************************************/ |
|
106 |
||
107 |
void check_master_state(void) |
|
108 |
{ |
|
109 |
ec_master_state_t ms; |
|
110 |
||
111 |
ecrt_master_state(master, &ms); |
|
112 |
||
113 |
if (ms.slaves_responding != master_state.slaves_responding) |
|
114 |
printf("%u slave(s).\n", ms.slaves_responding); |
|
115 |
if (ms.al_states != master_state.al_states) |
|
116 |
printf("AL states: 0x%02X.\n", ms.al_states); |
|
117 |
if (ms.link_up != master_state.link_up) |
|
118 |
printf("Link is %s.\n", ms.link_up ? "up" : "down"); |
|
119 |
||
120 |
master_state = ms; |
|
121 |
} |
|
122 |
||
123 |
/****************************************************************************/ |
|
124 |
||
125 |
void cyclic_task() |
|
126 |
{ |
|
127 |
int i; |
|
128 |
||
129 |
// receive process data |
|
130 |
ecrt_master_receive(master); |
|
131 |
ecrt_domain_process(domain1); |
|
132 |
||
133 |
// check process data state (optional) |
|
134 |
check_domain1_state(); |
|
135 |
||
136 |
if (counter) { |
|
137 |
counter--; |
|
138 |
} else { // do this at 1 Hz |
|
139 |
counter = FREQUENCY; |
|
140 |
||
141 |
// calculate new process data |
|
142 |
blink = !blink; |
|
143 |
||
144 |
// check for master state (optional) |
|
145 |
check_master_state(); |
|
146 |
||
147 |
} |
|
148 |
||
149 |
if (blink_counter) { |
|
150 |
blink_counter--; |
|
151 |
} else { |
|
152 |
blink_counter = 9; |
|
153 |
||
154 |
// calculate new process data |
|
155 |
blink = !blink; |
|
156 |
} |
|
157 |
||
158 |
// write process data |
|
159 |
EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99); |
|
160 |
EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02); |
|
161 |
||
162 |
app_time.tv_usec += 1000000 / FREQUENCY; |
|
163 |
if (app_time.tv_usec >= 1000000) { |
|
164 |
app_time.tv_usec -= 1000000; |
|
165 |
app_time.tv_sec++; |
|
166 |
} |
|
1466
362147819ca1
EC_TIMEVAL2NANO() takes no pointer argument any more.
Florian Pose <fp@igh-essen.com>
parents:
1449
diff
changeset
|
167 |
ecrt_master_application_time(master, EC_TIMEVAL2NANO(app_time)); |
1414 | 168 |
|
169 |
if (sync_ref_counter) { |
|
170 |
sync_ref_counter--; |
|
171 |
} else { |
|
172 |
sync_ref_counter = 9; |
|
1434
4c6fe0ae37f1
Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents:
1417
diff
changeset
|
173 |
ecrt_master_sync_reference_clock(master); |
1414 | 174 |
} |
175 |
ecrt_master_sync_slave_clocks(master); |
|
176 |
||
177 |
// send process data |
|
178 |
ecrt_domain_queue(domain1); |
|
179 |
ecrt_master_send(master); |
|
180 |
} |
|
181 |
||
182 |
/****************************************************************************/ |
|
183 |
||
184 |
void signal_handler(int signum) { |
|
185 |
switch (signum) { |
|
186 |
case SIGALRM: |
|
187 |
sig_alarms++; |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
/****************************************************************************/ |
|
193 |
||
194 |
int main(int argc, char **argv) |
|
195 |
{ |
|
196 |
ec_slave_config_t *sc; |
|
197 |
struct sigaction sa; |
|
198 |
struct itimerval tv; |
|
199 |
||
200 |
master = ecrt_request_master(0); |
|
201 |
if (!master) |
|
202 |
return -1; |
|
203 |
||
204 |
domain1 = ecrt_master_create_domain(master); |
|
205 |
if (!domain1) |
|
206 |
return -1; |
|
207 |
||
208 |
// Create configuration for bus coupler |
|
209 |
sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); |
|
210 |
if (!sc) |
|
211 |
return -1; |
|
212 |
||
213 |
if (!(sc = ecrt_master_slave_config(master, |
|
214 |
DigOutSlavePos, Beckhoff_EL2008))) { |
|
215 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
216 |
return -1; |
|
217 |
} |
|
218 |
||
219 |
off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
220 |
0x7000, 1, domain1, NULL); |
|
221 |
if (off_dig_out < 0) |
|
222 |
return -1; |
|
223 |
||
224 |
if (!(sc = ecrt_master_slave_config(master, |
|
225 |
CounterSlavePos, IDS_Counter))) { |
|
226 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
227 |
return -1; |
|
228 |
} |
|
229 |
||
230 |
off_counter_in = ecrt_slave_config_reg_pdo_entry(sc, |
|
231 |
0x6020, 0x11, domain1, NULL); |
|
232 |
if (off_counter_in < 0) |
|
233 |
return -1; |
|
234 |
||
235 |
off_counter_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
236 |
0x7020, 1, domain1, NULL); |
|
237 |
if (off_counter_out < 0) |
|
238 |
return -1; |
|
239 |
||
240 |
// configure SYNC signals for this slave |
|
1449
6d1ef8afeaab
Ported examples to new DC configuration function.
Florian Pose <fp@igh-essen.com>
parents:
1438
diff
changeset
|
241 |
ecrt_slave_config_dc(sc, 0x0700, 10000000, 4400000, 0, 0); |
1414 | 242 |
|
243 |
printf("Activating master...\n"); |
|
244 |
if (ecrt_master_activate(master)) |
|
245 |
return -1; |
|
246 |
||
247 |
if (!(domain1_pd = ecrt_domain_data(domain1))) { |
|
248 |
return -1; |
|
249 |
} |
|
250 |
||
251 |
#if PRIORITY |
|
252 |
pid_t pid = getpid(); |
|
253 |
if (setpriority(PRIO_PROCESS, pid, -19)) |
|
254 |
fprintf(stderr, "Warning: Failed to set priority: %s\n", |
|
255 |
strerror(errno)); |
|
256 |
#endif |
|
257 |
||
258 |
sa.sa_handler = signal_handler; |
|
259 |
sigemptyset(&sa.sa_mask); |
|
260 |
sa.sa_flags = 0; |
|
261 |
if (sigaction(SIGALRM, &sa, 0)) { |
|
262 |
fprintf(stderr, "Failed to install signal handler!\n"); |
|
263 |
return -1; |
|
264 |
} |
|
265 |
||
266 |
printf("Starting timer...\n"); |
|
267 |
tv.it_interval.tv_sec = 0; |
|
268 |
tv.it_interval.tv_usec = 1000000 / FREQUENCY; |
|
269 |
tv.it_value.tv_sec = 0; |
|
270 |
tv.it_value.tv_usec = 1000; |
|
271 |
if (setitimer(ITIMER_REAL, &tv, NULL)) { |
|
272 |
fprintf(stderr, "Failed to start timer: %s\n", strerror(errno)); |
|
273 |
return 1; |
|
274 |
} |
|
275 |
||
276 |
gettimeofday(&app_time, NULL); |
|
277 |
||
278 |
printf("Started.\n"); |
|
279 |
while (1) { |
|
280 |
pause(); |
|
281 |
||
282 |
#if 0 |
|
283 |
struct timeval t; |
|
284 |
gettimeofday(&t, NULL); |
|
285 |
printf("%u.%06u\n", t.tv_sec, t.tv_usec); |
|
286 |
#endif |
|
287 |
||
288 |
while (sig_alarms != user_alarms) { |
|
289 |
cyclic_task(); |
|
290 |
user_alarms++; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
return 0; |
|
295 |
} |
|
296 |
||
297 |
/****************************************************************************/ |