icubmotors.cpp
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2013 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifdef FARSA_USE_YARP_AND_ICUB
24 
25 #include "icubmotors.h"
26 #include "configurationhelper.h"
27 #include "phyicub.h"
28 #include "randomgenerator.h"
29 #include "motorcontrollers.h"
30 #include "logger.h"
31 #include <QStringList>
32 #include <QMap>
33 
34 namespace farsa {
35 
37  Motor(params, prefix),
38  icubResource("robot"),
39  neuronsIteratorResource("neuronsIterator")
40 {
41  // Reading parameters
42  icubResource = ConfigurationHelper::getString(params, prefix + "icub", icubResource);
44 
45  // Declaring the resources that are needed here
47 }
48 
50 {
51  // Nothing to do here
52 }
53 
54 void iCubMotor::save(ConfigurationParameters& params, QString prefix)
55 {
56  // Calling parent function
57  Motor::save(params, prefix);
58 
59  // Saving parameters
60  params.startObjectParameters(prefix, "iCubMotor", this);
61  params.createParameter(prefix, "icub", icubResource);
62  params.createParameter(prefix, "neuronsIterator", neuronsIteratorResource);
63 }
64 
65 void iCubMotor::describe(QString type)
66 {
67  // Calling parent function
68  Motor::describe(type);
69 
70  // Describing our parameters
71  Descriptor d = addTypeDescription(type, "The base class for iCub motors");
72  d.describeString("icub").def("robot").help("the name of the resource associated with the iCub robot to use (default is \"robot\")");
73  d.describeString("neuronsIterator").def("neuronsIterator").help("the name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
74 }
75 
76 void iCubMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
77 {
78  // Calling parent function
79  Motor::resourceChanged(resourceName, changeType);
80 
81  // Here we only check whether the resource has been deleted and reset the check flag, the
82  // actual work is done in subclasses
83  if (changeType == Deleted) {
85  return;
86  }
87 }
88 
90  iCubMotor(params, prefix),
91  icub(NULL),
92  icubMotors(NULL) {
93  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
94  // Declaring the resources that are needed here
96 }
97 
99  /* nothing to do */
100 }
101 
102 void iCubArmRandomMotor::save( ConfigurationParameters& params, QString prefix ) {
103  iCubMotor::save( params, prefix );
104  params.startObjectParameters( prefix, "iCubArmRandomMotor", this );
105  params.createParameter( prefix, "arm", icubArm );
106 }
107 
108 void iCubArmRandomMotor::describe( QString type ) {
109  iCubMotor::describe( type );
110  Descriptor d = addTypeDescription( type, "An example of Motor implementation that randomly set a position of the arm using configurePosture" );
111  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
112 }
113 
115  // Checking all resources we need exist
117 
118  // Acquiring the lock to get resources
119  ResourcesLocker locker( this );
120 
121  QMap<int, real> posture;
122  QStringList values;
123  for( int i=0; i<7; i++ ) {
124  double min, max;
125  icubMotors->getLimits( i, &min, &max );
126  double value = globalRNG->getDouble( min, max );
127  posture[startJointId+i] = value;
128  values << QString::number( value );
129  }
130  icub->configurePosture( posture );
131  //exp->setStatus( QString("MOTOR Settings: <")+values.join(", ")+QString(">") );
132  /*
133  for( int i=0; i<7; i++, gaexp->cupdate++ ) {
134  double min, max, value;
135  icubMotors->getEncoder(i, &value);
136  icubMotors->getLimits(i,&min,&max);
137  //normalizziamo i valori dei motori tra 0 ed 1;
138  gaexp->rajoints[i]=linearMap(value,min,max,0,1);
139  gaexp->evonet->setInput(gaexp->cupdate,gaexp->rajoints[i]);
140  }
141  */
142 }
143 
145  return 7;
146 }
147 
148 void iCubArmRandomMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
149  iCubMotor::resourceChanged(resourceName, changeType);
150 
151  if (changeType == Deleted) {
152  return;
153  }
154 
155  if (resourceName == icubResource) {
156  icub = getResource<iCubRobot>();
157  if ( icubArm == "right" ) {
158  startJointId = iCubRobot::right_shoulder_pitch;
160  } else {
161  startJointId = iCubRobot::left_shoulder_pitch;
163  }
164  } else if (resourceName == neuronsIteratorResource) {
165  QString lbl;
166 
167  if ( icubArm == "right" ) {
168  lbl="R";
169  } else {
170  lbl="L";
171  }
172 
173  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
174  evonetIt->setCurrentBlock( name() );
175  for( int i=0; i<7; i++, evonetIt->nextNeuron() ) {
176  evonetIt->setGraphicProperties( lbl+QString("A")+QString::number(i), 0.0, 1.0, Qt::red );
177  }
178  } else {
179  Logger::info("Unknown resource " + resourceName + " for " + name());
180  }
181 }
182 
184  iCubMotor(params, prefix),
185  icub(NULL),
186  icubMotors(NULL) {
187  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
188  is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
189  // Declaring the resources that are needed here
191 }
192 
194  /* nothing to do */
195 }
196 
198  iCubMotor::save( params, prefix );
199  params.startObjectParameters( prefix, "iCubArmPosToPostureMotor", this );
200  params.createParameter( prefix, "arm", icubArm );
201  params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
202 }
203 
205  iCubMotor::describe( type );
206  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." );
207  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
208  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" );
209 }
210 
212  // Checking all resources we need exist
214 
215  // Acquiring the lock to get resources
216  ResourcesLocker locker( this );
217 
218  QMap<int, real> posture;
219  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
220  evonetIt->setCurrentBlock( name() );
221  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
222  double actual;
223  double min,max;
224  icubMotors->getEncoder(i,&actual);
225  icubMotors->getLimits(i,&min,&max);
226  double a = evonetIt->getOutput();
227  posture[startJointId+i] = linearMap(a,0.0,1.0,min, max);
228  }
229  icub->configurePosture( posture );
230 }
231 
233  return (is7Dof ? 7 : 4);
234 }
235 
236 void iCubArmPosToPostureMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
237  iCubMotor::resourceChanged(resourceName, changeType);
238 
239  if (changeType == Deleted) {
240  return;
241  }
242 
243  if (resourceName == icubResource) {
244  icub = getResource<iCubRobot>();
245  if ( icubArm == "right" ) {
246  startJointId = iCubRobot::right_shoulder_pitch;
248  } else {
249  startJointId = iCubRobot::left_shoulder_pitch;
251  }
252  } else if (resourceName == neuronsIteratorResource) {
253  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
254  evonetIt->setCurrentBlock( name() );
255  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
256  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
257  }
258  } else {
259  Logger::info("Unknown resource " + resourceName + " for " + name());
260  }
261 }
262 
264  iCubMotor(params, prefix),
265  icub(NULL),
266  icubMotors(NULL) {
267  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
268  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
269  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
270  // Declaring the resources that are needed here
272 }
273 
275  /* nothing to do */
276 }
277 
279  iCubMotor::save( params, prefix );
280  params.startObjectParameters( prefix, "iCubTorsoPosToPostureMotor", this );
281  if ( !torsoDofs[0] ) {
282  params.createParameter( prefix, "disableYaw", "true" );
283  }
284  if ( !torsoDofs[1] ) {
285  params.createParameter( prefix, "disableRoll", "true" );
286  }
287  if ( !torsoDofs[2] ) {
288  params.createParameter( prefix, "disablePitch", "true" );
289  }
290 }
291 
293  iCubMotor::describe( type );
294  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." );
295  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
296  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
297  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
298 }
299 
301  // Checking all resources we need exist
303 
304  // Acquiring the lock to get resources
305  ResourcesLocker locker( this );
306 
307  QMap<int, real> posture;
308  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
309  evonetIt->setCurrentBlock( name() );
310  for( int i=0; i<3; i++ ) {
311  if ( torsoDofs[i] ) {
312  double actual;
313  double min,max;
314  icubMotors->getEncoder(i,&actual);
315  icubMotors->getLimits(i,&min,&max);
316  double a = evonetIt->getOutput();
317  posture[iCubRobot::torso_yaw+i] = linearMap(a,0.0,1.0,min, max);
318  evonetIt->nextNeuron();
319  } else {
320  posture[iCubRobot::torso_yaw+i] = 0.0;
321  }
322  }
323  icub->configurePosture( posture );
324 }
325 
327  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
328 }
329 
330 void iCubTorsoPosToPostureMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
331  iCubMotor::resourceChanged(resourceName, changeType);
332 
333  if (changeType == Deleted) {
334  return;
335  }
336 
337  if (resourceName == icubResource) {
338  icub = getResource<iCubRobot>();
340  } else if (resourceName == neuronsIteratorResource) {
341  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
342  evonetIt->setCurrentBlock( name() );
343  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
344  for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
345  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
346  }
347  } else {
348  Logger::info("Unknown resource " + resourceName + " for " + name());
349  }
350 }
351 
353  iCubMotor(params, prefix),
354  musclePairs(NULL),
355  icub(NULL),
356  icubMotors(NULL) {
357  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
358  // Declaring the resources that are needed here
360 }
361 
363  /*here we need to delete muscles objects */
364  for(int i=0;i<7;i++)
365  delete musclePairs[i];
366  delete[] musclePairs;
367 
368 }
369 void iCubArmMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
370  iCubMotor::save( params, prefix );
371  params.startObjectParameters( prefix, "iCubArmMusclesMotor", this );
372  params.createParameter( prefix, "arm", icubArm );
373 }
374 
375 void iCubArmMusclesMotor::describe( QString type ) {
376  iCubMotor::describe( type );
377  Descriptor d = addTypeDescription( type, "Muscular control of the right/left arm, 2 muscles for each joint" );
378  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
379 }
381  // Checking all resources we need exist
383 
384  // Acquiring the lock to get resources
385  ResourcesLocker locker( this );
386 
387  //to do:
388  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
389  evonetIt->setCurrentBlock( name() );
390  for( int i=0; i<7; i++ ) {
391 
392  double a=evonetIt->getOutput();
393  evonetIt->nextNeuron();
394  double b=evonetIt->getOutput();
395  evonetIt->nextNeuron();
396  musclePairs[i]->setActivation(a,b);
397  icubMotors->velocityMove(i,musclePairs[i]->apply());
398  }
399 }
400 
402  return 14;
403 }
404 
405 void iCubArmMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
406  iCubMotor::resourceChanged(resourceName, changeType);
407 
408  if (changeType == Deleted) {
409  return;
410  }
411 
412  if (resourceName == icubResource) {
413  const float maxVel = 50;
414  // const float viscos = 0.04;
415 
416  icub = getResource<iCubRobot>();
417  if ( icubArm == "right" ) {
419  } else {
421  }
422 
423  if (musclePairs != NULL) {
424  for(int i = 0; i < 7; i++) {
425  delete musclePairs[i];
426  }
427  delete[] musclePairs;
428  }
429 
430  musclePairs = new MusclePair*[7];
431  for (int i = 0; i < 7; i++) {
432  musclePairs[i] = new MusclePair(icubMotors->motors()[i],3.0f, maxVel, 2.5f, 3.7f);
433  }
434  } else if (resourceName == neuronsIteratorResource) {
435  QString label;
436  if ( icubArm == "right" ) {
437  label="R";
438  } else {
439  label="L";
440  }
441 
442  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
443  evonetIt->setCurrentBlock( name() );
444  for( int i = 0; i<7; i++ ) {
445  for (int m = 0;m < 2; m++) {
446  if (m == 0) {
447  evonetIt->setGraphicProperties(label + QString("A") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
448  } else {
449  evonetIt->setGraphicProperties(label + QString("A") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
450  }
451 
452  evonetIt->nextNeuron();
453  }
454  }
455  } else {
456  Logger::info("Unknown resource " + resourceName + " for " + name());
457  }
458 }
459 
461  iCubMotor(params, prefix),
462  musclePairs(NULL),
463  icub(NULL),
464  icubMotors(NULL) {
465  icubHand = ConfigurationHelper::getString( params, prefix+"hand", "right" );
466  // Declaring the resources that are needed here
468 }
469 
471  /*here we need to delete muscles objects */
472  for(int i=0;i<9;i++)
473  delete musclePairs[i];
474  delete[] musclePairs;
475 
476 }
477 void iCubHandMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
478  iCubMotor::save( params, prefix );
479  params.startObjectParameters( prefix, "iCubHandMusclesMotor", this );
480  params.createParameter( prefix, "hand", icubHand );
481 }
482 
483 void iCubHandMusclesMotor::describe( QString type ) {
484  iCubMotor::describe( type );
485  Descriptor d = addTypeDescription( type, "Muscular control of the right/left hand, 2 muscles for each joint" );
486  d.describeEnum( "hand" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The hand to move" );
487 }
489  // Checking all resources we need exist
491 
492  // Acquiring the lock to get resources
493  ResourcesLocker locker( this );
494 
495  //to do:
496  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
497  evonetIt->setCurrentBlock( name() );
498  for( int i=0; i<9; i++ ) {
499 
500  double a=evonetIt->getOutput();
501  evonetIt->nextNeuron();
502  double b=evonetIt->getOutput();
503  evonetIt->nextNeuron();
504  musclePairs[i]->setActivation(a,b);
505  icubMotors->velocityMove(i+7,musclePairs[i]->apply());
506  }
507 }
508 
510  return 18;
511 }
512 
513 void iCubHandMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
514  iCubMotor::resourceChanged(resourceName, changeType);
515 
516  if (changeType == Deleted) {
517  return;
518  }
519 
520  if (resourceName == icubResource) {
521  const float maxVel = 50;
522  // const float viscos = 0.04;
523 
524  icub = getResource<iCubRobot>();
525  if ( icubHand == "right" ) {
527  } else {
529  }
530 
531  if (musclePairs != NULL) {
532  for(int i = 0; i < 9; i++) {
533  delete musclePairs[i];
534  }
535  delete[] musclePairs;
536  }
537 
538  musclePairs = new MusclePair*[9];
539  for (int i = 0; i < 9; i++) {
540  musclePairs[i] = new MusclePair(icubMotors->motors()[i+7],3.0f, maxVel, 2.5f, 3.7f);
541  }
542  } else if (resourceName == neuronsIteratorResource) {
543  QString label;
544  if ( icubHand == "right" ) {
545  label = "R";
546  } else {
547  label = "L";
548  }
549 
550  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
551  evonetIt->setCurrentBlock( name() );
552  for( int i = 0; i < 9; i++ ) {
553  for (int m = 0; m < 2; m++ ) {
554  if ( m==0 ) {
555  evonetIt->setGraphicProperties(label + QString("F") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
556  } else {
557  evonetIt->setGraphicProperties(label + QString("F") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
558  }
559 
560  evonetIt->nextNeuron();
561  }
562  }
563  } else {
564  Logger::info("Unknown resource " + resourceName + " for " + name());
565  }
566 }
567 
569  iCubMotor(params, prefix),
570  musclePairs(NULL),
571  icub(NULL),
572  icubMotors(NULL) {
573  // Declaring the resources that are needed here
575 }
576 
578  /*here we need to delete muscles objects */
579  for(int i=0;i<2;i++)
580  delete musclePairs[i];
581  delete[] musclePairs;
582 
583 }
584 void iCubTorsoMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
585  iCubMotor::save( params, prefix );
586  params.startObjectParameters( prefix, "iCubTorsoMusclesMotor", this );
587 }
588 
589 void iCubTorsoMusclesMotor::describe( QString type ) {
590  iCubMotor::describe( type );
591  Descriptor d = addTypeDescription( type, "Muscular control of the Torso, 2 muscles for each joint" );
592 }
594  // Checking all resources we need exist
596 
597  // Acquiring the lock to get resources
598  ResourcesLocker locker( this );
599 
600  //to do:
601  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
602  evonetIt->setCurrentBlock( name() );
603  for( int i=0; i<2; i++ ) {
604 
605  int id=0;
606  double a=evonetIt->getOutput();
607  evonetIt->nextNeuron();
608  double b=evonetIt->getOutput();
609  evonetIt->nextNeuron();
610  musclePairs[i]->setActivation(a,b);
611  if (i==0) id=0;
612  if (i==1) id=2;
613  icubMotors->velocityMove(id,musclePairs[i]->apply());
614  }
615 }
616 
618  return 4;
619 }
620 
621 void iCubTorsoMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
622  iCubMotor::resourceChanged(resourceName, changeType);
623 
624  if (changeType == Deleted) {
625  return;
626  }
627 
628  if (resourceName == icubResource) {
629  const float maxVel = 50;
630  // const float viscos = 0.04;
631 
632  icub = getResource<iCubRobot>();
634 
635  if (musclePairs != NULL) {
636  for(int i = 0; i < 2; i++) {
637  delete musclePairs[i];
638  }
639  delete[] musclePairs;
640  }
641 
642  musclePairs = new MusclePair*[2];
643  for (int i = 0; i < 2; i++) {
644  int ir;
645 
646  if (i==0) ir=0;
647  if (i==1) ir=2;
648 
649  musclePairs[i] = new MusclePair(icubMotors->motors()[ir],3.0f, maxVel, 2.5f, 3.7f);
650  }
651  } else if (resourceName == neuronsIteratorResource) {
652  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
653  evonetIt->setCurrentBlock( name() );
654 
655  for( int i = 0; i < 2; i++ ) {
656  for (int m = 0; m < 2; m++) {
657  if (m == 0) {
658  evonetIt->setGraphicProperties( QString("T") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
659  } else {
660  evonetIt->setGraphicProperties( QString("T") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
661  }
662 
663  evonetIt->nextNeuron();
664  }
665  }
666  } else {
667  Logger::info("Unknown resource " + resourceName + " for " + name());
668  }
669 }
670 
672  iCubMotor(params, prefix),
673  musclePairs(NULL),
674  icub(NULL),
675  icubMotors(NULL) {
676  // Declaring the resources that are needed here
678 }
679 
681  /*here we need to delete muscles objects */
682  for(int i=0;i<2;i++)
683  delete musclePairs[i];
684  delete[] musclePairs;
685 
686 }
687 void iCubHeadMusclesMotor::save( ConfigurationParameters& params, QString prefix ) {
688  iCubMotor::save( params, prefix );
689  params.startObjectParameters( prefix, "iCubHeadMusclesMotor", this );
690 
691 }
692 
693 void iCubHeadMusclesMotor::describe( QString type ) {
694  iCubMotor::describe( type );
695  Descriptor d = addTypeDescription( type, "Muscular control of the head, 2 muscles for each joint" );
696 }
697 
699  // Checking all resources we need exist
701 
702  // Acquiring the lock to get resources
703  ResourcesLocker locker( this );
704 
705  //to do:
706  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
707  evonetIt->setCurrentBlock( name() );
708  for( int i=0; i<2; i++ ) {
709 
710  int id=0;
711  double a=evonetIt->getOutput();
712  evonetIt->nextNeuron();
713  double b=evonetIt->getOutput();
714  evonetIt->nextNeuron();
715  musclePairs[i]->setActivation(a,b);
716  if (i==0) id=0;
717  if (i==1) id=2;
718  icubMotors->velocityMove(i,musclePairs[id]->apply());
719  }
720 }
721 
723  return 4;
724 }
725 
726 void iCubHeadMusclesMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
727  iCubMotor::resourceChanged(resourceName, changeType);
728 
729  if (changeType == Deleted) {
730  return;
731  }
732 
733  if (resourceName == icubResource) {
734  // getting the resources and initializing MotorController
735  const float maxVel = 50;
736  // const float viscos = 0.04;
737 
738  icub = getResource<iCubRobot>();
740 
741  if (musclePairs != NULL) {
742  for(int i = 0; i < 2; i++) {
743  delete musclePairs[i];
744  }
745  delete[] musclePairs;
746  }
747 
748  musclePairs = new MusclePair*[2];
749  for (int i = 0; i < 2; i++) {
750  musclePairs[i] = new MusclePair(icubMotors->motors()[i],3.0f, maxVel, 2.5f, 3.7f);
751  }
752  } else if (resourceName == neuronsIteratorResource) {
753  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
754  evonetIt->setCurrentBlock( name() );
755  for( int i = 0; i < 2; i++ ) {
756  for (int m = 0;m < 2; m++) {
757  if ( m == 0 ) {
758  evonetIt->setGraphicProperties( QString("N") + QString::number(i) + QString("'"), 0.0, 1.0, Qt::red );
759  } else {
760  evonetIt->setGraphicProperties( QString("N") + QString::number(i) + QString("\""), 0.0, 1.0, Qt::red );
761  }
762 
763  evonetIt->nextNeuron();
764  }
765  }
766  } else {
767  Logger::info("Unknown resource " + resourceName + " for " + name());
768  }
769 }
770 
772  iCubMotor(params, prefix),
773  icub(NULL),
774  icubMotors(NULL) {
775  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
776  // Declaring the resources that are needed here
778 }
779 
781  /*nothing to do */
782 }
783 
784 void iCubArmPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
785  iCubMotor::save( params, prefix );
786  params.startObjectParameters( prefix, "iCubArmPosToVelMotor", this );
787  params.createParameter( prefix, "arm", icubArm );
788 }
789 
790 void iCubArmPosToVelMotor::describe( QString type ) {
791  iCubMotor::describe( type );
792  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right/left arm" );
793  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
794 }
795 
797  // Checking all resources we need exist
799 
800  // Acquiring the lock to get resources
801  ResourcesLocker locker( this );
802 
803  //to do:
804  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
805  evonetIt->setCurrentBlock( name() );
806  for( int i=0; i<7; i++ ) {
807  double actual;
808  double min,max;
809  double desiredPosture;
810  icubMotors->getEncoder(i,&actual);
811  icubMotors->getLimits(i,&min,&max);
812  double a=evonetIt->getOutput();
813  desiredPosture=linearMap(a,0.0,1.0,min, max);
814  evonetIt->nextNeuron();
815  icubMotors->velocityMove(i,pcontrol.velocityForJoint(desiredPosture, actual));
816  }
817 }
818 
820  return 7;
821 }
822 
823 void iCubArmPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
824  iCubMotor::resourceChanged(resourceName, changeType);
825 
826  if (changeType == Deleted) {
827  return;
828  }
829 
830  if (resourceName == icubResource) {
831  icub = getResource<iCubRobot>();
832  if ( icubArm == "right" ) {
834  } else {
836  }
837  } else if (resourceName == neuronsIteratorResource) {
838  QString label;
839  if ( icubArm == "right" ) {
840  label="R";
841  } else {
842  label="L";
843  }
844 
845  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
846  evonetIt->setCurrentBlock( name() );
847  for( int i=0; i<7; i++ ) {
848  evonetIt->setGraphicProperties(label+ QString("A")+QString::number(i), 0.0, 1.0, Qt::red ); //ex Ap2V
849 
850  evonetIt->nextNeuron();
851  }
852  } else {
853  Logger::info("Unknown resource " + resourceName + " for " + name());
854  }
855 }
856 
858  iCubMotor(params, prefix),
859  icub(NULL),
860  icubMotors(NULL) {
861  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
862  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
863  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
864  // Declaring the resources that are needed here
866 }
867 
869  /*nothing to do */
870 }
871 
872 void iCubTorsoPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
873  iCubMotor::save( params, prefix );
874  params.startObjectParameters( prefix, "iCubTorsoPosToVelMotor", this );
875  if ( !torsoDofs[0] ) {
876  params.createParameter( prefix, "disableYaw", "true" );
877  }
878  if ( !torsoDofs[1] ) {
879  params.createParameter( prefix, "disableRoll", "true" );
880  }
881  if ( !torsoDofs[2] ) {
882  params.createParameter( prefix, "disablePitch", "true" );
883  }
884 }
885 
886 void iCubTorsoPosToVelMotor::describe( QString type ) {
887  iCubMotor::describe( type );
888  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the torso" );
889  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
890  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
891  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
892 }
893 
895  // Checking all resources we need exist
897 
898  // Acquiring the lock to get resources
899  ResourcesLocker locker( this );
900 
901  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
902  evonetIt->setCurrentBlock( name() );
903  for( int i=0; i<3; i++ ) {
904  double vel = 0.0;
905  if ( torsoDofs[i] ) {
906  double actual;
907  double min,max;
908  double desiredPosture;
909  icubMotors->getEncoder(i,&actual);
910  icubMotors->getLimits(i,&min,&max);
911  double a=evonetIt->getOutput();
912  desiredPosture=linearMap(a,0.0,1.0,min, max);
913  vel = pcontrol.velocityForJoint(desiredPosture, actual);
914  evonetIt->nextNeuron();
915  }
916  icubMotors->velocityMove( i, vel );
917  }
918 }
919 
921  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
922 }
923 
924 void iCubTorsoPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
925  iCubMotor::resourceChanged(resourceName, changeType);
926 
927  if (changeType == Deleted) {
928  return;
929  }
930 
931  if (resourceName == icubResource) {
932  icub = getResource<iCubRobot>();
934  } else if (resourceName == neuronsIteratorResource) {
935  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
936  evonetIt->setCurrentBlock( name() );
937  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
938  for( int i=0; i<numDofs; i++ ) {
939  evonetIt->setGraphicProperties( QString("T")+QString::number(i), 0.0, 1.0, Qt::red );
940  evonetIt->nextNeuron();
941  }
942  } else {
943  Logger::info("Unknown resource " + resourceName + " for " + name());
944  }
945 }
946 
948  iCubMotor(params, prefix),
949  icub(NULL),
950  icubMotors(NULL) {
951  headDofs[0] =! ConfigurationHelper::getBool(params, prefix+"disableNeckPitch",false);
952  headDofs[1] =! ConfigurationHelper::getBool(params, prefix+"disableNeckRoll",false);
953  headDofs[2] =! ConfigurationHelper::getBool(params, prefix+"disableNeckYaw",false);
954  headDofs[3] =! ConfigurationHelper::getBool(params, prefix+"disableEyesTilt",false);
955  headDofs[4] =! ConfigurationHelper::getBool(params, prefix+"disableEyesVersion",false);
956  headDofs[5] =! ConfigurationHelper::getBool(params, prefix+"disableEyesVergence",false);
957 
958  const double k = ConfigurationHelper::getDouble(params, prefix + "k", 0.3);
959  const double maxvel = ConfigurationHelper::getDouble(params, prefix + "maxvel", 20.0);
960  pcontrol.setControlParameters(k, maxvel);
961 
962 
963  // Declaring the resources that are needed here
965 }
966 
968  /*nothing to do */
969 }
970 
971 void iCubHeadPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
972  iCubMotor::save( params, prefix );
973  params.startObjectParameters( prefix, "iCubHeadPosToVelMotor", this );
974  if (!headDofs[0])
975  params.createParameter(prefix,"disableNeckPitch","true");
976  if (!headDofs[1])
977  params.createParameter(prefix,"disableNeckRoll","true");
978  if (!headDofs[2])
979  params.createParameter(prefix,"disableNeckYaw","true");
980  if (!headDofs[3])
981  params.createParameter(prefix,"disableEyesTilt","true");
982  if (!headDofs[4])
983  params.createParameter(prefix,"disableEyesVersion","true");
984  if (!headDofs[5])
985  params.createParameter(prefix,"disableEyesVergence","true");
986 
987  params.createParameter(prefix, "k", QString::number(pcontrol.getK()));
988  params.createParameter(prefix, "maxvel", QString::number(pcontrol.getMaxVelocity()));
989 }
990 
991 void iCubHeadPosToVelMotor::describe( QString type ) {
992  iCubMotor::describe( type );
993  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the Head" );
994  d.describeBool("disableNeckPitch").def(false).help("Disables the #0 joint of the head: Neck Pitch");
995  d.describeBool("disableNeckRoll").def(false).help("Disables the #1 joint of the head: Neck Roll");
996  d.describeBool("disableNeckYaw").def(false).help("Disables the #2 joint of the head: Neck Yaw");
997  d.describeBool("disableEyesTilt").def(false).help("Disables the #3 joint of the head: Eyes tilt");
998  d.describeBool("disableEyesVersion").def(false).help("Disables the #4 joint of the head: Eyes version");
999  d.describeBool("disableEyesVergence").def(false).help("Disables the #5 joint of the head: Eyes vergence");
1000  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");
1001  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");
1002 
1003 }
1004 
1006  // Checking all resources we need exist
1008 
1009  // Acquiring the lock to get resources
1010  ResourcesLocker locker( this );
1011 
1012  //to do:
1013  int id=0;
1014  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1015  evonetIt->setCurrentBlock( name() );
1016 
1017  for(int i=0;i<6;i++)
1018  {
1019  if(headDofs[i])
1020  {
1021  double actual;
1022  double min,max;
1023  double desiredPosture;
1024  double a;
1025 
1026  icubMotors->getEncoder(i,&actual);
1027  icubMotors->getLimits(i,&min,&max);
1028  a=evonetIt->getOutput();
1029  evonetIt->nextNeuron();
1030  desiredPosture=linearMap(a,0.0,1.0,min, max);
1031 
1032  icubMotors->velocityMove(i, pcontrol.velocityForJoint(desiredPosture, actual));
1033  }else
1034  {
1035  icubMotors->velocityMove(i,0);
1036  }
1037  }
1038 
1039 
1040 }
1041 
1043  int s=0;
1044  for(int i=0;i<6;i++) s+=headDofs[i];
1045  return s;
1046 }
1047 
1048 void iCubHeadPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1049  iCubMotor::resourceChanged(resourceName, changeType);
1050 
1051  if (changeType == Deleted) {
1052  return;
1053  }
1054 
1055  if (resourceName == icubResource) {
1056  icub = getResource<iCubRobot>();
1058  } else if (resourceName == neuronsIteratorResource) {
1059  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1060  evonetIt->setCurrentBlock( name() );
1061 
1062  for( int i=0; i<size(); i++ ) {
1063  evonetIt->setGraphicProperties(QString("N")+QString::number(i), 0.0, 1.0, Qt::red );//ex Hp2v
1064  evonetIt->nextNeuron();
1065  }
1066  } else {
1067  Logger::info("Unknown resource " + resourceName + " for " + name());
1068  }
1069 }
1070 
1072  iCubMotor(params, prefix),
1073  icub(NULL),
1074  icubMotors(NULL) {
1075  icubHand = ConfigurationHelper::getString( params, prefix+"hand", "right" );
1076  // Declaring the resources that are needed here
1077  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1078 }
1079 
1081  /*nothing to do */
1082 
1083 
1084 }
1085 void iCubHandPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
1086  iCubMotor::save( params, prefix );
1087  params.startObjectParameters( prefix, "iCubHandPosToVelMotor", this );
1088  params.createParameter( prefix, "hand", icubHand );
1089 
1090 }
1091 
1092 void iCubHandPosToVelMotor::describe( QString type ) {
1093  iCubMotor::describe( type );
1094  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right or left Hand" );
1095  d.describeEnum( "hand" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The hand to move" );
1096 }
1097 
1099  // Checking all resources we need exist
1101 
1102  // Acquiring the lock to get resources
1103  ResourcesLocker locker( this );
1104 
1105  //to do:
1106  int id=0;
1107  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1108  evonetIt->setCurrentBlock( name() );
1109  for( int i=7; i<16; i++ ) {
1110  double actual;
1111  double min,max;
1112  double desiredPosture;
1113  icubMotors->getEncoder(i,&actual);
1114  icubMotors->getLimits(i,&min,&max);
1115  double a=evonetIt->getOutput();
1116  desiredPosture=linearMap(a,0.0,1.0,min, max);
1117  evonetIt->nextNeuron();
1118  icubMotors->velocityMove(i,pcontrol.velocityForJoint(desiredPosture, actual));
1119  }
1120 }
1121 
1123  return 9;
1124 }
1125 
1126 void iCubHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1127  iCubMotor::resourceChanged(resourceName, changeType);
1128 
1129  if (changeType == Deleted) {
1130  return;
1131  }
1132 
1133  if (resourceName == icubResource) {
1134  icub = getResource<iCubRobot>();
1135  if ( icubHand == "right" ) {
1137  } else {
1139  }
1140  } else if (resourceName == neuronsIteratorResource) {
1141  QString label;
1142 
1143  if ( icubHand == "right" ) {
1144  label="R";
1145  } else {
1146  label="L";
1147  }
1148 
1149  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1150  evonetIt->setCurrentBlock( name() );
1151  for( int i=0; i<9; i++ ) {
1152  evonetIt->setGraphicProperties(label+QString("F")+QString::number(i), 0.0, 1.0, Qt::red ); //ex HNp2v
1153 
1154  evonetIt->nextNeuron();
1155  }
1156  } else {
1157  Logger::info("Unknown resource " + resourceName + " for " + name());
1158  }
1159 }
1160 
1162  iCubMotor(params, prefix),
1163  m_headNeckController(NULL)
1164 {
1165  // Declaring the resources that are needed here
1167 }
1168 
1170 {
1171 }
1172 
1174 {
1175  iCubMotor::save(params, prefix);
1176  params.startObjectParameters(prefix, "iCubHeadVelocityMotor", this);
1177 }
1178 
1180 {
1181  iCubMotor::describe(type);
1182  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)");
1183 }
1184 
1186 {
1187  // Checking all resources we need exist
1189 
1190  // Acquiring the lock to get resources
1191  ResourcesLocker locker( this );
1192 
1193  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1194  evonetIt->setCurrentBlock(name());
1195 
1196  const double pan = evonetIt->getOutput();
1197  evonetIt->nextNeuron();
1198  const double tilt = evonetIt->getOutput();
1199  evonetIt->nextNeuron(); // Removed to avoid a warning from NeuronsIterator //remotion works only for the last sensor
1200  const double maxVelocity = 50.0;
1201 
1202  // Computing the velocity (in degrees/sec)
1203  double panVelocity = (pan - 0.5) * 2.0 * maxVelocity;
1204  double tiltVelocity = (tilt - 0.5) * 2.0 * maxVelocity;
1205 
1206  // Now moving the head
1207  m_headNeckController->velocityMove(2, panVelocity);
1208  m_headNeckController->velocityMove(0, tiltVelocity);
1209 }
1210 
1212 {
1213  return 2;
1214 }
1215 
1216 void iCubHeadVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1217 {
1218  iCubMotor::resourceChanged(resourceName, changeType);
1219 
1220  if (changeType == Deleted) {
1221  return;
1222  }
1223 
1224  if (resourceName == icubResource) {
1225  m_headNeckController = getResource<iCubRobot>()->headNeckController();
1226  } else if (resourceName == neuronsIteratorResource) {
1227  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1228  evonetIt->setCurrentBlock(name());
1229  evonetIt->setGraphicProperties(QString("N0"), 0.0, 1.0, Qt::blue); //ex hp
1230  evonetIt->nextNeuron();
1231  evonetIt->setGraphicProperties(QString("N1"), 0.0, 1.0, Qt::blue); //ex ht
1232  } else {
1233  Logger::info("Unknown resource " + resourceName + " for " + name());
1234  }
1235 }
1236 
1238  iCubMotor(params, prefix),
1239  m_icubHand("right"),
1240  m_jointsGroups(),
1241  m_maxClosure(1.0),
1242  m_armController(NULL),
1243  m_pcontrol()
1244 {
1245  m_icubHand = ConfigurationHelper::getString(params, prefix + "hand", "right");
1246 
1247  // Reading the fingers joints configuration. The configuration format is like this: joint1,joint2/joint5,joint6,joint3/joint4.
1248  // In this case we have three groups of joints: (joint1 and joint2), (joint5, joint6 and joint3) and (joint4). Joints in the
1249  // same group are controlled together. This parameter is compulsory, if it is not present, the program throws an exception.
1250  // The joint names are: Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others (case insensitive)
1251  const QString paramName = prefix + "jointsGroups";
1252  QString str = params.getValue(paramName);
1253  if (str.isEmpty()) {
1254  ConfigurationHelper::throwUserConfigError(paramName, "", "the parameter is compulsory but is not present in the configuration");
1255  }
1256 
1257  const QStringList groups = str.split("/", QString::SkipEmptyParts);
1258  if (groups.size() == 0) {
1259  ConfigurationHelper::throwUserConfigError(paramName, str, "invalid format (no joint group)");
1260  }
1261 
1262  // Resizing the vector of joint groups
1263  m_jointsGroups.resize(groups.size());
1264  for (int i = 0; i < m_jointsGroups.size(); i++) {
1265  // Splitting again
1266  const QStringList joints = groups[i].split(",", QString::SkipEmptyParts);
1267  if (joints.size() == 0) {
1268  ConfigurationHelper::throwUserConfigError(paramName, str, "a joint group has no components");
1269  }
1270 
1271  m_jointsGroups[i].resize(joints.size());
1272  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1273  // Now converting joint name into joint index. First converting to uppercase
1274  const QString jointName = joints[j].toUpper().trimmed();
1275  if (jointName == "APERTURE") {
1276  m_jointsGroups[i][j] = 7;
1277  } else if (jointName == "THUMB1") {
1278  m_jointsGroups[i][j] = 8;
1279  } else if (jointName == "THUMB2") {
1280  m_jointsGroups[i][j] = 9;
1281  } else if (jointName == "THUMB3") {
1282  m_jointsGroups[i][j] = 10;
1283  } else if (jointName == "INDEX1") {
1284  m_jointsGroups[i][j] = 11;
1285  } else if (jointName == "INDEX2") {
1286  m_jointsGroups[i][j] = 12;
1287  } else if (jointName == "MIDDLE1") {
1288  m_jointsGroups[i][j] = 13;
1289  } else if (jointName == "MIDDLE2") {
1290  m_jointsGroups[i][j] = 14;
1291  } else if (jointName == "OTHERS") {
1292  m_jointsGroups[i][j] = 15;
1293  } else {
1294  ConfigurationHelper::throwUserConfigError(paramName, str, "unknown joint name (" + jointName + ")");
1295  }
1296  }
1297  }
1298 
1299  // Now reading the parameter with the maximum joint closure
1300  m_maxClosure = ConfigurationHelper::getDouble(params, prefix + "maxClosure", 1.0);
1301 
1302  const double k = ConfigurationHelper::getDouble(params, prefix + "k", 0.3);
1303  const double maxvel = ConfigurationHelper::getDouble(params, prefix + "maxvel", 20.0);
1304  m_pcontrol.setControlParameters(k, maxvel);
1305 
1306  // Declaring the resources that are needed here
1308 }
1309 
1311 {
1312 }
1313 
1315 {
1316  iCubMotor::save( params, prefix );
1317  params.startObjectParameters(prefix, "ConfigurableHandPosToVelMotor", this);
1318  params.createParameter(prefix, "hand", m_icubHand);
1319 
1320  QString strJointsGroup;
1321  for (int g = 0; g < m_jointsGroups.size(); g++) {
1322  for (int j = 0; j < m_jointsGroups[g].size(); j++) {
1323  if (m_jointsGroups[g][j] == 7) {
1324  strJointsGroup += "Aperture";
1325  } else if (m_jointsGroups[g][j] == 8) {
1326  strJointsGroup += "Thumb1";
1327  } else if (m_jointsGroups[g][j] == 9) {
1328  strJointsGroup += "Thumb2";
1329  } else if (m_jointsGroups[g][j] == 10) {
1330  strJointsGroup += "Thumb3";
1331  } else if (m_jointsGroups[g][j] == 11) {
1332  strJointsGroup += "Index1";
1333  } else if (m_jointsGroups[g][j] == 12) {
1334  strJointsGroup += "Index2";
1335  } else if (m_jointsGroups[g][j] == 13) {
1336  strJointsGroup += "Middle1";
1337  } else if (m_jointsGroups[g][j] == 14) {
1338  strJointsGroup += "Middle2";
1339  } else if (m_jointsGroups[g][j] == 15) {
1340  strJointsGroup += "Others";
1341  } else {
1342  strJointsGroup += "ERROR";
1343  }
1344 
1345  if (j != (m_jointsGroups[g].size() - 1)) {
1346  strJointsGroup += ", ";
1347  }
1348  }
1349  if (g != (m_jointsGroups.size() - 1)) {
1350  strJointsGroup += " / ";
1351  }
1352  }
1353  params.createParameter(prefix, "jointsGroups", strJointsGroup);
1354 
1355  params.createParameter(prefix, "maxClosure", QString::number(m_maxClosure));
1356  params.createParameter(prefix, "k", QString::number(m_pcontrol.getK()));
1357  params.createParameter(prefix, "maxvel", QString::number(m_pcontrol.getMaxVelocity()));
1358 }
1359 
1361 {
1362  iCubMotor::describe( type );
1363  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)");
1364  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\"");
1365  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)");
1366  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");
1367  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");
1368  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");
1369 }
1370 
1372 {
1373  // Checking all resources we need exist
1375 
1376  // Acquiring the lock to get resources
1377  ResourcesLocker locker(this);
1378 
1379  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1380  evonetIt->setCurrentBlock(name());
1381 
1382  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1383  // The network output value is multiplied by m_maxClosure so that the maximum closure of fingers can be
1384  // constrained
1385  const double out = m_maxClosure * evonetIt->getOutput();
1386 
1387  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1388  double actual;
1389  double min, max;
1390 
1391  m_armController->getEncoder(m_jointsGroups[i][j], &actual);
1392  m_armController->getLimits(m_jointsGroups[i][j], &min, &max);
1393 
1394  const double desiredPosture = linearMap(out, 0.0, 1.0, min, max); // normalize the neuron output with respect to the joint range
1395 
1396  m_armController->velocityMove(m_jointsGroups[i][j], m_pcontrol.velocityForJoint(desiredPosture, actual));
1397  }
1398  }
1399 }
1400 
1402 {
1403  return m_jointsGroups.size();
1404 }
1405 
1406 void iCubConfigurableHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1407 {
1408  iCubMotor::resourceChanged(resourceName, changeType);
1409 
1410  if (changeType == Deleted) {
1411  return;
1412  }
1413 
1414  if (resourceName == icubResource) {
1415  iCubRobot* icub = getResource<iCubRobot>();
1416  if (m_icubHand == "left") {
1417  m_armController = icub->leftArmController();
1418  } else {
1419  m_armController = icub->rightArmController();
1420  }
1421  } else if (resourceName == neuronsIteratorResource) {
1422  QString label;
1423  if (m_icubHand == "left") {
1424  label = "L";
1425  } else {
1426  label = "R";
1427  }
1428 
1429  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1430  evonetIt->setCurrentBlock(name());
1431  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1432  evonetIt->setGraphicProperties(label + "F" + QString::number(i), 0.0, 1.0, Qt::blue);
1433  }
1434  } else {
1435  Logger::info("Unknown resource " + resourceName + " for " + name());
1436  }
1437 }
1438 
1440  iCubMotor(params, prefix),
1441  icub(NULL),
1442  icubMotors(NULL) {
1443  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
1444  is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
1445  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1446  // Declaring the resources that are needed here
1447  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1448 }
1449 
1451  /* nothing to do */
1452 }
1453 
1454 void iCubArmVelocityMotor::save( ConfigurationParameters& params, QString prefix ) {
1455  iCubMotor::save( params, prefix );
1456  params.startObjectParameters( prefix, "iCubArmVelocityMotor", this );
1457  params.createParameter( prefix, "arm", icubArm );
1458  params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
1459  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1460 }
1461 
1462 void iCubArmVelocityMotor::describe( QString type ) {
1463  iCubMotor::describe( type );
1464  Descriptor d = addTypeDescription( type, "Move the arm by velocity." );
1465  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
1466  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" );
1467  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1468 }
1469 
1471  // Checking all resources we need exist
1473 
1474  // Acquiring the lock to get resources
1475  ResourcesLocker locker( this );
1476 
1477  double vels[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
1478  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1479  evonetIt->setCurrentBlock( name() );
1480  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1481  vels[i] = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1482  }
1483  for( int i=0; i<7; i++ ) {
1484  icubMotors->velocityMove( i, vels[i] );
1485  }
1486 }
1487 
1489  return (is7Dof ? 7 : 4);
1490 }
1491 
1492 void iCubArmVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1493  iCubMotor::resourceChanged(resourceName, changeType);
1494 
1495  if (changeType == Deleted) {
1496  return;
1497  }
1498 
1499  if (resourceName == icubResource) {
1500  icub = getResource<iCubRobot>();
1501  if ( icubArm == "right" ) {
1502  startJointId = iCubRobot::right_shoulder_pitch;
1503  icubMotors = icub->rightArmController();
1504  } else {
1505  startJointId = iCubRobot::left_shoulder_pitch;
1506  icubMotors = icub->leftArmController();
1507  }
1508  } else if (resourceName == neuronsIteratorResource) {
1509  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1510  evonetIt->setCurrentBlock( name() );
1511  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1512  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1513  }
1514  } else {
1515  Logger::info("Unknown resource " + resourceName + " for " + name());
1516  }
1517 }
1518 
1520  iCubMotor(params, prefix),
1521  icub(NULL),
1522  icubMotors(NULL) {
1523  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
1524  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
1525  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
1526  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1527  // Declaring the resources that are needed here
1528  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1529 }
1530 
1532  /* nothing to do */
1533 }
1534 
1536  iCubMotor::save( params, prefix );
1537  params.startObjectParameters( prefix, "iCubTorsoVelocityMotor", this );
1538  if ( !torsoDofs[0] ) {
1539  params.createParameter( prefix, "disableYaw", "true" );
1540  }
1541  if ( !torsoDofs[1] ) {
1542  params.createParameter( prefix, "disableRoll", "true" );
1543  }
1544  if ( !torsoDofs[2] ) {
1545  params.createParameter( prefix, "disablePitch", "true" );
1546  }
1547  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1548 }
1549 
1550 void iCubTorsoVelocityMotor::describe( QString type ) {
1551  iCubMotor::describe( type );
1552  Descriptor d = addTypeDescription( type, "Move the torso by velocity." );
1553  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
1554  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
1555  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
1556  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1557 }
1558 
1560  // Checking all resources we need exist
1562 
1563  // Acquiring the lock to get resources
1564  ResourcesLocker locker( this );
1565 
1566  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1567  evonetIt->setCurrentBlock( name() );
1568  for( int i=0; i<3; i++ ) {
1569  double vel = 0.0;
1570  if ( torsoDofs[i] ) {
1571  vel = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1572  evonetIt->nextNeuron();
1573  }
1574  icubMotors->velocityMove( i, vel );
1575  }
1576 }
1577 
1579  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1580 }
1581 
1582 void iCubTorsoVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1583  iCubMotor::resourceChanged(resourceName, changeType);
1584 
1585  if (changeType == Deleted) {
1586  return;
1587  }
1588 
1589  if (resourceName == icubResource) {
1590  icub = getResource<iCubRobot>();
1591  icubMotors = icub->torsoController();
1592  } else if (resourceName == neuronsIteratorResource) {
1593  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1594  evonetIt->setCurrentBlock( name() );
1595  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1596  for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
1597  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1598  }
1599  } else {
1600  Logger::info("Unknown resource " + resourceName + " for " + name());
1601  }
1602 }
1603 
1604 } // end namespace farsa
1605 
1606 #endif