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