kheperamotors.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 "kheperamotors.h"
24 #include "configurationhelper.h"
25 #include "randomgenerator.h"
26 #include "logger.h"
27 
28 namespace farsa {
29 
31  Motor(params, prefix),
32  m_kheperaResource("robot"),
33  m_neuronsIteratorResource("neuronsIterator")
34 {
35  // Reading parameters
38 
39  // Declaring the resources that are needed here
41 }
42 
44 {
45  // Nothing to do here
46 }
47 
48 void KheperaMotor::save(ConfigurationParameters& params, QString prefix)
49 {
50  // Calling parent function
51  Motor::save(params, prefix);
52 
53  // Saving parameters
54  params.startObjectParameters(prefix, "KheperaMotor", this);
55  params.createParameter(prefix, "khepera", m_kheperaResource);
56  params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
57 }
58 
59 void KheperaMotor::describe(QString type)
60 {
61  // Calling parent function
62  Motor::describe(type);
63 
64  // Describing our parameters
65  Descriptor d = addTypeDescription(type, "The base class for khepera motors");
66  d.describeString("khepera").def("robot").help("The name of the resource associated with the Khepera robot to use (default is \"robot\")");
67  d.describeString("neuronsIterator").def("neuronsIterator").help("The name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
68 }
69 
70 void KheperaMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
71 {
72  // Calling parent function
73  Motor::resourceChanged(resourceName, changeType);
74 
75  // Here we only check whether the resource has been deleted and reset the check flag, the
76  // actual work is done in subclasses
77  if (changeType == Deleted) {
79  return;
80  }
81 }
82 
84  KheperaMotor(params, prefix),
85  m_robot(NULL),
86  m_neuronsIterator(NULL)
87 {
88 }
89 
91 {
92  // Nothing to do here
93 }
94 
96 {
97  // Calling parent function
98  KheperaMotor::save(params, prefix);
99 
100  // Saving parameters
101  params.startObjectParameters(prefix, "KheperaWheelVelocityMotor", this);
102 }
103 
105 {
106  // Calling parent function
108 
109  // Describing our parameters
110  Descriptor d = addTypeDescription(type, "The motor controlling the velocity of the wheels of the khepera robot");
111 }
112 
114 {
115  // Checking all resources we need exist
117 
118  // Acquiring the lock to get resources
119  ResourcesLocker locker( this );
120 
121  // Getting limit velocities for wheels
122  double minSpeed1;
123  double minSpeed2;
124  double maxSpeed1;
125  double maxSpeed2;
126  m_robot->wheelsController()->getSpeedLimits(minSpeed1, minSpeed2, maxSpeed1, maxSpeed2);
127 
128  // Computing desired wheel velocities. When appying noise we don't check boundaries, so robots could go a bit faster than their maximum speed
129  m_neuronsIterator->setCurrentBlock(name());
130  const double v1 = (maxSpeed1 - minSpeed1) * applyNoise(m_neuronsIterator->getOutput(), 0.0, -1.0) + minSpeed1;
131  m_neuronsIterator->nextNeuron();
132  const double v2 = (maxSpeed2 - minSpeed2) * applyNoise(m_neuronsIterator->getOutput(), 0.0, -1.0) + minSpeed2;
133 
134  m_robot->wheelsController()->setSpeeds(v1, v2);
135 }
136 
138 {
139  return 2;
140 }
141 
142 void KheperaWheelVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
143 {
144  // Calling parent function
145  KheperaMotor::resourceChanged(resourceName, changeType);
146 
147  if (changeType == Deleted) {
148  return;
149  }
150 
151  if (resourceName == m_kheperaResource) {
152  m_robot = getResource<PhyKhepera>();
153  } else if (resourceName == m_neuronsIteratorResource) {
154  m_neuronsIterator = getResource<NeuronsIterator>();
155  m_neuronsIterator->setCurrentBlock(name());
156  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
157  m_neuronsIterator->setGraphicProperties("W" + QString::number(i), 0.0, 1.0, Qt::red);
158  }
159  } else {
160  Logger::info("Unknown resource " + resourceName + " for " + name());
161  }
162 }
163 
164 }
void usableResources(QStringList resources)
double applyNoise(double v, double minValue, double maxValue) const
Adds noise to the value.
virtual ~KheperaWheelVelocityMotor()
Destructor.
virtual void save(ConfigurationParameters &params, QString prefix)
Saves current parameters into the given ConfigurationParameters object.
virtual void update()
Performs the motor update. This also modifies the activation of input neurons.
static void describe(QString type)
Describes all the parameters for this sensor.
static QString getString(ConfigurationParameters &params, QString paramPath, QString def=QString())
void save(ConfigurationParameters &params, QString prefix)
Save the parameters into the ConfigurationParameters.
QString m_kheperaResource
The name of the resource associated with the khepera robot.
void setSpeeds(QVector< double > speeds)
virtual int size()
Returns the number of neurons required by this motor.
QString name()
Return the name of the Sensor.
void checkAllNeededResourcesExist()
Checks whether all resources we need are existing and throws an exception if they aren't...
virtual void save(ConfigurationParameters &params, QString prefix)
Saves the parameters of the sensor into the ConfigurationParameters object.
void resetNeededResourcesCheck()
Resets the check on needed resources so that the next call to checkAllNeededResourcesExist() will per...
virtual void resourceChanged(QString resourceName, ResourceChangeType changeType)
The function called when a resource used here is changed.
virtual double getOutput()=0
Get the output of the current neuron.
virtual bool nextNeuron()=0
Go to the next neuron of the current block.
virtual bool setCurrentBlock(QString blockName)=0
Set the current blocks of neurons to iterate.
KheperaWheelVelocityMotor(ConfigurationParameters &params, QString prefix)
Constructor.
QString m_neuronsIteratorResource
The name of th resource associated with the neural network iterator.
WheelMotorController * wheelsController()
virtual void resourceChanged(QString name, ResourceChangeType changeType)
void getSpeedLimits(QVector< double > &minSpeeds, QVector< double > &maxSpeeds) const
static void info(QString msg)
KheperaMotor(ConfigurationParameters &params, QString prefix)
Constructor.
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
static void describe(QString type)
Generates a description of this class and its parameters.
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
QString actualResourceNameForMultirobot(QString resourceName) const
Returns the actual resource name to use.
The base abstract class for the Motor hierarchy.
virtual void setGraphicProperties(QString label, double minValue, double maxValue, QColor color)=0
Set the graphic properties for the current neuron (in case it will be visualized on a GUI) ...
static void describe(QString type)
Describe all the parameter for configuring the Motor.
virtual ~KheperaMotor()
Destructor.
void createParameter(QString groupPath, QString parameter)
The base abstract class for khepera motors.
Definition: kheperamotors.h:52