epuckmotors.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 "epuckmotors.h"
24 #include "configurationhelper.h"
25 #include "randomgenerator.h"
26 #include "logger.h"
27 
28 namespace farsa {
29 
31  Motor(params, prefix),
32  m_epuckResource("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 EpuckMotor::save(ConfigurationParameters& params, QString prefix)
49 {
50  // Calling parent function
51  Motor::save(params, prefix);
52 
53  // Saving parameters
54  params.startObjectParameters(prefix, "EpuckMotor", this);
55  params.createParameter(prefix, "epuck", m_epuckResource);
56  params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
57 }
58 
59 void EpuckMotor::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 e-puck motors");
66  d.describeString("epuck").def("robot").help("The name of the resource associated with the e-puck 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 EpuckMotor::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  EpuckMotor(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  EpuckMotor::save(params, prefix);
99 
100  // Saving parameters
101  params.startObjectParameters(prefix, "EpuckWheelVelocityMotor", this);
102 }
103 
105 {
106  // Calling parent function
107  EpuckMotor::describe(type);
108 
109  // Describing our parameters
110  Descriptor d = addTypeDescription(type, "The motor controlling the velocity of the wheels of the e-puck 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 EpuckWheelVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
143 {
144  // Calling parent function
145  EpuckMotor::resourceChanged(resourceName, changeType);
146 
147  if (changeType == Deleted) {
148  return;
149  }
150 
151  if (resourceName == m_epuckResource) {
152  m_robot = getResource<PhyEpuck>();
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 ~EpuckWheelVelocityMotor()
Destructor.
Definition: epuckmotors.cpp:90
virtual void update()
Performs the motor update. This also modifies the activation of input neurons.
virtual void resourceChanged(QString resourceName, ResourceChangeType changeType)
The function called when a resource used here is changed.
Definition: epuckmotors.cpp:70
static QString getString(ConfigurationParameters &params, QString paramPath, QString def=QString())
void save(ConfigurationParameters &params, QString prefix)
Save the parameters into the ConfigurationParameters.
virtual void save(ConfigurationParameters &params, QString prefix)
Saves current parameters into the given ConfigurationParameters object.
Definition: epuckmotors.cpp:95
static void describe(QString type)
Generates a description of this class and its parameters.
void setSpeeds(QVector< double > speeds)
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...
WheelMotorController * wheelsController()
void resetNeededResourcesCheck()
Resets the check on needed resources so that the next call to checkAllNeededResourcesExist() will per...
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.
virtual void resourceChanged(QString name, ResourceChangeType changeType)
static void describe(QString type)
Describes all the parameters for this sensor.
Definition: epuckmotors.cpp:59
void getSpeedLimits(QVector< double > &minSpeeds, QVector< double > &maxSpeeds) const
static void info(QString msg)
virtual int size()
Returns the number of neurons required by this motor.
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
QString actualResourceNameForMultirobot(QString resourceName) const
Returns the actual resource name to use.
EpuckMotor(ConfigurationParameters &params, QString prefix)
Constructor.
Definition: epuckmotors.cpp:30
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) ...
The base abstract class for e-puck motors.
Definition: epuckmotors.h:52
QString m_epuckResource
The name of the resource associated with the e-puck robot.
Definition: epuckmotors.h:104
static void describe(QString type)
Describe all the parameter for configuring the Motor.
QString m_neuronsIteratorResource
The name of th resource associated with the neural network iterator.
Definition: epuckmotors.h:110
virtual ~EpuckMotor()
Destructor.
Definition: epuckmotors.cpp:43
void createParameter(QString groupPath, QString parameter)
virtual void save(ConfigurationParameters &params, QString prefix)
Saves the parameters of the sensor into the ConfigurationParameters object.
Definition: epuckmotors.cpp:48
EpuckWheelVelocityMotor(ConfigurationParameters &params, QString prefix)
Constructor.
Definition: epuckmotors.cpp:83