abstractcontrolleradapter.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *    frankguettler@gmx.de                                                 *
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                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  *                                                                         *
00023  *   $Log: abstractcontrolleradapter.h,v $
00024  *   Revision 1.5  2010/01/26 09:39:29  martius
00025  *   inspectables are takes from the internal lists of abstractcontroller
00026  *
00027  *   Revision 1.4  2009/08/05 22:58:13  martius
00028  *   use copy constructor for abstractcontroller -> config and inspection works
00029  *   configurable interface passthrough
00030  *
00031  *   Revision 1.3  2009/03/25 11:46:58  robot1
00032  *   bugfix
00033  *
00034  *   Revision 1.2  2008/04/17 14:54:44  martius
00035  *   randomGen added, which is a random generator with long period and an
00036  *    internal state. Each Agent has an instance and passed it to the controller
00037  *    and the wiring. This is good for
00038  *   a) repeatability on agent basis,
00039  *   b) parallel execution as done in ode_robots
00040  *
00041  *   Revision 1.1  2007/03/22 08:05:03  robot3
00042  *   this is an adapter class which implements all needed things. This class is
00043  *   used for example by the DiscreteControllerAdapter.
00044  *
00045 
00046  *                                                                         *
00047  ***************************************************************************/
00048 #ifndef __ABSTRACTCONTROLLERADAPTER_H
00049 #define __ABSTRACTCONTROLLERADAPTER_H
00050 
00051 #include "abstractcontroller.h"
00052 
00053 #include <selforg/stl_adds.h>
00054 
00055 /**
00056  * Abstract adapter class (interface) for robot controller.
00057  * The controller gets a number of input sensor values each timestep
00058  *  and has to generate a number of output motor values.
00059  *
00060  * Interface assumes the following usage:
00061  *  - init() is called first to initialise the dimension of sensor- and motor space
00062  *  - each time step
00063  *     either step() or stepNoLearning() is called to ask the controller for motor values.
00064  *
00065  * This is an abstract adapter class, it's useful for implementing adapters such as the
00066  * DescreteController, which can be used with all Controllers.
00067  * 
00068  * Note that the configureable and inspectable classes hold internal lists
00069  *  that are copied here. If a new parameter is added to either interfaces
00070  *  after the call of the contructor then this will not be visible througt the adapter 
00071  *  (at least partially. get and set will work but not the printing (also used by store)) 
00072  */
00073 class AbstractControllerAdapter : public AbstractController {
00074 public:
00075 
00076   AbstractControllerAdapter(AbstractController* controller)
00077     : AbstractController(*controller), controller(controller)
00078   { // Note that we use the copy constructor of AbtractController here to 
00079     //  copy the inspectable and configureable lists
00080   }
00081 
00082   /// contructor (hint: use $ID$ for revision) 
00083   AbstractControllerAdapter(AbstractController* controller, 
00084                             const std::string& name, const std::string& revision)
00085     : AbstractController(*controller), controller(controller) 
00086   { // Note that we use the copy constructor of AbtractController here to 
00087     //  copy the inspectable and configureable lists
00088   }
00089 
00090 
00091   virtual ~AbstractControllerAdapter() {}
00092 
00093   /****************************************************************************/
00094   /*    AbstractControllerAdapter must implement the following classes:         */
00095   /*    AbstractController, Configurable, Inspectable, Storeable                    */
00096   /****************************************************************************/
00097   
00098   
00099   /****************************************************************************/
00100   /*    BEGIN methods of AbstractController                                     */
00101   /****************************************************************************/
00102 
00103   /** initialisation of the controller with the given sensor/ motornumber
00104    * Must NORMALLY be called before use. For all ControllerAdapters
00105    * call first AbstractControllerAdapter::init(sensornumber,motornumber)
00106    * if you overwrite this method
00107    */
00108   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
00109     //          this->sensorNumber=sensornumber;
00110     //          this->motorNumber=motornumber;
00111     controller->init(sensornumber,motornumber);
00112   }
00113 
00114   /** @return Number of sensors the controller
00115       was initialised with or 0 if not initialised */
00116   virtual int getSensorNumber() const { return controller->getSensorNumber();}
00117   
00118   /** @return Number of motors the controller
00119       was initialised with or 0 if not initialised */
00120   virtual int getMotorNumber() const {  return controller->getMotorNumber();}
00121   
00122   /** performs one step (includes learning).
00123       Calculates motor commands from sensor inputs.
00124       @param sensors sensors inputs scaled to [-1,1]
00125       @param sensornumber length of the sensor array
00126       @param motors motors outputs. MUST have enough space for motor values!
00127       @param motornumber length of the provided motor array
00128   */
00129   virtual void step(const sensor* sensors, int sensornumber,
00130                     motor* motors, int motornumber) { 
00131     controller->step(sensors, sensornumber, motors,  motornumber); 
00132   }
00133   
00134   /** performs one step without learning.
00135       @see step
00136   */
00137   virtual void stepNoLearning(const sensor* sensors , int sensornumber,
00138                               motor* motors, int motornumber) { 
00139     controller->stepNoLearning(sensors,sensornumber,motors,motornumber); 
00140   }
00141   
00142   /****************************************************************************/
00143   /*    END methods of AbstractController                                           */
00144   /****************************************************************************/
00145   
00146   /****************************************************************************/
00147   /*    BEGIN methods of Configurable                                           */
00148   /****************************************************************************/
00149   
00150   virtual paramval getParam(const paramkey& key) const {
00151     return AbstractController::getParam(key);
00152   }
00153   
00154   virtual bool setParam(const paramkey& key, paramval val){
00155     return AbstractController::setParam(key,val);
00156   }
00157   
00158   virtual paramlist getParamList() const {
00159     return AbstractController::getParamList();
00160   }  
00161 
00162   /****************************************************************************/
00163   /*    END methods of Configurable                                                 */
00164   /****************************************************************************/
00165   
00166   /****************************************************************************/
00167   /*    BEGIN methods of Inspectable                                            */
00168   /****************************************************************************/
00169   
00170   /** The list of the names of all internal parameters given by getInternalParams().
00171       The naming convention is "v[i]" for vectors
00172       and "A[i][j]" for matrices, where i, j start at 0.
00173       @return: list of keys
00174   */
00175   virtual iparamkeylist getInternalParamNames() const { 
00176     //controller->getInternalParamNames() 
00177     return AbstractController::getInternalParamNames();
00178   }
00179   
00180   /** @return: list of values
00181    */
00182   virtual iparamvallist getInternalParams() const { 
00183     return AbstractController::getInternalParams() ; }
00184   
00185   /****************************************************************************/
00186   /*    END methods of Inspectable                                                  */
00187   /****************************************************************************/
00188   
00189   /****************************************************************************/
00190   /*    BEGIN methods of Storable                                               */
00191   /****************************************************************************/
00192   
00193   /********* STORABLE INTERFACE ******/
00194   /// @see Storable
00195   virtual bool store(FILE* f) const { 
00196     return controller->store(f);
00197   }
00198   
00199   /// @see Storable
00200   virtual bool restore(FILE* f) { 
00201     return controller->restore(f);
00202   }
00203   
00204   
00205   /****************************************************************************/
00206   /*    END methods of Storable                                             */
00207   /****************************************************************************/
00208   
00209   
00210 protected:
00211   AbstractController* controller; // the controller for the adapter to handle
00212   //    int sensorNumber; // number of sensors the controller has
00213   //    int motorNumber; // number of motors the controller has
00214   
00215 };
00216 
00217 #endif
Generated on Fri Nov 4 10:59:38 2011 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3