author | Martin Troxler <ch1010277@ch10pc446> |
Tue, 09 Nov 2010 11:12:35 +0100 | |
changeset 1997 | 65bcd7e304cf |
parent 1941 | e12266a4f473 |
child 1970 | 00e18cef9fc8 |
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 |
if (blink_counter) { |
|
149 |
blink_counter--; |
|
150 |
} else { |
|
151 |
blink_counter = 9; |
|
152 |
||
153 |
// calculate new process data |
|
154 |
blink = !blink; |
|
155 |
} |
|
156 |
||
157 |
// write process data |
|
158 |
EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99); |
|
159 |
EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02); |
|
160 |
||
161 |
app_time.tv_usec += 1000000 / FREQUENCY; |
|
162 |
if (app_time.tv_usec >= 1000000) { |
|
163 |
app_time.tv_usec -= 1000000; |
|
164 |
app_time.tv_sec++; |
|
165 |
} |
|
1466
362147819ca1
EC_TIMEVAL2NANO() takes no pointer argument any more.
Florian Pose <fp@igh-essen.com>
parents:
1449
diff
changeset
|
166 |
ecrt_master_application_time(master, EC_TIMEVAL2NANO(app_time)); |
1414 | 167 |
|
168 |
if (sync_ref_counter) { |
|
169 |
sync_ref_counter--; |
|
170 |
} else { |
|
171 |
sync_ref_counter = 9; |
|
1434
4c6fe0ae37f1
Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents:
1417
diff
changeset
|
172 |
ecrt_master_sync_reference_clock(master); |
1414 | 173 |
} |
174 |
ecrt_master_sync_slave_clocks(master); |
|
175 |
||
176 |
// send process data |
|
177 |
ecrt_domain_queue(domain1); |
|
178 |
ecrt_master_send(master); |
|
179 |
} |
|
180 |
||
181 |
/****************************************************************************/ |
|
182 |
||
183 |
void signal_handler(int signum) { |
|
184 |
switch (signum) { |
|
185 |
case SIGALRM: |
|
186 |
sig_alarms++; |
|
187 |
break; |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
/****************************************************************************/ |
|
192 |
||
193 |
int main(int argc, char **argv) |
|
194 |
{ |
|
1804 | 195 |
ec_slave_config_t *sc; |
1414 | 196 |
struct sigaction sa; |
197 |
struct itimerval tv; |
|
198 |
||
199 |
master = ecrt_request_master(0); |
|
1804 | 200 |
if (!master) |
201 |
return -1; |
|
1414 | 202 |
|
203 |
domain1 = ecrt_master_create_domain(master); |
|
204 |
if (!domain1) |
|
205 |
return -1; |
|
206 |
||
207 |
// Create configuration for bus coupler |
|
208 |
sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); |
|
209 |
if (!sc) |
|
210 |
return -1; |
|
211 |
||
212 |
if (!(sc = ecrt_master_slave_config(master, |
|
213 |
DigOutSlavePos, Beckhoff_EL2008))) { |
|
214 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
215 |
return -1; |
|
216 |
} |
|
217 |
||
218 |
off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
219 |
0x7000, 1, domain1, NULL); |
|
220 |
if (off_dig_out < 0) |
|
221 |
return -1; |
|
222 |
||
223 |
if (!(sc = ecrt_master_slave_config(master, |
|
224 |
CounterSlavePos, IDS_Counter))) { |
|
225 |
fprintf(stderr, "Failed to get slave configuration.\n"); |
|
226 |
return -1; |
|
227 |
} |
|
228 |
||
229 |
off_counter_in = ecrt_slave_config_reg_pdo_entry(sc, |
|
230 |
0x6020, 0x11, domain1, NULL); |
|
231 |
if (off_counter_in < 0) |
|
232 |
return -1; |
|
233 |
||
234 |
off_counter_out = ecrt_slave_config_reg_pdo_entry(sc, |
|
235 |
0x7020, 1, domain1, NULL); |
|
236 |
if (off_counter_out < 0) |
|
237 |
return -1; |
|
238 |
||
239 |
// 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
|
240 |
ecrt_slave_config_dc(sc, 0x0700, 10000000, 4400000, 0, 0); |
1414 | 241 |
|
242 |
printf("Activating master...\n"); |
|
243 |
if (ecrt_master_activate(master)) |
|
244 |
return -1; |
|
245 |
||
246 |
if (!(domain1_pd = ecrt_domain_data(domain1))) { |
|
247 |
return -1; |
|
248 |
} |
|
249 |
||
250 |
#if PRIORITY |
|
251 |
pid_t pid = getpid(); |
|
252 |
if (setpriority(PRIO_PROCESS, pid, -19)) |
|
253 |
fprintf(stderr, "Warning: Failed to set priority: %s\n", |
|
254 |
strerror(errno)); |
|
255 |
#endif |
|
256 |
||
257 |
sa.sa_handler = signal_handler; |
|
258 |
sigemptyset(&sa.sa_mask); |
|
259 |
sa.sa_flags = 0; |
|
260 |
if (sigaction(SIGALRM, &sa, 0)) { |
|
261 |
fprintf(stderr, "Failed to install signal handler!\n"); |
|
262 |
return -1; |
|
263 |
} |
|
264 |
||
265 |
printf("Starting timer...\n"); |
|
266 |
tv.it_interval.tv_sec = 0; |
|
267 |
tv.it_interval.tv_usec = 1000000 / FREQUENCY; |
|
268 |
tv.it_value.tv_sec = 0; |
|
269 |
tv.it_value.tv_usec = 1000; |
|
270 |
if (setitimer(ITIMER_REAL, &tv, NULL)) { |
|
271 |
fprintf(stderr, "Failed to start timer: %s\n", strerror(errno)); |
|
272 |
return 1; |
|
273 |
} |
|
274 |
||
275 |
gettimeofday(&app_time, NULL); |
|
276 |
||
277 |
printf("Started.\n"); |
|
278 |
while (1) { |
|
279 |
pause(); |
|
280 |
||
281 |
#if 0 |
|
282 |
struct timeval t; |
|
283 |
gettimeofday(&t, NULL); |
|
284 |
printf("%u.%06u\n", t.tv_sec, t.tv_usec); |
|
285 |
#endif |
|
286 |
||
287 |
while (sig_alarms != user_alarms) { |
|
288 |
cyclic_task(); |
|
289 |
user_alarms++; |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
return 0; |
|
294 |
} |
|
295 |
||
296 |
/****************************************************************************/ |