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 |
}
|
|
167 |
|
|
168 |
if (sync_ref_counter) {
|
|
169 |
sync_ref_counter--;
|
|
170 |
} else {
|
|
171 |
sync_ref_counter = 9;
|
|
172 |
ecrt_master_sync_reference_clock(master, &app_time);
|
|
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 |
{
|
|
195 |
ec_slave_config_t *sc;
|
|
196 |
struct sigaction sa;
|
|
197 |
struct itimerval tv;
|
|
198 |
|
|
199 |
master = ecrt_request_master(0);
|
|
200 |
if (!master)
|
|
201 |
return -1;
|
|
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 |
#if 1
|
|
240 |
// configure SYNC signals for this slave
|
|
241 |
ecrt_slave_config_dc_assign_activate(sc, 0x0700);
|
|
242 |
ecrt_slave_config_dc_sync_cycle_times(sc, 10000000, 0);
|
|
243 |
ecrt_slave_config_dc_sync_shift_times(sc, 4400000, 0);
|
|
244 |
#endif
|
|
245 |
|
|
246 |
printf("Activating master...\n");
|
|
247 |
if (ecrt_master_activate(master))
|
|
248 |
return -1;
|
|
249 |
|
|
250 |
if (!(domain1_pd = ecrt_domain_data(domain1))) {
|
|
251 |
return -1;
|
|
252 |
}
|
|
253 |
|
|
254 |
#if PRIORITY
|
|
255 |
pid_t pid = getpid();
|
|
256 |
if (setpriority(PRIO_PROCESS, pid, -19))
|
|
257 |
fprintf(stderr, "Warning: Failed to set priority: %s\n",
|
|
258 |
strerror(errno));
|
|
259 |
#endif
|
|
260 |
|
|
261 |
sa.sa_handler = signal_handler;
|
|
262 |
sigemptyset(&sa.sa_mask);
|
|
263 |
sa.sa_flags = 0;
|
|
264 |
if (sigaction(SIGALRM, &sa, 0)) {
|
|
265 |
fprintf(stderr, "Failed to install signal handler!\n");
|
|
266 |
return -1;
|
|
267 |
}
|
|
268 |
|
|
269 |
printf("Starting timer...\n");
|
|
270 |
tv.it_interval.tv_sec = 0;
|
|
271 |
tv.it_interval.tv_usec = 1000000 / FREQUENCY;
|
|
272 |
tv.it_value.tv_sec = 0;
|
|
273 |
tv.it_value.tv_usec = 1000;
|
|
274 |
if (setitimer(ITIMER_REAL, &tv, NULL)) {
|
|
275 |
fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
|
|
276 |
return 1;
|
|
277 |
}
|
|
278 |
|
|
279 |
gettimeofday(&app_time, NULL);
|
|
280 |
|
|
281 |
printf("Started.\n");
|
|
282 |
while (1) {
|
|
283 |
pause();
|
|
284 |
|
|
285 |
#if 0
|
|
286 |
struct timeval t;
|
|
287 |
gettimeofday(&t, NULL);
|
|
288 |
printf("%u.%06u\n", t.tv_sec, t.tv_usec);
|
|
289 |
#endif
|
|
290 |
|
|
291 |
while (sig_alarms != user_alarms) {
|
|
292 |
cyclic_task();
|
|
293 |
user_alarms++;
|
|
294 |
}
|
|
295 |
}
|
|
296 |
|
|
297 |
return 0;
|
|
298 |
}
|
|
299 |
|
|
300 |
/****************************************************************************/
|