YouBotGripper.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2011
00004  * All rights reserved.
00005  *
00006  * Hochschule Bonn-Rhein-Sieg
00007  * University of Applied Sciences
00008  * Computer Science Department
00009  *
00010  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00011  *
00012  * Author:
00013  * Jan Paulus, Nico Hochgeschwender, Michael Reckhaus, Azamat Shakhimardanov
00014  * Supervised by:
00015  * Gerhard K. Kraetzschmar
00016  *
00017  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00018  *
00019  * This sofware is published under a dual-license: GNU Lesser General Public 
00020  * License LGPL 2.1 and BSD license. The dual-license implies that users of this
00021  * code may choose which terms they prefer.
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Hochschule Bonn-Rhein-Sieg nor the names of its
00034  *       contributors may be used to endorse or promote products derived from
00035  *       this software without specific prior written permission.
00036  *
00037  * This program is free software: you can redistribute it and/or modify
00038  * it under the terms of the GNU Lesser General Public License LGPL as
00039  * published by the Free Software Foundation, either version 2.1 of the
00040  * License, or (at your option) any later version or the BSD license.
00041  *
00042  * This program is distributed in the hope that it will be useful,
00043  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00044  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00045  * GNU Lesser General Public License LGPL and the BSD license for more details.
00046  *
00047  * You should have received a copy of the GNU Lesser General Public
00048  * License LGPL and BSD license along with this program.
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   // Bouml preserved body begin 0005EFF1
00058   this->jointNumber = jointNo;
00059   this->mailboxMsgRetries = 200;
00060   this->timeTillNextMailboxUpdate = 1; //ms
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   // Bouml preserved body end 0005EFF1
00067 }
00068 
00069 YouBotGripper::~YouBotGripper()
00070 {
00071   // Bouml preserved body begin 0005F071
00072   // Bouml preserved body end 0005F071
00073 }
00074 
00075 void YouBotGripper::getConfigurationParameter(GripperParameter& parameter) const
00076 {
00077   // Bouml preserved body begin 0005FBF1
00078   throw std::runtime_error("Please use YouBotGripperParameter");
00079   // Bouml preserved body end 0005FBF1
00080 }
00081 
00082 void YouBotGripper::setConfigurationParameter(const GripperParameter& parameter)
00083 {
00084   // Bouml preserved body begin 0005FA71
00085   throw std::runtime_error("Please use YouBotGripperParameter");
00086   // Bouml preserved body end 0005FA71
00087 }
00088 
00089 void YouBotGripper::getConfigurationParameter(GripperFirmwareVersion& parameter) const
00090 {
00091   // Bouml preserved body begin 000BEF71
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   //  versionString[0] = message.stctInput.replyAddress;
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   // LOG(warning) <<"version: "<< versionString;
00133   int controllerType = 0;
00134   float firmwareVersion = 0;
00135   sscanf(versionString, "%dV%f", &controllerType, &firmwareVersion); //KR842V20
00136 
00137   parameter.setParameter(controllerType, firmwareVersion);
00138 
00139   return;
00140   // Bouml preserved body end 000BEF71
00141 }
00142 
00143 void YouBotGripper::setConfigurationParameter(const CalibrateGripper& parameter)
00144 {
00145   // Bouml preserved body begin 00048271
00146 
00147   char index = 16; // Parameter 0 to 15 of bank 2 are password protected
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; //move gripper relative
00191     message.stctOutput.value = -maxenc;
00192     message.stctOutput.motorNumber = 0; //move bar 0
00193     setValueToMotorContoller(message);
00194     message.stctOutput.motorNumber = 1; //move bar 1
00195     setValueToMotorContoller(message);
00196 
00197     //open gripper
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     //close gripper
00217     message.stctOutput.moduleAddress = GRIPPER;
00218     message.stctOutput.commandNumber = MVP;
00219     message.stctOutput.typeNumber = 1; //move gripper relative
00220     message.stctOutput.value = maxenc;
00221     message.stctOutput.motorNumber = 0; //move bar 0
00222     setValueToMotorContoller(message);
00223     message.stctOutput.motorNumber = 1; //move bar 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     //stop Gripper motor
00241     /*
00242      message.stctOutput.moduleAddress = GRIPPER;
00243      message.stctOutput.commandNumber = MST;
00244      message.stctOutput.value = 0;
00245      message.stctOutput.motorNumber = 0; //move bar 0
00246      setValueToMotorContoller(message);
00247      message.stctOutput.motorNumber = 1; //move bar 1
00248      setValueToMotorContoller(message);
00249      */
00250 
00251     // set pose to zero as reference
00252     ActualPosition actualPose;
00253     actualPose.setParameter(0);
00254     bar1->setConfigurationParameter(actualPose);
00255     bar2->setConfigurationParameter(actualPose);
00256 
00257     // set a flag in the user variable to remember that it is calibrated
00258     this->setValueToMotorContoller(IsCalibratedSetMessage);
00259   }
00260 
00261   // Bouml preserved body end 00048271
00262 }
00263 
00264 void YouBotGripper::getConfigurationParameter(YouBotSlaveMailboxMsg& parameter) const
00265 {
00266   // Bouml preserved body begin 000DE971
00267   if (!retrieveValueFromMotorContoller(parameter))
00268   {
00269     throw JointParameterException("Unable to get parameter from the gripper");
00270   }
00271   this->parseMailboxStatusFlags(parameter);
00272   // Bouml preserved body end 000DE971
00273 }
00274 
00275 void YouBotGripper::getData(const GripperData& data) const
00276 {
00277   // Bouml preserved body begin 0005FB71
00278   LOG(info) << "Nothing to do";
00279   // Bouml preserved body end 0005FB71
00280 }
00281 
00282 void YouBotGripper::setData(const GripperData& data)
00283 {
00284   // Bouml preserved body begin 0005FAF1
00285   LOG(info) << "Nothing to do";
00286   // Bouml preserved body end 0005FAF1
00287 }
00288 
00289 void YouBotGripper::getData(OneDOFGripperData& data) const
00290 {
00291   // Bouml preserved body begin 000483F1
00292   LOG(info) << "Nothing to do";
00293   // Bouml preserved body end 000483F1
00294 }
00295 
00296 void YouBotGripper::setData(const OneDOFGripperData& data)
00297 {
00298   // Bouml preserved body begin 00048371
00299   LOG(info) << "Nothing to do";
00300   // Bouml preserved body end 00048371
00301 }
00302 
00303 void YouBotGripper::setData(const GripperBarSpacingSetPoint& barSpacing)
00304 {
00305   // Bouml preserved body begin 0005F8F1
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   // Bouml preserved body end 0005F8F1
00317 }
00318 
00319 void YouBotGripper::getData(GripperSensedBarSpacing& barSpacing) const
00320 {
00321   // Bouml preserved body begin 0005F971
00322   GripperSensedBarPosition bar1Position;
00323   GripperSensedBarPosition bar2Position;
00324   bar1->getData(bar1Position);
00325   bar2->getData(bar2Position);
00326 
00327   barSpacing.barSpacing = bar1Position.barPosition + bar2Position.barPosition;
00328 
00329   // Bouml preserved body end 0005F971
00330 }
00331 
00332 void YouBotGripper::open()
00333 {
00334   // Bouml preserved body begin 000E3BF1
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   // Bouml preserved body end 000E3BF1
00354 }
00355 
00356 void YouBotGripper::close()
00357 {
00358   // Bouml preserved body begin 00103D71
00359   GripperBarEncoderSetpoint setpointBar1;
00360   GripperBarEncoderSetpoint setpointBar2;
00361   setpointBar1.barEncoder = 0;
00362   setpointBar2.barEncoder = 0;
00363 
00364   bar1->setData(setpointBar1);
00365   bar2->setData(setpointBar2);
00366   // Bouml preserved body end 00103D71
00367 }
00368 
00369 YouBotGripperBar& YouBotGripper::getGripperBar1()
00370 {
00371   // Bouml preserved body begin 000E0FF1
00372   if (bar1 == NULL)
00373     throw std::runtime_error("gripper bar 1 is missing");
00374 
00375   return *bar1;
00376   // Bouml preserved body end 000E0FF1
00377 }
00378 
00379 YouBotGripperBar& YouBotGripper::getGripperBar2()
00380 {
00381   // Bouml preserved body begin 000E1071
00382   if (bar2 == NULL)
00383     throw std::runtime_error("gripper bar 2 is missing");
00384 
00385   return *bar2;
00386   // Bouml preserved body end 000E1071
00387 }
00388 
00389 void YouBotGripper::parseMailboxStatusFlags(const YouBotSlaveMailboxMsg& mailboxMsg) const
00390 {
00391   // Bouml preserved body begin 00075C71
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       //    throw JointParameterException(errorMessage + "invalid command");
00405       break;
00406     case WRONG_TYPE:
00407       LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " has a wrong type!";
00408       //    throw JointParameterException(errorMessage + "wrong type");
00409       break;
00410     case INVALID_VALUE:
00411       LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " Value: "
00412           << mailboxMsg.stctOutput.value << " is a invalid value!";
00413       //    throw JointParameterException(errorMessage + "invalid value");
00414       break;
00415     case CONFIGURATION_EEPROM_LOCKED:
00416       LOG(error) << errorMessage << "Parameter name: " << mailboxMsg.parameterName << " Configuration EEPROM locked";
00417       //    throw JointParameterException(errorMessage + "configuration EEPROM locked");
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       //    throw JointParameterException(errorMessage + "command not available");
00423       break;
00424   }
00425 
00426   // Bouml preserved body end 00075C71
00427 }
00428 
00429 bool YouBotGripper::setValueToMotorContoller(const YouBotSlaveMailboxMsg& mailboxMsg) const
00430 {
00431   // Bouml preserved body begin 0005EF71
00432 
00433   YouBotSlaveMailboxMsg mailboxMsgBuffer;
00434   mailboxMsgBuffer = mailboxMsg;
00435   bool unvalid = true;
00436   unsigned int retry = 0;
00437 
00438   ethercatMaster->setMailboxMsgBuffer(mailboxMsgBuffer, this->jointNumber);
00439   //    LOG(trace) << "set Output CommandNumber " << (int) mailboxMsgBuffer.stctOutput.commandNumber
00440   //                  << " moduleAddress " << (int) mailboxMsgBuffer.stctOutput.moduleAddress
00441   //                  << " motorNumber " << (int) mailboxMsgBuffer.stctOutput.motorNumber
00442   //                  << " typeNumber " << (int) mailboxMsgBuffer.stctOutput.typeNumber
00443   //                  << " value " << mailboxMsgBuffer.stctOutput.value;
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     //      LOG(trace) << "set Input CommandNumber " << (int) mailboxMsgBuffer.stctInput.commandNumber
00461     //                  << " moduleAddress " << (int) mailboxMsgBuffer.stctInput.moduleAddress
00462     //                  << " replyAddress " << (int) mailboxMsgBuffer.stctInput.replyAddress
00463     //                  << " status " << (int) mailboxMsgBuffer.stctInput.status
00464     //                  << " value " << mailboxMsgBuffer.stctInput.value;
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   // Bouml preserved body end 0005EF71
00478 }
00479 
00480 bool YouBotGripper::retrieveValueFromMotorContoller(YouBotSlaveMailboxMsg& message) const
00481 {
00482   // Bouml preserved body begin 0005EEF1
00483 
00484   bool unvalid = true;
00485   unsigned int retry = 0;
00486 
00487   ethercatMaster->setMailboxMsgBuffer(message, this->jointNumber);
00488   //     LOG(trace) << "get Output CommandNumber " << (int) message.stctOutput.commandNumber
00489   //                  << " moduleAddress " << (int) message.stctOutput.moduleAddress
00490   //                  << " motorNumber " << (int) message.stctOutput.motorNumber
00491   //                  << " typeNumber " << (int) message.stctOutput.typeNumber
00492   //                  << " value " << message.stctOutput.value
00493   //                  << " No " << this->jointNumber;
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     //      LOG(trace) << "get input CommandNumber " << (int) message.stctInput.commandNumber
00510     //                 << " moduleAddress " << (int) message.stctInput.moduleAddress
00511     //                 << " replyAddress " << (int) message.stctInput.replyAddress
00512     //                 << " status " << (int) message.stctInput.status
00513     //                 << " value " << message.stctInput.value
00514     //                 << " No " << this->jointNumber;
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   // Bouml preserved body end 0005EEF1
00529 }
00530 
00531 } // namespace youbot


youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Oct 6 2014 09:08:01