fp@0: //--------------------------------------------------------------- fp@0: // fp@0: // m a i n _ g u i . c p p fp@0: // fp@0: // $LastChangedDate$ fp@0: // $Author$ fp@0: // fp@0: //--------------------------------------------------------------- fp@0: fp@0: #include fp@0: #include // memset() fp@0: #include // usleep() fp@0: #include fp@0: fp@0: #include fp@0: #include fp@0: #include fp@0: #include fp@0: #include fp@0: #include fp@0: using namespace fltk; fp@0: fp@0: #include "ec_globals.h" fp@0: #include "ec_master.h" fp@0: fp@0: #define SLIDER_UPDATE_CYCLE 0.02 fp@0: #define VALUES_UPDATE_CYCLE 0.50 fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: unsigned short int write_value; fp@0: signed short int read_value; fp@0: unsigned char dig_value; fp@0: fp@0: void write_data(unsigned char *); fp@0: void read_data(unsigned char *); fp@0: fp@0: void slider_write_callback(Widget *, void *); fp@0: void slider_read_timeout(void *); fp@0: void values_timeout(void *); fp@0: fp@0: Window *window; fp@0: Slider *slider_read, *slider_write; fp@0: ValueOutput *output_cycle, *output_jitter, *output_work, *output_busy, *output_bus; fp@0: CheckButton *check1, *check2, *check3, *check4; fp@0: EtherCAT_master_t master; fp@0: fp@0: double max_cycle, max_jitter, max_work, max_busy, max_bus; fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: #define SLAVE_COUNT 7 fp@0: fp@0: EtherCAT_slave_t slaves[SLAVE_COUNT] = fp@0: { fp@0: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL5001), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3102) fp@0: }; fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: int main(int argc, char **argv) fp@0: { fp@0: //unsigned int i; fp@0: EtherCAT_slave_t *buskoppler, *input, *output, *dig_in, *dig_out; fp@0: struct sched_param sched; fp@0: fp@0: printf("CatEther-Testprogramm.\n\n"); fp@0: fp@0: //---------- fp@0: fp@0: #if 1 fp@0: printf("Setting highest task priority...\n"); fp@0: fp@0: sched.sched_priority = sched_get_priority_max(SCHED_RR); fp@0: if (sched_setscheduler(0, SCHED_RR, &sched) == -1) fp@0: { fp@0: fprintf(stderr, "ERROR: Could not set priority: %s\n", strerror(errno)); fp@0: return -1; fp@0: } fp@0: #endif fp@0: fp@0: //---------- fp@0: fp@0: printf("Initializing master...\n"); fp@0: EtherCAT_master_init(&master, "eth1"); fp@0: fp@0: printf("Checking slaves...\n"); fp@0: if (EtherCAT_check_slaves(&master, slaves, SLAVE_COUNT) != 0) fp@0: { fp@0: fprintf(stderr, "ERROR while searching for slaves!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: //---------- fp@0: fp@0: // Check for slaves fp@0: fp@0: buskoppler = &slaves[0]; fp@0: output = &slaves[1]; fp@0: dig_in = &slaves[3]; fp@0: dig_out = &slaves[5]; fp@0: input = &slaves[6]; fp@0: fp@0: // Set Mapping addresses fp@0: fp@0: output->logical_address0 = 0x00000000; fp@0: input->logical_address0 = 0x00000004; fp@0: dig_in->logical_address0 = 0x0000000F; fp@0: dig_out->logical_address0 = 0x0000000E; fp@0: fp@0: //---------- fp@0: fp@0: printf("Init output slave...\n"); fp@0: fp@0: if (EtherCAT_activate_slave(&master, output) != 0) fp@0: { fp@0: fprintf(stderr, "ERROR: Could not init slave!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: printf("Init input slave...\n"); fp@0: fp@0: if (EtherCAT_activate_slave(&master, input) != 0) fp@0: { fp@0: fprintf(stderr, "ERROR: Could not init slave!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: printf("Init digital input slave...\n"); fp@0: fp@0: if (EtherCAT_activate_slave(&master, dig_in) != 0) fp@0: { fp@0: fprintf(stderr, "ERROR: Could not init slave!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: printf("Init digital output slave...\n"); fp@0: fp@0: if (EtherCAT_activate_slave(&master, dig_out) != 0) fp@0: { fp@0: fprintf(stderr, "ERROR: Could not init slave!\n"); fp@0: return -1; fp@0: } fp@0: fp@0: //---------- fp@0: fp@0: printf("Starting FLTK window...\n"); fp@0: fp@0: window = new Window(300, 300); fp@0: window->begin(); fp@0: fp@0: slider_read = new FillSlider(50, 10, 40, 280); fp@0: slider_read->set_vertical(); fp@0: slider_read->buttoncolor(BLUE); fp@0: fp@0: slider_read->deactivate(); fp@0: fp@0: slider_write = new Slider(110, 10, 40, 280); fp@0: slider_write->set_vertical(); fp@0: slider_write->callback(slider_write_callback, NULL); fp@0: fp@0: output_cycle = new ValueOutput(200, 50, 90, 25, "Cycle time [µs]"); fp@0: output_cycle->align(ALIGN_LEFT | ALIGN_TOP); fp@0: fp@0: output_jitter = new ValueOutput(200, 90, 90, 25, "Jitter [%]"); fp@0: output_jitter->align(ALIGN_LEFT | ALIGN_TOP); fp@0: fp@0: output_work = new ValueOutput(200, 130, 90, 25, "Work time [µs]"); fp@0: output_work->align(ALIGN_LEFT | ALIGN_TOP); fp@0: fp@0: output_busy = new ValueOutput(200, 170, 90, 25, "Busy rate [%]"); fp@0: output_busy->align(ALIGN_LEFT | ALIGN_TOP); fp@0: fp@0: output_bus = new ValueOutput(200, 210, 90, 25, "Bus time [µs]"); fp@0: output_bus->align(ALIGN_LEFT | ALIGN_TOP); fp@0: fp@0: check1 = new CheckButton(200, 240, 30, 25, "1"); fp@0: check2 = new CheckButton(250, 240, 30, 25, "2"); fp@0: check3 = new CheckButton(200, 270, 30, 25, "3"); fp@0: check4 = new CheckButton(250, 270, 30, 25, "4"); fp@0: fp@0: // output_cycle = new Output(200, 35, 90, 25); fp@0: fp@0: window->end(); fp@0: window->show(); fp@0: fp@0: add_timeout(SLIDER_UPDATE_CYCLE, slider_read_timeout, NULL); fp@0: add_timeout(VALUES_UPDATE_CYCLE, values_timeout, NULL); fp@0: fp@0: printf("Starting thread...\n"); fp@0: fp@0: if (EtherCAT_start(&master, 20, write_data, read_data, 10000) != 0) fp@0: { fp@0: return -1; fp@0: } fp@0: fp@0: run(); // Start FLTK loop fp@0: fp@0: remove_timeout(slider_read_timeout, NULL); fp@0: remove_timeout(values_timeout, NULL); fp@0: fp@0: printf("Stopping master thread...\n"); fp@0: EtherCAT_stop(&master); fp@0: fp@0: printf("Deactivating slaves...\n"); fp@0: fp@0: EtherCAT_deactivate_slave(&master, dig_out); fp@0: EtherCAT_deactivate_slave(&master, dig_in); fp@0: EtherCAT_deactivate_slave(&master, input); fp@0: EtherCAT_deactivate_slave(&master, output); fp@0: EtherCAT_deactivate_slave(&master, buskoppler); fp@0: fp@0: EtherCAT_master_clear(&master); fp@0: fp@0: printf("Finished.\n"); fp@0: fp@0: return 0; fp@0: } fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: void write_data(unsigned char *data) fp@0: { fp@0: data[0] = write_value & 0xFF; fp@0: data[1] = (write_value & 0xFF00) >> 8; fp@0: fp@0: data[14] = (write_value * 16 / 32767) & 0x0F; fp@0: } fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: void read_data(unsigned char *data) fp@0: { fp@0: read_value = data[5] | data[6] << 8; fp@0: dig_value = data[15]; fp@0: } fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: void slider_read_timeout(void *data) fp@0: { fp@0: slider_read->value((double) read_value / 65536 + 0.5); fp@0: slider_read->redraw(); fp@0: fp@0: check1->value(dig_value & 1); fp@0: check2->value(dig_value & 2); fp@0: check3->value(dig_value & 4); fp@0: check4->value(dig_value & 8); fp@0: fp@0: if (max_cycle < master.last_cycle_time) max_cycle = master.last_cycle_time; fp@0: if (max_jitter < master.last_jitter) max_jitter = master.last_jitter; fp@0: if (max_work < master.last_cycle_work_time) max_work = master.last_cycle_work_time; fp@0: if (max_busy < master.last_cycle_busy_rate) max_busy = master.last_cycle_busy_rate; fp@0: if (max_bus < master.bus_time) max_bus = master.bus_time; fp@0: fp@0: repeat_timeout(SLIDER_UPDATE_CYCLE, slider_read_timeout, NULL); fp@0: } fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: void values_timeout(void *data) fp@0: { fp@0: output_cycle->value(max_cycle * 1000000.0); fp@0: output_jitter->value(max_jitter); fp@0: output_work->value(max_work * 1000000.0); fp@0: output_busy->value(max_busy); fp@0: output_bus->value(max_bus * 1000000.0); fp@0: fp@0: max_cycle = max_jitter = max_work = max_busy = max_bus = 0.0; fp@0: fp@0: repeat_timeout(VALUES_UPDATE_CYCLE, values_timeout, NULL); fp@0: } fp@0: fp@0: //--------------------------------------------------------------- fp@0: fp@0: void slider_write_callback(Widget *sender, void *data) fp@0: { fp@0: write_value = (short unsigned int) (32767 * slider_write->value() + 0.5); fp@0: } fp@0: fp@0: //---------------------------------------------------------------