epucksensors.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  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22  ********************************************************************************/
23 
24 #include "epucksensors.h"
25 #include "configurationhelper.h"
26 #include "logger.h"
27 #include "graphicalwobject.h"
28 
29 namespace farsa {
30 
32  Sensor(params, prefix),
33  m_epuckResource("robot"),
34  m_neuronsIteratorResource("neuronsIterator")
35 {
36  // Reading parameters
39 
40  // Declaring the resources that are needed here
42 }
43 
45 {
46  // Nothing to do here
47 }
48 
49 void EpuckSensor::save(ConfigurationParameters& params, QString prefix)
50 {
51  // Calling parent function
52  Sensor::save(params, prefix);
53 
54  // Saving parameters
55  params.startObjectParameters(prefix, "EpuckSensor", this);
56  params.createParameter(prefix, "epuck", m_epuckResource);
57  params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
58 }
59 
60 void EpuckSensor::describe(QString type)
61 {
62  // Calling parent function
63  Sensor::describe(type);
64 
65  // Describing our parameters
66  Descriptor d = addTypeDescription(type, "The base class for e-puck sensors");
67  d.describeString("epuck").def("robot").help("the name of the resource associated with the e-puck robot to use (default is \"robot\")");
68  d.describeString("neuronsIterator").def("neuronsIterator").help("the name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
69 }
70 
71 void EpuckSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
72 {
73  // Calling parent function
74  Sensor::resourceChanged(resourceName, changeType);
75 
76  // Here we only check whether the resource has been deleted and reset the check flag, the
77  // actual work is done in subclasses
78  if (changeType == Deleted) {
80  return;
81  }
82 }
83 
85  EpuckSensor(params, prefix),
86  m_robot(NULL),
87  m_neuronsIterator(NULL)
88 {
89 }
90 
92 {
93  // Nothing to do here
94 }
95 
97 {
98  // Calling parent function
99  EpuckSensor::save(params, prefix);
100 
101  // Saving parameters
102  params.startObjectParameters(prefix, "EpuckProximityIRSensor", this);
103 }
104 
106 {
107  // Calling parent function
108  EpuckSensor::describe(type);
109 
110  // Describing our parameters
111  Descriptor d = addTypeDescription(type, "The infrared proximity sensors of the e-puck robot", "The infrared proximity sensors of the e-puck robot. These are the very short range IR sensors all around the base");
112 }
113 
115 {
116  // Checking all resources we need exist
118 
119  // Acquiring the lock to get resources
120  ResourcesLocker locker( this );
121 
122  m_neuronsIterator->setCurrentBlock(name());
123  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
124  m_neuronsIterator->setInput(m_robot->proximityIRSensorController()->activation(i));
125  }
126 }
127 
129 {
130  return 8;
131 }
132 
133 void EpuckProximityIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
134 {
135  // Calling parent function
136  EpuckSensor::resourceChanged(resourceName, changeType);
137 
138  if (changeType == Deleted) {
139  return;
140  }
141 
142  if (resourceName == m_epuckResource) {
143  m_robot = getResource<PhyEpuck>();
144 
145  // Eanbling sensors
146  m_robot->proximityIRSensorController()->setEnabled(true);
147  } else if (resourceName == m_neuronsIteratorResource) {
148  m_neuronsIterator = getResource<NeuronsIterator>();
149  m_neuronsIterator->setCurrentBlock(name());
150  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
151  m_neuronsIterator->setGraphicProperties("ir" + QString::number(i), 0.0, 1.0, Qt::red);
152  }
153  } else {
154  Logger::info("Unknown resource " + resourceName + " for " + name());
155  }
156 }
157 
159  EpuckSensor(params, prefix),
160  m_robot(NULL),
161  m_neuronsIterator(NULL)
162 {
163 }
164 
166 {
167  // Nothing to do here
168 }
169 
171 {
172  // Calling parent function
173  EpuckSensor::save(params, prefix);
174 
175  // Saving parameters
176  params.startObjectParameters(prefix, "EpuckGroundIRSensor", this);
177 }
178 
180 {
181  // Calling parent function
182  EpuckSensor::describe(type);
183 
184  // Describing our parameters
185  Descriptor d = addTypeDescription(type, "The infrared ground sensors of the e-puck robot", "The infrared ground sensors of the e-puck robot. These are the three ground sensors in the frontal part of the robot.");
186 }
187 
189 {
190  // Checking all resources we need exist
192 
193  // Acquiring the lock to get resources
194  ResourcesLocker locker( this );
195 
196  m_neuronsIterator->setCurrentBlock(name());
197  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
198  m_neuronsIterator->setInput(m_robot->groundIRSensorController()->activation(i));
199  }
200 }
201 
203 {
204  return 3;
205 }
206 
207 void EpuckGroundIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
208 {
209  // Calling parent function
210  EpuckSensor::resourceChanged(resourceName, changeType);
211 
212  if (changeType == Deleted) {
213  return;
214  }
215 
216  if (resourceName == m_epuckResource) {
217  m_robot = getResource<PhyEpuck>();
218 
219  // Eanbling sensors
220  m_robot->groundIRSensorController()->setEnabled(true);
221  } else if (resourceName == m_neuronsIteratorResource) {
222  m_neuronsIterator = getResource<NeuronsIterator>();
223  m_neuronsIterator->setCurrentBlock(name());
224  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
225  m_neuronsIterator->setGraphicProperties("gb" + QString::number(i), 0.0, 1.0, Qt::red);
226  }
227  } else {
228  Logger::info("Unknown resource " + resourceName + " for " + name());
229  }
230 }
231 
233  EpuckSensor(params, prefix),
234  m_robot(NULL),
235  m_neuronsIterator(NULL),
236  m_numReceptors(ConfigurationHelper::getInt(params, prefix + "numReceptors", 8)),
237  m_aperture(ConfigurationHelper::getDouble(params, prefix + "aperture", 36.0f)),
238  m_camera(NULL),
239  m_drawCamera(ConfigurationHelper::getBool(params, prefix + "drawCamera", true))
240 {
241 }
242 
244 {
245  // Deleting the camera
246  delete m_camera;
247 }
248 
250 {
251  // Calling parent function
252  EpuckSensor::save(params, prefix);
253 
254  // Saving parameters
255  params.startObjectParameters(prefix, "EpuckLinearCameraSensor", this);
256  params.createParameter(prefix, "numReceptors", QString::number(m_numReceptors));
257  params.createParameter(prefix, "aperture", QString::number(m_aperture));
258  params.createParameter(prefix, "drawCamera", (m_drawCamera ? "true" : "false"));
259 }
260 
262 {
263  // Calling parent function
264  EpuckSensor::describe(type);
265 
266  // Describing our parameters
267  Descriptor d = addTypeDescription(type, "The linear camera sensor of the e-puck robot", "This is a linear camera with configurable aperture");
268  d.describeInt("numReceptors").def(8).limits(1, MaxInteger).help("The number of receptors of the sensor", "Each receptor returns three values, one for each of the three colors (red, green, blue). This means that the size returned by this sensor is 3 * numReceptors (default is 8)");
269  d.describeReal("aperture").def(360.0f).limits(0.0f, 360.0f).help("The aperture of the camera in degrees", "The real e-puck has a camera with an aperture of 36 degrees, but here you can use any value up to 360° (default is 36)");
270  d.describeBool("drawCamera").def(true).help("Whether to draw the camera or not");
271 }
272 
274 {
275  // Checking all resources we need exist
277 
278  // Acquiring the lock to get resources
279  ResourcesLocker locker( this );
280 
281  // Updating the camera
282  m_camera->update();
283 
284  // Reading activations: first the red one, then the green one and finally the blue one
285  m_neuronsIterator->setCurrentBlock(name());
286  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
287  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).redF());
288  }
289  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
290  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).greenF());
291  }
292  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
293  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).blueF());
294  }
295 }
296 
298 {
299  return m_numReceptors * 3;
300 }
301 
302 void EpuckLinearCameraSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
303 {
304  // Calling parent function
305  EpuckSensor::resourceChanged(resourceName, changeType);
306 
307  if (changeType == Deleted) {
308  // Deleting the camera if the robot was deleted
309  if (resourceName == m_epuckResource) {
310  delete m_camera;
311  m_camera = NULL;
312  }
313 
314  return;
315  }
316 
317  if (resourceName == m_epuckResource) {
318  m_robot = getResource<PhyEpuck>();
319 
320  // Now we can also create the camera
321  wMatrix mtr = wMatrix::roll(-PI_GRECO / 2.0);
322 #ifdef __GNUC__
323  #warning QUI INVECE DI UNA COSTANTE, CALCOLARSI UNA POSIZIONE DALLE DIMENSIONI DEL ROBOT
324 #endif
325  mtr.w_pos.z = 0.06;
326  m_camera = new LinearCamera(m_robot, mtr, toRad(m_aperture), m_numReceptors, Qt::black);
327 
328  // Sharing resources with the camera
329  m_camera->shareResourcesWith(this);
330  m_camera->drawCamera(m_drawCamera);
331  } else if (resourceName == m_neuronsIteratorResource) {
332  m_neuronsIterator = getResource<NeuronsIterator>();
333  m_neuronsIterator->setCurrentBlock(name());
334  int i = 0;
335  for (; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
336  m_neuronsIterator->setGraphicProperties("lr" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
337  }
338  for (; i < 2 * m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
339  m_neuronsIterator->setGraphicProperties("lg" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
340  }
341  for (; i < 3 * m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
342  m_neuronsIterator->setGraphicProperties("lb" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
343  }
344  } else {
345  Logger::info("Unknown resource " + resourceName + " for " + name());
346  }
347 }
348 
349 }