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
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  m_proportionalController(ConfigurationHelper::getDouble(params, prefix + "k", 0.3f), ConfigurationHelper::getDouble(params, prefix + "maxVelocity", 20.0f))
774 {
775 }
776 
778 {
779  // Nothing to do here
780 }
781 
783 {
784  // Calling parent function
785  iCubMotor::save( params, prefix );
786 
787  // Saving our parameters
788  params.startObjectParameters(prefix, "iCubPosToVelMotor", this);
789  params.createParameter(prefix, "k", QString::number(getK()));
790  params.createParameter(prefix, "maxVelocity", QString::number(getMaxVelocity()));
791 }
792 
793 void iCubPosToVelMotor::describe(QString type)
794 {
795  // Calling parent function
796  iCubMotor::describe(type);
797 
798  // Describing our parameters
799  Descriptor d = addTypeDescription(type, "The base class for iCub motors that are controlled by target posture and move the robot with velocity commands");
800  d.describeReal("k").def(0.3f).limits(0.0f, +Infinity).help("The gain of the proportional controller", "This is the gain of the proportional controller converting position displacement (desired position minus current position) to a velocity to apply to the joints. The default is 0.3");
801  d.describeReal("maxVelocity").def(20.0f).limits(0.0f, +Infinity).help("The absolute value of the maximum velocity for joints", "This is the absolute value of the maximum velocity applied to a joint by the proportional controller (i.e. the velocity applied to joints are always in the range [+maxvel, -maxvel]). The default is 20");
802 }
803 
805  iCubPosToVelMotor(params, prefix),
806  icub(NULL),
807  icubMotors(NULL) {
808  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
809  // Declaring the resources that are needed here
811 }
812 
814  /*nothing to do */
815 }
816 
817 void iCubArmPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
818  iCubPosToVelMotor::save( params, prefix );
819  params.startObjectParameters( prefix, "iCubArmPosToVelMotor", this );
820  params.createParameter( prefix, "arm", icubArm );
821 }
822 
823 void iCubArmPosToVelMotor::describe( QString type ) {
825  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the right/left arm" );
826  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
827 }
828 
830  // Checking all resources we need exist
832 
833  // Acquiring the lock to get resources
834  ResourcesLocker locker( this );
835 
836  //to do:
837  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
838  evonetIt->setCurrentBlock( name() );
839  for( int i=0; i<7; i++ ) {
840  double actual;
841  double min,max;
842  double desiredPosture;
843  icubMotors->getEncoder(i,&actual);
844  icubMotors->getLimits(i,&min,&max);
845  double a=evonetIt->getOutput();
846  desiredPosture=linearMap(a,0.0,1.0,min, max);
847  evonetIt->nextNeuron();
848  icubMotors->velocityMove(i,proportionalController().velocityForJoint(desiredPosture, actual));
849  }
850 }
851 
853  return 7;
854 }
855 
856 void iCubArmPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
857  iCubPosToVelMotor::resourceChanged(resourceName, changeType);
858 
859  if (changeType == Deleted) {
860  return;
861  }
862 
863  if (resourceName == icubResource) {
864  icub = getResource<iCubRobot>();
865  if ( icubArm == "right" ) {
867  } else {
869  }
870  } else if (resourceName == neuronsIteratorResource) {
871  QString label;
872  if ( icubArm == "right" ) {
873  label="R";
874  } else {
875  label="L";
876  }
877 
878  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
879  evonetIt->setCurrentBlock( name() );
880  for( int i=0; i<7; i++ ) {
881  evonetIt->setGraphicProperties(label+ QString("A")+QString::number(i), 0.0, 1.0, Qt::red ); //ex Ap2V
882 
883  evonetIt->nextNeuron();
884  }
885  } else {
886  Logger::info("Unknown resource " + resourceName + " for " + name());
887  }
888 }
889 
891  iCubPosToVelMotor(params, prefix),
892  icub(NULL),
893  icubMotors(NULL) {
894  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
895  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
896  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
897  // Declaring the resources that are needed here
899 }
900 
902  /*nothing to do */
903 }
904 
905 void iCubTorsoPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
906  iCubPosToVelMotor::save( params, prefix );
907  params.startObjectParameters( prefix, "iCubTorsoPosToVelMotor", this );
908  if ( !torsoDofs[0] ) {
909  params.createParameter( prefix, "disableYaw", "true" );
910  }
911  if ( !torsoDofs[1] ) {
912  params.createParameter( prefix, "disableRoll", "true" );
913  }
914  if ( !torsoDofs[2] ) {
915  params.createParameter( prefix, "disablePitch", "true" );
916  }
917 }
918 
919 void iCubTorsoPosToVelMotor::describe( QString type ) {
921  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the torso" );
922  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
923  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
924  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
925 }
926 
928  // Checking all resources we need exist
930 
931  // Acquiring the lock to get resources
932  ResourcesLocker locker( this );
933 
934  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
935  evonetIt->setCurrentBlock( name() );
936  for( int i=0; i<3; i++ ) {
937  double vel = 0.0;
938  if ( torsoDofs[i] ) {
939  double actual;
940  double min,max;
941  double desiredPosture;
942  icubMotors->getEncoder(i,&actual);
943  icubMotors->getLimits(i,&min,&max);
944  double a=evonetIt->getOutput();
945  desiredPosture=linearMap(a,0.0,1.0,min, max);
946  vel = proportionalController().velocityForJoint(desiredPosture, actual);
947  evonetIt->nextNeuron();
948  }
949  icubMotors->velocityMove( i, vel );
950  }
951 }
952 
954  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
955 }
956 
957 void iCubTorsoPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
958  iCubPosToVelMotor::resourceChanged(resourceName, changeType);
959 
960  if (changeType == Deleted) {
961  return;
962  }
963 
964  if (resourceName == icubResource) {
965  icub = getResource<iCubRobot>();
967  } else if (resourceName == neuronsIteratorResource) {
968  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
969  evonetIt->setCurrentBlock( name() );
970  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
971  for( int i=0; i<numDofs; i++ ) {
972  evonetIt->setGraphicProperties( QString("T")+QString::number(i), 0.0, 1.0, Qt::red );
973  evonetIt->nextNeuron();
974  }
975  } else {
976  Logger::info("Unknown resource " + resourceName + " for " + name());
977  }
978 }
979 
981  iCubPosToVelMotor(params, prefix),
982  icub(NULL),
983  icubMotors(NULL) {
984  headDofs[0] =! ConfigurationHelper::getBool(params, prefix+"disableNeckPitch",false);
985  headDofs[1] =! ConfigurationHelper::getBool(params, prefix+"disableNeckRoll",false);
986  headDofs[2] =! ConfigurationHelper::getBool(params, prefix+"disableNeckYaw",false);
987  headDofs[3] =! ConfigurationHelper::getBool(params, prefix+"disableEyesTilt",false);
988  headDofs[4] =! ConfigurationHelper::getBool(params, prefix+"disableEyesVersion",false);
989  headDofs[5] =! ConfigurationHelper::getBool(params, prefix+"disableEyesVergence",false);
990 
991  // Declaring the resources that are needed here
993 }
994 
996  /*nothing to do */
997 }
998 
999 void iCubHeadPosToVelMotor::save( ConfigurationParameters& params, QString prefix ) {
1000  iCubPosToVelMotor::save( params, prefix );
1001  params.startObjectParameters( prefix, "iCubHeadPosToVelMotor", this );
1002  if (!headDofs[0])
1003  params.createParameter(prefix,"disableNeckPitch","true");
1004  if (!headDofs[1])
1005  params.createParameter(prefix,"disableNeckRoll","true");
1006  if (!headDofs[2])
1007  params.createParameter(prefix,"disableNeckYaw","true");
1008  if (!headDofs[3])
1009  params.createParameter(prefix,"disableEyesTilt","true");
1010  if (!headDofs[4])
1011  params.createParameter(prefix,"disableEyesVersion","true");
1012  if (!headDofs[5])
1013  params.createParameter(prefix,"disableEyesVergence","true");
1014 }
1015 
1016 void iCubHeadPosToVelMotor::describe( QString type ) {
1018  Descriptor d = addTypeDescription( type, "Position mapped in velocity control of the Head" );
1019  d.describeBool("disableNeckPitch").def(false).help("Disables the #0 joint of the head: Neck Pitch");
1020  d.describeBool("disableNeckRoll").def(false).help("Disables the #1 joint of the head: Neck Roll");
1021  d.describeBool("disableNeckYaw").def(false).help("Disables the #2 joint of the head: Neck Yaw");
1022  d.describeBool("disableEyesTilt").def(false).help("Disables the #3 joint of the head: Eyes tilt");
1023  d.describeBool("disableEyesVersion").def(false).help("Disables the #4 joint of the head: Eyes version");
1024  d.describeBool("disableEyesVergence").def(false).help("Disables the #5 joint of the head: Eyes vergence");
1025 }
1026 
1028  // Checking all resources we need exist
1030 
1031  // Acquiring the lock to get resources
1032  ResourcesLocker locker( this );
1033 
1034  //to do:
1035  int id=0;
1036  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1037  evonetIt->setCurrentBlock( name() );
1038 
1039  for(int i=0;i<6;i++)
1040  {
1041  if(headDofs[i])
1042  {
1043  double actual;
1044  double min,max;
1045  double desiredPosture;
1046  double a;
1047 
1048  icubMotors->getEncoder(i,&actual);
1049  icubMotors->getLimits(i,&min,&max);
1050  a=evonetIt->getOutput();
1051  evonetIt->nextNeuron();
1052  desiredPosture=linearMap(a,0.0,1.0,min, max);
1053 
1054  icubMotors->velocityMove(i, proportionalController().velocityForJoint(desiredPosture, actual));
1055  }else
1056  {
1057  icubMotors->velocityMove(i,0);
1058  }
1059  }
1060 
1061 
1062 }
1063 
1065  int s=0;
1066  for(int i=0;i<6;i++) s+=headDofs[i];
1067  return s;
1068 }
1069 
1070 void iCubHeadPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1071  iCubPosToVelMotor::resourceChanged(resourceName, changeType);
1072 
1073  if (changeType == Deleted) {
1074  return;
1075  }
1076 
1077  if (resourceName == icubResource) {
1078  icub = getResource<iCubRobot>();
1080  } else if (resourceName == neuronsIteratorResource) {
1081  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1082  evonetIt->setCurrentBlock( name() );
1083 
1084  for( int i=0; i<size(); i++ ) {
1085  evonetIt->setGraphicProperties(QString("N")+QString::number(i), 0.0, 1.0, Qt::red );//ex Hp2v
1086  evonetIt->nextNeuron();
1087  }
1088  } else {
1089  Logger::info("Unknown resource " + resourceName + " for " + name());
1090  }
1091 }
1092 
1094  iCubPosToVelMotor(params, prefix),
1095  m_icubHand("right"),
1096  m_jointsGroups(),
1097  m_maxClosure(1.0),
1098  m_armController(NULL),
1099  m_pcontrol()
1100 {
1101  m_icubHand = ConfigurationHelper::getString(params, prefix + "hand", "right");
1102 
1103  // Reading the fingers joints configuration. The configuration format is like this: joint1,joint2/joint5,joint6,joint3/joint4.
1104  // In this case we have three groups of joints: (joint1 and joint2), (joint5, joint6 and joint3) and (joint4). Joints in the
1105  // same group are controlled together. This parameter is compulsory, if it is not present, the program throws an exception.
1106  // The joint names are: Aperture, Thumb1, Thumb2, Thumb3, Index1, Index2, Middle1, Middle2, Others (case insensitive)
1107  const QString paramName = prefix + "jointsGroups";
1108  QString str = params.getValue(paramName);
1109  if (str.isEmpty()) {
1110  ConfigurationHelper::throwUserConfigError(paramName, "", "the parameter is compulsory but is not present in the configuration");
1111  }
1112 
1113  const QStringList groups = str.split("/", QString::SkipEmptyParts);
1114  if (groups.size() == 0) {
1115  ConfigurationHelper::throwUserConfigError(paramName, str, "invalid format (no joint group)");
1116  }
1117 
1118  // Resizing the vector of joint groups
1119  m_jointsGroups.resize(groups.size());
1120  for (int i = 0; i < m_jointsGroups.size(); i++) {
1121  // Splitting again
1122  const QStringList joints = groups[i].split(",", QString::SkipEmptyParts);
1123  if (joints.size() == 0) {
1124  ConfigurationHelper::throwUserConfigError(paramName, str, "a joint group has no components");
1125  }
1126 
1127  m_jointsGroups[i].resize(joints.size());
1128  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1129  // Now converting joint name into joint index. First converting to uppercase
1130  const QString jointName = joints[j].toUpper().trimmed();
1131  if (jointName == "APERTURE") {
1132  m_jointsGroups[i][j] = 7;
1133  } else if (jointName == "THUMB1") {
1134  m_jointsGroups[i][j] = 8;
1135  } else if (jointName == "THUMB2") {
1136  m_jointsGroups[i][j] = 9;
1137  } else if (jointName == "THUMB3") {
1138  m_jointsGroups[i][j] = 10;
1139  } else if (jointName == "INDEX1") {
1140  m_jointsGroups[i][j] = 11;
1141  } else if (jointName == "INDEX2") {
1142  m_jointsGroups[i][j] = 12;
1143  } else if (jointName == "MIDDLE1") {
1144  m_jointsGroups[i][j] = 13;
1145  } else if (jointName == "MIDDLE2") {
1146  m_jointsGroups[i][j] = 14;
1147  } else if (jointName == "OTHERS") {
1148  m_jointsGroups[i][j] = 15;
1149  } else {
1150  ConfigurationHelper::throwUserConfigError(paramName, str, "unknown joint name (" + jointName + ")");
1151  }
1152  }
1153  }
1154 
1155  // Now reading the parameter with the maximum joint closure
1156  m_maxClosure = ConfigurationHelper::getDouble(params, prefix + "maxClosure", 1.0);
1157 
1158  // Declaring the resources that are needed here
1160 }
1161 
1163 {
1164 }
1165 
1167 {
1168  iCubPosToVelMotor::save( params, prefix );
1169  params.startObjectParameters(prefix, "ConfigurableHandPosToVelMotor", this);
1170  params.createParameter(prefix, "hand", m_icubHand);
1171 
1172  QString strJointsGroup;
1173  for (int g = 0; g < m_jointsGroups.size(); g++) {
1174  for (int j = 0; j < m_jointsGroups[g].size(); j++) {
1175  if (m_jointsGroups[g][j] == 7) {
1176  strJointsGroup += "Aperture";
1177  } else if (m_jointsGroups[g][j] == 8) {
1178  strJointsGroup += "Thumb1";
1179  } else if (m_jointsGroups[g][j] == 9) {
1180  strJointsGroup += "Thumb2";
1181  } else if (m_jointsGroups[g][j] == 10) {
1182  strJointsGroup += "Thumb3";
1183  } else if (m_jointsGroups[g][j] == 11) {
1184  strJointsGroup += "Index1";
1185  } else if (m_jointsGroups[g][j] == 12) {
1186  strJointsGroup += "Index2";
1187  } else if (m_jointsGroups[g][j] == 13) {
1188  strJointsGroup += "Middle1";
1189  } else if (m_jointsGroups[g][j] == 14) {
1190  strJointsGroup += "Middle2";
1191  } else if (m_jointsGroups[g][j] == 15) {
1192  strJointsGroup += "Others";
1193  } else {
1194  strJointsGroup += "ERROR";
1195  }
1196 
1197  if (j != (m_jointsGroups[g].size() - 1)) {
1198  strJointsGroup += ", ";
1199  }
1200  }
1201  if (g != (m_jointsGroups.size() - 1)) {
1202  strJointsGroup += " / ";
1203  }
1204  }
1205  params.createParameter(prefix, "jointsGroups", strJointsGroup);
1206 
1207  params.createParameter(prefix, "maxClosure", QString::number(m_maxClosure));
1208 }
1209 
1211 {
1213  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)");
1214  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\"");
1215  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)");
1216  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");
1217 }
1218 
1220 {
1221  // Checking all resources we need exist
1223 
1224  // Acquiring the lock to get resources
1225  ResourcesLocker locker(this);
1226 
1227  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1228  evonetIt->setCurrentBlock(name());
1229 
1230  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1231  // The network output value is multiplied by m_maxClosure so that the maximum closure of fingers can be
1232  // constrained
1233  const double out = m_maxClosure * evonetIt->getOutput();
1234 
1235  for (int j = 0; j < m_jointsGroups[i].size(); j++) {
1236  double actual;
1237  double min, max;
1238 
1239  m_armController->getEncoder(m_jointsGroups[i][j], &actual);
1240  m_armController->getLimits(m_jointsGroups[i][j], &min, &max);
1241 
1242  const double desiredPosture = linearMap(out, 0.0, 1.0, min, max); // normalize the neuron output with respect to the joint range
1243 
1244  m_armController->velocityMove(m_jointsGroups[i][j], proportionalController().velocityForJoint(desiredPosture, actual));
1245  }
1246  }
1247 }
1248 
1250 {
1251  return m_jointsGroups.size();
1252 }
1253 
1254 void iCubConfigurableHandPosToVelMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1255 {
1256  iCubPosToVelMotor::resourceChanged(resourceName, changeType);
1257 
1258  if (changeType == Deleted) {
1259  return;
1260  }
1261 
1262  if (resourceName == icubResource) {
1263  iCubRobot* icub = getResource<iCubRobot>();
1264  if (m_icubHand == "left") {
1265  m_armController = icub->leftArmController();
1266  } else {
1267  m_armController = icub->rightArmController();
1268  }
1269  } else if (resourceName == neuronsIteratorResource) {
1270  QString label;
1271  if (m_icubHand == "left") {
1272  label = "L";
1273  } else {
1274  label = "R";
1275  }
1276 
1277  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1278  evonetIt->setCurrentBlock(name());
1279  for (int i = 0; i < m_jointsGroups.size(); i++, evonetIt->nextNeuron()) {
1280  evonetIt->setGraphicProperties(label + "F" + QString::number(i), 0.0, 1.0, Qt::blue);
1281  }
1282  } else {
1283  Logger::info("Unknown resource " + resourceName + " for " + name());
1284  }
1285 }
1286 
1288  iCubMotor(params, prefix),
1289  m_headNeckController(NULL)
1290 {
1291  // Declaring the resources that are needed here
1293 }
1294 
1296 {
1297 }
1298 
1300 {
1301  iCubMotor::save(params, prefix);
1302  params.startObjectParameters(prefix, "iCubHeadVelocityMotor", this);
1303 }
1304 
1306 {
1307  iCubMotor::describe(type);
1308  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)");
1309 }
1310 
1312 {
1313  // Checking all resources we need exist
1315 
1316  // Acquiring the lock to get resources
1317  ResourcesLocker locker( this );
1318 
1319  NeuronsIterator* evonetIt = getResource<NeuronsIterator>(neuronsIteratorResource);
1320  evonetIt->setCurrentBlock(name());
1321 
1322  const double pan = evonetIt->getOutput();
1323  evonetIt->nextNeuron();
1324  const double tilt = evonetIt->getOutput();
1325  evonetIt->nextNeuron(); // Removed to avoid a warning from NeuronsIterator //remotion works only for the last sensor
1326  const double maxVelocity = 50.0;
1327 
1328  // Computing the velocity (in degrees/sec)
1329  double panVelocity = (pan - 0.5) * 2.0 * maxVelocity;
1330  double tiltVelocity = (tilt - 0.5) * 2.0 * maxVelocity;
1331 
1332  // Now moving the head
1333  m_headNeckController->velocityMove(2, panVelocity);
1334  m_headNeckController->velocityMove(0, tiltVelocity);
1335 }
1336 
1338 {
1339  return 2;
1340 }
1341 
1342 void iCubHeadVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
1343 {
1344  iCubMotor::resourceChanged(resourceName, changeType);
1345 
1346  if (changeType == Deleted) {
1347  return;
1348  }
1349 
1350  if (resourceName == icubResource) {
1351  m_headNeckController = getResource<iCubRobot>()->headNeckController();
1352  } else if (resourceName == neuronsIteratorResource) {
1353  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1354  evonetIt->setCurrentBlock(name());
1355  evonetIt->setGraphicProperties(QString("N0"), 0.0, 1.0, Qt::blue); //ex hp
1356  evonetIt->nextNeuron();
1357  evonetIt->setGraphicProperties(QString("N1"), 0.0, 1.0, Qt::blue); //ex ht
1358  } else {
1359  Logger::info("Unknown resource " + resourceName + " for " + name());
1360  }
1361 }
1362 
1364  iCubMotor(params, prefix),
1365  icub(NULL),
1366  icubMotors(NULL) {
1367  icubArm = ConfigurationHelper::getString( params, prefix+"arm", "right" );
1368  is7Dof = ConfigurationHelper::getBool( params, prefix+"full", true );
1369  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1370  // Declaring the resources that are needed here
1371  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1372 }
1373 
1375  /* nothing to do */
1376 }
1377 
1378 void iCubArmVelocityMotor::save( ConfigurationParameters& params, QString prefix ) {
1379  iCubMotor::save( params, prefix );
1380  params.startObjectParameters( prefix, "iCubArmVelocityMotor", this );
1381  params.createParameter( prefix, "arm", icubArm );
1382  params.createParameter( prefix, "full", (is7Dof ? "true" : "false" ) );
1383  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1384 }
1385 
1386 void iCubArmVelocityMotor::describe( QString type ) {
1387  iCubMotor::describe( type );
1388  Descriptor d = addTypeDescription( type, "Move the arm by velocity." );
1389  d.describeEnum( "arm" ).def( "right" ).values( QStringList()<<"right"<<"left" ).props( IsMandatory ).help( "The arm to move" );
1390  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" );
1391  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1392 }
1393 
1395  // Checking all resources we need exist
1397 
1398  // Acquiring the lock to get resources
1399  ResourcesLocker locker( this );
1400 
1401  double vels[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
1402  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1403  evonetIt->setCurrentBlock( name() );
1404  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1405  vels[i] = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1406  }
1407  for( int i=0; i<7; i++ ) {
1408  icubMotors->velocityMove( i, vels[i] );
1409  }
1410 }
1411 
1413  return (is7Dof ? 7 : 4);
1414 }
1415 
1416 void iCubArmVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1417  iCubMotor::resourceChanged(resourceName, changeType);
1418 
1419  if (changeType == Deleted) {
1420  return;
1421  }
1422 
1423  if (resourceName == icubResource) {
1424  icub = getResource<iCubRobot>();
1425  if ( icubArm == "right" ) {
1426  startJointId = iCubRobot::right_shoulder_pitch;
1427  icubMotors = icub->rightArmController();
1428  } else {
1429  startJointId = iCubRobot::left_shoulder_pitch;
1430  icubMotors = icub->leftArmController();
1431  }
1432  } else if (resourceName == neuronsIteratorResource) {
1433  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1434  evonetIt->setCurrentBlock( name() );
1435  for( int i=0; i<(is7Dof ? 7 : 4); i++, evonetIt->nextNeuron() ) {
1436  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1437  }
1438  } else {
1439  Logger::info("Unknown resource " + resourceName + " for " + name());
1440  }
1441 }
1442 
1444  iCubMotor(params, prefix),
1445  icub(NULL),
1446  icubMotors(NULL) {
1447  torsoDofs[0] = ! ConfigurationHelper::getBool( params, prefix+"disableYaw", false );
1448  torsoDofs[1] = ! ConfigurationHelper::getBool( params, prefix+"disableRoll", false );
1449  torsoDofs[2] = ! ConfigurationHelper::getBool( params, prefix+"disablePitch", false );
1450  maxVelocity = ConfigurationHelper::getDouble( params, prefix+"maxVelocity", 50.0 );
1451  // Declaring the resources that are needed here
1452  usableResources( QStringList() << icubResource << neuronsIteratorResource );
1453 }
1454 
1456  /* nothing to do */
1457 }
1458 
1460  iCubMotor::save( params, prefix );
1461  params.startObjectParameters( prefix, "iCubTorsoVelocityMotor", this );
1462  if ( !torsoDofs[0] ) {
1463  params.createParameter( prefix, "disableYaw", "true" );
1464  }
1465  if ( !torsoDofs[1] ) {
1466  params.createParameter( prefix, "disableRoll", "true" );
1467  }
1468  if ( !torsoDofs[2] ) {
1469  params.createParameter( prefix, "disablePitch", "true" );
1470  }
1471  params.createParameter( prefix, "maxVelocity", QString::number(maxVelocity) );
1472 }
1473 
1474 void iCubTorsoVelocityMotor::describe( QString type ) {
1475  iCubMotor::describe( type );
1476  Descriptor d = addTypeDescription( type, "Move the torso by velocity." );
1477  d.describeBool( "disableYaw" ).def(false).help( "Disable the movement of the first joint of the Torso corresponding to the yaw rotation" );
1478  d.describeBool( "disableRoll" ).def(false).help( "Disable the movement of the second joint of the Torso corresponding to the roll rotation" );
1479  d.describeBool( "disablePitch" ).def(false).help( "Disable the movement of the third joint of the Torso corresponding to the pitch rotation" );
1480  d.describeReal( "maxVelocity").def(50.0).limits(0,100).help( "The maximum velocity allowed." );
1481 }
1482 
1484  // Checking all resources we need exist
1486 
1487  // Acquiring the lock to get resources
1488  ResourcesLocker locker( this );
1489 
1490  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
1491  evonetIt->setCurrentBlock( name() );
1492  for( int i=0; i<3; i++ ) {
1493  double vel = 0.0;
1494  if ( torsoDofs[i] ) {
1495  vel = (evonetIt->getOutput()-0.5)*2.0*maxVelocity;
1496  evonetIt->nextNeuron();
1497  }
1498  icubMotors->velocityMove( i, vel );
1499  }
1500 }
1501 
1503  return torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1504 }
1505 
1506 void iCubTorsoVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
1507  iCubMotor::resourceChanged(resourceName, changeType);
1508 
1509  if (changeType == Deleted) {
1510  return;
1511  }
1512 
1513  if (resourceName == icubResource) {
1514  icub = getResource<iCubRobot>();
1515  icubMotors = icub->torsoController();
1516  } else if (resourceName == neuronsIteratorResource) {
1517  NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
1518  evonetIt->setCurrentBlock( name() );
1519  int numDofs = torsoDofs[0]+torsoDofs[1]+torsoDofs[2];
1520  for( int i=0; i<numDofs; i++, evonetIt->nextNeuron() ) {
1521  evonetIt->setGraphicProperties( QString("m")+QString::number(i), 0.0, 1.0, Qt::red );
1522  }
1523  } else {
1524  Logger::info("Unknown resource " + resourceName + " for " + name());
1525  }
1526 }
1527 
1528 } // end namespace farsa
1529 
1530 #endif