marxbotsensors.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 "marxbotsensors.h"
25 #include "configurationhelper.h"
26 #include "logger.h"
27 #include "graphicalwobject.h"
28 
29 namespace farsa {
30 
32  Sensor(params, prefix),
33  m_marxbotResource("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 MarXbotSensor::save(ConfigurationParameters& params, QString prefix)
50 {
51  // Calling parent function
52  Sensor::save(params, prefix);
53 
54  // Saving parameters
55  params.startObjectParameters(prefix, "MarXbotSensor", this);
56  params.createParameter(prefix, "marxbot", m_marxbotResource);
57  params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
58 }
59 
60 void MarXbotSensor::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 MarXbot sensors");
67  d.describeString("marxbot").def("robot").help("the name of the resource associated with the MarXbot 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 MarXbotSensor::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  MarXbotSensor(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  MarXbotSensor::save(params, prefix);
100 
101  // Saving parameters
102  params.startObjectParameters(prefix, "MarXbotProximityIRSensor", this);
103 }
104 
106 {
107  // Calling parent function
109 
110  // Describing our parameters
111  Descriptor d = addTypeDescription(type, "The infrared proximity sensors of the MarXbot robot", "The infrared proximity sensors of the MarXbot 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 24;
131 }
132 
133 void MarXbotProximityIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
134 {
135  // Calling parent function
136  MarXbotSensor::resourceChanged(resourceName, changeType);
137 
138  if (changeType == Deleted) {
139  return;
140  }
141 
142  if (resourceName == m_marxbotResource) {
143  m_robot = getResource<PhyMarXbot>();
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  MarXbotSensor(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  MarXbotSensor::save(params, prefix);
174 
175  // Saving parameters
176  params.startObjectParameters(prefix, "MarXbotGroundBottomIRSensor", this);
177 }
178 
180 {
181  // Calling parent function
183 
184  // Describing our parameters
185  Descriptor d = addTypeDescription(type, "The infrared ground bottom sensors of the MarXbot robot", "The infrared ground bottom sensors of the MarXbot robot. These are the four ground sensors below the robot battery pack.");
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->groundBottomIRSensorController()->activation(i));
199  }
200 }
201 
203 {
204  return 4;
205 }
206 
207 void MarXbotGroundBottomIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
208 {
209  // Calling parent function
210  MarXbotSensor::resourceChanged(resourceName, changeType);
211 
212  if (changeType == Deleted) {
213  return;
214  }
215 
216  if (resourceName == m_marxbotResource) {
217  m_robot = getResource<PhyMarXbot>();
218 
219  // Eanbling sensors
220  m_robot->groundBottomIRSensorController()->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  MarXbotSensor(params, prefix),
234  m_robot(NULL),
235  m_neuronsIterator(NULL)
236 {
237 }
238 
240 {
241  // Nothing to do here
242 }
243 
245 {
246  // Calling parent function
247  MarXbotSensor::save(params, prefix);
248 
249  // Saving parameters
250  params.startObjectParameters(prefix, "MarXbotGroundAroundIRSensor", this);
251 }
252 
254 {
255  // Calling parent function
257 
258  // Describing our parameters
259  Descriptor d = addTypeDescription(type, "The infrared ground around sensors of the MarXbot robot", "The infrared ground around sensors of the MarXbot robot. These are the eight ground sensors below the base of the robot (just above the wheels).");
260 }
261 
263 {
264  // Checking all resources we need exist
266 
267  // Acquiring the lock to get resources
268  ResourcesLocker locker( this );
269 
270  m_neuronsIterator->setCurrentBlock(name());
271  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
272  m_neuronsIterator->setInput(m_robot->groundAroundIRSensorController()->activation(i));
273  }
274 }
275 
277 {
278  return 8;
279 }
280 
281 void MarXbotGroundAroundIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
282 {
283  // Calling parent function
284  MarXbotSensor::resourceChanged(resourceName, changeType);
285 
286  if (changeType == Deleted) {
287  return;
288  }
289 
290  if (resourceName == m_marxbotResource) {
291  m_robot = getResource<PhyMarXbot>();
292 
293  // Eanbling sensors
294  m_robot->groundAroundIRSensorController()->setEnabled(true);
295  } else if (resourceName == m_neuronsIteratorResource) {
296  m_neuronsIterator = getResource<NeuronsIterator>();
297  m_neuronsIterator->setCurrentBlock(name());
298  for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
299  m_neuronsIterator->setGraphicProperties("ga" + QString::number(i), 0.0, 1.0, Qt::red);
300  }
301  } else {
302  Logger::info("Unknown resource " + resourceName + " for " + name());
303  }
304 }
305 
307  MarXbotSensor(params, prefix),
308  m_robot(NULL),
309  m_neuronsIterator(NULL),
310  m_numReceptors(ConfigurationHelper::getInt(params, prefix + "numReceptors", 8)),
311  m_aperture(ConfigurationHelper::getDouble(params, prefix + "aperture", 360.0f)),
312  m_camera(NULL),
313  m_drawCamera(ConfigurationHelper::getBool(params, prefix + "drawCamera", true))
314 {
315 }
316 
318 {
319  // Deleting the camera
320  delete m_camera;
321 }
322 
324 {
325  // Calling parent function
326  MarXbotSensor::save(params, prefix);
327 
328  // Saving parameters
329  params.startObjectParameters(prefix, "MarXbotLinearCameraSensor", this);
330  params.createParameter(prefix, "numReceptors", QString::number(m_numReceptors));
331  params.createParameter(prefix, "aperture", QString::number(m_aperture));
332  params.createParameter(prefix, "drawCamera", (m_drawCamera ? "true" : "false"));
333 }
334 
336 {
337  // Calling parent function
339 
340  // Describing our parameters
341  Descriptor d = addTypeDescription(type, "The linear camera sensor of the MarXbot robot", "This is a 360° linear camera");
342  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)");
343  d.describeReal("aperture").def(360.0f).limits(0.0f, 360.0f).help("The aperture of the camera in degrees", "The real MarXbot has an omnidirectional camera, so you can use here any value up to 360 degrees (default is 360)");
344  d.describeBool("drawCamera").def(true).help("Whether to draw the camera or not");
345 }
346 
348 {
349  // Checking all resources we need exist
351 
352  // Acquiring the lock to get resources
353  ResourcesLocker locker( this );
354 
355  // Updating the camera
356  m_camera->update();
357 
358  // Reading activations: first the red one, then the green one and finally the blue one
359  m_neuronsIterator->setCurrentBlock(name());
360  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
361  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).redF());
362  }
363  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
364  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).greenF());
365  }
366  for (int i = 0; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
367  m_neuronsIterator->setInput(m_camera->colorForReceptor(i).blueF());
368  }
369 }
370 
372 {
373  return m_numReceptors * 3;
374 }
375 
376 void MarXbotLinearCameraSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
377 {
378  // Calling parent function
379  MarXbotSensor::resourceChanged(resourceName, changeType);
380 
381  if (changeType == Deleted) {
382  // Deleting the camera if the robot was deleted
383  if (resourceName == m_marxbotResource) {
384  delete m_camera;
385  m_camera = NULL;
386  }
387 
388  return;
389  }
390 
391  if (resourceName == m_marxbotResource) {
392  m_robot = getResource<PhyMarXbot>();
393 
394  // Now we can also create the camera
395  wMatrix mtr = wMatrix::roll(-PI_GRECO / 2.0);
396 #ifdef __GNUC__
397  #warning QUI INVECE DI UNA COSTANTE, CALCOLARSI UNA POSIZIONE DALLE DIMENSIONI DEL ROBOT
398 #endif
399  mtr.w_pos.z = 0.13;
400  m_camera = new LinearCamera(m_robot, mtr, toRad(m_aperture), m_numReceptors, Qt::black);
401 
402  // Sharing resources with the camera
403  m_camera->shareResourcesWith(this);
404  m_camera->drawCamera(m_drawCamera);
405  } else if (resourceName == m_neuronsIteratorResource) {
406  m_neuronsIterator = getResource<NeuronsIterator>();
407  m_neuronsIterator->setCurrentBlock(name());
408  int i = 0;
409  for (; i < m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
410  m_neuronsIterator->setGraphicProperties("lr" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
411  }
412  for (; i < 2 * m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
413  m_neuronsIterator->setGraphicProperties("lg" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
414  }
415  for (; i < 3 * m_numReceptors; i++, m_neuronsIterator->nextNeuron()) {
416  m_neuronsIterator->setGraphicProperties("lb" + QString::number(i % m_numReceptors), 0.0, 1.0, Qt::red);
417  }
418  } else {
419  Logger::info("Unknown resource " + resourceName + " for " + name());
420  }
421 }
422 
423 #ifdef __GNUC__
424  #warning QUI VEDERE COME CODIFICARE IL SENSORE DI TRAZIONE
425 #endif
427  MarXbotSensor(params, prefix),
428  m_robot(NULL),
429  m_neuronsIterator(NULL)
430 {
431 }
432 
434 {
435  // Nothing to do here
436 }
437 
439 {
440  // Calling parent function
441  MarXbotSensor::save(params, prefix);
442 
443  // Saving parameters
444  params.startObjectParameters(prefix, "MarXbotTractionSensor", this);
445 }
446 
448 {
449  // Calling parent function
451 
452  // Describing our parameters
453  Descriptor d = addTypeDescription(type, "The traction sensors of the MarXbot robot", "The traction sensors of the MarXbot robot. It is placed between the base and the turret. Note that this sensor only works when the robot is in dynamic mode");
454 }
455 
457 {
458  // Checking all resources we need exist
460 
461  // Acquiring the lock to get resources
462  ResourcesLocker locker( this );
463 
464  m_neuronsIterator->setCurrentBlock(name());
465  const wVector& t = m_robot->tractionSensorController()->traction();
466  m_neuronsIterator->setInput(t.x);
467  m_neuronsIterator->nextNeuron();
468  m_neuronsIterator->setInput(t.y);
469  m_neuronsIterator->nextNeuron();
470  m_neuronsIterator->setInput(t.z);
471 }
472 
474 {
475  return 3;
476 }
477 
478 void MarXbotTractionSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
479 {
480  // Calling parent function
481  MarXbotSensor::resourceChanged(resourceName, changeType);
482 
483  if (changeType == Deleted) {
484  return;
485  }
486 
487  if (resourceName == m_marxbotResource) {
488  m_robot = getResource<PhyMarXbot>();
489 
490  // Eanbling sensors
491  m_robot->proximityIRSensorController()->setEnabled(true);
492  } else if (resourceName == m_neuronsIteratorResource) {
493  m_neuronsIterator = getResource<NeuronsIterator>();
494  m_neuronsIterator->setCurrentBlock(name());
495  m_neuronsIterator->setGraphicProperties("tx", -1.0, 1.0, Qt::red);
496  m_neuronsIterator->nextNeuron();
497  m_neuronsIterator->setGraphicProperties("ty", -1.0, 1.0, Qt::red);
498  m_neuronsIterator->nextNeuron();
499  m_neuronsIterator->setGraphicProperties("tz", -1.0, 1.0, Qt::red);
500  } else {
501  Logger::info("Unknown resource " + resourceName + " for " + name());
502  }
503 }
504 
505 }