YouBotManipulator.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/YouBotManipulator.hpp>
00052 namespace youbot
00053 {
00054 
00055 YouBotManipulator::YouBotManipulator(const std::string name, const std::string configFilePath) :
00056     ethercatMaster(EthercatMaster::getInstance("youbot-ethercat.cfg", configFilePath))
00057 {
00058   // Bouml preserved body begin 00067F71
00059 
00060   this->controllerType = 841;
00061   this->alternativeControllerType = 1610;
00062   this->supportedFirmwareVersions.push_back("148");
00063   this->supportedFirmwareVersions.push_back("200");
00064   this->actualFirmwareVersionAllJoints = "";
00065 
00066   string filename;
00067   filename = name;
00068   filename.append(".cfg");
00069   useGripper = true;
00070   configfile.reset(new ConfigFile(filename, configFilePath));
00071 
00072   if (ethercatMaster.isThreadActive())
00073   {
00074     ethercatMasterWithThread = static_cast<EthercatMasterWithThread*>(&(EthercatMaster::getInstance(filename,
00075                                                                                                     configFilePath,
00076                                                                                                     true)));
00077   }
00078   else
00079   {
00080     ethercatMasterWithThread = NULL;
00081   }
00082 
00083   this->initializeJoints();
00084 
00085   // Bouml preserved body end 00067F71
00086 }
00087 
00088 YouBotManipulator::~YouBotManipulator()
00089 {
00090   // Bouml preserved body begin 00067FF1
00091   if (ethercatMaster.isThreadActive())
00092   {
00093     for (unsigned int i = 0; i < ARMJOINTS; i++)
00094     {
00095       ethercatMasterWithThread->deleteJointTrajectoryControllerRegistration(this->joints[i].getJointNumber());
00096     }
00097   }
00098   // Bouml preserved body end 00067FF1
00099 }
00100 
00102 void YouBotManipulator::doJointCommutation()
00103 {
00104   // Bouml preserved body begin 000A3371
00105 
00106   if (this->actualFirmwareVersionAllJoints == "148")
00107   {
00108     this->commutationFirmware148();
00109   }
00110   else if (this->actualFirmwareVersionAllJoints == "200")
00111   {
00112     this->commutationFirmware200();
00113   }
00114   else
00115   {
00116     throw std::runtime_error("Unable to commutate joints - Unsupported firmware version!");
00117   }
00118   // Bouml preserved body end 000A3371
00119 }
00120 
00122 void YouBotManipulator::calibrateManipulator(const bool forceCalibration)
00123 {
00124   // Bouml preserved body begin 000A9C71
00125 
00126   //Calibrate all manipulator joints
00127   std::vector<JointRoundsPerMinuteSetpoint> calibrationVel;
00128   JointRoundsPerMinuteSetpoint tempdummy;
00129   tempdummy.rpm = 0;
00130   calibrationVel.assign(ARMJOINTS, tempdummy);
00131   std::vector<quantity<si::current> > maxCurrent;
00132   quantity<si::current> tempdummy2;
00133   maxCurrent.assign(ARMJOINTS, tempdummy2);
00134   std::vector<bool> doCalibration;
00135   doCalibration.assign(ARMJOINTS, true);
00136   std::string jointName;
00137 
00138   double dummy = 0;
00139   char index = 16; // Parameter 0 to 15 of bank 2 are password protected
00140 
00141   YouBotSlaveMailboxMsg IsCalibratedReadMessage;
00142   IsCalibratedReadMessage.stctOutput.moduleAddress = DRIVE;
00143   IsCalibratedReadMessage.stctOutput.commandNumber = GGP;
00144   IsCalibratedReadMessage.stctOutput.typeNumber = index;
00145   IsCalibratedReadMessage.stctOutput.motorNumber = USER_VARIABLE_BANK;
00146   IsCalibratedReadMessage.stctOutput.value = 0;
00147   IsCalibratedReadMessage.stctInput.value = 0;
00148 
00149   YouBotSlaveMailboxMsg IsCalibratedSetMessage;
00150   IsCalibratedSetMessage.stctOutput.moduleAddress = DRIVE;
00151   IsCalibratedSetMessage.stctOutput.commandNumber = SGP;
00152   IsCalibratedSetMessage.stctOutput.typeNumber = index;
00153   IsCalibratedSetMessage.stctOutput.motorNumber = USER_VARIABLE_BANK;
00154   IsCalibratedSetMessage.stctOutput.value = 1;
00155 
00156   //get parameters for calibration
00157   for (unsigned int i = 0; i < ARMJOINTS; i++)
00158   {
00159 
00160     std::stringstream jointNameStream;
00161     jointNameStream << "Joint_" << i + 1;
00162     jointName = jointNameStream.str();
00163     bool calib = true;
00164     configfile->readInto(calib, jointName, "DoCalibration");
00165     doCalibration[i] = calib;
00166 
00167     joints[i].getConfigurationParameter(IsCalibratedReadMessage);
00168     if (IsCalibratedReadMessage.stctInput.value == 1)
00169     {
00170       doCalibration[i] = false;
00171     }
00172 
00173     if (forceCalibration)
00174     {
00175       doCalibration[i] = true;
00176     }
00177 
00178     configfile->readInto(dummy, jointName, "CalibrationMaxCurrent_[ampere]");
00179     maxCurrent[i] = dummy * ampere;
00180     std::string direction;
00181     configfile->readInto(direction, jointName, "CalibrationDirection");
00182     GearRatio gearRatio;
00183     joints[i].getConfigurationParameter(gearRatio);
00184     double gearratio = 1;
00185     gearRatio.getParameter(gearratio);
00186 
00187     if (direction == "POSITIV")
00188     {
00189       calibrationVel[i].rpm = 1 / gearratio;
00190     }
00191     else if (direction == "NEGATIV")
00192     {
00193       calibrationVel[i].rpm = -1 / gearratio;
00194     }
00195     else
00196     {
00197       throw std::runtime_error("Wrong calibration direction for " + jointName);
00198     }
00199   }
00200 
00201   LOG(info) << "Calibrate Manipulator Joints ";
00202 
00203   std::vector<bool> finished;
00204   finished.assign(ARMJOINTS, false);
00205   JointSensedCurrent sensedCurrent;
00206 
00207   //move the joints slowly in calibration direction
00208   for (unsigned int i = 0; i < ARMJOINTS; i++)
00209   {
00210     if (doCalibration[i] == true)
00211     {
00212       joints[i].setData(calibrationVel[i]);
00213       if (!ethercatMaster.isThreadActive())
00214       {
00215         ethercatMaster.sendProcessData();
00216         ethercatMaster.receiveProcessData();
00217       }
00218     }
00219     else
00220     {
00221       finished[i] = true;
00222     }
00223   }
00224 
00225   //monitor the current to find end stop
00226   while (!(finished[0] && finished[1] && finished[2] && finished[3] && finished[4]))
00227   {
00228     for (unsigned int i = 0; i < ARMJOINTS; i++)
00229     {
00230       if (!ethercatMaster.isThreadActive())
00231       {
00232         ethercatMaster.sendProcessData();
00233         ethercatMaster.receiveProcessData();
00234       }
00235       joints[i].getData(sensedCurrent);
00236       //turn till a max current is reached
00237       if (abs(sensedCurrent.current) > abs(maxCurrent[i]))
00238       {
00239         //stop movement
00240         youbot::JointCurrentSetpoint currentStopMovement;
00241         currentStopMovement.current = 0 * ampere;
00242         joints[i].setData(currentStopMovement);
00243         if (!ethercatMaster.isThreadActive())
00244         {
00245           ethercatMaster.sendProcessData();
00246           ethercatMaster.receiveProcessData();
00247         }
00248         finished[i] = true;
00249       }
00250     }
00251     SLEEP_MILLISEC(1);
00252   }
00253 
00254   // wait to let the joint stop the motion
00255   SLEEP_MILLISEC(100);
00256 
00257   for (unsigned int i = 0; i < ARMJOINTS; i++)
00258   {
00259     if (doCalibration[i] == true)
00260     {
00261       //set encoder reference position
00262       joints[i].setEncoderToZero();
00263       if (!ethercatMaster.isThreadActive())
00264       {
00265         ethercatMaster.sendProcessData();
00266         ethercatMaster.receiveProcessData();
00267       }
00268       // set a flag in the user variable to remember that it is calibrated
00269       joints[i].setConfigurationParameter(IsCalibratedSetMessage);
00270       //     LOG(info) << "Calibration finished for joint: " << this->jointName;
00271     }
00272   }
00273 
00274   //setting joint Limits
00275   JointLimits jLimits;
00276   for (unsigned int i = 0; i < ARMJOINTS; i++)
00277   {
00278     long upperlimit = 0, lowerlimit = 0;
00279     std::stringstream jointNameStream;
00280     bool inverted = false;
00281     jointNameStream << "Joint_" << i + 1;
00282     jointName = jointNameStream.str();
00283     JointEncoderSetpoint minEncoderValue;
00284     configfile->readInto(lowerlimit, jointName, "LowerLimit_[encoderTicks]");
00285     configfile->readInto(upperlimit, jointName, "UpperLimit_[encoderTicks]");
00286     configfile->readInto(inverted, jointName, "InverseMovementDirection");
00287 
00288     if (inverted)
00289     {
00290       minEncoderValue.encoderTicks = lowerlimit + 1000;
00291     }
00292     else
00293     {
00294       minEncoderValue.encoderTicks = upperlimit - 1000;
00295     }
00296 
00297     jLimits.setParameter(lowerlimit, upperlimit, true);
00298     joints[i].setConfigurationParameter(jLimits);
00299     // joints[i].setData(minEncoderValue);
00300   }
00301 
00302   // Bouml preserved body end 000A9C71
00303 }
00304 
00305 void YouBotManipulator::calibrateGripper(const bool forceCalibration)
00306 {
00307   // Bouml preserved body begin 000A9CF1
00308   // Calibrating Gripper
00309   bool doCalibration = true;
00310   configfile->readInto(doCalibration, "Gripper", "DoCalibration");
00311   if (useGripper && doCalibration)
00312   {
00313     CalibrateGripper calibrate;
00314     calibrate.setParameter(forceCalibration);
00315     gripper->setConfigurationParameter(calibrate);
00316   }
00317   // Bouml preserved body end 000A9CF1
00318 }
00319 
00322 YouBotJoint& YouBotManipulator::getArmJoint(const unsigned int armJointNumber)
00323 {
00324   // Bouml preserved body begin 0004F7F1
00325 
00326   if (armJointNumber <= 0 || armJointNumber > ARMJOINTS)
00327   {
00328     throw std::out_of_range("Invalid Joint Number");
00329   }
00330   return joints[armJointNumber - 1];
00331   // Bouml preserved body end 0004F7F1
00332 }
00333 
00334 YouBotGripper& YouBotManipulator::getArmGripper()
00335 {
00336   // Bouml preserved body begin 0005F9F1
00337   if (!useGripper)
00338   {
00339     throw std::runtime_error("The gripper is disabled!");
00340   }
00341   return *gripper;
00342   // Bouml preserved body end 0005F9F1
00343 }
00344 
00348 void YouBotManipulator::setJointData(const std::vector<JointAngleSetpoint>& JointData)
00349 {
00350   // Bouml preserved body begin 0008FDF1
00351   if (JointData.size() != ARMJOINTS)
00352     throw std::out_of_range("Wrong number of JointAngleSetpoints");
00353 
00354   ethercatMaster.AutomaticSendOn(false);
00355   joints[0].setData(JointData[0]);
00356   joints[1].setData(JointData[1]);
00357   joints[2].setData(JointData[2]);
00358   joints[3].setData(JointData[3]);
00359   joints[4].setData(JointData[4]);
00360   ethercatMaster.AutomaticSendOn(true);
00361 
00362   // Bouml preserved body end 0008FDF1
00363 }
00364 
00368 void YouBotManipulator::getJointData(std::vector<JointSensedAngle>& data)
00369 {
00370   // Bouml preserved body begin 0008FE71
00371   data.resize(ARMJOINTS);
00372   ethercatMaster.AutomaticReceiveOn(false);
00373   joints[0].getData(data[0]);
00374   joints[1].getData(data[1]);
00375   joints[2].getData(data[2]);
00376   joints[3].getData(data[3]);
00377   joints[4].getData(data[4]);
00378   ethercatMaster.AutomaticReceiveOn(true);
00379   // Bouml preserved body end 0008FE71
00380 }
00381 
00385 void YouBotManipulator::setJointData(const std::vector<JointVelocitySetpoint>& JointData)
00386 {
00387   // Bouml preserved body begin 0008FEF1
00388   if (JointData.size() != ARMJOINTS)
00389     throw std::out_of_range("Wrong number of JointVelocitySetpoints");
00390 
00391   ethercatMaster.AutomaticSendOn(false);
00392   joints[0].setData(JointData[0]);
00393   joints[1].setData(JointData[1]);
00394   joints[2].setData(JointData[2]);
00395   joints[3].setData(JointData[3]);
00396   joints[4].setData(JointData[4]);
00397   ethercatMaster.AutomaticSendOn(true);
00398   // Bouml preserved body end 0008FEF1
00399 }
00400 
00404 void YouBotManipulator::getJointData(std::vector<JointSensedVelocity>& data)
00405 {
00406   // Bouml preserved body begin 0008FF71
00407   data.resize(ARMJOINTS);
00408   ethercatMaster.AutomaticReceiveOn(false);
00409   joints[0].getData(data[0]);
00410   joints[1].getData(data[1]);
00411   joints[2].getData(data[2]);
00412   joints[3].getData(data[3]);
00413   joints[4].getData(data[4]);
00414   ethercatMaster.AutomaticReceiveOn(true);
00415   // Bouml preserved body end 0008FF71
00416 }
00417 
00421 void YouBotManipulator::setJointData(const std::vector<JointCurrentSetpoint>& JointData)
00422 {
00423   // Bouml preserved body begin 000CDE71
00424   if (JointData.size() != ARMJOINTS)
00425     throw std::out_of_range("Wrong number of JointCurrentSetpoint");
00426 
00427   ethercatMaster.AutomaticSendOn(false);
00428   joints[0].setData(JointData[0]);
00429   joints[1].setData(JointData[1]);
00430   joints[2].setData(JointData[2]);
00431   joints[3].setData(JointData[3]);
00432   joints[4].setData(JointData[4]);
00433   ethercatMaster.AutomaticSendOn(true);
00434   // Bouml preserved body end 000CDE71
00435 }
00436 
00440 void YouBotManipulator::getJointData(std::vector<JointSensedCurrent>& data)
00441 {
00442   // Bouml preserved body begin 00090071
00443   data.resize(ARMJOINTS);
00444   ethercatMaster.AutomaticReceiveOn(false);
00445   joints[0].getData(data[0]);
00446   joints[1].getData(data[1]);
00447   joints[2].getData(data[2]);
00448   joints[3].getData(data[3]);
00449   joints[4].getData(data[4]);
00450   ethercatMaster.AutomaticReceiveOn(true);
00451   // Bouml preserved body end 00090071
00452 }
00453 
00457 void YouBotManipulator::setJointData(const std::vector<JointTorqueSetpoint>& JointData)
00458 {
00459   // Bouml preserved body begin 000CDEF1
00460   if (JointData.size() != ARMJOINTS)
00461     throw std::out_of_range("Wrong number of JointTorqueSetpoint");
00462 
00463   ethercatMaster.AutomaticSendOn(false);
00464   joints[0].setData(JointData[0]);
00465   joints[1].setData(JointData[1]);
00466   joints[2].setData(JointData[2]);
00467   joints[3].setData(JointData[3]);
00468   joints[4].setData(JointData[4]);
00469   ethercatMaster.AutomaticSendOn(true);
00470   // Bouml preserved body end 000CDEF1
00471 }
00472 
00476 void YouBotManipulator::getJointData(std::vector<JointSensedTorque>& data)
00477 {
00478   // Bouml preserved body begin 000CDF71
00479   data.resize(ARMJOINTS);
00480   ethercatMaster.AutomaticReceiveOn(false);
00481   joints[0].getData(data[0]);
00482   joints[1].getData(data[1]);
00483   joints[2].getData(data[2]);
00484   joints[3].getData(data[3]);
00485   joints[4].getData(data[4]);
00486   ethercatMaster.AutomaticReceiveOn(true);
00487   // Bouml preserved body end 000CDF71
00488 }
00489 
00491 void YouBotManipulator::commutationFirmware200()
00492 {
00493   // Bouml preserved body begin 0010D8F1
00494 
00495   InitializeJoint doInitialization;
00496   bool isInitialized = false;
00497   int noInitialization = 0;
00498   std::string jointName;
00499   unsigned int statusFlags;
00500   std::vector<bool> isCommutated;
00501   isCommutated.assign(ARMJOINTS, false);
00502   unsigned int u = 0;
00503   JointCurrentSetpoint zerocurrent;
00504   zerocurrent.current = 0.0 * ampere;
00505 
00506   ClearMotorControllerTimeoutFlag clearTimeoutFlag;
00507   for (unsigned int i = 1; i <= ARMJOINTS; i++)
00508   {
00509     this->getArmJoint(i).setConfigurationParameter(clearTimeoutFlag);
00510   }
00511 
00512   for (unsigned int i = 1; i <= ARMJOINTS; i++)
00513   {
00514     doInitialization.setParameter(false);
00515     this->getArmJoint(i).getConfigurationParameter(doInitialization);
00516     doInitialization.getParameter(isInitialized);
00517     if (!isInitialized)
00518     {
00519       noInitialization++;
00520     }
00521   }
00522 
00523   if (noInitialization != 0)
00524   {
00525     LOG(info) << "Manipulator Joint Commutation";
00526     doInitialization.setParameter(true);
00527 
00528     JointRoundsPerMinuteSetpoint rpmSetpoint(100);
00529 
00530     ethercatMaster.AutomaticReceiveOn(false);
00531     this->getArmJoint(1).setData(rpmSetpoint);
00532     this->getArmJoint(2).setData(rpmSetpoint);
00533     this->getArmJoint(3).setData(rpmSetpoint);
00534     this->getArmJoint(4).setData(rpmSetpoint);
00535     this->getArmJoint(5).setData(rpmSetpoint);
00536     ethercatMaster.AutomaticReceiveOn(true);
00537 
00538     // check for the next 5 sec if the joints are commutated
00539     for (u = 1; u <= 5000; u++)
00540     {
00541       for (unsigned int i = 1; i <= ARMJOINTS; i++)
00542       {
00543         this->getArmJoint(i).getStatus(statusFlags);
00544         if (statusFlags & INITIALIZED)
00545         {
00546           isCommutated[i - 1] = true;
00547           this->getArmJoint(i).setData(zerocurrent);
00548         }
00549       }
00550       if (!ethercatMaster.isThreadActive())
00551       {
00552         ethercatMaster.sendProcessData();
00553         ethercatMaster.receiveProcessData();
00554       }
00555       if (isCommutated[0] && isCommutated[1] && isCommutated[2] && isCommutated[3] && isCommutated[4])
00556       {
00557         break;
00558       }
00559       SLEEP_MILLISEC(1);
00560     }
00561 
00562     for (unsigned int i = 1; i <= ARMJOINTS; i++)
00563     {
00564       this->getArmJoint(i).setData(zerocurrent);
00565       if (!ethercatMaster.isThreadActive())
00566       {
00567         ethercatMaster.sendProcessData();
00568         ethercatMaster.receiveProcessData();
00569       }
00570       doInitialization.setParameter(false);
00571       this->getArmJoint(i).getConfigurationParameter(doInitialization);
00572       doInitialization.getParameter(isInitialized);
00573       if (!isInitialized)
00574       {
00575         std::stringstream jointNameStream;
00576         jointNameStream << "manipulator joint " << i;
00577         jointName = jointNameStream.str();
00578         throw std::runtime_error("Could not commutate " + jointName);
00579       }
00580     }
00581   }
00582   // Bouml preserved body end 0010D8F1
00583 }
00584 
00586 void YouBotManipulator::commutationFirmware148()
00587 {
00588   // Bouml preserved body begin 0010D971
00589 
00590   InitializeJoint doInitialization;
00591   bool isInitialized = false;
00592   int noInitialization = 0;
00593   std::string jointName;
00594 
00595   ClearMotorControllerTimeoutFlag clearTimeoutFlag;
00596   for (unsigned int i = 1; i <= ARMJOINTS; i++)
00597   {
00598     this->getArmJoint(i).setConfigurationParameter(clearTimeoutFlag);
00599   }
00600 
00601   for (unsigned int i = 1; i <= ARMJOINTS; i++)
00602   {
00603     doInitialization.setParameter(false);
00604     this->getArmJoint(i).getConfigurationParameter(doInitialization);
00605     doInitialization.getParameter(isInitialized);
00606     if (!isInitialized)
00607     {
00608       noInitialization++;
00609     }
00610   }
00611 
00612   if (noInitialization != 0)
00613   {
00614     LOG(info) << "Manipulator Joint Commutation";
00615     doInitialization.setParameter(true);
00616 
00617     ethercatMaster.AutomaticReceiveOn(false);
00618     this->getArmJoint(1).setConfigurationParameter(doInitialization);
00619     this->getArmJoint(2).setConfigurationParameter(doInitialization);
00620     this->getArmJoint(3).setConfigurationParameter(doInitialization);
00621     this->getArmJoint(4).setConfigurationParameter(doInitialization);
00622     this->getArmJoint(5).setConfigurationParameter(doInitialization);
00623     ethercatMaster.AutomaticReceiveOn(true);
00624 
00625     unsigned int statusFlags;
00626     std::vector<bool> isCommutated;
00627     isCommutated.assign(ARMJOINTS, false);
00628     unsigned int u = 0;
00629 
00630     // check for the next 5 sec if the joints are commutated
00631     for (u = 1; u <= 5000; u++)
00632     {
00633       for (unsigned int i = 1; i <= ARMJOINTS; i++)
00634       {
00635         if (!ethercatMaster.isThreadActive())
00636         {
00637           ethercatMaster.sendProcessData();
00638           ethercatMaster.receiveProcessData();
00639         }
00640         this->getArmJoint(i).getStatus(statusFlags);
00641         if (statusFlags & INITIALIZED)
00642         {
00643           isCommutated[i - 1] = true;
00644         }
00645       }
00646       if (isCommutated[0] && isCommutated[1] && isCommutated[2] && isCommutated[3] && isCommutated[4])
00647       {
00648         break;
00649       }
00650       SLEEP_MILLISEC(1);
00651     }
00652 
00653     SLEEP_MILLISEC(10); // the controller likes it
00654 
00655     for (unsigned int i = 1; i <= ARMJOINTS; i++)
00656     {
00657       doInitialization.setParameter(false);
00658       this->getArmJoint(i).getConfigurationParameter(doInitialization);
00659       doInitialization.getParameter(isInitialized);
00660       if (!isInitialized)
00661       {
00662         std::stringstream jointNameStream;
00663         jointNameStream << "manipulator joint " << i;
00664         jointName = jointNameStream.str();
00665         throw std::runtime_error("Could not commutate " + jointName);
00666       }
00667     }
00668   }
00669 
00670   // Bouml preserved body end 0010D971
00671 }
00672 
00673 void YouBotManipulator::initializeJoints()
00674 {
00675   // Bouml preserved body begin 00068071
00676 
00677   //   LOG(info) << "Initializing Joints";
00678 
00679   char* configLocation = getenv("YOUBOT_CONFIG_FOLDER_LOCATION");
00680   if (configLocation == NULL)
00681     throw std::runtime_error("YouBotArmTest.cpp: Could not find environment variable YOUBOT_CONFIG_FOLDER_LOCATION");
00682 
00683   //get number of slaves
00684   unsigned int noSlaves = ethercatMaster.getNumberOfSlaves();
00685 
00686   if (noSlaves < ARMJOINTS)
00687   {
00688     throw std::runtime_error("Not enough ethercat slaves were found to create a YouBotManipulator!");
00689   }
00690 
00691   unsigned int slaveNumber = 0;
00692   configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint1");
00693   if (slaveNumber <= noSlaves)
00694   {
00695     joints.push_back(new YouBotJoint(slaveNumber, configLocation));
00696   }
00697   else
00698   {
00699     throw std::out_of_range("The ethercat slave number is not available!");
00700   }
00701 
00702   configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint2");
00703   if (slaveNumber <= noSlaves)
00704   {
00705     joints.push_back(new YouBotJoint(slaveNumber, configLocation));
00706   }
00707   else
00708   {
00709     throw std::out_of_range("The ethercat slave number is not available!");
00710   }
00711 
00712   configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint3");
00713   if (slaveNumber <= noSlaves)
00714   {
00715     joints.push_back(new YouBotJoint(slaveNumber, configLocation));
00716   }
00717   else
00718   {
00719     throw std::out_of_range("The ethercat slave number is not available!");
00720   }
00721 
00722   configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint4");
00723   if (slaveNumber <= noSlaves)
00724   {
00725     joints.push_back(new YouBotJoint(slaveNumber, configLocation));
00726   }
00727   else
00728   {
00729     throw std::out_of_range("The ethercat slave number is not available!");
00730   }
00731 
00732   configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint5");
00733   if (slaveNumber <= noSlaves)
00734   {
00735     joints.push_back(new YouBotJoint(slaveNumber, configLocation));
00736   }
00737   else
00738   {
00739     throw std::out_of_range("The ethercat slave number is not available!");
00740   }
00741 
00742   //Configure Joint Parameters
00743   std::string jointName;
00744   JointName jName;
00745   GearRatio gearRatio;
00746   EncoderTicksPerRound ticksPerRound;
00747   InverseMovementDirection inverseDir;
00748   double gearRatio_numerator = 0;
00749   double gearRatio_denominator = 1;
00750   FirmwareVersion firmwareTypeVersion;
00751   TorqueConstant torqueConst;
00752   double trajectory_p = 0, trajectory_i = 0, trajectory_d = 0, trajectory_imax = 0, trajectory_imin = 0;
00753 
00754   for (unsigned int i = 0; i < ARMJOINTS; i++)
00755   {
00756     std::stringstream jointNameStream;
00757     jointNameStream << "Joint_" << i + 1;
00758     jointName = jointNameStream.str();
00759 
00760     joints[i].getConfigurationParameter(firmwareTypeVersion);
00761     std::string version;
00762     int controllerType;
00763     std::string firmwareVersion;
00764     firmwareTypeVersion.getParameter(controllerType, firmwareVersion);
00765 
00766     string name;
00767     configfile->readInto(name, jointName, "JointName");
00768     jName.setParameter(name);
00769 
00770     LOG(info) << name << "\t Controller Type: " << controllerType << "  Firmware version: " << firmwareVersion;
00771 
00772     if (this->controllerType != controllerType && alternativeControllerType != controllerType)
00773     {
00774       std::stringstream ss;
00775       ss << "The youBot manipulator motor controller have to be of type: " << this->controllerType << " or "
00776           << alternativeControllerType;
00777       throw std::runtime_error(ss.str().c_str());
00778     }
00779 
00780     //check if firmware is supported
00781     bool isfirmwareSupported = false;
00782     for (unsigned int d = 0; d < supportedFirmwareVersions.size(); d++)
00783     {
00784       if (this->supportedFirmwareVersions[d] == firmwareVersion)
00785       {
00786         isfirmwareSupported = true;
00787         break;
00788       }
00789     }
00790 
00791     if (!isfirmwareSupported)
00792     {
00793       throw std::runtime_error("Unsupported firmware version: " + firmwareVersion);
00794     }
00795 
00796     if (this->actualFirmwareVersionAllJoints == "")
00797     {
00798       this->actualFirmwareVersionAllJoints = firmwareVersion;
00799     }
00800 
00801     if (!(firmwareVersion == this->actualFirmwareVersionAllJoints))
00802     {
00803       throw std::runtime_error("All joints must have the same firmware version!");
00804     }
00805 
00806     configfile->readInto(gearRatio_numerator, jointName, "GearRatio_numerator");
00807     configfile->readInto(gearRatio_denominator, jointName, "GearRatio_denominator");
00808     gearRatio.setParameter(gearRatio_numerator / gearRatio_denominator);
00809     int ticks;
00810     configfile->readInto(ticks, jointName, "EncoderTicksPerRound");
00811     ticksPerRound.setParameter(ticks);
00812 
00813     double torqueConstant;
00814     configfile->readInto(torqueConstant, jointName, "TorqueConstant_[newton_meter_divided_by_ampere]");
00815     torqueConst.setParameter(torqueConstant);
00816 
00817     bool invdir = false;
00818     configfile->readInto(invdir, jointName, "InverseMovementDirection");
00819     inverseDir.setParameter(invdir);
00820 
00821     joints[i].setConfigurationParameter(jName);
00822     joints[i].setConfigurationParameter(gearRatio);
00823     joints[i].setConfigurationParameter(ticksPerRound);
00824     joints[i].setConfigurationParameter(torqueConst);
00825     joints[i].setConfigurationParameter(inverseDir);
00826 
00827     //Joint Trajectory Controller
00828     if (ethercatMaster.isThreadActive())
00829     {
00830       configfile->readInto(trajectory_p, jointName, "trajectory_controller_P");
00831       configfile->readInto(trajectory_i, jointName, "trajectory_controller_I");
00832       configfile->readInto(trajectory_d, jointName, "trajectory_controller_D");
00833       configfile->readInto(trajectory_imax, jointName, "trajectory_controller_I_max");
00834       configfile->readInto(trajectory_imin, jointName, "trajectory_controller_I_min");
00835       joints[i].trajectoryController.setConfigurationParameter(trajectory_p, trajectory_i, trajectory_d,
00836                                                                trajectory_imax, trajectory_imin);
00837       joints[i].trajectoryController.setEncoderTicksPerRound(ticks);
00838       joints[i].trajectoryController.setGearRatio(gearRatio_numerator / gearRatio_denominator);
00839       joints[i].trajectoryController.setInverseMovementDirection(invdir);
00840       ethercatMasterWithThread->registerJointTrajectoryController(&(joints[i].trajectoryController),
00841                                                                   joints[i].getJointNumber());
00842     }
00843   }
00844 
00845   configfile->readInto(useGripper, "Gripper", "EnableGripper");
00846 
00847   if (useGripper)
00848   {
00849     //Initializing Gripper
00850     configfile->readInto(slaveNumber, "JointTopology", "ManipulatorJoint5");
00851     this->gripper.reset(new YouBotGripper(slaveNumber, configLocation));
00852     BarSpacingOffset barOffest;
00853     MaxTravelDistance maxDistance;
00854     MaxEncoderValue maxEncoder;
00855     GripperBarName BarName;
00856     double dummy = 0;
00857     int controllerType;
00858     double firmwareVersion;
00859     string barname;
00860 
00861     GripperFirmwareVersion gripperVersion;
00862     this->gripper->getConfigurationParameter(gripperVersion);
00863     gripperVersion.getParameter(controllerType, firmwareVersion);
00864 
00865     LOG(info) << "Gripper" << "\t\t Controller Type: " << controllerType << "  Firmware version: " << firmwareVersion;
00866 
00867     // Gripper Bar 1
00868     configfile->readInto(barname, "GripperBar1", "BarName");
00869     BarName.setParameter(barname);
00870     this->gripper->getGripperBar1().setConfigurationParameter(BarName);
00871 
00872     configfile->readInto(dummy, "GripperBar1", "BarSpacingOffset_[meter]");
00873     barOffest.setParameter(dummy * meter);
00874     this->gripper->getGripperBar1().setConfigurationParameter(barOffest);
00875 
00876     configfile->readInto(dummy, "GripperBar1", "MaxTravelDistance_[meter]");
00877     maxDistance.setParameter(dummy * meter);
00878     this->gripper->getGripperBar1().setConfigurationParameter(maxDistance);
00879 
00880     int maxenc = 0;
00881     configfile->readInto(maxenc, "GripperBar1", "MaxEncoderValue");
00882     maxEncoder.setParameter(maxenc);
00883     this->gripper->getGripperBar1().setConfigurationParameter(maxEncoder);
00884 
00885     int stallThreshold = 0;
00886     configfile->readInto(stallThreshold, "GripperBar1", "StallGuard2Threshold");
00887     StallGuard2Threshold threshold;
00888     threshold.setParameter(stallThreshold);
00889     this->gripper->getGripperBar1().setConfigurationParameter(threshold);
00890 
00891     bool stallGuardFilter = false;
00892     configfile->readInto(stallGuardFilter, "GripperBar1", "StallGuard2FilterEnable");
00893     StallGuard2FilterEnable filter;
00894     filter.setParameter(stallGuardFilter);
00895     this->gripper->getGripperBar1().setConfigurationParameter(filter);
00896 
00897     // Gripper Bar 2
00898     configfile->readInto(barname, "GripperBar2", "BarName");
00899     BarName.setParameter(barname);
00900     this->gripper->getGripperBar2().setConfigurationParameter(BarName);
00901 
00902     configfile->readInto(dummy, "GripperBar2", "BarSpacingOffset_[meter]");
00903     barOffest.setParameter(dummy * meter);
00904     this->gripper->getGripperBar2().setConfigurationParameter(barOffest);
00905 
00906     configfile->readInto(dummy, "GripperBar2", "MaxTravelDistance_[meter]");
00907     maxDistance.setParameter(dummy * meter);
00908     this->gripper->getGripperBar2().setConfigurationParameter(maxDistance);
00909 
00910     configfile->readInto(maxenc, "GripperBar2", "MaxEncoderValue");
00911     maxEncoder.setParameter(maxenc);
00912     this->gripper->getGripperBar2().setConfigurationParameter(maxEncoder);
00913 
00914     configfile->readInto(stallThreshold, "GripperBar2", "StallGuard2Threshold");
00915     threshold.setParameter(stallThreshold);
00916     this->gripper->getGripperBar2().setConfigurationParameter(threshold);
00917 
00918     configfile->readInto(stallGuardFilter, "GripperBar2", "StallGuard2FilterEnable");
00919     filter.setParameter(stallGuardFilter);
00920     this->gripper->getGripperBar2().setConfigurationParameter(filter);
00921   }
00922 
00923   return;
00924   // Bouml preserved body end 00068071
00925 }
00926 
00927 
00928 bool YouBotManipulator::isEtherCATConnectionEstablished() {
00929   return (ethercatMasterWithThread!=NULL && ethercatMasterWithThread->isEtherCATConnectionEstablished());
00930 }
00931 
00932 } // namespace youbot


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