00001 #include "EthercatIO.hpp"
00002 #include "slaveconfigurations.hpp"
00003 #include "dev/DigitalEtherCATOutputDevice.hpp"
00004 #include "dev/EtherCATEncoder.hpp"
00005
00006 #include <rtt/Logger.hpp>
00007
00008 #include <native/task.h>
00009 #include <native/timer.h>
00010 #include <posix/pthread.h>
00011 #include <signal.h>
00012 #include <sys/mman.h>
00013 #include <math.h>
00014 #include <stdlib.h>
00015
00016 #include <al/ethercat_master.h>
00017 #include <al/ethercat_AL.h>
00018 #include <al/ethercat_process_data.h>
00019 #include <ethercat_xenomai_drv.h>
00020 #include <ethercat/netif.h>
00021 #include <dll/ethercat_dll.h>
00022 #include <dll/ethercat_frame.h>
00023 #include <dll/ethercat_slave_memory.h>
00024 #include <al/ethercat_slave_conf.h>
00025 #include <al/ethercat_slave_handler.h>
00026
00027 static void init_slave_db();
00028
00029 namespace OCL
00030 {
00031 using namespace RTT;
00032 using namespace std;
00033
00034 EthercatIO::EthercatIO(string name):
00035 TaskContext(name),
00036 rteth("rteth","for example: rteth0","rteth0"),
00037 get_pos("get_pos",&EthercatIO::mget_pos, this),
00038 set_pos("set_pos",&EthercatIO::mset_pos, this),
00039 get_turn("get_turn",&EthercatIO::mget_turn, this),
00040 set_turn("set_turn",&EthercatIO::mset_turn, this),
00041 upcounting("upcounting",&EthercatIO::mupcounting, this),
00042 fmmu_buffer(new unsigned char[40]),
00043 digoutputs(fmmu_buffer + 10,0,4),
00044 digoutputs2(fmmu_buffer + 10,4,4),
00045 diginputs(fmmu_buffer + 11,0,4),
00046 anaoutputs(fmmu_buffer,2,32676,0,10),
00047 anainputs(fmmu_buffer + 4,2,32676,0,10),
00048 enc(fmmu_buffer + 0x13,fmmu_buffer + 0x10, 65536, true)
00049 {
00050
00051 this->properties()->addProperty(&rteth);
00052 this->methods()->addMethod(&get_pos,"get the position");
00053 this->methods()->addMethod(&set_pos,"set the position","position","the value of the desired position");
00054 this->methods()->addMethod(&get_turn,"get the turn");
00055 this->methods()->addMethod(&set_turn,"set the turn","turn","The value of the desired turn");
00056 this->methods()->addMethod(&upcounting,"get the counting direction (up = true, down = false)");
00057 for(int i = 0; i< 40; i++)
00058 fmmu_buffer[i] = 0x00;
00059
00060 ni = init_ec( rteth.get().c_str() );
00061 init_slave_db();
00062 if(ni != 0) {
00063
00064 EtherCAT_DataLinkLayer::instance()->attach(ni);
00065 printf("Master initializing \n\n");
00066 EtherCAT_Master * EM = EtherCAT_Master::instance();
00067 EtherCAT_SlaveHandler * sh;
00068 if(EtherCAT_AL::instance()->isReady()) {
00069 for(int i = 1001; i<1010; i++){
00070
00071 sh = EM->get_slave_handler(i);
00072 if(!sh || !sh->to_state(EC_OP_STATE))
00073 printf("\nFailed to set slave %d to OP STATE\n", i);
00074 }
00075 int64_t timeout = 1000 * 50;
00076 if (set_socket_timeout(ni, timeout) == 0)
00077 printf("Socket timeout set to %d\n", (int)timeout);
00078 }
00079 }
00080 }
00081
00082 EthercatIO::~EthercatIO() {
00083 if(ni != 0)
00084 close_socket(ni);
00085 }
00086
00087 int EthercatIO::mget_pos(void) {
00088 return enc.positionGet();
00089 }
00090
00091 void EthercatIO::mset_pos(int pos) {
00092 enc.positionSet(pos);
00093 }
00094
00095 int EthercatIO::mget_turn(void) {
00096 return enc.turnGet();
00097 }
00098
00099 void EthercatIO::mset_turn(int turn){
00100 enc.turnSet(turn);
00101 }
00102
00103 bool EthercatIO::mupcounting(void) {
00104 return enc.upcounting();
00105 }
00106
00107 bool EthercatIO::startup() {
00108 cnt_dig = 0;
00109 prevvoltage = 50;
00110
00111 EtherCAT_AL * AL = EtherCAT_AL::instance();
00112 return ni && AL && AL->isReady();
00113 }
00114 void EthercatIO::update() {
00115 EtherCAT_Master *EM = EtherCAT_Master::instance();
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 enc.update();
00137 EM->txandrx_PD(40,fmmu_buffer);
00138
00139 }
00140
00141 void EthercatIO::shutdown() {
00142
00143 }
00144 }
00145
00146
00147 static void init_slave_db() {
00148 EtherCAT_SlaveDb * slave_db = EtherCAT_SlaveDb::instance(9);
00149 fmmu_config_EL4102[0] = fmmu0_EL4102;
00150 fmmu_config_EL4102[1] = fmmu1_EL4102;
00151 pd_config_EL4102[0] = syncman0_EL4102;
00152 pd_config_EL4102[1] = syncman1_EL4102;
00153 fmmu_config_EL3162[0] = fmmu0_EL3162;
00154 fmmu_config_EL3162[1] = fmmu1_EL3162;
00155 pd_config_EL3162[0] = syncman0_EL3162;
00156 pd_config_EL3162[1] = syncman1_EL3162;
00157 fmmu_config_EL2004_1[0] = fmmu0_EL2004_1;
00158 pd_config_EL2004_1[0] = syncman0_EL2004_1;
00159 fmmu_config_EL2004_2[0] = fmmu0_EL2004_2;
00160 pd_config_EL2004_2[0] = syncman0_EL2004_2;
00161 fmmu_config_EL1014_1[0] = fmmu0_EL1014_1;
00162 pd_config_EL1014_1[0] = syncman0_EL1014_1;
00163 fmmu_config_EL1014_2[0] = fmmu0_EL1014_2;
00164 pd_config_EL1014_2[0] = syncman0_EL1014_2;
00165 fmmu_config_EL5101_1[0] = fmmu0_EL5101_1;
00166 fmmu_config_EL5101_1[1] = fmmu1_EL5101_1;
00167 pd_config_EL5101_1[0] = syncman0_EL5101_1;
00168 pd_config_EL5101_1[1] = syncman1_EL5101_1;
00169 fmmu_config_EL5101_2[0] = fmmu0_EL5101_2;
00170 fmmu_config_EL5101_2[1] = fmmu1_EL5101_2;
00171 pd_config_EL5101_2[0] = syncman0_EL5101_2;
00172 pd_config_EL5101_2[1] = syncman1_EL5101_2;
00173 slave_db->set_conf(&EC_EK1100,0);
00174 slave_db->set_conf(&EC_EL4102,1);
00175 slave_db->set_conf(&EC_EL3162,2);
00176 slave_db->set_conf(&EC_EL2004_1,3);
00177 slave_db->set_conf(&EC_EL2004_2,4);
00178 slave_db->set_conf(&EC_EL1014_1,5);
00179 slave_db->set_conf(&EC_EL1014_2,6);
00180 slave_db->set_conf(&EC_EL5101_1,7);
00181 slave_db->set_conf(&EC_EL5101_2,8);
00182 }