00001 #include "EthercatDemoIO.hpp"
00002
00003 #include <rtt/Logger.hpp>
00004
00005 #include <native/task.h>
00006 #include <native/timer.h>
00007 #include <posix/pthread.h>
00008 #include <signal.h>
00009 #include <sys/mman.h>
00010 #include <math.h>
00011 #include <stdlib.h>
00012
00013 #include <al/ethercat_master.h>
00014 #include <al/ethercat_AL.h>
00015 #include <al/ethercat_process_data.h>
00016 #include <ethercat_xenomai_drv.h>
00017 #include <ethercat/netif.h>
00018 #include <dll/ethercat_dll.h>
00019 #include <dll/ethercat_frame.h>
00020 #include <dll/ethercat_slave_memory.h>
00021 #include <al/ethercat_slave_conf.h>
00022 #include <al/ethercat_slave_handler.h>
00023
00024
00025
00026
00028 EtherCAT_FMMU_Config fmmu_config_EK1100(0);
00029 EtherCAT_PD_Config pd_config_EK1100(0);
00030 EtherCAT_SlaveConfig EC_EK1100(0x044c2c52,0x00010000,1001,&fmmu_config_EK1100,&pd_config_EK1100);
00031
00033 EtherCAT_FMMU_Config fmmu_config_EL4102(2);
00034 EC_FMMU fmmu0_EL4102(0x00080000,1,0,0,0x080D,0,true,false,true);
00035 EC_FMMU fmmu1_EL4102(0x00010000,4,0,7,0x1000,0,false,true,true);
00036
00037 EC_SyncMan syncman_mbx0_EL4102(0x1800,246,EC_QUEUED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00038 EC_SyncMan syncman_mbx1_EL4102(0x18f6,246,EC_QUEUED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00039 EtherCAT_MbxConfig mbx_conf_EL4102 = {syncman_mbx0_EL4102, syncman_mbx1_EL4102};
00040
00041 EtherCAT_PD_Config pd_config_EL4102(2);
00042 EC_SyncMan
00043 syncman0_EL4102(0x1000,4,EC_BUFFERED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00044 EC_SyncMan
00045 syncman1_EL4102(0x1100,0,EC_BUFFERED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00046
00047 EtherCAT_SlaveConfig EC_EL4102(0x10063052,0,1002,&fmmu_config_EL4102,&pd_config_EL4102,&mbx_conf_EL4102);
00048
00049
00051 EtherCAT_FMMU_Config fmmu_config_EL3162(2);
00052 EC_FMMU fmmu0_EL3162(0x00080000,1,1,1,0x080D,0,true,false,true);
00053 EC_FMMU fmmu1_EL3162(0x00010004,6,0,7,0x1100,0,true,false,true);
00054
00055 EC_SyncMan syncman_mbx0_EL3162(0x1800,246,EC_QUEUED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00056 EC_SyncMan syncman_mbx1_EL3162(0x18f6,246,EC_QUEUED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00057 EtherCAT_MbxConfig mbx_conf_EL3162 = {syncman_mbx0_EL3162, syncman_mbx1_EL3162};
00058
00059 EtherCAT_PD_Config pd_config_EL3162(2);
00060 EC_SyncMan syncman0_EL3162(0x1000,0,EC_BUFFERED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,false);
00061 EC_SyncMan syncman1_EL3162(0x1100,6,EC_BUFFERED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00062
00063 EtherCAT_SlaveConfig EC_EL3162(0x0C5A3052,0,1003,&fmmu_config_EL3162, &pd_config_EL3162, &mbx_conf_EL3162);
00064
00065
00067 EtherCAT_FMMU_Config fmmu_config_EL2004_1(1);
00068 EC_FMMU fmmu0_EL2004_1(0x0001000A,1,0,3,0x0F00,0,false,true,true);
00069
00070 EtherCAT_PD_Config pd_config_EL2004_1(1);
00071 EC_SyncMan syncman0_EL2004_1(0x0F00,1,EC_QUEUED,EC_WRITTEN_FROM_MASTER,false,true,false,false,false,false,EC_FIRST_BUFFER,true);
00072
00073 EtherCAT_SlaveConfig EC_EL2004_1(0x7d43052,0,1004,&fmmu_config_EL2004_1,&pd_config_EL2004_1);
00074
00076 EtherCAT_FMMU_Config fmmu_config_EL2004_2(1);
00077 EC_FMMU fmmu0_EL2004_2(0x0001000A,1,4,7,0x0F00,0,false,true,true);
00078
00079 EtherCAT_PD_Config pd_config_EL2004_2(1);
00080 EC_SyncMan syncman0_EL2004_2(0x0F00,1,EC_QUEUED,EC_WRITTEN_FROM_MASTER,false,true,false,false,false,false,EC_FIRST_BUFFER,true);
00081
00082 EtherCAT_SlaveConfig EC_EL2004_2(0x7d43052,0,1005,&fmmu_config_EL2004_2,&pd_config_EL2004_2);
00083
00085 EtherCAT_FMMU_Config fmmu_config_EL1014_1(1);
00086 EC_FMMU fmmu0_EL1014_1(0x0001000B,1,0,3,0x1000,0,true,false,true);
00087
00088 EtherCAT_PD_Config pd_config_EL1014_1(1);
00089 EC_SyncMan syncman0_EL1014_1(0x1000,1,EC_BUFFERED,EC_READ_FROM_MASTER,false,false,false,false,false,false,EC_FIRST_BUFFER,true);
00090
00091 EtherCAT_SlaveConfig EC_EL1014_1(0x03F63052,0,1006,&fmmu_config_EL1014_1, &pd_config_EL1014_1);
00092
00094 EtherCAT_FMMU_Config fmmu_config_EL1014_2(1);
00095 EC_FMMU fmmu0_EL1014_2(0x0001000B,1,4,7,0x1000,0,true,false,true);
00096
00097 EtherCAT_PD_Config pd_config_EL1014_2(1);
00098 EC_SyncMan syncman0_EL1014_2(0x1000,1,EC_BUFFERED,EC_READ_FROM_MASTER,false,false,false,false,false,false,EC_FIRST_BUFFER,true);
00099
00100 EtherCAT_SlaveConfig EC_EL1014_2(0x03F63052,0,1007,&fmmu_config_EL1014_2, &pd_config_EL1014_2);
00101
00103 EtherCAT_FMMU_Config fmmu_config_EL5101_1(2);
00104 EC_FMMU fmmu0_EL5101_1(0x00010800,3,0,7,0x1000,0,false,true,true);
00105 EC_FMMU fmmu1_EL5101_1(0x00011000,5,0,7,0x1100,0,true,false,true);
00106
00107 EC_SyncMan syncman_mbx0_EL5101_1(0x1800,246,EC_QUEUED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00108 EC_SyncMan syncman_mbx1_EL5101_1(0x18f6,246,EC_QUEUED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00109 EtherCAT_MbxConfig mbx_conf_EL5101_1 = {syncman_mbx0_EL5101_1, syncman_mbx1_EL5101_1};
00110
00111 EtherCAT_PD_Config pd_config_EL5101_1(2);
00112 EC_SyncMan syncman0_EL5101_1(0x1000,3,EC_BUFFERED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00113 EC_SyncMan syncman1_EL5101_1(0x1100,5,EC_BUFFERED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00114
00115 EtherCAT_SlaveConfig EC_EL5101_1(0x13ED3052,0x270B0000,1008,&fmmu_config_EL5101_1, &pd_config_EL5101_1, &mbx_conf_EL5101_1);
00116
00118 EtherCAT_FMMU_Config fmmu_config_EL5101_2(2);
00119 EC_FMMU fmmu0_EL5101_2(0x00010803,3,0,7,0x1000,0,false,true,true);
00120 EC_FMMU fmmu1_EL5101_2(0x00011005,5,0,7,0x1100,0,true,false,true);
00121
00122 EC_SyncMan syncman_mbx0_EL5101_2(0x1800,246,EC_QUEUED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00123 EC_SyncMan syncman_mbx1_EL5101_2(0x18f6,246,EC_QUEUED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00124 EtherCAT_MbxConfig mbx_conf_EL5101_2 = {syncman_mbx0_EL5101_2, syncman_mbx1_EL5101_2};
00125
00126 EtherCAT_PD_Config pd_config_EL5101_2(2);
00127 EC_SyncMan syncman0_EL5101_2(0x1000,3,EC_BUFFERED,EC_WRITTEN_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00128 EC_SyncMan syncman1_EL5101_2(0x1100,5,EC_BUFFERED,EC_READ_FROM_MASTER,true,false,false,false,false,false,EC_FIRST_BUFFER,true);
00129
00130 EtherCAT_SlaveConfig EC_EL5101_2(0x13ED3052,0x270B0000,1009,&fmmu_config_EL5101_2, &pd_config_EL5101_2, &mbx_conf_EL5101_2);
00131
00132
00133 static void init_slave_db() {
00134 EtherCAT_SlaveDb * slave_db = EtherCAT_SlaveDb::instance(9);
00135 fmmu_config_EL4102[0] = fmmu0_EL4102;
00136 fmmu_config_EL4102[1] = fmmu1_EL4102;
00137 pd_config_EL4102[0] = syncman0_EL4102;
00138 pd_config_EL4102[1] = syncman1_EL4102;
00139 fmmu_config_EL3162[0] = fmmu0_EL3162;
00140 fmmu_config_EL3162[1] = fmmu1_EL3162;
00141 pd_config_EL3162[0] = syncman0_EL3162;
00142 pd_config_EL3162[1] = syncman1_EL3162;
00143 fmmu_config_EL2004_1[0] = fmmu0_EL2004_1;
00144 pd_config_EL2004_1[0] = syncman0_EL2004_1;
00145 fmmu_config_EL2004_2[0] = fmmu0_EL2004_2;
00146 pd_config_EL2004_2[0] = syncman0_EL2004_2;
00147 fmmu_config_EL1014_1[0] = fmmu0_EL1014_1;
00148 pd_config_EL1014_1[0] = syncman0_EL1014_1;
00149 fmmu_config_EL1014_2[0] = fmmu0_EL1014_2;
00150 pd_config_EL1014_2[0] = syncman0_EL1014_2;
00151 fmmu_config_EL5101_1[0] = fmmu0_EL5101_1;
00152 fmmu_config_EL5101_1[1] = fmmu1_EL5101_1;
00153 pd_config_EL5101_1[0] = syncman0_EL5101_1;
00154 pd_config_EL5101_1[1] = syncman1_EL5101_1;
00155 fmmu_config_EL5101_2[0] = fmmu0_EL5101_2;
00156 fmmu_config_EL5101_2[1] = fmmu1_EL5101_2;
00157 pd_config_EL5101_2[0] = syncman0_EL5101_2;
00158 pd_config_EL5101_2[1] = syncman1_EL5101_2;
00159 slave_db->set_conf(&EC_EK1100,0);
00160 slave_db->set_conf(&EC_EL4102,1);
00161 slave_db->set_conf(&EC_EL3162,2);
00162 slave_db->set_conf(&EC_EL2004_1,3);
00163 slave_db->set_conf(&EC_EL2004_2,4);
00164 slave_db->set_conf(&EC_EL1014_1,5);
00165 slave_db->set_conf(&EC_EL1014_2,6);
00166 slave_db->set_conf(&EC_EL5101_1,7);
00167 slave_db->set_conf(&EC_EL5101_2,8);
00168 }
00169
00170
00171
00172 namespace OCL
00173 {
00174 using namespace RTT;
00175 using namespace std;
00176
00177 EthercatDemoIO::EthercatDemoIO(string name):
00178 TaskContext(name),
00179 rteth("rteth","for example: rteth0","rteth0"),
00180 gen_sin("gen_sin",&EthercatDemoIO::mgen_sin, this),
00181 stop_master("stop_master", &EthercatDemoIO::mstop_master, this)
00182 {
00183 this->properties()->addProperty(&rteth);
00184 this->methods()->addMethod(&gen_sin);
00185 this->methods()->addMethod(&stop_master);
00186
00187 ni = init_ec( rteth.get().c_str() );
00188 init_slave_db();
00189 if(ni != 0) {
00190
00191 EtherCAT_DataLinkLayer::instance()->attach(ni);
00192 printf("Master initializing \n\n");
00193 EtherCAT_Master * EM = EtherCAT_Master::instance();
00194 EtherCAT_SlaveHandler * sh;
00195 for(int i = 1001; i<1010; i++){
00196 printf("Getting slave handler\n");
00197 sh = EM->get_slave_handler(i);
00198 if(sh && sh->to_state(EC_OP_STATE))
00199 printf("Slave %d succesfully set to OP STATE\n", i);
00200 else
00201 printf("\nFailed to set slave %d to OP STATE\n", i);
00202 }
00203 int64_t timeout = 1000 * 50;
00204 if (set_socket_timeout(ni, timeout) == 0)
00205 printf("Socket timeout set to %d\n", (int)timeout);
00206 }
00207 }
00208
00209 EthercatDemoIO::~EthercatDemoIO() {
00210 if(ni != 0)
00211 close_socket(ni);
00212 }
00213
00214 bool EthercatDemoIO::mgen_sin(double f) {
00215 if(f <= 0)
00216 return false;
00217 sinus_freq = f;
00218 return true;
00219 }
00220
00221 void EthercatDemoIO::mstop_master(void) {
00222
00223 }
00224
00225 bool EthercatDemoIO::startup() {
00226 cnt = 0; cnt_dig = 0;
00227 voltage_i = 0;
00228 voltage_f = 0; time = 0;
00229 sinus_freq = 1000;
00230 EtherCAT_AL * AL = EtherCAT_AL::instance();
00231 return ni && AL && AL->isReady();
00232 }
00233 void EthercatDemoIO::update() {
00234 EtherCAT_Master *EM = EtherCAT_Master::instance();
00235
00236 unsigned char msg[10] = {0};
00237 int samples = (int)(1000.0 / this->engine()->getActivity()->getPeriod() / sinus_freq);
00238 if(cnt > samples)
00239 cnt = 0;
00240 time = M_PI * 2 * cnt / samples;
00241 voltage_f = (sin(time) * 5 + 5) * 32767 / 10;
00242 voltage_i = (int) voltage_f;
00243 msg[0] = voltage_i; msg[1] = voltage_i>>8;
00244
00245
00246
00247 cnt++; cnt_dig++;
00248 EM->txandrx_PD(sizeof(msg),msg);
00249 int volt = msg[6];
00250 volt = volt<<8;
00251 volt = volt | msg[5];
00252
00253
00254 }
00255
00256 void EthercatDemoIO::shutdown() {
00257
00258 }
00259 }