1 //--------------------------------------------------------------- |
|
2 // |
|
3 // m a i n _ g u i . c p p |
|
4 // |
|
5 // $LastChangedDate$ |
|
6 // $Author$ |
|
7 // |
|
8 //--------------------------------------------------------------- |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <string.h> // memset() |
|
12 #include <unistd.h> // usleep() |
|
13 #include <signal.h> |
|
14 |
|
15 #include <fltk/Window.h> |
|
16 #include <fltk/Slider.h> |
|
17 #include <fltk/ValueOutput.h> |
|
18 #include <fltk/FillSlider.h> |
|
19 #include <fltk/CheckButton.h> |
|
20 #include <fltk/run.h> |
|
21 using namespace fltk; |
|
22 |
|
23 #include "ec_globals.h" |
|
24 #include "ec_master.h" |
|
25 |
|
26 #define SLIDER_UPDATE_CYCLE 0.02 |
|
27 #define VALUES_UPDATE_CYCLE 0.50 |
|
28 |
|
29 //--------------------------------------------------------------- |
|
30 |
|
31 unsigned short int write_value; |
|
32 signed short int read_value; |
|
33 unsigned char dig_value; |
|
34 |
|
35 void write_data(unsigned char *); |
|
36 void read_data(unsigned char *); |
|
37 |
|
38 void slider_write_callback(Widget *, void *); |
|
39 void slider_read_timeout(void *); |
|
40 void values_timeout(void *); |
|
41 |
|
42 Window *window; |
|
43 Slider *slider_read, *slider_write; |
|
44 ValueOutput *output_cycle, *output_jitter, *output_work, *output_busy, *output_bus; |
|
45 CheckButton *check1, *check2, *check3, *check4; |
|
46 EtherCAT_master_t master; |
|
47 |
|
48 double max_cycle, max_jitter, max_work, max_busy, max_bus; |
|
49 |
|
50 //--------------------------------------------------------------- |
|
51 |
|
52 #define SLAVE_COUNT 7 |
|
53 |
|
54 EtherCAT_slave_t slaves[SLAVE_COUNT] = |
|
55 { |
|
56 ECAT_INIT_SLAVE(Beckhoff_EK1100), |
|
57 ECAT_INIT_SLAVE(Beckhoff_EL4102), |
|
58 ECAT_INIT_SLAVE(Beckhoff_EL3162), |
|
59 ECAT_INIT_SLAVE(Beckhoff_EL1014), |
|
60 ECAT_INIT_SLAVE(Beckhoff_EL5001), |
|
61 ECAT_INIT_SLAVE(Beckhoff_EL2004), |
|
62 ECAT_INIT_SLAVE(Beckhoff_EL3102) |
|
63 }; |
|
64 |
|
65 //--------------------------------------------------------------- |
|
66 |
|
67 int main(int argc, char **argv) |
|
68 { |
|
69 //unsigned int i; |
|
70 EtherCAT_slave_t *buskoppler, *input, *output, *dig_in, *dig_out; |
|
71 struct sched_param sched; |
|
72 |
|
73 printf("CatEther-Testprogramm.\n\n"); |
|
74 |
|
75 //---------- |
|
76 |
|
77 #if 1 |
|
78 printf("Setting highest task priority...\n"); |
|
79 |
|
80 sched.sched_priority = sched_get_priority_max(SCHED_RR); |
|
81 if (sched_setscheduler(0, SCHED_RR, &sched) == -1) |
|
82 { |
|
83 fprintf(stderr, "ERROR: Could not set priority: %s\n", strerror(errno)); |
|
84 return -1; |
|
85 } |
|
86 #endif |
|
87 |
|
88 //---------- |
|
89 |
|
90 printf("Initializing master...\n"); |
|
91 EtherCAT_master_init(&master, "eth1"); |
|
92 |
|
93 printf("Checking slaves...\n"); |
|
94 if (EtherCAT_check_slaves(&master, slaves, SLAVE_COUNT) != 0) |
|
95 { |
|
96 fprintf(stderr, "ERROR while searching for slaves!\n"); |
|
97 return -1; |
|
98 } |
|
99 |
|
100 //---------- |
|
101 |
|
102 // Check for slaves |
|
103 |
|
104 buskoppler = &slaves[0]; |
|
105 output = &slaves[1]; |
|
106 dig_in = &slaves[3]; |
|
107 dig_out = &slaves[5]; |
|
108 input = &slaves[6]; |
|
109 |
|
110 // Set Mapping addresses |
|
111 |
|
112 output->logical_address0 = 0x00000000; |
|
113 input->logical_address0 = 0x00000004; |
|
114 dig_in->logical_address0 = 0x0000000F; |
|
115 dig_out->logical_address0 = 0x0000000E; |
|
116 |
|
117 //---------- |
|
118 |
|
119 printf("Init output slave...\n"); |
|
120 |
|
121 if (EtherCAT_activate_slave(&master, output) != 0) |
|
122 { |
|
123 fprintf(stderr, "ERROR: Could not init slave!\n"); |
|
124 return -1; |
|
125 } |
|
126 |
|
127 printf("Init input slave...\n"); |
|
128 |
|
129 if (EtherCAT_activate_slave(&master, input) != 0) |
|
130 { |
|
131 fprintf(stderr, "ERROR: Could not init slave!\n"); |
|
132 return -1; |
|
133 } |
|
134 |
|
135 printf("Init digital input slave...\n"); |
|
136 |
|
137 if (EtherCAT_activate_slave(&master, dig_in) != 0) |
|
138 { |
|
139 fprintf(stderr, "ERROR: Could not init slave!\n"); |
|
140 return -1; |
|
141 } |
|
142 |
|
143 printf("Init digital output slave...\n"); |
|
144 |
|
145 if (EtherCAT_activate_slave(&master, dig_out) != 0) |
|
146 { |
|
147 fprintf(stderr, "ERROR: Could not init slave!\n"); |
|
148 return -1; |
|
149 } |
|
150 |
|
151 //---------- |
|
152 |
|
153 printf("Starting FLTK window...\n"); |
|
154 |
|
155 window = new Window(300, 300); |
|
156 window->begin(); |
|
157 |
|
158 slider_read = new FillSlider(50, 10, 40, 280); |
|
159 slider_read->set_vertical(); |
|
160 slider_read->buttoncolor(BLUE); |
|
161 |
|
162 slider_read->deactivate(); |
|
163 |
|
164 slider_write = new Slider(110, 10, 40, 280); |
|
165 slider_write->set_vertical(); |
|
166 slider_write->callback(slider_write_callback, NULL); |
|
167 |
|
168 output_cycle = new ValueOutput(200, 50, 90, 25, "Cycle time [µs]"); |
|
169 output_cycle->align(ALIGN_LEFT | ALIGN_TOP); |
|
170 |
|
171 output_jitter = new ValueOutput(200, 90, 90, 25, "Jitter [%]"); |
|
172 output_jitter->align(ALIGN_LEFT | ALIGN_TOP); |
|
173 |
|
174 output_work = new ValueOutput(200, 130, 90, 25, "Work time [µs]"); |
|
175 output_work->align(ALIGN_LEFT | ALIGN_TOP); |
|
176 |
|
177 output_busy = new ValueOutput(200, 170, 90, 25, "Busy rate [%]"); |
|
178 output_busy->align(ALIGN_LEFT | ALIGN_TOP); |
|
179 |
|
180 output_bus = new ValueOutput(200, 210, 90, 25, "Bus time [µs]"); |
|
181 output_bus->align(ALIGN_LEFT | ALIGN_TOP); |
|
182 |
|
183 check1 = new CheckButton(200, 240, 30, 25, "1"); |
|
184 check2 = new CheckButton(250, 240, 30, 25, "2"); |
|
185 check3 = new CheckButton(200, 270, 30, 25, "3"); |
|
186 check4 = new CheckButton(250, 270, 30, 25, "4"); |
|
187 |
|
188 // output_cycle = new Output(200, 35, 90, 25); |
|
189 |
|
190 window->end(); |
|
191 window->show(); |
|
192 |
|
193 add_timeout(SLIDER_UPDATE_CYCLE, slider_read_timeout, NULL); |
|
194 add_timeout(VALUES_UPDATE_CYCLE, values_timeout, NULL); |
|
195 |
|
196 printf("Starting thread...\n"); |
|
197 |
|
198 if (EtherCAT_start(&master, 20, write_data, read_data, 10000) != 0) |
|
199 { |
|
200 return -1; |
|
201 } |
|
202 |
|
203 run(); // Start FLTK loop |
|
204 |
|
205 remove_timeout(slider_read_timeout, NULL); |
|
206 remove_timeout(values_timeout, NULL); |
|
207 |
|
208 printf("Stopping master thread...\n"); |
|
209 EtherCAT_stop(&master); |
|
210 |
|
211 printf("Deactivating slaves...\n"); |
|
212 |
|
213 EtherCAT_deactivate_slave(&master, dig_out); |
|
214 EtherCAT_deactivate_slave(&master, dig_in); |
|
215 EtherCAT_deactivate_slave(&master, input); |
|
216 EtherCAT_deactivate_slave(&master, output); |
|
217 EtherCAT_deactivate_slave(&master, buskoppler); |
|
218 |
|
219 EtherCAT_master_clear(&master); |
|
220 |
|
221 printf("Finished.\n"); |
|
222 |
|
223 return 0; |
|
224 } |
|
225 |
|
226 //--------------------------------------------------------------- |
|
227 |
|
228 void write_data(unsigned char *data) |
|
229 { |
|
230 data[0] = write_value & 0xFF; |
|
231 data[1] = (write_value & 0xFF00) >> 8; |
|
232 |
|
233 data[14] = (write_value * 16 / 32767) & 0x0F; |
|
234 } |
|
235 |
|
236 //--------------------------------------------------------------- |
|
237 |
|
238 void read_data(unsigned char *data) |
|
239 { |
|
240 read_value = data[5] | data[6] << 8; |
|
241 dig_value = data[15]; |
|
242 } |
|
243 |
|
244 //--------------------------------------------------------------- |
|
245 |
|
246 void slider_read_timeout(void *data) |
|
247 { |
|
248 slider_read->value((double) read_value / 65536 + 0.5); |
|
249 slider_read->redraw(); |
|
250 |
|
251 check1->value(dig_value & 1); |
|
252 check2->value(dig_value & 2); |
|
253 check3->value(dig_value & 4); |
|
254 check4->value(dig_value & 8); |
|
255 |
|
256 if (max_cycle < master.last_cycle_time) max_cycle = master.last_cycle_time; |
|
257 if (max_jitter < master.last_jitter) max_jitter = master.last_jitter; |
|
258 if (max_work < master.last_cycle_work_time) max_work = master.last_cycle_work_time; |
|
259 if (max_busy < master.last_cycle_busy_rate) max_busy = master.last_cycle_busy_rate; |
|
260 if (max_bus < master.bus_time) max_bus = master.bus_time; |
|
261 |
|
262 repeat_timeout(SLIDER_UPDATE_CYCLE, slider_read_timeout, NULL); |
|
263 } |
|
264 |
|
265 //--------------------------------------------------------------- |
|
266 |
|
267 void values_timeout(void *data) |
|
268 { |
|
269 output_cycle->value(max_cycle * 1000000.0); |
|
270 output_jitter->value(max_jitter); |
|
271 output_work->value(max_work * 1000000.0); |
|
272 output_busy->value(max_busy); |
|
273 output_bus->value(max_bus * 1000000.0); |
|
274 |
|
275 max_cycle = max_jitter = max_work = max_busy = max_bus = 0.0; |
|
276 |
|
277 repeat_timeout(VALUES_UPDATE_CYCLE, values_timeout, NULL); |
|
278 } |
|
279 |
|
280 //--------------------------------------------------------------- |
|
281 |
|
282 void slider_write_callback(Widget *sender, void *data) |
|
283 { |
|
284 write_value = (short unsigned int) (32767 * slider_write->value() + 0.5); |
|
285 } |
|
286 |
|
287 //--------------------------------------------------------------- |
|