experiments/src/sensors.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 "sensors.h"
00025 #include "configurationhelper.h"
00026 #include "motorcontrollers.h"
00027 #include "logger.h"
00028 #include "graphicalwobject.h"
00029 #include <QStringList>
00030 
00031 namespace farsa {
00032 
00033 
00034 
00035 //ObjectPositionSensor : begin implementation
00036 // it returns the absolute coordinate of an object into the world
00037 ObjectPositionSensor::ObjectPositionSensor(ConfigurationParameters &params, QString prefix) :
00038     Sensor(params, prefix) {
00039     neuronsIteratorResource = ConfigurationHelper::getString(params, prefix + "neuronsIterator", neuronsIteratorResource);
00040     objectName = ConfigurationHelper::getString( params, prefix+"object", "object" );
00041     QVector<double> vec1 = ConfigurationHelper::getVector( params, prefix+"bbMin" );
00042     QVector<double> vec2 = ConfigurationHelper::getVector( params, prefix+"bbMax" );
00043     if ( vec1.size() == 3 && vec2.size() == 3 ) {
00044         linearize = true;
00045         bbMin = wVector( vec1[0], vec1[1], vec1[2] );
00046         bbMax = wVector( vec2[0], vec2[1], vec2[2] );
00047     } else {
00048         linearize = false;
00049         if ( ! (vec1.isEmpty() && vec2.isEmpty()) ) {
00050             Logger::warning( QString("ObjectPositionSensor %1 - bbMin and/or bbMax parameters are not well specified; they will be ignored").arg(name()) );
00051         }
00052     }
00053 
00054     // Declaring the resources that are needed here
00055     usableResources( QStringList() << objectName << neuronsIteratorResource );
00056 }
00057 
00058 ObjectPositionSensor::~ObjectPositionSensor() {
00059     // nothing to do
00060 }
00061 
00062 void ObjectPositionSensor::describe( QString type ) {
00063     Sensor::describe( type );
00064     Descriptor d = addTypeDescription( type, "Sensor for reading the three absolute coordinate (position into the worlf frame) of an object" );
00065     d.describeString("neuronsIterator").def("neuronsIterator").help("the name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
00066     d.describeString( "object" ).def( "object" ).props( IsMandatory ).help( "The name of the resource associated with the object to track with this sensor" );
00067     d.describeReal( "bbMin" ).props( IsList ).help( "The minimum 3D point used for linearize the object position into [0,1]" );
00068     d.describeReal( "bbMax" ).props( IsList ).help( "The maximum 3D point used for linearize the object position into [0,1]" );
00069 }
00070 
00071 void ObjectPositionSensor::update() {
00072     // Checking all resources we need exist
00073     checkAllNeededResourcesExist();
00074 
00075     // Acquiring the lock to get resources
00076     ResourcesLocker locker( this );
00077 
00078     WObject* object = getResource<WObject>( objectName );
00079     wVector pos = object->matrix().w_pos;
00080     NeuronsIterator* evonetIt = getResource<NeuronsIterator>( neuronsIteratorResource );
00081     evonetIt->setCurrentBlock( name() );
00082     for( int i=0; i<3; i++, evonetIt->nextNeuron() ) {
00083         if ( linearize ) {
00084             // linearize into [0,1]
00085             evonetIt->setInput( linearMap( pos[i], bbMin[i], bbMax[i], 0, 1 ) );
00086         } else {
00087             evonetIt->setInput( pos[i] );
00088         }
00089     }
00090 }
00091 
00092 int ObjectPositionSensor::size() {
00093     return 3;
00094 }
00095 
00096 void ObjectPositionSensor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00097     if (changeType == Deleted) {
00098         resetNeededResourcesCheck();
00099         return;
00100     }
00101 
00102     if (resourceName == objectName) {
00103         // Nothing to do here, we get the object with getResource() in update()
00104     } else if (resourceName == neuronsIteratorResource) {
00105         NeuronsIterator* evonetIt = getResource<NeuronsIterator>();
00106         evonetIt->setCurrentBlock( name() );
00107         for( int i=0; i<3; i++, evonetIt->nextNeuron() ) {
00108             evonetIt->setGraphicProperties( QString("obj")+QString::number(i), -10.0, 10.0, Qt::red );
00109         }
00110     } else {
00111         Logger::info("Unknown resource " + resourceName + " for " + name());
00112     }
00113 }
00114 
00115 void ObjectPositionSensor::save(ConfigurationParameters &params, QString prefix)
00116 {
00117     Sensor::save( params, prefix );
00118     params.startObjectParameters( prefix, "ObjectPositionSensor", this );
00119     params.createParameter(prefix, "neuronsIterator", neuronsIteratorResource);
00120     params.createParameter( prefix, "object", objectName );
00121     if ( linearize ) {
00122         params.createParameter( prefix, "bbMin", QString("%1 %2 %3").arg(bbMin[0]).arg(bbMin[1]).arg(bbMin[2]) );
00123         params.createParameter( prefix, "bbMax", QString("%1 %2 %3").arg(bbMax[0]).arg(bbMax[1]).arg(bbMax[2]) );
00124     }
00125 }
00126 //ObjectPositionSensor : end implementation
00127 
00128 } // end namespace farsa
00129