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
129  m_neuronsIterator->setCurrentBlock(name());
130  const double v1 = (maxSpeed1 - minSpeed1) * m_neuronsIterator->getOutput() + minSpeed1;
131  m_neuronsIterator->nextNeuron();
132  const double v2 = (maxSpeed2 - minSpeed2) * m_neuronsIterator->getOutput() + 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 }