icubmotors.cpp
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
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  // Declaring the resources that are needed here
863 }
864 
866  /*nothing to do */
867 }
868 
869 void iCubTorsoPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
870  iCubMotor::save( params, prefix );
871  params.startObjectParameters( prefix, "iCubTorsoPosToVelMotor", this );
872 }
873 
874 void iCubTorsoPosToVelMotor::describe( QString type ) {
875  iCubMotor::describe( type );
876  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the torso" );
877 }
878 
880  // Checking all resources we need exist
882 
883  // Acquiring the lock to get resources
884  ResourcesLocker locker( this );
885 
886  //to do:
887  int id=0;
888  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
889  evonetIt->setCurrentBlock( name() );
890  for( int i=0; i<2; i++ ) {
891  double actual;
892  double min,max;
893  double desiredPosture;
894  icubMotors->getEncoder(i,&actual);
895  icubMotors->getLimits(i,&min,&max);
896  double a=evonetIt->getOutput();
897  desiredPosture=linearMap(a,0.0,1.0,min, max);
898  evonetIt->nextNeuron();
899  if (i==0) id=0;
900  if (i==1) id=2;
901  icubMotors->velocityMove(id,pcontrol.velocityForJoint(desiredPosture, actual));
902  }
903 }
904 
906  return 2;
907 }
908 
909 void iCubTorsoPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
910  iCubMotor::resourceChanged(resourceName, changeType);
911 
912  if (changeType == Deleted) {
913  return;
914  }
915 
916  if (resourceName == icubResource) {
917  icub = getResource<iCubRobot>();
919  } else if (resourceName == neuronsIteratorResource) {
920  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
921  evonetIt->setCurrentBlock( name() );
922  for( int i=0; i<2; i++ ) {
923  evonetIt->setGraphicProperties(QString("T")+QString::number(i), 0.0, 1.0, Qt::red ); //ex Tp2v
924 
925  evonetIt->nextNeuron();
926  }
927  } else {
928  Logger::info("Unknown resource " + resourceName + " for " + name());
929  }
930 }
931 
933  iCubMotor(params, prefix),
934  icub(NULL),
935  icubMotors(NULL) {
936  // Declaring the resources that are needed here
938 }
939 
941  /*nothing to do */
942 }
943 
944 void iCubHeadPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
945  iCubMotor::save( params, prefix );
946  params.startObjectParameters( prefix, "iCubHeadPosToVelMotor", this );
947 }
948 
949 void iCubHeadPosToVelMotor::describe( QString type ) {
950  iCubMotor::describe( type );
951  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the Head" );
952 }
953 
955  // Checking all resources we need exist
957 
958  // Acquiring the lock to get resources
959  ResourcesLocker locker( this );
960 
961  //to do:
962  int id=0;
963  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
964  evonetIt->setCurrentBlock( name() );
965 
966  double actual;
967  double min,max;
968  double desiredPosture;
969  double a;
970 
971  icubMotors->getEncoder(0,&actual);
972  icubMotors->getLimits(0,&min,&max);
973  a=evonetIt->getOutput();
974  evonetIt->nextNeuron();
975  desiredPosture=linearMap(a,0.0,1.0,min, max);
976  icubMotors->velocityMove(0,pcontrol.velocityForJoint(desiredPosture, actual));
977 
978  icubMotors->getEncoder(2,&actual);
979  icubMotors->getLimits(2,&min,&max);
980  a=evonetIt->getOutput();
981  desiredPosture=linearMap(a,0.0,1.0,min, max);
982  icubMotors->velocityMove(2,pcontrol.velocityForJoint(desiredPosture, actual));
983 }
984 
986  return 2;
987 }
988 
989 void iCubHeadPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
990  iCubMotor::resourceChanged(resourceName, changeType);
991 
992  if (changeType == Deleted) {
993  return;
994  }
995 
996  if (resourceName == icubResource) {
997  icub = getResource<iCubRobot>();
999  } else if (resourceName == neuronsIteratorResource) {
1000  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1001  evonetIt->setCurrentBlock( name() );
1002 
1003  for( int i=0; i<2; i++ ) {
1004  evonetIt->setGraphicProperties(QString("N")+QString::number(i), 0.0, 1.0, Qt::red );//ex Hp2v
1005  evonetIt->nextNeuron();
1006  }
1007  } else {
1008  Logger::info("Unknown resource " + resourceName + " for " + name());
1009  }
1010 }
1011 
1013  iCubMotor(params, prefix),
1014  icub(NULL),
1015  icubMotors(NULL) {
1016  icubHand = ConfigurationHelper::getString( params, prefix+"hand", "right" );
1017  // Declaring the resources that are needed here
1018  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1019 }
1020 
1022  /*nothing to do */
1023 
1024 
1025 }
1026 void iCubHandPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
1027  iCubMotor::save( params, prefix );
1028  params.startObjectParameters( prefix, "iCubHandPosToVelMotor", this );
1029  params.createParameter( prefix, "hand", icubHand );
1030 
1031 }
1032 
1033 void iCubHandPosToVelMotor::describe( QString type ) {
1034  iCubMotor::describe( type );
1035  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right or left Hand" );
1036  d.describeEnum( "hand" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The hand to move" );
1037 }
1038 
1040  // Checking all resources we need exist
1042 
1043  // Acquiring the lock to get resources
1044  ResourcesLocker locker( this );
1045 
1046  //to do:
1047  int id=0;
1048  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1049  evonetIt->setCurrentBlock( name() );
1050  for( int i=7; i<16; i++ ) {
1051  double actual;
1052  double min,max;
1053  double desiredPosture;
1054  icubMotors->getEncoder(i,&actual);
1055  icubMotors->getLimits(i,&min,&max);
1056  double a=evonetIt->getOutput();
1057  desiredPosture=linearMap(a,0.0,1.0,min, max);
1058  evonetIt->nextNeuron();
1059  icubMotors->velocityMove(i,pcontrol.velocityForJoint(desiredPosture, actual));
1060  }
1061 }
1062 
1064  return 9;
1065 }
1066 
1067 void iCubHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1068  iCubMotor::resourceChanged(resourceName, changeType);
1069 
1070  if (changeType == Deleted) {
1071  return;
1072  }
1073 
1074  if (resourceName == icubResource) {
1075  icub = getResource<iCubRobot>();
1076  if ( icubHand == "right" ) {
1078  } else {
1080  }
1081  } else if (resourceName == neuronsIteratorResource) {
1082  QString label;
1083 
1084  if ( icubHand == "right" ) {
1085  label="R";
1086  } else {
1087  label="L";
1088  }
1089 
1090  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1091  evonetIt->setCurrentBlock( name() );
1092  for( int i=0; i<9; i++ ) {
1093  evonetIt->setGraphicProperties(label+QString("F")+QString::number(i), 0.0, 1.0, Qt::red ); //ex HNp2v
1094 
1095  evonetIt->nextNeuron();
1096  }
1097  } else {
1098  Logger::info("Unknown resource " + resourceName + " for " + name());
1099  }
1100 }
1101 
1103  iCubMotor(params, prefix),
1104  m_headNeckController(NULL)
1105 {
1106  // Declaring the resources that are needed here
1108 }
1109 
1111 {
1112 }
1113 
1115 {
1116  iCubMotor::save(params, prefix);
1117  params.startObjectParameters(prefix, "iCubHeadVelocityMotor", this);
1118 }
1119 
1121 {
1122  iCubMotor::describe(type);
1123  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)");
1124 }
1125 
1127 {
1128  // Checking all resources we need exist
1130 
1131  // Acquiring the lock to get resources
1132  ResourcesLocker locker( this );
1133 
1134  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1135  evonetIt->setCurrentBlock(name());
1136 
1137  const double pan = evonetIt->getOutput();
1138  evonetIt->nextNeuron();
1139  const double tilt = evonetIt->getOutput();
1140  evonetIt->nextNeuron(); // Removed to avoid a warning from NeuronsIterator //remotion works only for the last sensor
1141  const double maxVelocity = 50.0;
1142 
1143  // Computing the velocity (in degrees/sec)
1144  double panVelocity = (pan - 0.5) * 2.0 * maxVelocity;
1145  double tiltVelocity = (tilt - 0.5) * 2.0 * maxVelocity;
1146 
1147  // Now moving the head
1148  m_headNeckController->velocityMove(2, panVelocity);
1149  m_headNeckController->velocityMove(0, tiltVelocity);
1150 }
1151 
1153 {
1154  return 2;
1155 }
1156 
1157 void iCubHeadVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1158 {
1159  iCubMotor::resourceChanged(resourceName, changeType);
1160 
1161  if (changeType == Deleted) {
1162  return;
1163  }
1164 
1165  if (resourceName == icubResource) {
1166  m_headNeckController = getResource<iCubRobot>()->headNeckController();
1167  } else if (resourceName == neuronsIteratorResource) {
1168  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1169  evonetIt->setCurrentBlock(name());
1170  evonetIt->setGraphicProperties(QString("N0"), 0.0, 1.0, Qt::blue); //ex hp
1171  evonetIt->nextNeuron();
1172  evonetIt->setGraphicProperties(QString("N1"), 0.0, 1.0, Qt::blue); //ex ht
1173  } else {
1174  Logger::info("Unknown resource " + resourceName + " for " + name());
1175  }
1176 }
1177 
1179  iCubMotor(params, prefix),
1180  m_icubHand("right"),
1181  m_jointsGroups(),
1182  m_maxClosure(1.0),
1183  m_armController(NULL),
1184  m_pcontrol()
1185 {
1186  m_icubHand = ConfigurationHelper::getString(params, prefix + "hand", "right");
1187 
1188  // Reading the fingers joints configuration. The configuration format is like this: joint1,joint2/joint5,joint6,joint3/joint4.
1189  // In this case we have three groups of joints: (joint1 and joint2), (joint5, joint6 and joint3) and (joint4). Joints in the
1190  // same group are controlled together. This parameter is compulsory, if it is not present, the program throws an exception.
1191  // The joint names are: Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others (case insensitive)
1192  const QString paramName = prefix + "jointsGroups";
1193  QString str = params.getValue(paramName);
1194  if (str.isEmpty()) {
1195  ConfigurationHelper::throwUserConfigError(paramName, "", "the parameter is compulsory but is not present in the configuration");
1196  }
1197 
1198  const QStringList groups = str.split("/", QString::SkipEmptyParts);
1199  if (groups.size() == 0) {
1200  ConfigurationHelper::throwUserConfigError(paramName, str, "invalid format (no joint group)");
1201  }
1202 
1203  // Resizing the vector of joint groups
1204  m_jointsGroups.resize(groups.size());
1205  for (int i = 0; i < m_jointsGroups.size(); i++) {
1206  // Splitting again
1207  const QStringList joints = groups[i].split(",", QString::SkipEmptyParts);
1208  if (joints.size() == 0) {
1209  ConfigurationHelper::throwUserConfigError(paramName, str, "a joint group has no components");
1210  }
1211 
1212  m_jointsGroups[i].resize(joints.size());
1213  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1214  // Now converting joint name into joint index. First converting to uppercase
1215  const QString jointName = joints[j].toUpper().trimmed();
1216  if (jointName == "APERTURE") {
1217  m_jointsGroups[i][j] = 7;
1218  } else if (jointName == "THUMB1") {
1219  m_jointsGroups[i][j] = 8;
1220  } else if (jointName == "THUMB2") {
1221  m_jointsGroups[i][j] = 9;
1222  } else if (jointName == "THUMB3") {
1223  m_jointsGroups[i][j] = 10;
1224  } else if (jointName == "INDEX1") {
1225  m_jointsGroups[i][j] = 11;
1226  } else if (jointName == "INDEX2") {
1227  m_jointsGroups[i][j] = 12;
1228  } else if (jointName == "MIDDLE1") {
1229  m_jointsGroups[i][j] = 13;
1230  } else if (jointName == "MIDDLE2") {
1231  m_jointsGroups[i][j] = 14;
1232  } else if (jointName == "OTHERS") {
1233  m_jointsGroups[i][j] = 15;
1234  } else {
1235  ConfigurationHelper::throwUserConfigError(paramName, str, "unknown joint name (" + jointName + ")");
1236  }
1237  }
1238  }
1239 
1240  // Now reading the parameter with the maximum joint closure
1241  m_maxClosure = ConfigurationHelper::getDouble(params, prefix + "maxClosure", 1.0);
1242 
1243  const double k = ConfigurationHelper::getDouble(params, prefix + "k", 0.3);
1244  const double maxvel = ConfigurationHelper::getDouble(params, prefix + "maxvel", 20.0);
1245  m_pcontrol.setControlParameters(k, maxvel);
1246 
1247  // Declaring the resources that are needed here
1249 }
1250 
1252 {
1253 }
1254 
1256 {
1257  iCubMotor::save( params, prefix );
1258  params.startObjectParameters(prefix, "ConfigurableHandPosToVelMotor", this);
1259  params.createParameter(prefix, "hand", m_icubHand);
1260 
1261  QString strJointsGroup;
1262  for (int g = 0; g < m_jointsGroups.size(); g++) {
1263  for (int j = 0; j < m_jointsGroups[g].size(); j++) {
1264  if (m_jointsGroups[g][j] == 7) {
1265  strJointsGroup += "Aperture";
1266  } else if (m_jointsGroups[g][j] == 8) {
1267  strJointsGroup += "Thumb1";
1268  } else if (m_jointsGroups[g][j] == 9) {
1269  strJointsGroup += "Thumb2";
1270  } else if (m_jointsGroups[g][j] == 10) {
1271  strJointsGroup += "Thumb3";
1272  } else if (m_jointsGroups[g][j] == 11) {
1273  strJointsGroup += "Index1";
1274  } else if (m_jointsGroups[g][j] == 12) {
1275  strJointsGroup += "Index2";
1276  } else if (m_jointsGroups[g][j] == 13) {
1277  strJointsGroup += "Middle1";
1278  } else if (m_jointsGroups[g][j] == 14) {
1279  strJointsGroup += "Middle2";
1280  } else if (m_jointsGroups[g][j] == 15) {
1281  strJointsGroup += "Others";
1282  } else {
1283  strJointsGroup += "ERROR";
1284  }
1285 
1286  if (j != (m_jointsGroups[g].size() - 1)) {
1287  strJointsGroup += ", ";
1288  }
1289  }
1290  if (g != (m_jointsGroups.size() - 1)) {
1291  strJointsGroup += " / ";
1292  }
1293  }
1294  params.createParameter(prefix, "jointsGroups", strJointsGroup);
1295 
1296  params.createParameter(prefix, "maxClosure", QString::number(m_maxClosure));
1297  params.createParameter(prefix, "k", QString::number(m_pcontrol.getK()));
1298  params.createParameter(prefix, "maxvel", QString::number(m_pcontrol.getMaxVelocity()));
1299 }
1300 
1302 {
1303  iCubMotor::describe( type );
1304  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)");
1305  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\"");
1306  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)");
1307  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");
1308  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");
1309  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");
1310 }
1311 
1313 {
1314  // Checking all resources we need exist
1316 
1317  // Acquiring the lock to get resources
1318  ResourcesLocker locker(this);
1319 
1320  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1321  evonetIt->setCurrentBlock(name());
1322 
1323  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1324  // The network output value is multiplied by m_maxClosure so that the maximum closure of fingers can be
1325  // constrained
1326  const double out = m_maxClosure * evonetIt->getOutput();
1327 
1328  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1329  double actual;
1330  double min, max;
1331 
1332  m_armController->getEncoder(m_jointsGroups[i][j], &actual);
1333  m_armController->getLimits(m_jointsGroups[i][j], &min, &max);
1334 
1335  const double desiredPosture = linearMap(out, 0.0, 1.0, min, max); // normalize the neuron output with respect to the joint range
1336 
1337  m_armController->velocityMove(m_jointsGroups[i][j], m_pcontrol.velocityForJoint(desiredPosture, actual));
1338  }
1339  }
1340 }
1341 
1343 {
1344  return m_jointsGroups.size();
1345 }
1346 
1347 void iCubConfigurableHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1348 {
1349  iCubMotor::resourceChanged(resourceName, changeType);
1350 
1351  if (changeType == Deleted) {
1352  return;
1353  }
1354 
1355  if (resourceName == icubResource) {
1356  iCubRobot* icub = getResource<iCubRobot>();
1357  if (m_icubHand == "left") {
1358  m_armController = icub->leftArmController();
1359  } else {
1360  m_armController = icub->rightArmController();
1361  }
1362  } else if (resourceName == neuronsIteratorResource) {
1363  QString label;
1364  if (m_icubHand == "left") {
1365  label = "L";
1366  } else {
1367  label = "R";
1368  }
1369 
1370  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1371  evonetIt->setCurrentBlock(name());
1372  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1373  evonetIt->setGraphicProperties(label + "F" + QString::number(i), 0.0, 1.0, Qt::blue);
1374  }
1375  } else {
1376  Logger::info("Unknown resource " + resourceName + " for " + name());
1377  }
1378 }
1379 
1381  iCubMotor(params, prefix),
1382  icub(NULL),
1383  icubMotors(NULL) {
1384  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
1385  is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
1386  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1387  // Declaring the resources that are needed here
1388  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1389 }
1390 
1392  /* nothing to do */
1393 }
1394 
1395 void iCubArmVelocityMotor::save( ConfigurationParameters& params, QString prefix ) {
1396  iCubMotor::save( params, prefix );
1397  params.startObjectParameters( prefix, "iCubArmVelocityMotor", this );
1398  params.createParameter( prefix, "arm", icubArm );
1399  params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
1400  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1401 }
1402 
1403 void iCubArmVelocityMotor::describe( QString type ) {
1404  iCubMotor::describe( type );
1405  Descriptor d = addTypeDescription( type, "Move the arm by velocity." );
1406  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
1407  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" );
1408  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1409 }
1410 
1412  // Checking all resources we need exist
1414 
1415  // Acquiring the lock to get resources
1416  ResourcesLocker locker( this );
1417 
1418  double vels[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
1419  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1420  evonetIt->setCurrentBlock( name() );
1421  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1422  vels[i] = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1423  }
1424  for( int i=0; i<7; i++ ) {
1425  icubMotors->velocityMove( i, vels[i] );
1426  }
1427 }
1428 
1430  return (is7Dof ? 7 : 4);
1431 }
1432 
1433 void iCubArmVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1434  iCubMotor::resourceChanged(resourceName, changeType);
1435 
1436  if (changeType == Deleted) {
1437  return;
1438  }
1439 
1440  if (resourceName == icubResource) {
1441  icub = getResource<iCubRobot>();
1442  if ( icubArm == "right" ) {
1443  startJointId = iCubRobot::right_shoulder_pitch;
1444  icubMotors = icub->rightArmController();
1445  } else {
1446  startJointId = iCubRobot::left_shoulder_pitch;
1447  icubMotors = icub->leftArmController();
1448  }
1449  } else if (resourceName == neuronsIteratorResource) {
1450  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1451  evonetIt->setCurrentBlock( name() );
1452  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1453  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1454  }
1455  } else {
1456  Logger::info("Unknown resource " + resourceName + " for " + name());
1457  }
1458 }
1459 
1461  iCubMotor(params, prefix),
1462  icub(NULL),
1463  icubMotors(NULL) {
1464  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
1465  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
1466  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
1467  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1468  // Declaring the resources that are needed here
1469  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1470 }
1471 
1473  /* nothing to do */
1474 }
1475 
1477  iCubMotor::save( params, prefix );
1478  params.startObjectParameters( prefix, "iCubTorsoPosToPostureMotor", this );
1479  if ( !torsoDofs[0] ) {
1480  params.createParameter( prefix, "disableYaw", "true" );
1481  }
1482  if ( !torsoDofs[1] ) {
1483  params.createParameter( prefix, "disableRoll", "true" );
1484  }
1485  if ( !torsoDofs[2] ) {
1486  params.createParameter( prefix, "disablePitch", "true" );
1487  }
1488  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1489 }
1490 
1491 void iCubTorsoVelocityMotor::describe( QString type ) {
1492  iCubMotor::describe( type );
1493  Descriptor d = addTypeDescription( type, "Move the torso by velocity." );
1494  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
1495  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
1496  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
1497  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1498 }
1499 
1501  // Checking all resources we need exist
1503 
1504  // Acquiring the lock to get resources
1505  ResourcesLocker locker( this );
1506 
1507  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1508  evonetIt->setCurrentBlock( name() );
1509  for( int i=0; i<3; i++ ) {
1510  double vel = 0.0;
1511  if ( torsoDofs[i] ) {
1512  vel = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1513  evonetIt->nextNeuron();
1514  }
1515  icubMotors->velocityMove( i, vel );
1516  }
1517 }
1518 
1520  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1521 }
1522 
1523 void iCubTorsoVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1524  iCubMotor::resourceChanged(resourceName, changeType);
1525 
1526  if (changeType == Deleted) {
1527  return;
1528  }
1529 
1530  if (resourceName == icubResource) {
1531  icub = getResource<iCubRobot>();
1532  icubMotors = icub->torsoController();
1533  } else if (resourceName == neuronsIteratorResource) {
1534  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1535  evonetIt->setCurrentBlock( name() );
1536  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1537  for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
1538  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1539  }
1540  } else {
1541  Logger::info("Unknown resource " + resourceName + " for " + name());
1542  }
1543 }
1544 
1545 } // end namespace farsa
1546 
1547 #endif