experiments/src/icubmotors.cpp

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00007  *                                                                              *
00008  *  This program is free software; you can redistribute it and/or modify        *
00009  *  it under the terms of the GNU General Public License as published by        *
00010  *  the Free Software Foundation; either version 2 of the License, or           *
00011  *  (at your option) any later version.                                         *
00012  *                                                                              *
00013  *  This program is distributed in the hope that it will be useful,             *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00016  *  GNU General Public License for more details.                                *
00017  *                                                                              *
00018  *  You should have received a copy of the GNU General Public License           *
00019  *  along with this program; if not, write to the Free Software                 *
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00021  ********************************************************************************/
00022 
00023 #ifdef FARSA_USE_YARP_AND_ICUB
00024 
00025 #include "icubmotors.h"
00026 #include "configurationhelper.h"
00027 #include "phyicub.h"
00028 #include "randomgenerator.h"
00029 #include "motorcontrollers.h"
00030 #include "logger.h"
00031 #include <QStringList>
00032 #include <QMap>
00033 
00034 namespace farsa {
00035 
00036 iCubMotor::iCubMotor(ConfigurationParameters& params, QString prefix) :
00037     Motor(params, prefix),
00038     icubResource("robot"),
00039     neuronsIteratorResource("neuronsIterator")
00040 {
00041     // Reading parameters
00042     icubResource = ConfigurationHelper::getString(params, prefix + "icub", icubResource);
00043     neuronsIteratorResource = ConfigurationHelper::getString(params, prefix + "neuronsIterator", neuronsIteratorResource);
00044 
00045     // Declaring the resources that are needed here
00046     usableResources(QStringList() << icubResource << neuronsIteratorResource);
00047 }
00048 
00049 iCubMotor::~iCubMotor()
00050 {
00051     // Nothing to do here
00052 }
00053 
00054 void iCubMotor::save(ConfigurationParameters& params, QString prefix)
00055 {
00056     // Calling parent function
00057     Motor::save(params, prefix);
00058 
00059     // Saving parameters
00060     params.startObjectParameters(prefix, "iCubMotor", this);
00061     params.createParameter(prefix, "icub", icubResource);
00062     params.createParameter(prefix, "neuronsIterator", neuronsIteratorResource);
00063 }
00064 
00065 void iCubMotor::describe(QString type)
00066 {
00067     // Calling parent function
00068     Motor::describe(type);
00069 
00070     // Describing our parameters
00071     Descriptor d = addTypeDescription(type, "The base class for iCub motors");
00072     d.describeString("icub").def("robot").help("the name of the resource associated with the iCub robot to use (default is \"robot\")");
00073     d.describeString("neuronsIterator").def("neuronsIterator").help("the name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
00074 }
00075 
00076 void iCubMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00077 {
00078     // Calling parent function
00079     Motor::resourceChanged(resourceName, changeType);
00080 
00081     // Here we only check whether the resource has been deleted and reset the check flag, the
00082     // actual work is done in subclasses
00083     if (changeType == Deleted) {
00084         resetNeededResourcesCheck();
00085         return;
00086     }
00087 }
00088 
00089 iCubArmRandomMotor::iCubArmRandomMotor( ConfigurationParameters& params, QString prefix ) :
00090     iCubMotor(params, prefix),
00091     icub(NULL),
00092     icubMotors(NULL) {
00093     icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
00094     // Declaring the resources that are needed here
00095     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00096 }
00097 
00098 iCubArmRandomMotor::~iCubArmRandomMotor() {
00099     /* nothing to do */
00100 }
00101 
00102 void iCubArmRandomMotor::save( ConfigurationParameters& params, QString prefix ) {
00103     iCubMotor::save( params, prefix );
00104     params.startObjectParameters( prefix, "iCubArmRandomMotor", this );
00105     params.createParameter( prefix, "arm", icubArm );
00106 }
00107 
00108 void iCubArmRandomMotor::describe( QString type ) {
00109     iCubMotor::describe( type );
00110     Descriptor d = addTypeDescription( type, "An example of Motor implementation that randomly set a position of the arm using configurePosture" );
00111     d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
00112 }
00113 
00114 void iCubArmRandomMotor::update() {
00115     // Checking all resources we need exist
00116     checkAllNeededResourcesExist();
00117 
00118     // Acquiring the lock to get resources
00119     ResourcesLocker locker( this );
00120 
00121     QMap<int, real> posture;
00122     QStringList values;
00123     for( int i=0; i<7; i++ ) {
00124         double min, max;
00125         icubMotors->getLimits( i, &min, &max );
00126         double value = globalRNG->getDouble( min, max );
00127         posture[startJointId+i] = value;
00128         values << QString::number( value );
00129     }
00130     icub->configurePosture( posture );
00131     //exp->setStatus( QString("MOTOR Settings: <")+values.join(", ")+QString(">") );
00132     /*
00133     for( int i=0; i<7; i++, gaexp->cupdate++ ) {
00134         double min, max, value;
00135         icubMotors->getEncoder(i, &value);
00136         icubMotors->getLimits(i,&min,&max);
00137         //normalizziamo i valori dei motori tra 0 ed 1;
00138         gaexp->rajoints[i]=linearMap(value,min,max,0,1);
00139         gaexp->evonet->setInput(gaexp->cupdate,gaexp->rajoints[i]);
00140     }
00141     */
00142 }
00143 
00144 int iCubArmRandomMotor::size() {
00145     return 7;
00146 }
00147 
00148 void iCubArmRandomMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00149     iCubMotor::resourceChanged(resourceName, changeType);
00150 
00151     if (changeType == Deleted) {
00152         return;
00153     }
00154 
00155     if (resourceName == icubResource) {
00156         icub = getResource<iCubRobot>();
00157         if ( icubArm == "right" ) {
00158             startJointId = iCubRobot::right_shoulder_pitch;
00159             icubMotors = icub->rightArmController();
00160         } else {
00161             startJointId = iCubRobot::left_shoulder_pitch;
00162             icubMotors = icub->leftArmController();
00163         }
00164     } else if (resourceName == neuronsIteratorResource) {
00165         QString lbl;
00166 
00167         if ( icubArm == "right" ) {
00168             lbl="R";
00169         } else {
00170             lbl="L";
00171         }
00172 
00173         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00174         evonetIt->setCurrentBlock( name() );
00175         for( int i=0; i<7; i++, evonetIt->nextNeuron() ) {
00176             evonetIt->setGraphicProperties( lbl+QString("A")+QString::number(i), 0.0, 1.0, Qt::red );
00177         }
00178     } else {
00179         Logger::info("Unknown resource " + resourceName + " for " + name());
00180     }
00181 }
00182 
00183 iCubArmPosToPostureMotor::iCubArmPosToPostureMotor( ConfigurationParameters& params, QString prefix ) :
00184     iCubMotor(params, prefix),
00185     icub(NULL),
00186     icubMotors(NULL) {
00187     icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
00188     is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
00189     // Declaring the resources that are needed here
00190     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00191 }
00192 
00193 iCubArmPosToPostureMotor::~iCubArmPosToPostureMotor() {
00194     /* nothing to do */
00195 }
00196 
00197 void iCubArmPosToPostureMotor::save( ConfigurationParameters& params, QString prefix ) {
00198     iCubMotor::save( params, prefix );
00199     params.startObjectParameters( prefix, "iCubArmPosToPostureMotor", this );
00200     params.createParameter( prefix, "arm", icubArm );
00201     params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
00202 }
00203 
00204 void iCubArmPosToPostureMotor::describe( QString type ) {
00205     iCubMotor::describe( type );
00206     Descriptor d = addTypeDescription( type, "Move the arm in the position using the configurePosture method. The movement of this motor is instantaneuos, it only require one step. And hence, any physics is ignored. Use this motor only in kinematic mode of the iCub." );
00207     d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
00208     d.describeBool( "full" ).def( "true" ).help( "If full is false only the first 4 DoF will be moved; if full is true all 7 DoF of the arm (including the wrist) will be moved" );
00209 }
00210 
00211 void iCubArmPosToPostureMotor::update() {
00212     // Checking all resources we need exist
00213     checkAllNeededResourcesExist();
00214 
00215     // Acquiring the lock to get resources
00216     ResourcesLocker locker( this );
00217 
00218     QMap<int, real> posture;
00219     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00220     evonetIt->setCurrentBlock( name() );
00221     for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
00222         double actual;
00223         double min,max;
00224         icubMotors->getEncoder(i,&actual);
00225         icubMotors->getLimits(i,&min,&max);
00226         double a = evonetIt->getOutput();
00227         posture[startJointId+i] = linearMap(a,0.0,1.0,min, max);
00228     }
00229     icub->configurePosture( posture );
00230 }
00231 
00232 int iCubArmPosToPostureMotor::size() {
00233     return (is7Dof ? 7 : 4);
00234 }
00235 
00236 void iCubArmPosToPostureMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00237     iCubMotor::resourceChanged(resourceName, changeType);
00238 
00239     if (changeType == Deleted) {
00240         return;
00241     }
00242 
00243     if (resourceName == icubResource) {
00244         icub = getResource<iCubRobot>();
00245         if ( icubArm == "right" ) {
00246             startJointId = iCubRobot::right_shoulder_pitch;
00247             icubMotors = icub->rightArmController();
00248         } else {
00249             startJointId = iCubRobot::left_shoulder_pitch;
00250             icubMotors = icub->leftArmController();
00251         }
00252     } else if (resourceName == neuronsIteratorResource) {
00253         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00254         evonetIt->setCurrentBlock( name() );
00255         for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
00256             evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
00257         }
00258     } else {
00259         Logger::info("Unknown resource " + resourceName + " for " + name());
00260     }
00261 }
00262 
00263 iCubTorsoPosToPostureMotor::iCubTorsoPosToPostureMotor( ConfigurationParameters& params, QString prefix ) :
00264     iCubMotor(params, prefix),
00265     icub(NULL),
00266     icubMotors(NULL) {
00267     torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
00268     torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
00269     torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
00270     // Declaring the resources that are needed here
00271     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00272 }
00273 
00274 iCubTorsoPosToPostureMotor::~iCubTorsoPosToPostureMotor() {
00275     /* nothing to do */
00276 }
00277 
00278 void iCubTorsoPosToPostureMotor::save( ConfigurationParameters& params, QString prefix ) {
00279     iCubMotor::save( params, prefix );
00280     params.startObjectParameters( prefix, "iCubTorsoPosToPostureMotor", this );
00281     if ( !torsoDofs[0] ) {
00282         params.createParameter( prefix, "disableYaw", "true" );
00283     }
00284     if ( !torsoDofs[1] ) {
00285         params.createParameter( prefix, "disableRoll", "true" );
00286     }
00287     if ( !torsoDofs[2] ) {
00288         params.createParameter( prefix, "disablePitch", "true" );
00289     }
00290 }
00291 
00292 void iCubTorsoPosToPostureMotor::describe( QString type ) {
00293     iCubMotor::describe( type );
00294     Descriptor d = addTypeDescription( type, "Move the torso in the position using the configurePosture method. The movement of this motor is instantaneuos, it only require one step. And hence, any physics is ignored. Use this motor only in kinematic mode of the iCub." );
00295     d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
00296     d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
00297     d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
00298 }
00299 
00300 void iCubTorsoPosToPostureMotor::update() {
00301     // Checking all resources we need exist
00302     checkAllNeededResourcesExist();
00303 
00304     // Acquiring the lock to get resources
00305     ResourcesLocker locker( this );
00306 
00307     QMap<int, real> posture;
00308     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00309     evonetIt->setCurrentBlock( name() );
00310     for( int i=0; i<3; i++ ) {
00311         if ( torsoDofs[i] ) {
00312             double actual;
00313             double min,max;
00314             icubMotors->getEncoder(i,&actual);
00315             icubMotors->getLimits(i,&min,&max);
00316             double a = evonetIt->getOutput();
00317             posture[iCubRobot::torso_yaw+i] = linearMap(a,0.0,1.0,min, max);
00318             evonetIt->nextNeuron();
00319         } else {
00320             posture[iCubRobot::torso_yaw+i] = 0.0;
00321         }
00322     }
00323     icub->configurePosture( posture );
00324 }
00325 
00326 int iCubTorsoPosToPostureMotor::size() {
00327     return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
00328 }
00329 
00330 void iCubTorsoPosToPostureMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00331     iCubMotor::resourceChanged(resourceName, changeType);
00332 
00333     if (changeType == Deleted) {
00334         return;
00335     }
00336 
00337     if (resourceName == icubResource) {
00338         icub = getResource<iCubRobot>();
00339         icubMotors = icub->torsoController();
00340     } else if (resourceName == neuronsIteratorResource) {
00341         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00342         evonetIt->setCurrentBlock( name() );
00343         int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
00344         for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
00345             evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
00346         }
00347     } else {
00348         Logger::info("Unknown resource " + resourceName + " for " + name());
00349     }
00350 }
00351 
00352 iCubArmMusclesMotor::iCubArmMusclesMotor(farsa::ConfigurationParameters &params, QString prefix):
00353     iCubMotor(params, prefix),
00354     musclePairs(NULL),
00355     icub(NULL),
00356     icubMotors(NULL) {
00357     icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
00358     // Declaring the resources that are needed here
00359     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00360 }
00361 
00362 iCubArmMusclesMotor::~iCubArmMusclesMotor() {
00363     /*here we need to delete muscles objects */
00364     for(int i=0;i<7;i++)
00365         delete musclePairs[i];
00366     delete[] musclePairs;
00367 
00368 }
00369 void iCubArmMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
00370     iCubMotor::save( params, prefix );
00371     params.startObjectParameters( prefix, "iCubArmMusclesMotor", this );
00372     params.createParameter( prefix, "arm", icubArm );
00373 }
00374 
00375 void iCubArmMusclesMotor::describe( QString type ) {
00376     iCubMotor::describe( type );
00377     Descriptor d = addTypeDescription( type, "Muscular control of the right/left arm, 2 muscles for each joint" );
00378     d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
00379 }
00380 void iCubArmMusclesMotor::update() {
00381     // Checking all resources we need exist
00382     checkAllNeededResourcesExist();
00383 
00384     // Acquiring the lock to get resources
00385     ResourcesLocker locker( this );
00386 
00387     //to do:
00388     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00389     evonetIt->setCurrentBlock( name() );
00390     for( int i=0; i<7; i++ ) {
00391 
00392         double a=evonetIt->getOutput();
00393         evonetIt->nextNeuron();
00394         double b=evonetIt->getOutput();
00395         evonetIt->nextNeuron();
00396         musclePairs[i]->setActivation(a,b);
00397         icubMotors->velocityMove(i,musclePairs[i]->apply());
00398     }
00399 }
00400 
00401 int iCubArmMusclesMotor::size() {
00402     return 14;
00403 }
00404 
00405 void iCubArmMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00406     iCubMotor::resourceChanged(resourceName, changeType);
00407 
00408     if (changeType == Deleted) {
00409         return;
00410     }
00411 
00412     if (resourceName == icubResource) {
00413         const float maxVel = 50;
00414         // const float viscos = 0.04;
00415 
00416         icub = getResource<iCubRobot>();
00417         if ( icubArm == "right" ) {
00418             icubMotors = icub->rightArmController();
00419         } else {
00420             icubMotors = icub->leftArmController();
00421         }
00422 
00423         if (musclePairs != NULL) {
00424             for(int i = 0; i < 7; i++) {
00425                 delete musclePairs[i];
00426             }
00427             delete[] musclePairs;
00428         }
00429 
00430         musclePairs = new MusclePair*[7];
00431         for (int i = 0; i < 7; i++) {
00432             musclePairs[i] = new MusclePair(icubMotors->motors()[i],3.0f, maxVel, 2.5f, 3.7f);
00433         }
00434     } else if (resourceName == neuronsIteratorResource) {
00435         QString label;
00436         if ( icubArm == "right" ) {
00437             label="R";
00438         } else {
00439             label="L";
00440         }
00441 
00442         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00443         evonetIt->setCurrentBlock( name() );
00444         for( int i = 0; i<7; i++ ) {
00445             for (int m = 0;m < 2; m++) {
00446                 if (m == 0) {
00447                     evonetIt->setGraphicProperties(label + QString("A") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
00448                 } else {
00449                     evonetIt->setGraphicProperties(label + QString("A") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
00450                 }
00451 
00452                 evonetIt->nextNeuron();
00453             }
00454         }
00455     } else {
00456         Logger::info("Unknown resource " + resourceName + " for " + name());
00457     }
00458 }
00459 
00460 iCubHandMusclesMotor::iCubHandMusclesMotor(farsa::ConfigurationParameters &params, QString prefix):
00461     iCubMotor(params, prefix),
00462     musclePairs(NULL),
00463     icub(NULL),
00464     icubMotors(NULL) {
00465     icubHand = ConfigurationHelper::getString( params, prefix+"hand", "right" );
00466     // Declaring the resources that are needed here
00467     usableResources( QStringList() << icubResource << neuronsIteratorResource);
00468 }
00469 
00470 iCubHandMusclesMotor::~iCubHandMusclesMotor() {
00471     /*here we need to delete muscles objects */
00472     for(int i=0;i<9;i++)
00473         delete musclePairs[i];
00474     delete[] musclePairs;
00475 
00476 }
00477 void iCubHandMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
00478     iCubMotor::save( params, prefix );
00479     params.startObjectParameters( prefix, "iCubHandMusclesMotor", this );
00480     params.createParameter( prefix, "hand", icubHand );
00481 }
00482 
00483 void iCubHandMusclesMotor::describe( QString type ) {
00484     iCubMotor::describe( type );
00485     Descriptor d = addTypeDescription( type, "Muscular control of the right/left hand, 2 muscles for each joint" );
00486     d.describeEnum( "hand" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The hand to move" );
00487 }
00488 void iCubHandMusclesMotor::update() {
00489     // Checking all resources we need exist
00490     checkAllNeededResourcesExist();
00491 
00492     // Acquiring the lock to get resources
00493     ResourcesLocker locker( this );
00494 
00495     //to do:
00496     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00497     evonetIt->setCurrentBlock( name() );
00498     for( int i=0; i<9; i++ ) {
00499 
00500         double a=evonetIt->getOutput();
00501         evonetIt->nextNeuron();
00502         double b=evonetIt->getOutput();
00503         evonetIt->nextNeuron();
00504         musclePairs[i]->setActivation(a,b);
00505         icubMotors->velocityMove(i+7,musclePairs[i]->apply());
00506     }
00507 }
00508 
00509 int iCubHandMusclesMotor::size() {
00510     return 18;
00511 }
00512 
00513 void iCubHandMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00514     iCubMotor::resourceChanged(resourceName, changeType);
00515 
00516     if (changeType == Deleted) {
00517         return;
00518     }
00519 
00520     if (resourceName == icubResource) {
00521         const float maxVel = 50;
00522         // const float viscos = 0.04;
00523 
00524         icub = getResource<iCubRobot>();
00525         if ( icubHand == "right" ) {
00526             icubMotors = icub->rightArmController();
00527         } else {
00528             icubMotors = icub->leftArmController();
00529         }
00530 
00531         if (musclePairs != NULL) {
00532             for(int i = 0; i < 9; i++) {
00533                 delete musclePairs[i];
00534             }
00535             delete[] musclePairs;
00536         }
00537 
00538         musclePairs = new MusclePair*[9];
00539         for (int i = 0; i < 9; i++) {
00540             musclePairs[i] = new MusclePair(icubMotors->motors()[i+7],3.0f, maxVel, 2.5f, 3.7f);
00541         }
00542     } else if (resourceName == neuronsIteratorResource) {
00543         QString label;
00544         if ( icubHand == "right" ) {
00545             label = "R";
00546         } else {
00547             label = "L";
00548         }
00549 
00550         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00551         evonetIt->setCurrentBlock( name() );
00552         for( int i = 0; i < 9; i++ ) {
00553             for (int m = 0; m < 2; m++ ) {
00554                 if ( m==0 ) {
00555                     evonetIt->setGraphicProperties(label + QString("F") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
00556                 } else {
00557                     evonetIt->setGraphicProperties(label + QString("F") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
00558                 }
00559 
00560                 evonetIt->nextNeuron();
00561             }
00562         }
00563     } else {
00564         Logger::info("Unknown resource " + resourceName + " for " + name());
00565     }
00566 }
00567 
00568 iCubTorsoMusclesMotor::iCubTorsoMusclesMotor(farsa::ConfigurationParameters &params, QString prefix):
00569     iCubMotor(params, prefix),
00570     musclePairs(NULL),
00571     icub(NULL),
00572     icubMotors(NULL) {
00573     // Declaring the resources that are needed here
00574     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00575 }
00576 
00577 iCubTorsoMusclesMotor::~iCubTorsoMusclesMotor() {
00578     /*here we need to delete muscles objects */
00579     for(int i=0;i<2;i++)
00580         delete musclePairs[i];
00581     delete[] musclePairs;
00582 
00583 }
00584 void iCubTorsoMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
00585     iCubMotor::save( params, prefix );
00586     params.startObjectParameters( prefix, "iCubTorsoMusclesMotor", this );
00587 }
00588 
00589 void iCubTorsoMusclesMotor::describe( QString type ) {
00590     iCubMotor::describe( type );
00591     Descriptor d = addTypeDescription( type, "Muscular control of the Torso, 2 muscles for each joint" );
00592 }
00593 void iCubTorsoMusclesMotor::update() {
00594     // Checking all resources we need exist
00595     checkAllNeededResourcesExist();
00596 
00597     // Acquiring the lock to get resources
00598     ResourcesLocker locker( this );
00599 
00600     //to do:
00601     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00602     evonetIt->setCurrentBlock( name() );
00603     for( int i=0; i<2; i++ ) {
00604 
00605         int id=0;
00606         double a=evonetIt->getOutput();
00607         evonetIt->nextNeuron();
00608         double b=evonetIt->getOutput();
00609         evonetIt->nextNeuron();
00610         musclePairs[i]->setActivation(a,b);
00611         if (i==0) id=0;
00612         if (i==1) id=2;
00613         icubMotors->velocityMove(id,musclePairs[i]->apply());
00614     }
00615 }
00616 
00617 int iCubTorsoMusclesMotor::size() {
00618     return 4;
00619 }
00620 
00621 void iCubTorsoMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00622     iCubMotor::resourceChanged(resourceName, changeType);
00623 
00624     if (changeType == Deleted) {
00625         return;
00626     }
00627 
00628     if (resourceName == icubResource) {
00629         const float maxVel = 50;
00630         // const float viscos = 0.04;
00631 
00632         icub = getResource<iCubRobot>();
00633         icubMotors = icub->torsoController();
00634 
00635         if (musclePairs != NULL) {
00636             for(int i = 0; i < 2; i++) {
00637                 delete musclePairs[i];
00638             }
00639             delete[] musclePairs;
00640         }
00641 
00642         musclePairs = new MusclePair*[2];
00643         for (int i = 0; i < 2; i++) {
00644             int ir;
00645 
00646             if (i==0) ir=0;
00647             if (i==1) ir=2;
00648 
00649             musclePairs[i] = new MusclePair(icubMotors->motors()[ir],3.0f, maxVel, 2.5f, 3.7f);
00650         }
00651     } else if (resourceName == neuronsIteratorResource) {
00652         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00653         evonetIt->setCurrentBlock( name() );
00654 
00655         for( int i = 0; i < 2; i++ ) {
00656             for (int m = 0; m < 2; m++) {
00657                 if (m == 0) {
00658                     evonetIt->setGraphicProperties( QString("T") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
00659                 } else {
00660                     evonetIt->setGraphicProperties( QString("T") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
00661                 }
00662 
00663                 evonetIt->nextNeuron();
00664             }
00665         }
00666     } else {
00667         Logger::info("Unknown resource " + resourceName + " for " + name());
00668     }
00669 }
00670 
00671 iCubHeadMusclesMotor::iCubHeadMusclesMotor(farsa::ConfigurationParameters &params, QString prefix):
00672     iCubMotor(params, prefix),
00673     musclePairs(NULL),
00674     icub(NULL),
00675     icubMotors(NULL) {
00676     // Declaring the resources that are needed here
00677     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00678 }
00679 
00680 iCubHeadMusclesMotor::~iCubHeadMusclesMotor() {
00681     /*here we need to delete muscles objects */
00682     for(int i=0;i<2;i++)
00683         delete musclePairs[i];
00684     delete[] musclePairs;
00685 
00686 }
00687 void iCubHeadMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
00688     iCubMotor::save( params, prefix );
00689     params.startObjectParameters( prefix, "iCubHeadMusclesMotor", this );
00690     
00691 }
00692 
00693 void iCubHeadMusclesMotor::describe( QString type ) {
00694     iCubMotor::describe( type );
00695     Descriptor d = addTypeDescription( type, "Muscular control of the head, 2 muscles for each joint" );
00696 }
00697 
00698 void iCubHeadMusclesMotor::update() {
00699     // Checking all resources we need exist
00700     checkAllNeededResourcesExist();
00701 
00702     // Acquiring the lock to get resources
00703     ResourcesLocker locker( this );
00704 
00705     //to do:
00706     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00707     evonetIt->setCurrentBlock( name() );
00708     for( int i=0; i<2; i++ ) {
00709 
00710         int id=0;
00711         double a=evonetIt->getOutput();
00712         evonetIt->nextNeuron();
00713         double b=evonetIt->getOutput();
00714         evonetIt->nextNeuron();
00715         musclePairs[i]->setActivation(a,b);
00716         if (i==0) id=0;
00717         if (i==1) id=2;
00718         icubMotors->velocityMove(i,musclePairs[id]->apply());
00719     }
00720 }
00721 
00722 int iCubHeadMusclesMotor::size() {
00723     return 4;
00724 }
00725 
00726 void iCubHeadMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00727     iCubMotor::resourceChanged(resourceName, changeType);
00728 
00729     if (changeType == Deleted) {
00730         return;
00731     }
00732 
00733     if (resourceName == icubResource) {
00734         // getting the resources and initializing MotorController
00735         const float maxVel = 50;
00736         // const float viscos = 0.04;
00737 
00738         icub = getResource<iCubRobot>();
00739         icubMotors = icub->headNeckController();
00740 
00741         if (musclePairs != NULL) {
00742             for(int i = 0; i < 2; i++) {
00743                 delete musclePairs[i];
00744             }
00745             delete[] musclePairs;
00746         }
00747 
00748         musclePairs = new MusclePair*[2];
00749         for (int i = 0; i < 2; i++) {
00750             musclePairs[i] = new MusclePair(icubMotors->motors()[i],3.0f, maxVel, 2.5f, 3.7f);
00751         }
00752     } else if (resourceName == neuronsIteratorResource) {
00753         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00754         evonetIt->setCurrentBlock( name() );
00755         for( int i = 0; i < 2; i++ ) {
00756             for (int m = 0;m < 2; m++) {
00757                 if ( m == 0 ) {
00758                     evonetIt->setGraphicProperties( QString("N") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
00759                 } else {
00760                     evonetIt->setGraphicProperties( QString("N") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
00761                 }
00762 
00763                 evonetIt->nextNeuron();
00764             }
00765         }
00766     } else {
00767         Logger::info("Unknown resource " + resourceName + " for " + name());
00768     }
00769 }
00770 
00771 iCubArmPosToVelMotor::iCubArmPosToVelMotor(farsa::ConfigurationParameters &params, QString prefix):
00772     iCubMotor(params, prefix),
00773     icub(NULL),
00774     icubMotors(NULL) {
00775     icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
00776     markParameterAsRuntime( "/PID/k", &pcontrol, &ProportionalController::setK, &ProportionalController::getK );
00777     // Declaring the resources that are needed here
00778     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00779 }
00780 
00781 iCubArmPosToVelMotor::~iCubArmPosToVelMotor() {
00782     /*nothing to do */
00783 }
00784 
00785 void iCubArmPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
00786     iCubMotor::save( params, prefix );
00787     params.startObjectParameters( prefix, "iCubArmPosToVelMotor", this );
00788     params.createParameter( prefix, "arm", icubArm );
00789 }
00790 
00791 void iCubArmPosToVelMotor::describe( QString type ) {
00792     iCubMotor::describe( type );
00793     Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right/left arm" );
00794     d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
00795 }
00796 
00797 void iCubArmPosToVelMotor::update() {
00798     // Checking all resources we need exist
00799     checkAllNeededResourcesExist();
00800 
00801     // Acquiring the lock to get resources
00802     ResourcesLocker locker( this );
00803 
00804     //to do:
00805     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00806     evonetIt->setCurrentBlock( name() );
00807     for( int i=0; i<7; i++ ) {
00808         double actual;
00809         double min,max;
00810         double desiredPosture;
00811         icubMotors->getEncoder(i,&actual);
00812         icubMotors->getLimits(i,&min,&max);
00813         double a=evonetIt->getOutput();
00814         desiredPosture=linearMap(a,0.0,1.0,min, max);
00815         evonetIt->nextNeuron();         
00816         icubMotors->velocityMove(i,pcontrol.velocityForJoint(desiredPosture, actual));
00817     }
00818 }
00819 
00820 int iCubArmPosToVelMotor::size() {
00821     return 7;
00822 }
00823 
00824 void iCubArmPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00825     iCubMotor::resourceChanged(resourceName, changeType);
00826 
00827     if (changeType == Deleted) {
00828         return;
00829     }
00830 
00831     if (resourceName == icubResource) {
00832         icub = getResource<iCubRobot>();
00833         if ( icubArm == "right" ) {
00834             icubMotors = icub->rightArmController();
00835         } else {
00836             icubMotors = icub->leftArmController();
00837         }
00838     } else if (resourceName == neuronsIteratorResource) {
00839         QString label;
00840         if ( icubArm == "right" ) {
00841             label="R";
00842         } else {
00843             label="L";
00844         }
00845 
00846         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00847         evonetIt->setCurrentBlock( name() );
00848         for( int i=0; i<7; i++ ) {
00849             evonetIt->setGraphicProperties(label+ QString("A")+QString::number(i), 0.0, 1.0, Qt::red ); //ex Ap2V
00850 
00851             evonetIt->nextNeuron();
00852         }
00853     } else {
00854         Logger::info("Unknown resource " + resourceName + " for " + name());
00855     }
00856 }
00857 
00858 iCubTorsoPosToVelMotor::iCubTorsoPosToVelMotor(farsa::ConfigurationParameters &params, QString prefix):
00859     iCubMotor(params, prefix),
00860     icub(NULL),
00861     icubMotors(NULL) {
00862     markParameterAsRuntime( "/PID/k", &pcontrol, &ProportionalController::setK, &ProportionalController::getK );
00863     // Declaring the resources that are needed here
00864     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00865 }
00866 
00867 iCubTorsoPosToVelMotor::~iCubTorsoPosToVelMotor() {
00868     /*nothing to do */
00869 }
00870 
00871 void iCubTorsoPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
00872     iCubMotor::save( params, prefix );
00873     params.startObjectParameters( prefix, "iCubTorsoPosToVelMotor", this );
00874 }
00875 
00876 void iCubTorsoPosToVelMotor::describe( QString type ) {
00877     iCubMotor::describe( type );
00878     Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the torso" );
00879 }
00880 
00881 void iCubTorsoPosToVelMotor::update() {
00882     // Checking all resources we need exist
00883     checkAllNeededResourcesExist();
00884 
00885     // Acquiring the lock to get resources
00886     ResourcesLocker locker( this );
00887 
00888     //to do:
00889     int id=0;
00890     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00891     evonetIt->setCurrentBlock( name() );
00892     for( int i=0; i<2; i++ ) {
00893         double actual;
00894         double min,max;
00895         double desiredPosture;
00896         icubMotors->getEncoder(i,&actual);
00897         icubMotors->getLimits(i,&min,&max);
00898         double a=evonetIt->getOutput();
00899         desiredPosture=linearMap(a,0.0,1.0,min, max);
00900         evonetIt->nextNeuron(); 
00901         if (i==0) id=0;
00902         if (i==1) id=2;
00903         icubMotors->velocityMove(id,pcontrol.velocityForJoint(desiredPosture, actual));
00904     }
00905 }
00906 
00907 int iCubTorsoPosToVelMotor::size() {
00908     return 2;
00909 }
00910 
00911 void iCubTorsoPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00912     iCubMotor::resourceChanged(resourceName, changeType);
00913 
00914     if (changeType == Deleted) {
00915         return;
00916     }
00917 
00918     if (resourceName == icubResource) {
00919         icub = getResource<iCubRobot>();
00920         icubMotors=icub->torsoController();
00921     } else if (resourceName == neuronsIteratorResource) {
00922         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00923         evonetIt->setCurrentBlock( name() );
00924         for( int i=0; i<2; i++ ) {
00925             evonetIt->setGraphicProperties(QString("T")+QString::number(i), 0.0, 1.0, Qt::red ); //ex Tp2v
00926 
00927             evonetIt->nextNeuron();
00928         }
00929     } else {
00930         Logger::info("Unknown resource " + resourceName + " for " + name());
00931     }
00932 }
00933 
00934 iCubHeadPosToVelMotor::iCubHeadPosToVelMotor(farsa::ConfigurationParameters &params, QString prefix):
00935     iCubMotor(params, prefix),
00936     icub(NULL),
00937     icubMotors(NULL) {
00938     markParameterAsRuntime( "/PID/k", &pcontrol, &ProportionalController::setK, &ProportionalController::getK );
00939     // Declaring the resources that are needed here
00940     usableResources( QStringList() << icubResource << neuronsIteratorResource );
00941 }
00942 
00943 iCubHeadPosToVelMotor::~iCubHeadPosToVelMotor() {
00944     /*nothing to do */
00945 }
00946 
00947 void iCubHeadPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
00948     iCubMotor::save( params, prefix );
00949     params.startObjectParameters( prefix, "iCubHeadPosToVelMotor", this );
00950 }
00951 
00952 void iCubHeadPosToVelMotor::describe( QString type ) {
00953     iCubMotor::describe( type );
00954     Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the Head" );
00955 }
00956 
00957 void iCubHeadPosToVelMotor::update() {
00958     // Checking all resources we need exist
00959     checkAllNeededResourcesExist();
00960 
00961     // Acquiring the lock to get resources
00962     ResourcesLocker locker( this );
00963 
00964     //to do:
00965     int id=0;
00966     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00967     evonetIt->setCurrentBlock( name() );
00968 
00969     double actual;
00970     double min,max;
00971     double desiredPosture;
00972     double a;
00973 
00974     icubMotors->getEncoder(0,&actual);
00975     icubMotors->getLimits(0,&min,&max);
00976     a=evonetIt->getOutput();
00977     evonetIt->nextNeuron();
00978     desiredPosture=linearMap(a,0.0,1.0,min, max);
00979     icubMotors->velocityMove(0,pcontrol.velocityForJoint(desiredPosture, actual));
00980 
00981     icubMotors->getEncoder(2,&actual);
00982     icubMotors->getLimits(2,&min,&max);
00983     a=evonetIt->getOutput();
00984     desiredPosture=linearMap(a,0.0,1.0,min, max);
00985     icubMotors->velocityMove(2,pcontrol.velocityForJoint(desiredPosture, actual));
00986 }
00987 
00988 int iCubHeadPosToVelMotor::size() {
00989     return 2;
00990 }
00991 
00992 void iCubHeadPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00993     iCubMotor::resourceChanged(resourceName, changeType);
00994 
00995     if (changeType == Deleted) {
00996         return;
00997     }
00998 
00999     if (resourceName == icubResource) {
01000         icub = getResource<iCubRobot>();
01001         icubMotors=icub->headNeckController();
01002     } else if (resourceName == neuronsIteratorResource) {
01003         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01004         evonetIt->setCurrentBlock( name() );
01005 
01006         for( int i=0; i<2; i++ ) {
01007             evonetIt->setGraphicProperties(QString("N")+QString::number(i), 0.0, 1.0, Qt::red );//ex Hp2v
01008             evonetIt->nextNeuron();
01009         }
01010     } else {
01011         Logger::info("Unknown resource " + resourceName + " for " + name());
01012     }
01013 }
01014 
01015 iCubHandPosToVelMotor::iCubHandPosToVelMotor(farsa::ConfigurationParameters &params, QString prefix):
01016     iCubMotor(params, prefix),
01017     icub(NULL),
01018     icubMotors(NULL) {
01019     icubHand = ConfigurationHelper::getString( params, prefix+"hand", "right" );
01020     markParameterAsRuntime( "/PID/k", &pcontrol, &ProportionalController::setK, &ProportionalController::getK );
01021     // Declaring the resources that are needed here
01022     usableResources( QStringList() << icubResource << neuronsIteratorResource );
01023 }
01024 
01025 iCubHandPosToVelMotor::~iCubHandPosToVelMotor() {
01026     /*nothing to do */
01027     
01028 
01029 }
01030 void iCubHandPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
01031     iCubMotor::save( params, prefix );
01032     params.startObjectParameters( prefix, "iCubHandPosToVelMotor", this );
01033     params.createParameter( prefix, "hand", icubHand );
01034     
01035 }
01036 
01037 void iCubHandPosToVelMotor::describe( QString type ) {
01038     iCubMotor::describe( type );
01039     Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right or left Hand" );
01040     d.describeEnum( "hand" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The hand to move" );
01041 }
01042 
01043 void iCubHandPosToVelMotor::update() {
01044     // Checking all resources we need exist
01045     checkAllNeededResourcesExist();
01046 
01047     // Acquiring the lock to get resources
01048     ResourcesLocker locker( this );
01049 
01050     //to do:
01051     int id=0;
01052     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
01053     evonetIt->setCurrentBlock( name() );
01054     for( int i=7; i<16; i++ ) {
01055         double actual;
01056         double min,max;
01057         double desiredPosture;
01058         icubMotors->getEncoder(i,&actual);
01059         icubMotors->getLimits(i,&min,&max);
01060         double a=evonetIt->getOutput();
01061         desiredPosture=linearMap(a,0.0,1.0,min, max);
01062         evonetIt->nextNeuron(); 
01063         icubMotors->velocityMove(i,pcontrol.velocityForJoint(desiredPosture, actual));
01064     }
01065 }
01066 
01067 int iCubHandPosToVelMotor::size() {
01068     return 9;
01069 }
01070 
01071 void iCubHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
01072     iCubMotor::resourceChanged(resourceName, changeType);
01073 
01074     if (changeType == Deleted) {
01075         return;
01076     }
01077 
01078     if (resourceName == icubResource) {
01079         icub = getResource<iCubRobot>();
01080         if ( icubHand == "right" ) {
01081             icubMotors = icub->rightArmController();
01082         } else {
01083             icubMotors = icub->leftArmController();
01084         }
01085     } else if (resourceName == neuronsIteratorResource) {
01086         QString label;
01087 
01088         if ( icubHand == "right" ) {
01089             label="R";
01090         } else {
01091             label="L";
01092         }
01093 
01094         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01095         evonetIt->setCurrentBlock( name() );
01096         for( int i=0; i<9; i++ ) {
01097             evonetIt->setGraphicProperties(label+QString("F")+QString::number(i), 0.0, 1.0, Qt::red ); //ex HNp2v
01098 
01099             evonetIt->nextNeuron();
01100         }
01101     } else {
01102         Logger::info("Unknown resource " + resourceName + " for " + name());
01103     }
01104 }
01105 
01106 iCubHeadVelocityMotor::iCubHeadVelocityMotor(ConfigurationParameters& params, QString prefix) :
01107     iCubMotor(params, prefix),
01108     m_headNeckController(NULL)
01109 {
01110     // Declaring the resources that are needed here
01111     usableResources(QStringList() << icubResource << neuronsIteratorResource);
01112 }
01113 
01114 iCubHeadVelocityMotor::~iCubHeadVelocityMotor()
01115 {
01116 }
01117 
01118 void iCubHeadVelocityMotor::save(ConfigurationParameters& params, QString prefix)
01119 {
01120     iCubMotor::save(params, prefix);
01121     params.startObjectParameters(prefix, "iCubHeadVelocityMotor", this);
01122 }
01123 
01124 void iCubHeadVelocityMotor::describe(QString type)
01125 {
01126     iCubMotor::describe(type);
01127     Descriptor d = addTypeDescription(type, "Head velocity motor", "The motor to move the head of the icub in velocity. This controls two degrees of freedom: pan (head joint 2) and tilt (head joint 0)");
01128 }
01129 
01130 void iCubHeadVelocityMotor::update()
01131 {
01132     // Checking all resources we need exist
01133     checkAllNeededResourcesExist();
01134 
01135     // Acquiring the lock to get resources
01136     ResourcesLocker locker( this );
01137 
01138     NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
01139     evonetIt->setCurrentBlock(name());
01140 
01141     const double pan = evonetIt->getOutput();
01142     evonetIt->nextNeuron();
01143     const double tilt = evonetIt->getOutput();
01144     evonetIt->nextNeuron(); // Removed to avoid a warning from NeuronsIterator //remotion works only for the last sensor 
01145     const double maxVelocity = 50.0;
01146 
01147     // Computing the velocity (in degrees/sec)
01148     double panVelocity = (pan - 0.5) * 2.0 * maxVelocity;
01149     double tiltVelocity = (tilt - 0.5) * 2.0 * maxVelocity;
01150 
01151     // Now moving the head
01152     m_headNeckController->velocityMove(2, panVelocity);
01153     m_headNeckController->velocityMove(0, tiltVelocity);
01154 }
01155 
01156 int iCubHeadVelocityMotor::size()
01157 {
01158     return 2;
01159 }
01160 
01161 void iCubHeadVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
01162 {
01163     iCubMotor::resourceChanged(resourceName, changeType);
01164 
01165     if (changeType == Deleted) {
01166         return;
01167     }
01168 
01169     if (resourceName == icubResource) {
01170         m_headNeckController = getResource<iCubRobot>()->headNeckController();
01171     } else if (resourceName == neuronsIteratorResource) {
01172         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01173         evonetIt->setCurrentBlock(name());
01174         evonetIt->setGraphicProperties(QString("N0"), 0.0, 1.0, Qt::blue); //ex hp
01175         evonetIt->nextNeuron();
01176         evonetIt->setGraphicProperties(QString("N1"), 0.0, 1.0, Qt::blue); //ex ht
01177     } else {
01178         Logger::info("Unknown resource " + resourceName + " for " + name());
01179     }
01180 }
01181 
01182 iCubConfigurableHandPosToVelMotor::iCubConfigurableHandPosToVelMotor(ConfigurationParameters& params, QString prefix) :
01183     iCubMotor(params, prefix),
01184     m_icubHand("right"),
01185     m_jointsGroups(),
01186     m_maxClosure(1.0),
01187     m_armController(NULL),
01188     m_pcontrol()
01189 {
01190     m_icubHand = ConfigurationHelper::getString(params, prefix + "hand", "right");
01191 
01192     // Reading the fingers joints configuration. The configuration format is like this: joint1,joint2/joint5,joint6,joint3/joint4.
01193     // In this case we have three groups of joints: (joint1 and joint2), (joint5, joint6 and joint3) and (joint4). Joints in the
01194     // same group are controlled together. This parameter is compulsory, if it is not present, the program throws an exception.
01195     // The joint names are: Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others (case insensitive)
01196     const QString paramName = prefix + "jointsGroups";
01197     QString str = params.getValue(paramName);
01198     if (str.isEmpty()) {
01199         ConfigurationHelper::throwUserConfigError(paramName, "", "the parameter is compulsory but is not present in the configuration");
01200     }
01201 
01202     const QStringList groups = str.split("/", QString::SkipEmptyParts);
01203     if (groups.size() == 0) {
01204         ConfigurationHelper::throwUserConfigError(paramName, str, "invalid format (no joint group)");
01205     }
01206 
01207     // Resizing the vector of joint groups
01208     m_jointsGroups.resize(groups.size());
01209     for (int i = 0; i < m_jointsGroups.size(); i++) {
01210         // Splitting again
01211         const QStringList joints = groups[i].split(",", QString::SkipEmptyParts);
01212         if (joints.size() == 0) {
01213             ConfigurationHelper::throwUserConfigError(paramName, str, "a joint group has no components");
01214         }
01215 
01216         m_jointsGroups[i].resize(joints.size());
01217         for (int j = 0; j < m_jointsGroups[i].size(); j++) {
01218             // Now converting joint name into joint index. First converting to uppercase
01219             const QString jointName = joints[j].toUpper().trimmed();
01220             if (jointName == "APERTURE") {
01221                 m_jointsGroups[i][j] = 7;
01222             } else if (jointName == "THUMB1") {
01223                 m_jointsGroups[i][j] = 8;
01224             } else if (jointName == "THUMB2") {
01225                 m_jointsGroups[i][j] = 9;
01226             } else if (jointName == "THUMB3") {
01227                 m_jointsGroups[i][j] = 10;
01228             } else if (jointName == "INDEX1") {
01229                 m_jointsGroups[i][j] = 11;
01230             } else if (jointName == "INDEX2") {
01231                 m_jointsGroups[i][j] = 12;
01232             } else if (jointName == "MIDDLE1") {
01233                 m_jointsGroups[i][j] = 13;
01234             } else if (jointName == "MIDDLE2") {
01235                 m_jointsGroups[i][j] = 14;
01236             } else if (jointName == "OTHERS") {
01237                 m_jointsGroups[i][j] = 15;
01238             } else {
01239                 ConfigurationHelper::throwUserConfigError(paramName, str, "unknown joint name (" + jointName + ")");
01240             }
01241         }
01242     }
01243 
01244     // Now reading the parameter with the maximum joint closure
01245     m_maxClosure = ConfigurationHelper::getDouble(params, prefix + "maxClosure", 1.0);
01246 
01247     const double k = ConfigurationHelper::getDouble(params, prefix + "k", 0.3);
01248     const double maxvel = ConfigurationHelper::getDouble(params, prefix + "maxvel", 20.0);
01249     m_pcontrol.setControlParameters(k, maxvel);
01250 
01251     // Declaring the resources that are needed here
01252     usableResources(QStringList() << icubResource << neuronsIteratorResource);
01253 }
01254 
01255 iCubConfigurableHandPosToVelMotor::~iCubConfigurableHandPosToVelMotor()
01256 {
01257 }
01258 
01259 void iCubConfigurableHandPosToVelMotor::save(ConfigurationParameters& params, QString prefix)
01260 {
01261     iCubMotor::save( params, prefix );
01262     params.startObjectParameters(prefix, "ConfigurableHandPosToVelMotor", this);
01263     params.createParameter(prefix, "hand", m_icubHand);
01264 
01265     QString strJointsGroup;
01266     for (int g = 0; g < m_jointsGroups.size(); g++) {
01267         for (int j = 0; j < m_jointsGroups[g].size(); j++) {
01268             if (m_jointsGroups[g][j] == 7) {
01269                 strJointsGroup += "Aperture";
01270             } else if (m_jointsGroups[g][j] == 8) {
01271                 strJointsGroup += "Thumb1";
01272             } else if (m_jointsGroups[g][j] == 9) {
01273                 strJointsGroup += "Thumb2";
01274             } else if (m_jointsGroups[g][j] == 10) {
01275                 strJointsGroup += "Thumb3";
01276             } else if (m_jointsGroups[g][j] == 11) {
01277                 strJointsGroup += "Index1";
01278             } else if (m_jointsGroups[g][j] == 12) {
01279                 strJointsGroup += "Index2";
01280             } else if (m_jointsGroups[g][j] == 13) {
01281                 strJointsGroup += "Middle1";
01282             } else if (m_jointsGroups[g][j] == 14) {
01283                 strJointsGroup += "Middle2";
01284             } else if (m_jointsGroups[g][j] == 15) {
01285                 strJointsGroup += "Others";
01286             } else {
01287                 strJointsGroup += "ERROR";
01288             }
01289 
01290             if (j != (m_jointsGroups[g].size() - 1)) {
01291                 strJointsGroup += ", ";
01292             }
01293         }
01294         if (g != (m_jointsGroups.size() - 1)) {
01295             strJointsGroup += " / ";
01296         }
01297     }
01298     params.createParameter(prefix, "jointsGroups", strJointsGroup);
01299 
01300     params.createParameter(prefix, "maxClosure", QString::number(m_maxClosure));
01301     params.createParameter(prefix, "k", QString::number(m_pcontrol.getK()));
01302     params.createParameter(prefix, "maxvel", QString::number(m_pcontrol.getMaxVelocity()));
01303 }
01304 
01305 void iCubConfigurableHandPosToVelMotor::describe(QString type)
01306 {
01307     iCubMotor::describe( type );
01308     Descriptor d = addTypeDescription(type, "Hand motor", "A motor for the iCub hand. This allows to specify which joints should be controlled together (see the jointsGroups parameter)");
01309     d.describeEnum("hand").def("right").values(QStringList() << "right" << "left").props(IsMandatory).help("The hand to use", "The hand whose distance from the object should be returned. Choose between \"right\" and \"left\"");
01310     d.describeString("jointsGroups").def("Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others").props(IsMandatory).help("The parameter which specifies the joint configuration", "This parameter allows specifying which joints should be controlled together. The format is like this: joint1,joint2/joint5,joint6,joint3/joint4. In this case we have three groups of joints: (joint1 and joint2), (joint5, joint6 and joint3) and (joint4). Joints in the same group are controlled together, while joints that are not listed in any group are not controlled. This parameter is compulsory, if it is not present, the program throws an exception. The allowed joint names are: Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others (case insensitive)");
01311     d.describeReal("maxClosure").def(1.0).limits(0.0, 1.0).help("The limit to joint movement", "This parameter allows limiting joint excursion to a portion of the original one. If set to 1.0 all joints move within their limits, otherwise their upper limit is set to maxClosure portion of the original upper limit. This applies to all controlled joints. A value of 0.0 disables all joint movements");
01312     d.describeReal("k").def(0.3).limits(0.0, +std::numeric_limits<double>::infinity()).help("The gain for the proportional controller", "This parameter is the gain for the proportional controller converting position displacement (desired position minus current position) to a velocity to apply to the joint");
01313     d.describeReal("maxvel").def(20.0).limits(0.0, +std::numeric_limits<double>::infinity()).help("The maximum velocity for joints", "This is the maximum allowed velocity that can be applied to a joint (absolute value). Velocities greater than this are truncated to +maxvel or -maxvel");
01314 }
01315 
01316 void iCubConfigurableHandPosToVelMotor::update()
01317 {
01318     // Checking all resources we need exist
01319     checkAllNeededResourcesExist();
01320 
01321     // Acquiring the lock to get resources
01322     ResourcesLocker locker(this);
01323 
01324     NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
01325     evonetIt->setCurrentBlock(name());
01326 
01327     for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
01328         // The network output value is multiplied by m_maxClosure so that the maximum closure of fingers can be
01329         // constrained
01330         const double out = m_maxClosure * evonetIt->getOutput();
01331 
01332         for (int j = 0; j < m_jointsGroups[i].size(); j++) {
01333             double actual;
01334             double min, max;
01335 
01336             m_armController->getEncoder(m_jointsGroups[i][j], &actual);
01337             m_armController->getLimits(m_jointsGroups[i][j], &min, &max);
01338 
01339             const double desiredPosture = linearMap(out, 0.0, 1.0, min, max); // normalize the neuron output with respect to the joint range
01340 
01341             m_armController->velocityMove(m_jointsGroups[i][j], m_pcontrol.velocityForJoint(desiredPosture, actual));
01342         }
01343     }
01344 }
01345 
01346 int iCubConfigurableHandPosToVelMotor::size()
01347 {
01348     return m_jointsGroups.size();
01349 }
01350 
01351 void iCubConfigurableHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
01352 {
01353     iCubMotor::resourceChanged(resourceName, changeType);
01354 
01355     if (changeType == Deleted) {
01356         return;
01357     }
01358 
01359     if (resourceName == icubResource) {
01360         iCubRobot* icub = getResource<iCubRobot>();
01361         if (m_icubHand == "left") {
01362             m_armController = icub->leftArmController();
01363         } else {
01364             m_armController = icub->rightArmController();
01365         }
01366     } else if (resourceName == neuronsIteratorResource) {
01367         QString label;
01368         if (m_icubHand == "left") {
01369             label = "L";
01370         } else {
01371             label = "R";
01372         }
01373 
01374         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01375         evonetIt->setCurrentBlock(name());
01376         for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
01377             evonetIt->setGraphicProperties(label + "F" + QString::number(i), 0.0, 1.0, Qt::blue);
01378         }
01379     } else {
01380         Logger::info("Unknown resource " + resourceName + " for " + name());
01381     }
01382 }
01383 
01384 iCubArmVelocityMotor::iCubArmVelocityMotor( ConfigurationParameters& params, QString prefix ) :
01385     iCubMotor(params, prefix),
01386     icub(NULL),
01387     icubMotors(NULL) {
01388     icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
01389     is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
01390     maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
01391     // Declaring the resources that are needed here
01392     usableResources( QStringList() << icubResource << neuronsIteratorResource );
01393 }
01394 
01395 iCubArmVelocityMotor::~iCubArmVelocityMotor() {
01396     /* nothing to do */
01397 }
01398 
01399 void iCubArmVelocityMotor::save( ConfigurationParameters& params, QString prefix ) {
01400     iCubMotor::save( params, prefix );
01401     params.startObjectParameters( prefix, "iCubArmVelocityMotor", this );
01402     params.createParameter( prefix, "arm", icubArm );
01403     params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
01404     params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
01405 }
01406 
01407 void iCubArmVelocityMotor::describe( QString type ) {
01408     iCubMotor::describe( type );
01409     Descriptor d = addTypeDescription( type, "Move the arm by velocity." );
01410     d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
01411     d.describeBool( "full" ).def( "true" ).help( "If full is false only the first 4 DoF will be moved; if full is true all 7 DoF of the arm (including the wrist) will be moved" );
01412     d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
01413 }
01414 
01415 void iCubArmVelocityMotor::update() {
01416     // Checking all resources we need exist
01417     checkAllNeededResourcesExist();
01418 
01419     // Acquiring the lock to get resources
01420     ResourcesLocker locker( this );
01421 
01422     double vels[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
01423     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
01424     evonetIt->setCurrentBlock( name() );
01425     for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
01426         vels[i] = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
01427     }
01428     for( int i=0; i<7; i++ ) {
01429         icubMotors->velocityMove( i, vels[i] );
01430     }
01431 }
01432 
01433 int iCubArmVelocityMotor::size() {
01434     return (is7Dof ? 7 : 4);
01435 }
01436 
01437 void iCubArmVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
01438     iCubMotor::resourceChanged(resourceName, changeType);
01439 
01440     if (changeType == Deleted) {
01441         return;
01442     }
01443 
01444     if (resourceName == icubResource) {
01445         icub = getResource<iCubRobot>();
01446         if ( icubArm == "right" ) {
01447             startJointId = iCubRobot::right_shoulder_pitch;
01448             icubMotors = icub->rightArmController();
01449         } else {
01450             startJointId = iCubRobot::left_shoulder_pitch;
01451             icubMotors = icub->leftArmController();
01452         }
01453     } else if (resourceName == neuronsIteratorResource) {
01454         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01455         evonetIt->setCurrentBlock( name() );
01456         for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
01457             evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
01458         }
01459     } else {
01460         Logger::info("Unknown resource " + resourceName + " for " + name());
01461     }
01462 }
01463 
01464 iCubTorsoVelocityMotor::iCubTorsoVelocityMotor( ConfigurationParameters& params, QString prefix ) :
01465     iCubMotor(params, prefix),
01466     icub(NULL),
01467     icubMotors(NULL) {
01468     torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
01469     torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
01470     torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
01471     maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
01472     // Declaring the resources that are needed here
01473     usableResources( QStringList() << icubResource << neuronsIteratorResource );
01474 }
01475 
01476 iCubTorsoVelocityMotor::~iCubTorsoVelocityMotor() {
01477     /* nothing to do */
01478 }
01479 
01480 void iCubTorsoVelocityMotor::save( ConfigurationParameters& params, QString prefix ) {
01481     iCubMotor::save( params, prefix );
01482     params.startObjectParameters( prefix, "iCubTorsoPosToPostureMotor", this );
01483     if ( !torsoDofs[0] ) {
01484         params.createParameter( prefix, "disableYaw", "true" );
01485     }
01486     if ( !torsoDofs[1] ) {
01487         params.createParameter( prefix, "disableRoll", "true" );
01488     }
01489     if ( !torsoDofs[2] ) {
01490         params.createParameter( prefix, "disablePitch", "true" );
01491     }
01492     params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
01493 }
01494 
01495 void iCubTorsoVelocityMotor::describe( QString type ) {
01496     iCubMotor::describe( type );
01497     Descriptor d = addTypeDescription( type, "Move the torso by velocity." );
01498     d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
01499     d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
01500     d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
01501     d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
01502 }
01503 
01504 void iCubTorsoVelocityMotor::update() {
01505     // Checking all resources we need exist
01506     checkAllNeededResourcesExist();
01507 
01508     // Acquiring the lock to get resources
01509     ResourcesLocker locker( this );
01510 
01511     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
01512     evonetIt->setCurrentBlock( name() );
01513     for( int i=0; i<3; i++ ) {
01514         double vel = 0.0;
01515         if ( torsoDofs[i] ) {
01516             vel = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
01517             evonetIt->nextNeuron();
01518         }
01519         icubMotors->velocityMove( i, vel );
01520     }
01521 }
01522 
01523 int iCubTorsoVelocityMotor::size() {
01524     return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
01525 }
01526 
01527 void iCubTorsoVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
01528     iCubMotor::resourceChanged(resourceName, changeType);
01529 
01530     if (changeType == Deleted) {
01531         return;
01532     }
01533 
01534     if (resourceName == icubResource) {
01535         icub = getResource<iCubRobot>();
01536         icubMotors = icub->torsoController();
01537     } else if (resourceName == neuronsIteratorResource) {
01538         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
01539         evonetIt->setCurrentBlock( name() );
01540         int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
01541         for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
01542             evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
01543         }
01544     } else {
01545         Logger::info("Unknown resource " + resourceName + " for " + name());
01546     }
01547 }
01548 
01549 } // end namespace farsa
01550 
01551 #endif