Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 extern "C"
00052 {
00053 #include <youbot_driver/soem/ethercattype.h>
00054 #include <youbot_driver/soem/nicdrv.h>
00055 #include <youbot_driver/soem/ethercatbase.h>
00056 #include <youbot_driver/soem/ethercatmain.h>
00057 #include <youbot_driver/soem/ethercatconfig.h>
00058 #include <youbot_driver/soem/ethercatcoe.h>
00059 #include <youbot_driver/soem/ethercatdc.h>
00060 #include <youbot_driver/soem/ethercatprint.h>
00061 }
00062 #include <youbot_driver/youbot/EthercatMasterWithoutThread.hpp>
00063
00064 namespace youbot
00065 {
00066
00067 EthercatMasterWithoutThread::EthercatMasterWithoutThread(const std::string& configFile,
00068 const std::string& configFilePath)
00069 {
00070
00071
00072 this->ethercatConnectionEstablished = false;
00073 ethernetDevice = "eth0";
00074 mailboxTimeout = 4000;
00075 ethercatTimeout = 500;
00076 configfile = NULL;
00077 this->configFileName = configFile;
00078 this->configFilepath = configFilePath;
00079
00080
00081 for (unsigned int i = 0; i < 4096; i++)
00082 {
00083 IOmap_[i] = 0;
00084 }
00085
00086 configfile = new ConfigFile(this->configFileName, this->configFilepath);
00087
00088
00089 configfile->readInto(ethernetDevice, "EtherCAT", "EthernetDevice");
00090 configfile->readInto(ethercatTimeout, "EtherCAT", "EtherCATTimeout_[usec]");
00091 configfile->readInto(mailboxTimeout, "EtherCAT", "MailboxTimeout_[usec]");
00092
00093 this->initializeEthercat();
00094
00095
00096 }
00097
00098 EthercatMasterWithoutThread::~EthercatMasterWithoutThread()
00099 {
00100
00101 this->closeEthercat();
00102 if (configfile != NULL)
00103 delete configfile;
00104
00105 }
00106
00107 bool EthercatMasterWithoutThread::isThreadActive()
00108 {
00109
00110 return false;
00111
00112 }
00113
00115 unsigned int EthercatMasterWithoutThread::getNumberOfSlaves() const
00116 {
00117
00118 return this->nrOfSlaves;
00119
00120 }
00121
00122 void EthercatMasterWithoutThread::AutomaticSendOn(const bool enableAutomaticSend)
00123 {
00124
00125 LOG(trace) << "automatic send is not possible if the EtherCAT master has no thread";
00126
00127 return;
00128
00129 }
00130
00131 void EthercatMasterWithoutThread::AutomaticReceiveOn(const bool enableAutomaticReceive)
00132 {
00133
00134 LOG(trace) << "automatic receive is not possible if the EtherCAT master has no thread";
00135 return;
00136
00137 }
00138
00141 void EthercatMasterWithoutThread::getEthercatDiagnosticInformation(std::vector<ec_slavet>& ethercatSlaveInfos)
00142 {
00143
00144 ethercatSlaveInfos = this->ethercatSlaveInfo;
00145 for (unsigned int i = 0; i < ethercatSlaveInfos.size(); i++)
00146 {
00147 ethercatSlaveInfos[i].inputs = NULL;
00148 ethercatSlaveInfos[i].outputs = NULL;
00149 }
00150
00151 }
00152
00155 bool EthercatMasterWithoutThread::sendProcessData()
00156 {
00157
00158
00159 for (unsigned int i = 0; i < processDataBuffer.size(); i++)
00160 {
00161
00162 *(ethercatOutputBufferVector[i]) = (processDataBuffer[i]).stctOutput;
00163 }
00164
00165
00166 if (ec_send_processdata() == 0)
00167 {
00168 return false;
00169 }
00170
00171 return true;
00172
00173
00174 }
00175
00178 bool EthercatMasterWithoutThread::receiveProcessData()
00179 {
00180
00181
00182
00183 if (ec_receive_processdata(this->ethercatTimeout) == 0)
00184 {
00185 return false;
00186 }
00187
00188 for (unsigned int i = 0; i < processDataBuffer.size(); i++)
00189 {
00190
00191 (processDataBuffer[i]).stctInput = *(ethercatInputBufferVector[i]);
00192 }
00193
00194 return true;
00195
00196
00197 }
00198
00201 bool EthercatMasterWithoutThread::isErrorInSoemDriver()
00202 {
00203
00204
00205 return ec_iserror();
00206
00207
00208 }
00209
00210 bool EthercatMasterWithoutThread::isEtherCATConnectionEstablished()
00211 {
00212
00213 return this->ethercatConnectionEstablished;
00214
00215 }
00216
00217 void EthercatMasterWithoutThread::registerJointLimitMonitor(JointLimitMonitor* object, const unsigned int JointNumber)
00218 {
00219
00220
00221
00222 }
00223
00225 void EthercatMasterWithoutThread::initializeEthercat()
00226 {
00227
00228
00229
00230 if (ec_init(ethernetDevice.c_str()))
00231 {
00232 LOG(info) << "Initializing EtherCAT on " << ethernetDevice << " without communication thread";
00233
00234 if (ec_config(TRUE, &IOmap_) > 0)
00235 {
00236
00237 LOG(trace) << ec_slavecount << " EtherCAT slaves found and configured.";
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
00271 if (ec_slave[0].state != EC_STATE_SAFE_OP)
00272 {
00273 LOG(warning) << "Not all EtherCAT slaves reached safe operational state.";
00274 ec_readstate();
00275
00276 for (int i = 1; i <= ec_slavecount; i++)
00277 {
00278 if (ec_slave[i].state != EC_STATE_SAFE_OP)
00279 {
00280 LOG(info) << "Slave " << i << " State=" << ec_slave[i].state << " StatusCode=" << ec_slave[i].ALstatuscode
00281 << " : " << ec_ALstatuscode2string(ec_slave[i].ALstatuscode);
00282
00283 }
00284 }
00285 }
00286
00287
00288
00289
00290 LOG(trace) << "Request operational state for all EtherCAT slaves";
00291
00292 ec_slave[0].state = EC_STATE_OPERATIONAL;
00293
00294
00295 ec_send_processdata();
00296 ec_receive_processdata(EC_TIMEOUTRET);
00297
00298 ec_writestate(0);
00299
00300
00301 ec_statecheck(0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE);
00302 if (ec_slave[0].state == EC_STATE_OPERATIONAL)
00303 {
00304 LOG(trace) << "Operational state reached for all EtherCAT slaves.";
00305 }
00306 else
00307 {
00308 throw std::runtime_error("Not all EtherCAT slaves reached operational state.");
00309
00310 }
00311
00312 }
00313 else
00314 {
00315 throw std::runtime_error("No EtherCAT slaves found!");
00316 }
00317
00318 }
00319 else
00320 {
00321 throw std::runtime_error("No socket connection on " + ethernetDevice + "\nExcecute as root");
00322 }
00323
00324 std::string baseJointControllerName = "TMCM-174";
00325 std::string baseJointControllerNameAlternative = "TMCM-1632";
00326 std::string manipulatorJointControllerName = "TMCM-174";
00327 std::string ManipulatorJointControllerNameAlternative = "TMCM-1610";
00328 std::string actualSlaveName;
00329 nrOfSlaves = 0;
00330 YouBotSlaveMsg emptySlaveMsg;
00331
00332 configfile->readInto(baseJointControllerName, "BaseJointControllerName");
00333 configfile->readInto(baseJointControllerNameAlternative, "BaseJointControllerNameAlternative");
00334 configfile->readInto(manipulatorJointControllerName, "ManipulatorJointControllerName");
00335 configfile->readInto(ManipulatorJointControllerNameAlternative, "ManipulatorJointControllerNameAlternative");
00336
00337
00338 for (int cnt = 1; cnt <= ec_slavecount; cnt++)
00339 {
00340 LOG(trace) << "Slave: " << cnt << " Name: " << ec_slave[cnt].name << " Output size: " << ec_slave[cnt].Obits
00341 << "bits Input size: " << ec_slave[cnt].Ibits << "bits State: " << ec_slave[cnt].state << " delay: "
00342 << ec_slave[cnt].pdelay;
00343
00344 ethercatSlaveInfo.push_back(ec_slave[cnt]);
00345
00346 actualSlaveName = ec_slave[cnt].name;
00347 if ((actualSlaveName == baseJointControllerName || actualSlaveName == baseJointControllerNameAlternative
00348 || actualSlaveName == manipulatorJointControllerName
00349 || actualSlaveName == ManipulatorJointControllerNameAlternative) && ec_slave[cnt].Obits > 0
00350 && ec_slave[cnt].Ibits > 0)
00351 {
00352 nrOfSlaves++;
00353 processDataBuffer.push_back(emptySlaveMsg);
00354 ethercatOutputBufferVector.push_back((SlaveMessageOutput*)(ec_slave[cnt].outputs));
00355 ethercatInputBufferVector.push_back((SlaveMessageInput*)(ec_slave[cnt].inputs));
00356 YouBotSlaveMailboxMsg emptyMailboxSlaveMsg(cnt);
00357 firstMailboxBufferVector.push_back(emptyMailboxSlaveMsg);
00358 }
00359 }
00360
00361 if (nrOfSlaves > 0)
00362 {
00363 LOG(info) << nrOfSlaves << " EtherCAT slaves found";
00364 this->ethercatConnectionEstablished = true;
00365 }
00366 else
00367 {
00368 throw std::runtime_error("No EtherCAT slave could be found");
00369 return;
00370 }
00371
00372 return;
00373
00374 }
00375
00377 bool EthercatMasterWithoutThread::closeEthercat()
00378 {
00379
00380
00381 this->ethercatConnectionEstablished = false;
00382
00383 ec_slave[0].state = EC_STATE_SAFE_OP;
00384
00385
00386 ec_writestate(0);
00387
00388
00389 ec_close();
00390
00391 return true;
00392
00393 }
00394
00398 void EthercatMasterWithoutThread::setMsgBuffer(const YouBotSlaveMsg& msgBuffer, const unsigned int jointNumber)
00399 {
00400
00401
00402 processDataBuffer[jointNumber - 1].stctOutput = msgBuffer.stctOutput;
00403
00404
00405 }
00406
00410 void EthercatMasterWithoutThread::getMsgBuffer(const unsigned int jointNumber, YouBotSlaveMsg& returnMsg)
00411 {
00412
00413
00414 returnMsg = processDataBuffer[jointNumber - 1];
00415
00416
00417 }
00418
00422 void EthercatMasterWithoutThread::setMailboxMsgBuffer(const YouBotSlaveMailboxMsg& msgBuffer,
00423 const unsigned int jointNumber)
00424 {
00425
00426
00427 firstMailboxBufferVector[jointNumber - 1].stctOutput = msgBuffer.stctOutput;
00428 sendMailboxMessage(firstMailboxBufferVector[jointNumber - 1]);
00429 return;
00430
00431 }
00432
00436 bool EthercatMasterWithoutThread::getMailboxMsgBuffer(YouBotSlaveMailboxMsg& mailboxMsg, const unsigned int jointNumber)
00437 {
00438
00439 bool returnvalue = receiveMailboxMessage(firstMailboxBufferVector[jointNumber - 1]);
00440 mailboxMsg.stctInput = firstMailboxBufferVector[jointNumber - 1].stctInput;
00441 return returnvalue;
00442
00443 }
00444
00447 bool EthercatMasterWithoutThread::sendMailboxMessage(const YouBotSlaveMailboxMsg& mailboxMsg)
00448 {
00449
00450 mailboxBufferSend[0] = mailboxMsg.stctOutput.moduleAddress;
00451 mailboxBufferSend[1] = mailboxMsg.stctOutput.commandNumber;
00452 mailboxBufferSend[2] = mailboxMsg.stctOutput.typeNumber;
00453 mailboxBufferSend[3] = mailboxMsg.stctOutput.motorNumber;
00454 mailboxBufferSend[4] = mailboxMsg.stctOutput.value >> 24;
00455 mailboxBufferSend[5] = mailboxMsg.stctOutput.value >> 16;
00456 mailboxBufferSend[6] = mailboxMsg.stctOutput.value >> 8;
00457 mailboxBufferSend[7] = mailboxMsg.stctOutput.value & 0xff;
00458 if (ec_mbxsend(mailboxMsg.slaveNumber, &mailboxBufferSend, mailboxTimeout))
00459 {
00460 return true;
00461 }
00462 else
00463 {
00464 return false;
00465 }
00466
00467 }
00468
00471 bool EthercatMasterWithoutThread::receiveMailboxMessage(YouBotSlaveMailboxMsg& mailboxMsg)
00472 {
00473
00474 if (ec_mbxreceive(mailboxMsg.slaveNumber, &mailboxBufferReceive, mailboxTimeout))
00475 {
00476 mailboxMsg.stctInput.replyAddress = (int)mailboxBufferReceive[0];
00477 mailboxMsg.stctInput.moduleAddress = (int)mailboxBufferReceive[1];
00478 mailboxMsg.stctInput.status = (int)mailboxBufferReceive[2];
00479 mailboxMsg.stctInput.commandNumber = (int)mailboxBufferReceive[3];
00480 mailboxMsg.stctInput.value = (mailboxBufferReceive[4] << 24 | mailboxBufferReceive[5] << 16
00481 | mailboxBufferReceive[6] << 8 | mailboxBufferReceive[7]);
00482 return true;
00483 }
00484 return false;
00485
00486 }
00487
00488 void EthercatMasterWithoutThread::parseYouBotErrorFlags(const YouBotSlaveMsg& messageBuffer)
00489 {
00490
00491 std::stringstream errorMessageStream;
00492 errorMessageStream << " ";
00493 std::string errorMessage;
00494 errorMessage = errorMessageStream.str();
00495
00496 if (messageBuffer.stctInput.errorFlags & OVER_CURRENT)
00497 {
00498 LOG(error) << errorMessage << "got over current";
00499
00500 }
00501
00502 if (messageBuffer.stctInput.errorFlags & UNDER_VOLTAGE)
00503 {
00504 LOG(error) << errorMessage << "got under voltage";
00505
00506 }
00507
00508 if (messageBuffer.stctInput.errorFlags & OVER_VOLTAGE)
00509 {
00510 LOG(error) << errorMessage << "got over voltage";
00511
00512 }
00513
00514 if (messageBuffer.stctInput.errorFlags & OVER_TEMPERATURE)
00515 {
00516 LOG(error) << errorMessage << "got over temperature";
00517
00518 }
00519
00520 if (messageBuffer.stctInput.errorFlags & MOTOR_HALTED)
00521 {
00522
00523
00524 }
00525
00526 if (messageBuffer.stctInput.errorFlags & HALL_SENSOR_ERROR)
00527 {
00528 LOG(error) << errorMessage << "got hall sensor problem";
00529
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 if (messageBuffer.stctInput.errorFlags & VELOCITY_MODE)
00548 {
00549
00550
00551 }
00552
00553 if (messageBuffer.stctInput.errorFlags & POSITION_MODE)
00554 {
00555
00556
00557 }
00558
00559 if (messageBuffer.stctInput.errorFlags & TORQUE_MODE)
00560 {
00561
00562
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 if (messageBuffer.stctInput.errorFlags & POSITION_REACHED)
00576 {
00577
00578
00579 }
00580
00581 if (messageBuffer.stctInput.errorFlags & INITIALIZED)
00582 {
00583
00584
00585 }
00586
00587 if (messageBuffer.stctInput.errorFlags & TIMEOUT)
00588 {
00589 LOG(error) << errorMessage << "has a timeout";
00590
00591 }
00592
00593 if (messageBuffer.stctInput.errorFlags & I2T_EXCEEDED)
00594 {
00595 LOG(error) << errorMessage << "exceeded I2t";
00596
00597 }
00598
00599
00600 }
00601
00602 std::string EthercatMasterWithoutThread::configFileName;
00603
00604 std::string EthercatMasterWithoutThread::configFilepath;
00605
00606 }