abstractcontrolleradapter.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 LpzRobots development team                    *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
00005  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00006  *    Ralf Der       <ralfder at mis dot mpg dot 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  ***************************************************************************/
00024 #ifndef __ABSTRACTCONTROLLERADAPTER_H
00025 #define __ABSTRACTCONTROLLERADAPTER_H
00026 
00027 #include "abstractcontroller.h"
00028 
00029 #include <selforg/stl_adds.h>
00030 
00031 /**
00032  * Abstract adapter class (interface) for robot controller.
00033  * The controller gets a number of input sensor values each timestep
00034  *  and has to generate a number of output motor values.
00035  *
00036  * Interface assumes the following usage:
00037  *  - init() is called first to initialise the dimension of sensor- and motor space
00038  *  - each time step
00039  *     either step() or stepNoLearning() is called to ask the controller for motor values.
00040  *
00041  * With the adapter class you can easily overwrite e.g. init(), step().
00042  *
00043  * This is an abstract adapter class, it's useful for implementing adapters such as the
00044  * DescreteController, which can be used with all Controllers.
00045  * 
00046  * Note that the configureable and inspectable classes are registered at this
00047  *  adapter class.
00048  *
00049  *  The store and restore-functionality is lead through, thus only store() and restore()
00050  *  from
00051  */
00052 class AbstractControllerAdapter : public AbstractController {
00053 public:
00054 
00055   AbstractControllerAdapter(AbstractController* controller, const std::string& name, const std::string& revision)
00056     : AbstractController(name, revision), controller(controller)
00057   {
00058     //  register the inspectable and configureable controller
00059     addConfigurable(controller);
00060     addInspectable(controller);
00061   }
00062 
00063   virtual ~AbstractControllerAdapter() {}
00064 
00065   /****************************************************************************/
00066   /*    AbstractControllerAdapter must implement the following classes:         */
00067   /*    AbstractController, Configurable, Inspectable, Storeable                    */
00068   /****************************************************************************/
00069   
00070   
00071   /****************************************************************************/
00072   /*    BEGIN methods of AbstractController                                     */
00073   /****************************************************************************/
00074 
00075   /** initialisation of the controller with the given sensor/ motornumber
00076    * Must NORMALLY be called before use. For all ControllerAdapters
00077    * call first AbstractControllerAdapter::init(sensornumber,motornumber)
00078    * if you overwrite this method
00079    */
00080   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
00081     controller->init(sensornumber,motornumber);
00082   }
00083 
00084   /** @return Number of sensors the controller
00085       was initialised with or 0 if not initialised */
00086   virtual int getSensorNumber() const { return controller->getSensorNumber();}
00087   
00088   /** @return Number of motors the controller
00089       was initialised with or 0 if not initialised */
00090   virtual int getMotorNumber() const {  return controller->getMotorNumber();}
00091   
00092   /** performs one step (includes learning).
00093       Calculates motor commands from sensor inputs.
00094       @param sensors sensors inputs scaled to [-1,1]
00095       @param sensornumber length of the sensor array
00096       @param motors motors outputs. MUST have enough space for motor values!
00097       @param motornumber length of the provided motor array
00098   */
00099   virtual void step(const sensor* sensors, int sensornumber,
00100                     motor* motors, int motornumber) { 
00101     controller->step(sensors, sensornumber, motors,  motornumber); 
00102   }
00103   
00104   /** performs one step without learning.
00105       @see step
00106   */
00107   virtual void stepNoLearning(const sensor* sensors , int sensornumber,
00108                               motor* motors, int motornumber) { 
00109     controller->stepNoLearning(sensors,sensornumber,motors,motornumber); 
00110   }
00111   
00112   /****************************************************************************/
00113   /*    END methods of AbstractController                                           */
00114   /****************************************************************************/
00115   
00116   /****************************************************************************/
00117   /*    BEGIN methods of Configurable                                           */
00118   /****************************************************************************/
00119   
00120   // nothing needed to be overwrited
00121 
00122   /****************************************************************************/
00123   /*    END methods of Configurable                                                 */
00124   /****************************************************************************/
00125   
00126   /****************************************************************************/
00127   /*    BEGIN methods of Inspectable                                            */
00128   /****************************************************************************/
00129   
00130   // nothing needed to be overwrited
00131   
00132   /****************************************************************************/
00133   /*    END methods of Inspectable                                                  */
00134   /****************************************************************************/
00135   
00136   /****************************************************************************/
00137   /*    BEGIN methods of Storable                                               */
00138   /****************************************************************************/
00139   
00140   /********* STORABLE INTERFACE ******/
00141   /// @see Storable
00142   virtual bool store(FILE* f) const { 
00143     return controller->store(f);
00144   }
00145   
00146   /// @see Storable
00147   virtual bool restore(FILE* f) { 
00148     return controller->restore(f);
00149   }
00150   
00151   
00152   /****************************************************************************/
00153   /*    END methods of Storable                                             */
00154   /****************************************************************************/
00155   
00156   
00157 protected:
00158   AbstractController* controller; // the controller for the adapter to handle
00159   
00160 };
00161 
00162 #endif
Generated on Thu Jun 28 14:45:35 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3