motors.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 #include "motors.h"
24 #include "configurationhelper.h"
25 #include "randomgenerator.h"
26 #include "motorcontrollers.h"
27 #include "logger.h"
28 #include <QStringList>
29 #include <QMap>
30 
31 namespace farsa {
32 
33 //implementing fake motors
34 FakeMotor::FakeMotor( ConfigurationParameters& params, QString prefix ) :
35  Motor(params, prefix)
36  {
37  nFakeMotors=0;
38  nFakeMotors = ConfigurationHelper::getInt( params, prefix+"nMotors", 0 );
39  // Declaring the resources that are needed here
40  usableResources( QStringList() << "neuronsIterator" );
41 }
42 
44  /* nothing to do */
45 }
46 
47 void FakeMotor::save( ConfigurationParameters& params, QString prefix ) {
48  Motor::save( params, prefix );
49  params.startObjectParameters( prefix, "FakeMotor", this );
50  params.createParameter( prefix, "nMotors", QString::number(nFakeMotors) );
51 }
52 
53 void FakeMotor::describe( QString type ) {
54  Motor::describe( type );
55  Descriptor d = addTypeDescription( type, "Fake motors: useful to add additional outputs (not linked with real motors) to the neural controller" );
56  d.describeInt( "nFakeMotors" ).def(0).limits(0,100).props( IsMandatory ).help( "Numbers of additional motors not linked with any actual motors (i.e. fake). Useful to add additional outputs" );
57 }
58 
60  // Checking all resources we need exist
62 }
63 
65  return nFakeMotors;
66 }
67 
68 void FakeMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
69  if (changeType == Deleted) {
71  return;
72  }
73 
74  if (resourceName == "neuronsIterator") {
75  NeuronsIterator* evonetIt = getResource<NeuronsIterator>( "neuronsIterator" );
76  evonetIt->setCurrentBlock( name() );
77  for( int i=0; i<nFakeMotors; i++, evonetIt->nextNeuron() ) {
78  evonetIt->setGraphicProperties( QString("Fk")+QString::number(i), 0.0, 1.0, Qt::red );
79  }
80  } else {
81  Logger::info("Unknown resource " + resourceName + " for " + name());
82  }
83 }
84 
85 ProportionalController::ProportionalController(){
86 
87  setControlParameters(0.3,20);
88 }
89 ProportionalController::~ProportionalController(){
90  /*so far, nothing to do */
91 }
92 
93 void ProportionalController::setControlParameters(double k, double maxvel){
94  if (k >= 0.0f) {
95  m_k = k;
96  }
97  if (maxvel >= 0.0f) {
98  m_maxvel = maxvel;
99  }
100 }
101 
102 double ProportionalController::getK() const {
103  return m_k;
104 }
105 
106 void ProportionalController::setK(double k){
107  if (k >= 0.0f)
108  m_k = k;
109 }
110 double ProportionalController::getMaxVelocity(){
111  return m_maxvel;
112 }
113 void ProportionalController::setMaxVelocity(double maxVel){
114  if (maxVel >= 0.0f)
115  m_maxvel = maxVel;
116 }
117 
118 
119 double ProportionalController::velocityForJoint(double desired, double current){
120  double vel = m_k * (desired - current);
121 
122  if (vel > +m_maxvel) {
123  vel = +m_maxvel;
124  } else if (vel < -m_maxvel) {
125  vel = -m_maxvel;
126  }
127 
128  return vel;
129 }
130 
131 } // end namespace farsa
132