kheperasensors.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 "kheperasensors.h"
25 #include "configurationhelper.h"
26 #include "logger.h"
27 #include "graphicalwobject.h"
28 
29 namespace farsa {
30 
32  Sensor(params, prefix),
33  m_kheperaResource("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 KheperaSensor::save(ConfigurationParameters& params, QString prefix)
50 {
51  // Calling parent function
52  Sensor::save(params, prefix);
53 
54  // Saving parameters
55  params.startObjectParameters(prefix, "KheperaSensor", this);
56  params.createParameter(prefix, "khepera", m_kheperaResource);
57  params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
58 }
59 
60 void KheperaSensor::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 Khepera sensors");
67  d.describeString("khepera").def("robot").help("the name of the resource associated with the khepera 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 KheperaSensor::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  KheperaSensor(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  KheperaSensor::save(params, prefix);
100 
101  // Saving parameters
102  params.startObjectParameters(prefix, "KheperaProximityIRSensor", this);
103 }
104 
106 {
107  // Calling parent function
109 
110  // Describing our parameters
111  Descriptor d = addTypeDescription(type, "The infrared proximity sensors of the Khepera robot", "The infrared proximity sensors of the Khepera II 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 KheperaProximityIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
134 {
135  // Calling parent function
136  KheperaSensor::resourceChanged(resourceName, changeType);
137 
138  if (changeType == Deleted) {
139  return;
140  }
141 
142  if (resourceName == m_kheperaResource) {
143  m_robot = getResource<PhyKhepera>();
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 
158 }