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 #include "youbot/YouBotJoint.hpp"
00052 namespace youbot {
00053
00054 YouBotJoint::YouBotJoint(const unsigned int jointNo, const std::string& configFilePath) {
00055
00056 this->storage.jointNumber = jointNo;
00057 timeTillNextMailboxUpdate = 1;
00058 mailboxMsgRetries = 100;
00059 this->storage.inverseMovementDirection = false;
00060 this->storage.lowerLimit = 0;
00061 this->storage.upperLimit = 0;
00062 this->storage.areLimitsActive = false;
00063 std::stringstream jointNameStream;
00064 jointNameStream << "Joint " << this->storage.jointNumber << " ";
00065 this->storage.jointNumberStr = jointNameStream.str();
00066 ethercatMaster = &(EthercatMaster::getInstance("youbot-ethercat.cfg", configFilePath));
00067
00068
00069 }
00070
00071 YouBotJoint::~YouBotJoint() {
00072
00073
00074 }
00075
00076 void YouBotJoint::setConfigurationParameter(const JointParameter& parameter) {
00077
00078 throw std::runtime_error("Please use YouBotJointParameters");
00079
00080 }
00081
00082 void YouBotJoint::getConfigurationParameter(JointParameter& parameter) {
00083
00084 throw std::runtime_error("Please use YouBotJointParameters");
00085
00086 }
00087
00088 void YouBotJoint::getConfigurationParameter(YouBotJointParameterReadOnly& parameter) {
00089
00090
00091 if (parameter.getType() == MOTOR_CONTOLLER_PARAMETER) {
00092
00093 YouBotSlaveMailboxMsg message;
00094 parameter.getYouBotMailboxMsg(message, GAP, storage);
00095
00096 message.parameterName = parameter.getName();
00097 if (retrieveValueFromMotorContoller(message)) {
00098 parameter.setYouBotMailboxMsg(message, storage);
00099 } else {
00100 throw JointParameterException("Unable to get parameter: " + parameter.getName() + " from joint: " + this->storage.jointName);
00101 }
00102 }else{
00103 throw JointParameterException("Parameter " + parameter.getName() + " is not a motor controller parameter of a joint");
00104 }
00105
00106 }
00107
00108 void YouBotJoint::getConfigurationParameter(YouBotJointParameter& parameter) {
00109
00110 if (parameter.getType() == MOTOR_CONTOLLER_PARAMETER) {
00111
00112 YouBotSlaveMailboxMsg message;
00113 parameter.getYouBotMailboxMsg(message, GAP, storage);
00114
00115 message.parameterName = parameter.getName();
00116 if (retrieveValueFromMotorContoller(message)) {
00117 parameter.setYouBotMailboxMsg(message, storage);
00118 } else {
00119 throw JointParameterException("Unable to get parameter: " + parameter.getName() + " from joint: " + this->storage.jointName);
00120 }
00121 }else{
00122 throw JointParameterException("Parameter " + parameter.getName() + " is not a motor controller parameter of a joint");
00123 }
00124
00125 }
00126
00127 void YouBotJoint::setConfigurationParameter(const YouBotJointParameter& parameter) {
00128
00129 if (parameter.getType() == MOTOR_CONTOLLER_PARAMETER) {
00130
00131 YouBotSlaveMailboxMsg message;
00132 parameter.getYouBotMailboxMsg(message, SAP, storage);
00133
00134 message.parameterName = parameter.getName();
00135 if (!setValueToMotorContoller(message)) {
00136 throw JointParameterException("Unable to set parameter: " + parameter.getName() + " to joint: " + this->storage.jointName);
00137 }
00138 }else{
00139 throw JointParameterException("Parameter " + parameter.getName() + " is not a motor controller parameter of a joint");
00140 }
00141
00142 }
00143
00144 void YouBotJoint::getConfigurationParameter(JointName& parameter) {
00145
00146 parameter.value = this->storage.jointName;
00147
00148 }
00149
00150 void YouBotJoint::setConfigurationParameter(const JointName& parameter) {
00151
00152 this->storage.jointName = parameter.value;
00153
00154 }
00155
00156 void YouBotJoint::getConfigurationParameter(GearRatio& parameter) {
00157
00158 parameter.setParameter(this->storage.gearRatio);
00159
00160 }
00161
00162 void YouBotJoint::setConfigurationParameter(const GearRatio& parameter) {
00163
00164 if (parameter.value == 0) {
00165 throw std::out_of_range("A Gear Ratio of zero is not allowed");
00166 }
00167 this->storage.gearRatio = parameter.value;
00168
00169 }
00170
00171 void YouBotJoint::getConfigurationParameter(EncoderTicksPerRound& parameter) {
00172
00173 parameter.setParameter(this->storage.encoderTicksPerRound);
00174
00175 }
00176
00177 void YouBotJoint::setConfigurationParameter(const EncoderTicksPerRound& parameter) {
00178
00179 if (parameter.value == 0) {
00180 throw std::out_of_range("Zero Encoder Ticks per Round are not allowed");
00181 }
00182 this->storage.encoderTicksPerRound = parameter.value;
00183
00184 }
00185
00186 void YouBotJoint::setConfigurationParameter(const CalibrateJoint& parameter) {
00187
00188 if (parameter.doCalibration) {
00189 LOG(info) << "Calibrate Joint: " << this->storage.jointName;
00190
00191 int calibrationVel = 0;
00192
00193 messageBuffer.stctOutput.controllerMode = VELOCITY_CONTROL;
00194 if (parameter.calibrationDirection == POSITIV) {
00195 calibrationVel = 1.0 / storage.gearRatio;
00196 } else if (parameter.calibrationDirection == NEGATIV) {
00197 calibrationVel = -1.0 / storage.gearRatio;
00198 } else {
00199 throw std::runtime_error("No calibration direction for joint: " + this->storage.jointName);
00200 }
00201
00202 if (this->storage.inverseMovementDirection == true) {
00203 calibrationVel *= -1;
00204 }
00205
00206 JointSensedCurrent sensedCurrent;
00207
00208 messageBuffer.stctOutput.controllerMode = VELOCITY_CONTROL;
00209 messageBuffer.stctOutput.value = calibrationVel;
00210 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00211
00212 sensedCurrent.current = 0;
00213
00214 while (abs(sensedCurrent.current) < abs(parameter.maxCurrent)) {
00215 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00216 this->getData(sensedCurrent);
00217 }
00218
00219
00220 messageBuffer.stctOutput.controllerMode = VELOCITY_CONTROL;
00221 messageBuffer.stctOutput.value = 0;
00222
00223 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00224
00225
00226 SLEEP_MILLISEC(500);
00227 messageBuffer.stctOutput.controllerMode = SET_POSITION_TO_REFERENCE;
00228 messageBuffer.stctOutput.value = 0;
00229 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00230
00231
00232 SLEEP_MILLISEC(100);
00233 messageBuffer.stctOutput.controllerMode = POSITION_CONTROL;
00234 messageBuffer.stctOutput.value = 0;
00235
00236 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00237
00238
00239 }
00240
00241
00242 }
00243
00244 void YouBotJoint::setConfigurationParameter(const InverseMovementDirection& parameter) {
00245
00246 this->storage.inverseMovementDirection = parameter.value;
00247
00248 }
00249
00250 void YouBotJoint::getConfigurationParameter(InverseMovementDirection& parameter) {
00251
00252 parameter.setParameter(this->storage.inverseMovementDirection);
00253
00254 }
00255
00256 void YouBotJoint::setConfigurationParameter(const JointLimits& parameter) {
00257
00258
00259 this->storage.lowerLimit = parameter.lowerLimit;
00260 this->storage.upperLimit = parameter.upperLimit;
00261 this->storage.areLimitsActive = parameter.areLimitsActive;
00262 MotorAcceleration acc;
00263 quantity<angular_acceleration> accValue;
00264
00265
00266
00267 if(this->storage.areLimitsActive){
00268 this->getConfigurationParameter(acc);
00269 acc.getParameter(accValue);
00270 this->limitMonitor.reset(new JointLimitMonitor(this->storage, accValue));
00271 ethercatMaster->registerJointLimitMonitor(this->limitMonitor.get(), this->storage.jointNumber);
00272 }else{
00273 this->limitMonitor.reset(NULL);
00274 }
00275
00276
00277
00278 }
00279
00280 void YouBotJoint::getConfigurationParameter(JointLimits& parameter) {
00281
00282 parameter.setParameter(this->storage.lowerLimit, this->storage.upperLimit, this->storage.areLimitsActive);
00283
00284 }
00285
00286 void YouBotJoint::getConfigurationParameter(JointLimitsRadian& parameter) {
00287
00288 quantity<plane_angle> lowlimit = ((double) this->storage.lowerLimit / storage.encoderTicksPerRound) * storage.gearRatio * (2.0 * M_PI) * radian;
00289 quantity<plane_angle> uplimit = ((double) this->storage.upperLimit / storage.encoderTicksPerRound) * storage.gearRatio * (2.0 * M_PI) * radian;
00290 parameter.setParameter(lowlimit, uplimit, this->storage.areLimitsActive);
00291
00292 }
00293
00294
00295 void YouBotJoint::setConfigurationParameter(const InitializeJoint& parameter) {
00296
00297 if (parameter.value) {
00298 messageBuffer.stctOutput.controllerMode = INITIALIZE;
00299 messageBuffer.stctOutput.value = 0;
00300
00301 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00302 }
00303
00304 }
00305
00306 void YouBotJoint::getConfigurationParameter(FirmwareVersion& parameter) {
00307
00308
00309 YouBotSlaveMailboxMsg message;
00310 parameter.getYouBotMailboxMsg(message, GAP, storage);
00311
00312 bool unvalid = true;
00313 unsigned int retry = 0;
00314
00315 ethercatMaster->setMailboxMsgBuffer(message, this->storage.jointNumber);
00316
00317 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00318
00319 do {
00320 if( ethercatMaster->getMailboxMsgBuffer(message, this->storage.jointNumber) ) {
00321 unvalid = false;
00322 } else {
00323 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00324 retry++;
00325 }
00326 } while (retry < mailboxMsgRetries && unvalid);
00327
00328 if (unvalid) {
00329 this->parseMailboxStatusFlags(message);
00330 throw std::runtime_error("Unable to get firmware version for joint: " + this->storage.jointName);
00331 return;
00332 }
00333
00334 char versionString[9] = {0};
00335 versionString[0] = message.stctInput.replyAddress;
00336 versionString[1] = message.stctInput.moduleAddress;
00337 versionString[2] = message.stctInput.status;
00338 versionString[3] = message.stctInput.commandNumber;
00339 versionString[4] = message.stctInput.value >> 24;
00340 versionString[5] = message.stctInput.value >> 16;
00341 versionString[6] = message.stctInput.value >> 8;
00342 versionString[7] = message.stctInput.value & 0xff;
00343
00344 int controllerType = 0;
00345 char firmwareVersion[9] = {0};
00346 sscanf (versionString,"%dV%s",&controllerType, firmwareVersion);
00347 std::string version(firmwareVersion);
00348 size_t founddot = version.find(".");
00349 while(founddot != std::string::npos){
00350 version.erase(founddot,1);
00351 founddot = version.find(".");
00352 }
00353 parameter.setParameter(controllerType, version);
00354 return;
00355
00356 }
00357
00358
00359 void YouBotJoint::setConfigurationParameter(const YouBotSlaveMailboxMsg& parameter) {
00360
00361 if (!setValueToMotorContoller(parameter)) {
00362 throw JointParameterException("Unable to set parameter at joint: " + this->storage.jointName);
00363 }
00364
00365 }
00366
00367
00368 void YouBotJoint::getConfigurationParameter(YouBotSlaveMailboxMsg& parameter) {
00369
00370 if (!retrieveValueFromMotorContoller(parameter)) {
00371 throw JointParameterException("Unable to get parameter from joint: " + this->storage.jointName);
00372 }
00373 this->parseMailboxStatusFlags(parameter);
00374
00375 }
00376
00377 void YouBotJoint::getConfigurationParameter(TorqueConstant& parameter) {
00378
00379 parameter.setParameter(this->storage.torqueConstant);
00380
00381 }
00382
00383 void YouBotJoint::setConfigurationParameter(const TorqueConstant& parameter) {
00384
00385 parameter.getParameter(this->storage.torqueConstant);
00386
00387 }
00388
00389
00390
00391 void YouBotJoint::storeConfigurationParameterPermanent(const YouBotJointParameter& parameter) {
00392
00393 if (parameter.getType() == MOTOR_CONTOLLER_PARAMETER) {
00394
00395 this->setConfigurationParameter(parameter);
00396
00397 YouBotSlaveMailboxMsg message;
00398 parameter.getYouBotMailboxMsg(message, STAP, storage);
00399
00400 if (!setValueToMotorContoller(message)) {
00401 throw JointParameterException("Unable to store parameter: " + parameter.getName() + " to joint: " + this->storage.jointName);
00402 }
00403 }else{
00404 throw JointParameterException("Parameter " + parameter.getName() + " is not a motor controller parameter of a joint");
00405 }
00406
00407 }
00408
00409
00410 void YouBotJoint::restoreConfigurationParameter(YouBotJointParameter& parameter) {
00411
00412 if (parameter.getType() == MOTOR_CONTOLLER_PARAMETER) {
00413
00414 YouBotSlaveMailboxMsg message;
00415 parameter.getYouBotMailboxMsg(message, RSAP, storage);
00416
00417 if (!setValueToMotorContoller(message)) {
00418 throw JointParameterException("Unable to restore parameter: " + parameter.getName() + " at joint: " + this->storage.jointName);
00419 }
00420
00421 this->getConfigurationParameter(parameter);
00422 }else{
00423 throw JointParameterException("Parameter " + parameter.getName() + " is not a motor controller parameter of a joint");
00424 }
00425
00426 }
00427
00428 void YouBotJoint::setData(const JointDataSetpoint& data) {
00429
00430 LOG(info) << "Nothing to do";
00431
00432 }
00433
00434 void YouBotJoint::getData(JointData& data) {
00435
00436 LOG(info) << "Nothing to do";
00437
00438 }
00439
00440
00441
00442 void YouBotJoint::setData(const JointAngleSetpoint& data) {
00443
00444
00445 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00446 throw EtherCATConnectionException("No EtherCAT connection");
00447 }
00448
00449
00450 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00451 this->parseYouBotErrorFlags(messageBuffer);
00452
00453 if (storage.gearRatio == 0) {
00454 throw std::out_of_range("A Gear Ratio of zero is not allowed");
00455 }
00456
00457 if (storage.encoderTicksPerRound == 0) {
00458 throw std::out_of_range("Zero Encoder Ticks per Round are not allowed");
00459 }
00460
00461 if (this->limitMonitor != 0)
00462 this->limitMonitor->checkLimitsPositionControl(data.angle);
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 messageBuffer.stctOutput.controllerMode = POSITION_CONTROL;
00484 messageBuffer.stctOutput.value = (int32) round((data.angle.value() * ((double) storage.encoderTicksPerRound / (2.0 * M_PI))) / storage.gearRatio);
00485
00486
00487 if (storage.inverseMovementDirection) {
00488 messageBuffer.stctOutput.value *= -1;
00489 }
00490
00491 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00492
00493 }
00494
00495
00496
00497 void YouBotJoint::setData(const JointEncoderSetpoint& data) {
00498
00499
00500 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00501 throw EtherCATConnectionException("No EtherCAT connection");
00502 }
00503
00504
00505 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00506 this->parseYouBotErrorFlags(messageBuffer);
00507
00508 if (this->limitMonitor != 0)
00509 this->limitMonitor->checkLimitsEncoderPosition(data.encoderTicks);
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 messageBuffer.stctOutput.controllerMode = POSITION_CONTROL;
00523 messageBuffer.stctOutput.value = data.encoderTicks;
00524
00525 if (storage.inverseMovementDirection) {
00526 messageBuffer.stctOutput.value *= -1;
00527 }
00528
00529 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00530
00531 }
00532
00533
00534
00535 void YouBotJoint::getData(JointSensedAngle& data) {
00536
00537
00538 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00539 throw EtherCATConnectionException("No EtherCAT connection");
00540 }
00541
00542 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00543 this->parseYouBotErrorFlags(messageBuffer);
00544
00545 if (storage.gearRatio == 0) {
00546 throw std::out_of_range("A Gear Ratio of zero is not allowed");
00547 }
00548 if (storage.encoderTicksPerRound == 0) {
00549 throw std::out_of_range("Zero Encoder Ticks per Round are not allowed");
00550 }
00551
00552 data.angle = ((double) messageBuffer.stctInput.actualPosition / storage.encoderTicksPerRound) * storage.gearRatio * (2.0 * M_PI) * radian;
00553
00554 if (storage.inverseMovementDirection) {
00555 data.angle = -data.angle;
00556 }
00557
00558 }
00559
00560
00561
00562 void YouBotJoint::setData(const JointVelocitySetpoint& data) {
00563
00564 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00565 throw EtherCATConnectionException("No EtherCAT connection");
00566 }
00567
00568
00569 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00570 this->parseYouBotErrorFlags(messageBuffer);
00571
00572 messageBuffer.stctOutput.controllerMode = VELOCITY_CONTROL;
00573
00574 if (storage.gearRatio == 0) {
00575 throw std::out_of_range("A Gear Ratio of 0 is not allowed");
00576 }
00577
00578 messageBuffer.stctOutput.value = (int32) round((data.angularVelocity.value() / (storage.gearRatio * 2.0 * M_PI)) * 60.0);
00579 if (storage.inverseMovementDirection) {
00580 messageBuffer.stctOutput.value *= -1;
00581 }
00582
00583
00584 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00585
00586 }
00587
00588
00589
00590 void YouBotJoint::getData(JointSensedVelocity& data) {
00591
00592 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00593 throw EtherCATConnectionException("No EtherCAT connection");
00594 }
00595
00596
00597 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00598 this->parseYouBotErrorFlags(messageBuffer);
00599
00600 if (storage.gearRatio == 0) {
00601 throw std::out_of_range("A Gear Ratio of 0 is not allowed");
00602 }
00603 double motorRPM = messageBuffer.stctInput.actualVelocity;
00604
00605 data.angularVelocity = ((motorRPM / 60.0) * storage.gearRatio * 2.0 * M_PI) * radian_per_second;
00606
00607 if (storage.inverseMovementDirection) {
00608 data.angularVelocity *= -1;
00609 }
00610
00611 }
00612
00613
00614
00615 void YouBotJoint::getData(JointSensedRoundsPerMinute& data) {
00616
00617 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00618 throw EtherCATConnectionException("No EtherCAT connection");
00619 }
00620
00621
00622 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00623 this->parseYouBotErrorFlags(messageBuffer);
00624
00625 data.rpm = messageBuffer.stctInput.actualVelocity;
00626
00627 if (storage.inverseMovementDirection) {
00628 data.rpm *= -1;
00629 }
00630
00631 }
00632
00633
00634
00635 void YouBotJoint::setData(const JointRoundsPerMinuteSetpoint& data) {
00636
00637 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00638 throw EtherCATConnectionException("No EtherCAT connection");
00639 }
00640
00641
00642 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00643 this->parseYouBotErrorFlags(messageBuffer);
00644
00645 messageBuffer.stctOutput.controllerMode = VELOCITY_CONTROL;
00646 messageBuffer.stctOutput.value = data.rpm;
00647
00648 if (storage.inverseMovementDirection) {
00649 messageBuffer.stctOutput.value *= -1;
00650 }
00651
00652 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00653
00654 }
00655
00656
00657
00658 void YouBotJoint::getData(JointSensedCurrent& data) {
00659
00660 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00661 throw EtherCATConnectionException("No EtherCAT connection");
00662 }
00663
00664
00665 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00666 this->parseYouBotErrorFlags(messageBuffer);
00667
00668 double current = messageBuffer.stctInput.actualCurrent;
00669 data.current = current / 1000.0 * ampere;
00670
00671 if (storage.inverseMovementDirection) {
00672 data.current *= -1;
00673 }
00674
00675 }
00676
00677
00678
00679 void YouBotJoint::setData(const JointCurrentSetpoint& data) {
00680
00681 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00682 throw EtherCATConnectionException("No EtherCAT connection");
00683 }
00684
00685
00686 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00687 this->parseYouBotErrorFlags(messageBuffer);
00688
00689 messageBuffer.stctOutput.controllerMode = CURRENT_MODE;
00690 messageBuffer.stctOutput.value = (int32) (data.current.value() * 1000.0);
00691
00692 if (storage.inverseMovementDirection) {
00693 messageBuffer.stctOutput.value *= -1;
00694 }
00695 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00696
00697 }
00698
00699
00700
00701 void YouBotJoint::getData(JointSensedEncoderTicks& data) {
00702
00703 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00704 throw EtherCATConnectionException("No EtherCAT connection");
00705 }
00706
00707
00708 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00709 this->parseYouBotErrorFlags(messageBuffer);
00710
00711
00712 data.encoderTicks = messageBuffer.stctInput.actualPosition ;
00713
00714 if (storage.inverseMovementDirection) {
00715 data.encoderTicks *= -1;
00716 }
00717
00718
00719 }
00720
00721
00722
00723
00724 void YouBotJoint::setData(const SlaveMessageOutput& data) {
00725
00726
00727 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00728 throw EtherCATConnectionException("No EtherCAT connection");
00729 }
00730
00731 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00732 this->parseYouBotErrorFlags(messageBuffer);
00733
00734 messageBuffer.stctOutput = data;
00735
00736 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
00737
00738 }
00739
00740
00741
00742
00743 void YouBotJoint::getData(YouBotSlaveMsg& data) {
00744
00745
00746 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00747 throw EtherCATConnectionException("No EtherCAT connection");
00748 }
00749
00750 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00751 this->parseYouBotErrorFlags(messageBuffer);
00752
00753 data = messageBuffer;
00754
00755 }
00756
00757
00758
00759 void YouBotJoint::setData(const JointTorqueSetpoint& data) {
00760
00761 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00762 throw EtherCATConnectionException("No EtherCAT connection");
00763 }
00764
00765 JointCurrentSetpoint currentSetpoint;
00766
00767 if (this->storage.torqueConstant == 0) {
00768 throw std::out_of_range("A torque constant of 0 is not allowed");
00769 }
00770
00771 currentSetpoint.current = ((data.torque.value()*this->storage.gearRatio)/this->storage.torqueConstant) * ampere;
00772 this->setData(currentSetpoint);
00773
00774 }
00775
00776
00777
00778 void YouBotJoint::getData(JointSensedTorque& data) {
00779
00780 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00781 throw EtherCATConnectionException("No EtherCAT connection");
00782 }
00783
00784 JointSensedCurrent sensedCurrent;
00785 this->getData(sensedCurrent);
00786
00787 if (this->storage.gearRatio == 0) {
00788 throw std::out_of_range("A Gear Ratio of 0 is not allowed");
00789 }
00790 data.torque = ((sensedCurrent.current.value() * this->storage.torqueConstant) / this->storage.gearRatio) * newton_meter;
00791
00792 }
00793
00794
00795
00796 void YouBotJoint::getData(JointAngleSetpoint& data) {
00797
00798
00799 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00800 throw EtherCATConnectionException("No EtherCAT connection");
00801 }
00802
00803 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00804 this->parseYouBotErrorFlags(messageBuffer);
00805
00806 if (storage.gearRatio == 0) {
00807 throw std::out_of_range("A Gear Ratio of zero is not allowed");
00808 }
00809 if (storage.encoderTicksPerRound == 0) {
00810 throw std::out_of_range("Zero Encoder Ticks per Round are not allowed");
00811 }
00812
00813 data.angle = ((double) messageBuffer.stctInput.targetPosition / storage.encoderTicksPerRound) * storage.gearRatio * (2.0 * M_PI) * radian;
00814
00815 if (storage.inverseMovementDirection) {
00816 data.angle = -data.angle;
00817 }
00818
00819 }
00820
00821
00822
00823 void YouBotJoint::getData(JointVelocitySetpoint& data) {
00824
00825 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00826 throw EtherCATConnectionException("No EtherCAT connection");
00827 }
00828
00829
00830 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00831 this->parseYouBotErrorFlags(messageBuffer);
00832
00833 if (storage.gearRatio == 0) {
00834 throw std::out_of_range("A Gear Ratio of 0 is not allowed");
00835 }
00836 double motorRPM = messageBuffer.stctInput.targetVelocity;
00837
00838 data.angularVelocity = ((motorRPM / 60.0) * storage.gearRatio * 2.0 * M_PI) * radian_per_second;
00839
00840 if (storage.inverseMovementDirection) {
00841 data.angularVelocity *= -1;
00842 }
00843
00844 }
00845
00846
00847
00848 void YouBotJoint::getData(JointCurrentSetpoint& data) {
00849
00850 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00851 throw EtherCATConnectionException("No EtherCAT connection");
00852 }
00853
00854
00855 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00856 this->parseYouBotErrorFlags(messageBuffer);
00857
00858 double current = messageBuffer.stctInput.targetCurrent;
00859 data.current = current / 1000.0 * ampere;
00860
00861 if (storage.inverseMovementDirection) {
00862 data.current *= -1;
00863 }
00864
00865 }
00866
00867
00868
00869 void YouBotJoint::getData(JointRampGeneratorVelocity& data) {
00870
00871 if(!ethercatMaster->isEtherCATConnectionEstablished()){
00872 throw EtherCATConnectionException("No EtherCAT connection");
00873 }
00874
00875
00876 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00877 this->parseYouBotErrorFlags(messageBuffer);
00878
00879 if (storage.gearRatio == 0) {
00880 throw std::out_of_range("A Gear Ratio of 0 is not allowed");
00881 }
00882 double motorRPM = messageBuffer.stctInput.rampGeneratorVelocity;
00883
00884 data.angularVelocity = ((motorRPM / 60.0) * storage.gearRatio * 2.0 * M_PI) * radian_per_second;
00885
00886 if (storage.inverseMovementDirection) {
00887 data.angularVelocity *= -1;
00888 }
00889
00890 }
00891
00892 void YouBotJoint::getUserVariable(const unsigned int index, int& data) {
00893
00894
00895 if(index == 0 || index > 55){
00896 throw JointParameterException("User variable index is out of range use 1-55 at: " + this->storage.jointName);
00897 }
00898
00899 YouBotSlaveMailboxMsg message;
00900 message.stctOutput.moduleAddress = DRIVE;
00901 message.stctOutput.commandNumber = GGP;
00902 message.stctOutput.typeNumber = index;
00903 message.stctOutput.motorNumber = USER_VARIABLE_BANK;
00904 message.stctOutput.value = 0;
00905
00906 if (!retrieveValueFromMotorContoller(message)) {
00907 throw JointParameterException("Unable to get parameter from joint: " + this->storage.jointName);
00908 }
00909 this->parseMailboxStatusFlags(message);
00910
00911 data = message.stctInput.value;
00912
00913 }
00914
00915 void YouBotJoint::setUserVariable(const unsigned int index, const int data) {
00916
00917
00918 if(index < 17 || index > 55){
00919 throw JointParameterException("User variable index is out of range use 17-55 at: " + this->storage.jointName);
00920 }
00921
00922 YouBotSlaveMailboxMsg message;
00923 message.stctOutput.moduleAddress = DRIVE;
00924 message.stctOutput.commandNumber = GGP;
00925 message.stctOutput.typeNumber = index;
00926 message.stctOutput.motorNumber = USER_VARIABLE_BANK;
00927 message.stctOutput.value = data;
00928
00929 if (!setValueToMotorContoller(message)) {
00930 throw JointParameterException("Unable to set parameter at joint: " + this->storage.jointName);
00931 }
00932 this->parseMailboxStatusFlags(message);
00933
00934 }
00935
00936
00937 void YouBotJoint::getStatus(std::vector<std::string>& statusMessages) {
00938
00939
00940 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
00941
00942
00943
00944
00945 if (messageBuffer.stctInput.errorFlags & OVER_CURRENT) {
00946 statusMessages.push_back(this->storage.jointName + " got over current");
00947 }
00948
00949 if (messageBuffer.stctInput.errorFlags & UNDER_VOLTAGE) {
00950 statusMessages.push_back(this->storage.jointName + " got under voltage");
00951 }
00952
00953 if (messageBuffer.stctInput.errorFlags & OVER_VOLTAGE) {
00954 statusMessages.push_back(this->storage.jointName + " got over voltage");
00955 }
00956
00957 if (messageBuffer.stctInput.errorFlags & OVER_TEMPERATURE) {
00958 statusMessages.push_back(this->storage.jointName + " got over temperature");
00959 }
00960
00961 if (messageBuffer.stctInput.errorFlags & MOTOR_HALTED) {
00962 statusMessages.push_back(this->storage.jointName + " is halted");
00963 }
00964
00965 if (messageBuffer.stctInput.errorFlags & HALL_SENSOR_ERROR) {
00966 statusMessages.push_back(this->storage.jointName + " got hall sensor problem");
00967 }
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981 if (messageBuffer.stctInput.errorFlags & VELOCITY_MODE) {
00982 statusMessages.push_back(this->storage.jointName + " has velocity mode active");
00983 }
00984
00985 if (messageBuffer.stctInput.errorFlags & POSITION_MODE) {
00986 statusMessages.push_back(this->storage.jointName + " has position mode active");
00987 }
00988
00989 if (messageBuffer.stctInput.errorFlags & TORQUE_MODE) {
00990 statusMessages.push_back(this->storage.jointName + " has torque mode active");
00991 }
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001 if (messageBuffer.stctInput.errorFlags & POSITION_REACHED) {
01002 statusMessages.push_back(this->storage.jointName + " has position reached");
01003 }
01004
01005 if (messageBuffer.stctInput.errorFlags & INITIALIZED) {
01006 statusMessages.push_back(this->storage.jointName + " is initialized");
01007 }
01008
01009 if (messageBuffer.stctInput.errorFlags & TIMEOUT) {
01010 statusMessages.push_back(this->storage.jointName + " has a timeout");
01011 }
01012
01013 if (messageBuffer.stctInput.errorFlags & I2T_EXCEEDED) {
01014 statusMessages.push_back(this->storage.jointName + " exceeded I2t");
01015 }
01016
01017
01018
01019 }
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 void YouBotJoint::getStatus(unsigned int& statusFlags) {
01041
01042
01043 ethercatMaster->getMsgBuffer(this->storage.jointNumber, messageBuffer);
01044
01045 statusFlags = messageBuffer.stctInput.errorFlags;
01046
01047 }
01048
01049
01050 void YouBotJoint::setEncoderToZero() {
01051
01052 if(!ethercatMaster->isEtherCATConnectionEstablished()){
01053 throw EtherCATConnectionException("No EtherCAT connection");
01054 }
01055
01056 YouBotSlaveMailboxMsg message;
01057 message.stctOutput.commandNumber = SAP;
01058 message.stctOutput.moduleAddress = DRIVE;
01059 message.stctOutput.typeNumber = 1;
01060 message.stctOutput.value = 0;
01061
01062 if (!setValueToMotorContoller(message)) {
01063 throw JointParameterException("Unable to set the encoders to zero at joint: " + this->storage.jointName);
01064 }
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074 }
01075
01076 void YouBotJoint::noMoreAction() {
01077
01078 if(!ethercatMaster->isEtherCATConnectionEstablished()){
01079 throw EtherCATConnectionException("No EtherCAT connection");
01080 }
01081
01082
01083 messageBuffer.stctOutput.controllerMode = NO_MORE_ACTION;
01084 messageBuffer.stctOutput.value = 0;
01085
01086 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
01087
01088 }
01089
01090 void YouBotJoint::stopJoint() {
01091
01092 if(!ethercatMaster->isEtherCATConnectionEstablished()){
01093 throw EtherCATConnectionException("No EtherCAT connection");
01094 }
01095
01096
01097 messageBuffer.stctOutput.controllerMode = MOTOR_STOP;
01098 messageBuffer.stctOutput.value = 0;
01099
01100 ethercatMaster->setMsgBuffer(messageBuffer, this->storage.jointNumber);
01101
01102 }
01103
01104 unsigned int YouBotJoint::getJointNumber() {
01105
01106 return this->storage.jointNumber;
01107
01108 }
01109
01110 void YouBotJoint::parseYouBotErrorFlags(const YouBotSlaveMsg& messageBuffer) {
01111
01112
01113 if (messageBuffer.stctInput.errorFlags & OVER_CURRENT) {
01114 LOG(warning) << this->storage.jointName << " over current";
01115
01116 }
01117
01118 if (messageBuffer.stctInput.errorFlags & UNDER_VOLTAGE) {
01119 LOG(warning) << this->storage.jointName << " under voltage";
01120
01121 }
01122
01123 if (messageBuffer.stctInput.errorFlags & OVER_VOLTAGE) {
01124 LOG(warning) << this->storage.jointName << " over voltage";
01125
01126 }
01127
01128 if (messageBuffer.stctInput.errorFlags & OVER_TEMPERATURE) {
01129 LOG(warning) << this->storage.jointName << " over temperature";
01130
01131 }
01132
01133 if (messageBuffer.stctInput.errorFlags & MOTOR_HALTED) {
01134
01135
01136 }
01137
01138 if (messageBuffer.stctInput.errorFlags & HALL_SENSOR_ERROR) {
01139 LOG(warning) << this->storage.jointName << " hall sensor problem";
01140
01141 }
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158 if (messageBuffer.stctInput.errorFlags & VELOCITY_MODE) {
01159
01160
01161 }
01162
01163 if (messageBuffer.stctInput.errorFlags & POSITION_MODE) {
01164
01165
01166 }
01167
01168 if (messageBuffer.stctInput.errorFlags & TORQUE_MODE) {
01169
01170
01171 }
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183 if (messageBuffer.stctInput.errorFlags & POSITION_REACHED) {
01184
01185
01186 }
01187
01188 if (!(messageBuffer.stctInput.errorFlags & INITIALIZED)) {
01189
01190
01191 }
01192
01193 if (messageBuffer.stctInput.errorFlags & TIMEOUT) {
01194 LOG(warning) << this->storage.jointName << " exceeded timeout";
01195
01196 }
01197
01198 if (messageBuffer.stctInput.errorFlags & I2T_EXCEEDED) {
01199 LOG(warning) << this->storage.jointName << " exceeded I2t";
01200
01201 }
01202
01203
01204 }
01205
01206 void YouBotJoint::parseMailboxStatusFlags(const YouBotSlaveMailboxMsg& mailboxMsg) {
01207
01208
01209 switch(mailboxMsg.stctInput.status){
01210 case NO_ERROR:
01211 break;
01212 case INVALID_COMMAND:
01213 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << "; Command no: " << mailboxMsg.stctOutput.commandNumber << " is an invalid command!" ;
01214
01215 break;
01216 case WRONG_TYPE:
01217 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << " has a wrong type!";
01218
01219 break;
01220 case INVALID_VALUE:
01221 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << " Value: " << mailboxMsg.stctOutput.value << " is a invalid value!";
01222
01223 break;
01224 case CONFIGURATION_EEPROM_LOCKED:
01225 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << " - Configuration EEPROM locked";
01226
01227 break;
01228 case COMMAND_NOT_AVAILABLE:
01229 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << " - Command is not available!";
01230
01231 break;
01232 case REPLY_WRITE_PROTECTED:
01233 LOG(error) << this->storage.jointName << "Parameter name: " << mailboxMsg.parameterName << " - Permissions denied!";
01234
01235 break;
01236 }
01237
01238
01239
01240 }
01241
01242 bool YouBotJoint::retrieveValueFromMotorContoller(YouBotSlaveMailboxMsg& message) {
01243
01244
01245 bool unvalid = true;
01246 unsigned int retry = 0;
01247
01248 ethercatMaster->setMailboxMsgBuffer(message, this->storage.jointNumber);
01249
01250 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
01251
01252 do {
01253 ethercatMaster->getMailboxMsgBuffer(message, this->storage.jointNumber);
01254
01255
01256
01257
01258
01259
01260 if (message.stctOutput.commandNumber == message.stctInput.commandNumber &&
01261 message.stctInput.status == NO_ERROR) {
01262 unvalid = false;
01263 } else {
01264 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
01265 retry++;
01266 }
01267 } while (retry < mailboxMsgRetries && unvalid);
01268
01269 if (unvalid) {
01270 this->parseMailboxStatusFlags(message);
01271 return false;
01272 } else {
01273 return true;
01274 }
01275
01276
01277 }
01278
01279 bool YouBotJoint::setValueToMotorContoller(const YouBotSlaveMailboxMsg& mailboxMsg) {
01280
01281
01282 YouBotSlaveMailboxMsg mailboxMsgBuffer;
01283 mailboxMsgBuffer = mailboxMsg;
01284 bool unvalid = true;
01285 unsigned int retry = 0;
01286
01287 ethercatMaster->setMailboxMsgBuffer(mailboxMsgBuffer, this->storage.jointNumber);
01288
01289 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
01290
01291 do {
01292 ethercatMaster->getMailboxMsgBuffer(mailboxMsgBuffer, this->storage.jointNumber);
01293
01294
01295
01296
01297
01298
01299 if (mailboxMsgBuffer.stctOutput.commandNumber == mailboxMsgBuffer.stctInput.commandNumber &&
01300 mailboxMsgBuffer.stctOutput.value == mailboxMsgBuffer.stctInput.value &&
01301 mailboxMsgBuffer.stctInput.status == NO_ERROR) {
01302 unvalid = false;
01303 } else {
01304 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
01305 retry++;
01306 }
01307 } while (retry < mailboxMsgRetries && unvalid);
01308
01309 if (unvalid) {
01310 this->parseMailboxStatusFlags(mailboxMsgBuffer);
01311 return false;
01312 } else {
01313 return true;
01314 }
01315
01316
01317 }
01318
01319
01320 }