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 #include <youbot_driver/youbot/YouBotGripper.hpp>
00052 namespace youbot
00053 {
00054
00055 YouBotGripper::YouBotGripper(const unsigned int jointNo, const std::string& configFilePath)
00056 {
00057
00058 this->jointNumber = jointNo;
00059 this->mailboxMsgRetries = 200;
00060 this->timeTillNextMailboxUpdate = 1;
00061
00062 ethercatMaster = &(EthercatMaster::getInstance("youbot-ethercat.cfg", configFilePath));
00063 bar1.reset(new YouBotGripperBar(0, jointNo, configFilePath));
00064 bar2.reset(new YouBotGripperBar(1, jointNo, configFilePath));
00065
00066
00067 }
00068
00069 YouBotGripper::~YouBotGripper()
00070 {
00071
00072
00073 }
00074
00075 void YouBotGripper::getConfigurationParameter(GripperParameter& parameter) const
00076 {
00077
00078 throw std::runtime_error("Please use YouBotGripperParameter");
00079
00080 }
00081
00082 void YouBotGripper::setConfigurationParameter(const GripperParameter& parameter)
00083 {
00084
00085 throw std::runtime_error("Please use YouBotGripperParameter");
00086
00087 }
00088
00089 void YouBotGripper::getConfigurationParameter(GripperFirmwareVersion& parameter) const
00090 {
00091
00092
00093 YouBotSlaveMailboxMsg message;
00094 parameter.getYouBotMailboxMsg(message);
00095
00096 bool unvalid = true;
00097 unsigned int retry = 0;
00098
00099 ethercatMaster->setMailboxMsgBuffer(message, this->jointNumber);
00100 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00101
00102 do
00103 {
00104 if (ethercatMaster->getMailboxMsgBuffer(message, this->jointNumber))
00105 {
00106 unvalid = false;
00107 }
00108 else
00109 {
00110 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00111 retry++;
00112 }
00113 } while (retry < mailboxMsgRetries && unvalid);
00114
00115 if (unvalid)
00116 {
00117 this->parseMailboxStatusFlags(message);
00118 throw std::runtime_error("Unable to get firmware version of the gripper");
00119 return;
00120 }
00121
00122 char versionString[8] = {0};
00123
00124 versionString[0] = message.stctInput.moduleAddress;
00125 versionString[1] = message.stctInput.status;
00126 versionString[2] = message.stctInput.commandNumber;
00127 versionString[3] = message.stctInput.value >> 24;
00128 versionString[4] = message.stctInput.value >> 16;
00129 versionString[5] = message.stctInput.value >> 8;
00130 versionString[6] = message.stctInput.value & 0xff;
00131
00132
00133 int controllerType = 0;
00134 float firmwareVersion = 0;
00135 sscanf(versionString, "%dV%f", &controllerType, &firmwareVersion);
00136
00137 parameter.setParameter(controllerType, firmwareVersion);
00138
00139 return;
00140
00141 }
00142
00143 void YouBotGripper::setConfigurationParameter(const CalibrateGripper& parameter)
00144 {
00145
00146
00147 char index = 16;
00148 YouBotSlaveMailboxMsg IsCalibratedReadMessage;
00149 IsCalibratedReadMessage.stctOutput.moduleAddress = GRIPPER;
00150 IsCalibratedReadMessage.stctOutput.commandNumber = GGP;
00151 IsCalibratedReadMessage.stctOutput.typeNumber = index;
00152 IsCalibratedReadMessage.stctOutput.motorNumber = USER_VARIABLE_BANK;
00153 IsCalibratedReadMessage.stctOutput.value = 0;
00154 IsCalibratedReadMessage.stctInput.value = 0;
00155
00156 YouBotSlaveMailboxMsg IsCalibratedSetMessage;
00157 IsCalibratedSetMessage.stctOutput.moduleAddress = GRIPPER;
00158 IsCalibratedSetMessage.stctOutput.commandNumber = SGP;
00159 IsCalibratedSetMessage.stctOutput.typeNumber = index;
00160 IsCalibratedSetMessage.stctOutput.motorNumber = USER_VARIABLE_BANK;
00161 IsCalibratedSetMessage.stctOutput.value = 1;
00162
00163 bool doCalibration = true;
00164 if (parameter.value == false)
00165 {
00166 if (!retrieveValueFromMotorContoller(IsCalibratedReadMessage))
00167 {
00168 IsCalibratedReadMessage.stctInput.value = 0;
00169 }
00170
00171 if (IsCalibratedReadMessage.stctInput.value == 1)
00172 {
00173 doCalibration = false;
00174 }
00175 }
00176
00177 if (doCalibration)
00178 {
00179 LOG(info) << "Calibrate Gripper";
00180
00181 YouBotSlaveMailboxMsg message;
00182
00183 unsigned int maxenc = 0;
00184 MaxEncoderValue maxencoder;
00185 bar1->getConfigurationParameter(maxencoder);
00186 maxencoder.getParameter(maxenc);
00187
00188 message.stctOutput.moduleAddress = GRIPPER;
00189 message.stctOutput.commandNumber = MVP;
00190 message.stctOutput.typeNumber = 1;
00191 message.stctOutput.value = -maxenc;
00192 message.stctOutput.motorNumber = 0;
00193 setValueToMotorContoller(message);
00194 message.stctOutput.motorNumber = 1;
00195 setValueToMotorContoller(message);
00196
00197
00198 TargetPositionReached bar1TargetReched;
00199 TargetPositionReached bar2TargetReched;
00200 bool targetReachedBar1 = false;
00201 bool targetReachedBar2 = false;
00202
00203 for (int i = 0; i < 40; i++)
00204 {
00205 SLEEP_MILLISEC(100);
00206 bar1->getConfigurationParameter(bar1TargetReched);
00207 bar1TargetReched.getParameter(targetReachedBar1);
00208 bar2->getConfigurationParameter(bar2TargetReched);
00209 bar2TargetReched.getParameter(targetReachedBar2);
00210 if (targetReachedBar1 && targetReachedBar2)
00211 {
00212 break;
00213 }
00214 }
00215
00216
00217 message.stctOutput.moduleAddress = GRIPPER;
00218 message.stctOutput.commandNumber = MVP;
00219 message.stctOutput.typeNumber = 1;
00220 message.stctOutput.value = maxenc;
00221 message.stctOutput.motorNumber = 0;
00222 setValueToMotorContoller(message);
00223 message.stctOutput.motorNumber = 1;
00224 setValueToMotorContoller(message);
00225
00226 targetReachedBar1 = false;
00227 targetReachedBar2 = false;
00228
00229 for (int i = 0; i < 40; i++)
00230 {
00231 SLEEP_MILLISEC(100);
00232 bar1->getConfigurationParameter(bar1TargetReched);
00233 bar1TargetReched.getParameter(targetReachedBar1);
00234 bar2->getConfigurationParameter(bar2TargetReched);
00235 bar2TargetReched.getParameter(targetReachedBar2);
00236 if (targetReachedBar1 && targetReachedBar2)
00237 break;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 ActualPosition actualPose;
00253 actualPose.setParameter(0);
00254 bar1->setConfigurationParameter(actualPose);
00255 bar2->setConfigurationParameter(actualPose);
00256
00257
00258 this->setValueToMotorContoller(IsCalibratedSetMessage);
00259 }
00260
00261
00262 }
00263
00264 void YouBotGripper::getConfigurationParameter(YouBotSlaveMailboxMsg& parameter) const
00265 {
00266
00267 if (!retrieveValueFromMotorContoller(parameter))
00268 {
00269 throw JointParameterException("Unable to get parameter from the gripper");
00270 }
00271 this->parseMailboxStatusFlags(parameter);
00272
00273 }
00274
00275 void YouBotGripper::getData(const GripperData& data) const
00276 {
00277
00278 LOG(info) << "Nothing to do";
00279
00280 }
00281
00282 void YouBotGripper::setData(const GripperData& data)
00283 {
00284
00285 LOG(info) << "Nothing to do";
00286
00287 }
00288
00289 void YouBotGripper::getData(OneDOFGripperData& data) const
00290 {
00291
00292 LOG(info) << "Nothing to do";
00293
00294 }
00295
00296 void YouBotGripper::setData(const OneDOFGripperData& data)
00297 {
00298
00299 LOG(info) << "Nothing to do";
00300
00301 }
00302
00303 void YouBotGripper::setData(const GripperBarSpacingSetPoint& barSpacing)
00304 {
00305
00306
00307 GripperBarPositionSetPoint setpointBar1;
00308 GripperBarPositionSetPoint setpointBar2;
00309
00310 setpointBar1.barPosition = barSpacing.barSpacing / 2.0;
00311 setpointBar2.barPosition = barSpacing.barSpacing / 2.0;
00312
00313 bar1->setData(setpointBar1);
00314 bar2->setData(setpointBar2);
00315
00316
00317 }
00318
00319 void YouBotGripper::getData(GripperSensedBarSpacing& barSpacing) const
00320 {
00321
00322 GripperSensedBarPosition bar1Position;
00323 GripperSensedBarPosition bar2Position;
00324 bar1->getData(bar1Position);
00325 bar2->getData(bar2Position);
00326
00327 barSpacing.barSpacing = bar1Position.barPosition + bar2Position.barPosition;
00328
00329
00330 }
00331
00332 void YouBotGripper::open()
00333 {
00334
00335
00336 MaxEncoderValue maxEnc;
00337 unsigned int bar1MaxEncoderValue = 0;
00338 unsigned int bar2MaxEncoderValue = 0;
00339
00340 bar1->getConfigurationParameter(maxEnc);
00341 maxEnc.getParameter(bar1MaxEncoderValue);
00342 bar2->getConfigurationParameter(maxEnc);
00343 maxEnc.getParameter(bar2MaxEncoderValue);
00344
00345 GripperBarEncoderSetpoint setpointBar1;
00346 GripperBarEncoderSetpoint setpointBar2;
00347 setpointBar1.barEncoder = bar1MaxEncoderValue;
00348 setpointBar2.barEncoder = bar2MaxEncoderValue;
00349
00350 bar1->setData(setpointBar1);
00351 bar2->setData(setpointBar2);
00352
00353
00354 }
00355
00356 void YouBotGripper::close()
00357 {
00358
00359 GripperBarEncoderSetpoint setpointBar1;
00360 GripperBarEncoderSetpoint setpointBar2;
00361 setpointBar1.barEncoder = 0;
00362 setpointBar2.barEncoder = 0;
00363
00364 bar1->setData(setpointBar1);
00365 bar2->setData(setpointBar2);
00366
00367 }
00368
00369 YouBotGripperBar& YouBotGripper::getGripperBar1()
00370 {
00371
00372 if (bar1 == NULL)
00373 throw std::runtime_error("gripper bar 1 is missing");
00374
00375 return *bar1;
00376
00377 }
00378
00379 YouBotGripperBar& YouBotGripper::getGripperBar2()
00380 {
00381
00382 if (bar2 == NULL)
00383 throw std::runtime_error("gripper bar 2 is missing");
00384
00385 return *bar2;
00386
00387 }
00388
00389 void YouBotGripper::parseMailboxStatusFlags(const YouBotSlaveMailboxMsg& mailboxMsg) const
00390 {
00391
00392 std::stringstream errorMessageStream;
00393 errorMessageStream << "Joint " << this->jointNumber << ": ";
00394 std::string errorMessage;
00395 errorMessage = errorMessageStream.str();
00396
00397 switch (mailboxMsg.stctInput.status)
00398 {
00399 case NO_ERROR:
00400 break;
00401 case INVALID_COMMAND:
00402 LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << "; Command no: "
00403 << mailboxMsg.stctOutput.commandNumber << " is an invalid command!";
00404
00405 break;
00406 case WRONG_TYPE:
00407 LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " has a wrong type!";
00408
00409 break;
00410 case INVALID_VALUE:
00411 LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " Value: "
00412 << mailboxMsg.stctOutput.value << " is a invalid value!";
00413
00414 break;
00415 case CONFIGURATION_EEPROM_LOCKED:
00416 LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " Configuration EEPROM locked";
00417
00418 break;
00419 case COMMAND_NOT_AVAILABLE:
00420 LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << "; Command no: "
00421 << mailboxMsg.stctOutput.commandNumber << "Command is not available!";
00422
00423 break;
00424 }
00425
00426
00427 }
00428
00429 bool YouBotGripper::setValueToMotorContoller(const YouBotSlaveMailboxMsg& mailboxMsg) const
00430 {
00431
00432
00433 YouBotSlaveMailboxMsg mailboxMsgBuffer;
00434 mailboxMsgBuffer = mailboxMsg;
00435 bool unvalid = true;
00436 unsigned int retry = 0;
00437
00438 ethercatMaster->setMailboxMsgBuffer(mailboxMsgBuffer, this->jointNumber);
00439
00440
00441
00442
00443
00444
00445 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00446
00447 do
00448 {
00449
00450 if (ethercatMaster->getMailboxMsgBuffer(mailboxMsgBuffer, this->jointNumber)
00451 && mailboxMsgBuffer.stctInput.status == NO_ERROR)
00452 {
00453 unvalid = false;
00454 }
00455 else
00456 {
00457 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00458 retry++;
00459 }
00460
00461
00462
00463
00464
00465 } while (retry < mailboxMsgRetries && unvalid);
00466
00467 if (unvalid)
00468 {
00469 this->parseMailboxStatusFlags(mailboxMsgBuffer);
00470 return false;
00471 }
00472 else
00473 {
00474 return true;
00475 }
00476
00477
00478 }
00479
00480 bool YouBotGripper::retrieveValueFromMotorContoller(YouBotSlaveMailboxMsg& message) const
00481 {
00482
00483
00484 bool unvalid = true;
00485 unsigned int retry = 0;
00486
00487 ethercatMaster->setMailboxMsgBuffer(message, this->jointNumber);
00488
00489
00490
00491
00492
00493
00494
00495 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00496
00497 do
00498 {
00499
00500 if (ethercatMaster->getMailboxMsgBuffer(message, this->jointNumber) && message.stctInput.status == NO_ERROR)
00501 {
00502 unvalid = false;
00503 }
00504 else
00505 {
00506 SLEEP_MILLISEC(timeTillNextMailboxUpdate);
00507 retry++;
00508 }
00509
00510
00511
00512
00513
00514
00515
00516 } while (retry < mailboxMsgRetries && unvalid);
00517
00518 if (unvalid)
00519 {
00520 this->parseMailboxStatusFlags(message);
00521 return false;
00522 }
00523 else
00524 {
00525 return true;
00526 }
00527
00528
00529 }
00530
00531 }