experiments/src/marxbotmotors.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  *                                                                              *
00008  *  This program is free software; you can redistribute it and/or modify        *
00009  *  it under the terms of the GNU General Public License as published by        *
00010  *  the Free Software Foundation; either version 2 of the License, or           *
00011  *  (at your option) any later version.                                         *
00012  *                                                                              *
00013  *  This program is distributed in the hope that it will be useful,             *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00016  *  GNU General Public License for more details.                                *
00017  *                                                                              *
00018  *  You should have received a copy of the GNU General Public License           *
00019  *  along with this program; if not, write to the Free Software                 *
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00021  ********************************************************************************/
00022 
00023 #include "marxbotmotors.h"
00024 #include "configurationhelper.h"
00025 #include "randomgenerator.h"
00026 #include "logger.h"
00027 
00028 namespace farsa {
00029 
00030 MarXbotMotor::MarXbotMotor(ConfigurationParameters& params, QString prefix) :
00031     Motor(params, prefix),
00032     m_marxbotResource("robot"),
00033     m_neuronsIteratorResource("neuronsIterator")
00034 {
00035     // Reading parameters
00036     m_marxbotResource = ConfigurationHelper::getString(params, prefix + "marxbot", m_marxbotResource);
00037     m_neuronsIteratorResource = ConfigurationHelper::getString(params, prefix + "neuronsIterator", m_neuronsIteratorResource);
00038 
00039     // Declaring the resources that are needed here
00040     usableResources(QStringList() << m_marxbotResource << m_neuronsIteratorResource);
00041 }
00042 
00043 MarXbotMotor::~MarXbotMotor()
00044 {
00045     // Nothing to do here
00046 }
00047 
00048 void MarXbotMotor::save(ConfigurationParameters& params, QString prefix)
00049 {
00050     // Calling parent function
00051     Motor::save(params, prefix);
00052 
00053     // Saving parameters
00054     params.startObjectParameters(prefix, "MarXbotMotor", this);
00055     params.createParameter(prefix, "marxbot", m_marxbotResource);
00056     params.createParameter(prefix, "neuronsIterator", m_neuronsIteratorResource);
00057 }
00058 
00059 void MarXbotMotor::describe(QString type)
00060 {
00061     // Calling parent function
00062     Motor::describe(type);
00063 
00064     // Describing our parameters
00065     Descriptor d = addTypeDescription(type, "The base class for MarXbot motors");
00066     d.describeString("marxbot").def("robot").help("The name of the resource associated with the MarXbot robot to use (default is \"robot\")");
00067     d.describeString("neuronsIterator").def("neuronsIterator").help("The name of the resource associated with the neural network iterator (default is \"neuronsIterator\")");
00068 }
00069 
00070 void MarXbotMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00071 {
00072     // Calling parent function
00073     Motor::resourceChanged(resourceName, changeType);
00074 
00075     // Here we only check whether the resource has been deleted and reset the check flag, the
00076     // actual work is done in subclasses
00077     if (changeType == Deleted) {
00078         resetNeededResourcesCheck();
00079         return;
00080     }
00081 }
00082 
00083 MarXbotWheelVelocityMotor::MarXbotWheelVelocityMotor(ConfigurationParameters& params, QString prefix) :
00084     MarXbotMotor(params, prefix),
00085     m_robot(NULL),
00086     m_neuronsIterator(NULL)
00087 {
00088 }
00089 
00090 MarXbotWheelVelocityMotor::~MarXbotWheelVelocityMotor()
00091 {
00092     // Nothing to do here
00093 }
00094 
00095 void MarXbotWheelVelocityMotor::save(ConfigurationParameters& params, QString prefix)
00096 {
00097     // Calling parent function
00098     MarXbotMotor::save(params, prefix);
00099 
00100     // Saving parameters
00101     params.startObjectParameters(prefix, "MarXbotWheelVelocityMotor", this);
00102 }
00103 
00104 void MarXbotWheelVelocityMotor::describe(QString type)
00105 {
00106     // Calling parent function
00107     MarXbotMotor::describe(type);
00108 
00109     // Describing our parameters
00110     Descriptor d = addTypeDescription(type, "The motor controlling the velocity of the wheels of the MarXbot robot");
00111 }
00112 
00113 void MarXbotWheelVelocityMotor::update()
00114 {
00115     // Checking all resources we need exist
00116     checkAllNeededResourcesExist();
00117 
00118     // Acquiring the lock to get resources
00119     ResourcesLocker locker( this );
00120 
00121     // Getting limit velocities for wheels
00122     double minSpeed1;
00123     double minSpeed2;
00124     double maxSpeed1;
00125     double maxSpeed2;
00126     m_robot->wheelsController()->getSpeedLimits(minSpeed1, minSpeed2, maxSpeed1, maxSpeed2);
00127 
00128     // Computing desired wheel velocities
00129     m_neuronsIterator->setCurrentBlock(name());
00130     const double v1 = (maxSpeed1 - minSpeed1) * m_neuronsIterator->getOutput() + minSpeed1;
00131     m_neuronsIterator->nextNeuron();
00132     const double v2 = (maxSpeed2 - minSpeed2) * m_neuronsIterator->getOutput() + minSpeed2;
00133 
00134     m_robot->wheelsController()->setSpeeds(v1, v2);
00135 }
00136 
00137 int MarXbotWheelVelocityMotor::size()
00138 {
00139     return 2;
00140 }
00141 
00142 void MarXbotWheelVelocityMotor::resourceChanged(QString resourceName, ResourceChangeType changeType)
00143 {
00144     // Calling parent function
00145     MarXbotMotor::resourceChanged(resourceName, changeType);
00146 
00147     if (changeType == Deleted) {
00148         return;
00149     }
00150 
00151     if (resourceName == m_marxbotResource) {
00152         m_robot = getResource<PhyMarXbot>();
00153     } else if (resourceName == m_neuronsIteratorResource) {
00154         m_neuronsIterator = getResource<NeuronsIterator>();
00155         m_neuronsIterator->setCurrentBlock(name());
00156         for (int i = 0; i < size(); i++, m_neuronsIterator->nextNeuron()) {
00157             m_neuronsIterator->setGraphicProperties("W" + QString::number(i), 0.0, 1.0, Qt::red);
00158         }
00159     } else {
00160         Logger::info("Unknown resource " + resourceName + " for " + name());
00161     }
00162 }
00163 
00164 }