experiments/src/motors.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 "motors.h"
00024 #include "configurationhelper.h"
00025 #include "randomgenerator.h"
00026 #include "motorcontrollers.h"
00027 #include "logger.h"
00028 #include <QStringList>
00029 #include <QMap>
00030 
00031 namespace farsa {
00032 
00033 //implementing fake motors
00034 FakeMotor::FakeMotor( ConfigurationParameters& params, QString prefix ) :
00035     Motor(params, prefix)
00036     {
00037     nFakeMotors=0;
00038     nFakeMotors = ConfigurationHelper::getInt( params, prefix+"nMotors", 0 );
00039     // Declaring the resources that are needed here
00040     usableResources( QStringList() << "neuronsIterator" );
00041 }
00042 
00043 FakeMotor::~FakeMotor() {
00044     /* nothing to do */
00045 }
00046 
00047 void FakeMotor::save( ConfigurationParameters& params, QString prefix ) {
00048     Motor::save( params, prefix );
00049     params.startObjectParameters( prefix, "FakeMotor", this );
00050     params.createParameter( prefix, "nMotors", QString::number(nFakeMotors) );
00051 }
00052 
00053 void FakeMotor::describe( QString type ) {
00054     Motor::describe( type );
00055     Descriptor d = addTypeDescription( type, "Fake motors: useful to add additional outputs (not linked with real motors) to the neural controller" );
00056     d.describeInt( "nFakeMotors" ).def(0).limits(0,100).props( IsMandatory ).help( "Numbers of additional motors not linked with any actual motors (i.e. fake). Useful to add additional outputs" );
00057 }
00058 
00059 void FakeMotor::update() {
00060     // Checking all resources we need exist
00061     checkAllNeededResourcesExist();
00062 }
00063 
00064 int FakeMotor::size() {
00065     return nFakeMotors;
00066 }
00067 
00068 void FakeMotor::resourceChanged(QString resourceName, ResourceChangeType changeType) {
00069     if (changeType == Deleted) {
00070         resetNeededResourcesCheck();
00071         return;
00072     }
00073 
00074     if (resourceName == "neuronsIterator") {
00075         NeuronsIterator* evonetIt = getResource<NeuronsIterator>( "neuronsIterator" );
00076         evonetIt->setCurrentBlock( name() );
00077         for( int i=0; i<nFakeMotors; i++, evonetIt->nextNeuron() ) {
00078             evonetIt->setGraphicProperties( QString("Fk")+QString::number(i), 0.0, 1.0, Qt::red );
00079         }
00080     } else {
00081         Logger::info("Unknown resource " + resourceName + " for " + name());
00082     }
00083 }
00084 
00085 ProportionalController::ProportionalController(){
00086 
00087     setControlParameters(0.3,20);
00088 }
00089 ProportionalController::~ProportionalController(){
00090     /*so far, nothing to do */
00091 }
00092 
00093 void ProportionalController::setControlParameters(double k, double maxvel){
00094     if (k >= 0.0f) {
00095         m_k = k;
00096     }
00097     if (maxvel >= 0.0f) {
00098         m_maxvel = maxvel;
00099     }
00100 }
00101 
00102 double ProportionalController::getK() const {
00103     return m_k;
00104 }
00105 
00106 void ProportionalController::setK(double k){
00107     if (k >= 0.0f)
00108         m_k = k;
00109 }
00110 double ProportionalController::getMaxVelocity(){
00111     return m_maxvel;
00112 }
00113 void ProportionalController::setMaxVelocity(double maxVel){
00114     if (maxVel >= 0.0f)
00115         m_maxvel = maxVel;
00116 }
00117 
00118 
00119 double ProportionalController::velocityForJoint(double desired, double current){
00120     double vel = m_k * (desired - current);
00121 
00122     if (vel > +m_maxvel) {
00123         vel = +m_maxvel;
00124     } else if (vel < -m_maxvel) {
00125         vel = -m_maxvel;
00126     }
00127 
00128     return vel;
00129 }
00130 
00131 } // end namespace farsa
00132