experiments/src/marxbotsensors.cpp

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00007  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #include "marxbotsensors.h"
00025 #include "configurationhelper.h"
00026 #include "logger.h"
00027 #include "graphicalwobject.h"
00028 
00029 namespace farsa {
00030 
00031 MarXbotSensor::MarXbotSensor(ConfigurationParameters& params, QString prefix) :
00032     Sensor(params, prefix),
00033     m_marxbotResource("robot"),
00034     m_neuronsIteratorResource("neuronsIterator")
00035 {
00036     // Reading parameters
00037     m_marxbotResource = ConfigurationHelper::getString(params, prefix + "marxbot", m_marxbotResource);
00038     m_neuronsIteratorResource = ConfigurationHelper::getString(params, prefix + "neuronsIterator", m_neuronsIteratorResource);
00039 
00040     // Declaring the resources that are needed here
00041     usableResources(QStringList() << m_marxbotResource << m_neuronsIteratorResource);
00042 }
00043 
00044 MarXbotSensor::~MarXbotSensor()
00045 {
00046     // Nothing to do here
00047 }
00048 
00049 void MarXbotSensor::save(ConfigurationParameters& params, QString prefix)
00050 {
00051     // Calling parent function
00052     Sensor::save(params, prefix);
00053 
00054     // Saving parameters
00055     params.startObjectParameters(prefix, "MarXbotSensor", this);
00056     params.createParameter(prefix, "marxbot", m_marxbotResource);
00057     params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
00058 }
00059 
00060 void MarXbotSensor::describe(QString type)
00061 {
00062     // Calling parent function
00063     Sensor::describe(type);
00064 
00065     // Describing our parameters
00066     Descriptor d = addTypeDescription(type, "The base class for MarXbot sensors");
00067     d.describeString("marxbot").def("robot").help("the name of the resource associated with the MarXbot robot to use (default is \"robot\")");
00068     d.describeString("neuronsIterator").def("neuronsIterator").help("the name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
00069 }
00070 
00071 void MarXbotSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00072 {
00073     // Calling parent function
00074     Sensor::resourceChanged(resourceName, changeType);
00075 
00076     // Here we only check whether the resource has been deleted and reset the check flag, the
00077     // actual work is done in subclasses
00078     if (changeType == Deleted) {
00079         resetNeededResourcesCheck();
00080         return;
00081     }
00082 }
00083 
00084 MarXbotProximityIRSensor::MarXbotProximityIRSensor(ConfigurationParameters& params, QString prefix) :
00085     MarXbotSensor(params, prefix),
00086     m_robot(NULL),
00087     m_neuronsIterator(NULL)
00088 {
00089 }
00090 
00091 MarXbotProximityIRSensor::~MarXbotProximityIRSensor()
00092 {
00093     // Nothing to do here
00094 }
00095 
00096 void MarXbotProximityIRSensor::save(ConfigurationParameters& params, QString prefix)
00097 {
00098     // Calling parent function
00099     MarXbotSensor::save(params, prefix);
00100 
00101     // Saving parameters
00102     params.startObjectParameters(prefix, "MarXbotProximityIRSensor", this);
00103 }
00104 
00105 void MarXbotProximityIRSensor::describe(QString type)
00106 {
00107     // Calling parent function
00108     MarXbotSensor::describe(type);
00109 
00110     // Describing our parameters
00111     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");
00112 }
00113 
00114 void MarXbotProximityIRSensor::update()
00115 {
00116     // Checking all resources we need exist
00117     checkAllNeededResourcesExist();
00118 
00119     // Acquiring the lock to get resources
00120     ResourcesLocker locker( this );
00121 
00122     m_neuronsIterator->setCurrentBlock(name());
00123     for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00124         m_neuronsIterator->setInput(m_robot->proximityIRSensorController()->activation(i));
00125     }
00126 }
00127 
00128 int MarXbotProximityIRSensor::size()
00129 {
00130     return 24;
00131 }
00132 
00133 void MarXbotProximityIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00134 {
00135     // Calling parent function
00136     MarXbotSensor::resourceChanged(resourceName, changeType);
00137 
00138     if (changeType == Deleted) {
00139         return;
00140     }
00141 
00142     if (resourceName == m_marxbotResource) {
00143         m_robot = getResource<PhyMarXbot>();
00144     } else if (resourceName == m_neuronsIteratorResource) {
00145         m_neuronsIterator = getResource<NeuronsIterator>();
00146         m_neuronsIterator->setCurrentBlock(name());
00147         for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00148             m_neuronsIterator->setGraphicProperties("ir" + QString::number(i), 0.0, 1.0, Qt::red);
00149         }
00150     } else {
00151         Logger::info("Unknown resource " + resourceName + " for " + name());
00152     }
00153 }
00154 
00155 MarXbotGroundBottomIRSensor::MarXbotGroundBottomIRSensor(ConfigurationParameters& params, QString prefix) :
00156     MarXbotSensor(params, prefix),
00157     m_robot(NULL),
00158     m_neuronsIterator(NULL)
00159 {
00160 }
00161 
00162 MarXbotGroundBottomIRSensor::~MarXbotGroundBottomIRSensor()
00163 {
00164     // Nothing to do here
00165 }
00166 
00167 void MarXbotGroundBottomIRSensor::save(ConfigurationParameters& params, QString prefix)
00168 {
00169     // Calling parent function
00170     MarXbotSensor::save(params, prefix);
00171 
00172     // Saving parameters
00173     params.startObjectParameters(prefix, "MarXbotGroundBottomIRSensor", this);
00174 }
00175 
00176 void MarXbotGroundBottomIRSensor::describe(QString type)
00177 {
00178     // Calling parent function
00179     MarXbotSensor::describe(type);
00180 
00181     // Describing our parameters
00182     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.");
00183 }
00184 
00185 void MarXbotGroundBottomIRSensor::update()
00186 {
00187     // Checking all resources we need exist
00188     checkAllNeededResourcesExist();
00189 
00190     // Acquiring the lock to get resources
00191     ResourcesLocker locker( this );
00192 
00193     m_neuronsIterator->setCurrentBlock(name());
00194     for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00195         m_neuronsIterator->setInput(m_robot->groundBottomIRSensorController()->activation(i));
00196     }
00197 }
00198 
00199 int MarXbotGroundBottomIRSensor::size()
00200 {
00201     return 4;
00202 }
00203 
00204 void MarXbotGroundBottomIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00205 {
00206     // Calling parent function
00207     MarXbotSensor::resourceChanged(resourceName, changeType);
00208 
00209     if (changeType == Deleted) {
00210         return;
00211     }
00212 
00213     if (resourceName == m_marxbotResource) {
00214         m_robot = getResource<PhyMarXbot>();
00215     } else if (resourceName == m_neuronsIteratorResource) {
00216         m_neuronsIterator = getResource<NeuronsIterator>();
00217         m_neuronsIterator->setCurrentBlock(name());
00218         for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00219             m_neuronsIterator->setGraphicProperties("gb" + QString::number(i), 0.0, 1.0, Qt::red);
00220         }
00221     } else {
00222         Logger::info("Unknown resource " + resourceName + " for " + name());
00223     }
00224 }
00225 
00226 MarXbotGroundAroundIRSensor::MarXbotGroundAroundIRSensor(ConfigurationParameters& params, QString prefix) :
00227     MarXbotSensor(params, prefix),
00228     m_robot(NULL),
00229     m_neuronsIterator(NULL)
00230 {
00231 }
00232 
00233 MarXbotGroundAroundIRSensor::~MarXbotGroundAroundIRSensor()
00234 {
00235     // Nothing to do here
00236 }
00237 
00238 void MarXbotGroundAroundIRSensor::save(ConfigurationParameters& params, QString prefix)
00239 {
00240     // Calling parent function
00241     MarXbotSensor::save(params, prefix);
00242 
00243     // Saving parameters
00244     params.startObjectParameters(prefix, "MarXbotGroundAroundIRSensor", this);
00245 }
00246 
00247 void MarXbotGroundAroundIRSensor::describe(QString type)
00248 {
00249     // Calling parent function
00250     MarXbotSensor::describe(type);
00251 
00252     // Describing our parameters
00253     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).");
00254 }
00255 
00256 void MarXbotGroundAroundIRSensor::update()
00257 {
00258     // Checking all resources we need exist
00259     checkAllNeededResourcesExist();
00260 
00261     // Acquiring the lock to get resources
00262     ResourcesLocker locker( this );
00263 
00264     m_neuronsIterator->setCurrentBlock(name());
00265     for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00266         m_neuronsIterator->setInput(m_robot->groundAroundIRSensorController()->activation(i));
00267     }
00268 }
00269 
00270 int MarXbotGroundAroundIRSensor::size()
00271 {
00272     return 8;
00273 }
00274 
00275 void MarXbotGroundAroundIRSensor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00276 {
00277     // Calling parent function
00278     MarXbotSensor::resourceChanged(resourceName, changeType);
00279 
00280     if (changeType == Deleted) {
00281         return;
00282     }
00283 
00284     if (resourceName == m_marxbotResource) {
00285         m_robot = getResource<PhyMarXbot>();
00286     } else if (resourceName == m_neuronsIteratorResource) {
00287         m_neuronsIterator = getResource<NeuronsIterator>();
00288         m_neuronsIterator->setCurrentBlock(name());
00289         for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00290             m_neuronsIterator->setGraphicProperties("ga" + QString::number(i), 0.0, 1.0, Qt::red);
00291         }
00292     } else {
00293         Logger::info("Unknown resource " + resourceName + " for " + name());
00294     }
00295 }
00296 
00297 }