Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
abstractcontrolleradapter.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __ABSTRACTCONTROLLERADAPTER_H
25 #define __ABSTRACTCONTROLLERADAPTER_H
26 
27 #include "abstractcontroller.h"
28 
29 #include <selforg/stl_adds.h>
30 
31 /**
32  * Abstract adapter class (interface) for robot controller.
33  * The controller gets a number of input sensor values each timestep
34  * and has to generate a number of output motor values.
35  *
36  * Interface assumes the following usage:
37  * - init() is called first to initialise the dimension of sensor- and motor space
38  * - each time step
39  * either step() or stepNoLearning() is called to ask the controller for motor values.
40  *
41  * With the adapter class you can easily overwrite e.g. init(), step().
42  *
43  * This is an abstract adapter class, it's useful for implementing adapters such as the
44  * DescreteController, which can be used with all Controllers.
45  *
46  * Note that the configureable and inspectable classes are registered at this
47  * adapter class.
48  *
49  * The store and restore-functionality is lead through, thus only store() and restore()
50  * from
51  */
53 public:
54 
55  AbstractControllerAdapter(AbstractController* controller, const std::string& name, const std::string& revision)
56  : AbstractController(name, revision), controller(controller)
57  {
58  // register the inspectable and configureable controller
59  addConfigurable(controller);
60  addInspectable(controller);
61  }
62 
64 
65  /****************************************************************************/
66  /* AbstractControllerAdapter must implement the following classes: */
67  /* AbstractController, Configurable, Inspectable, Storeable */
68  /****************************************************************************/
69 
70 
71  /****************************************************************************/
72  /* BEGIN methods of AbstractController */
73  /****************************************************************************/
74 
75  /** initialisation of the controller with the given sensor/ motornumber
76  * Must NORMALLY be called before use. For all ControllerAdapters
77  * call first AbstractControllerAdapter::init(sensornumber,motornumber)
78  * if you overwrite this method
79  */
80  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
81  controller->init(sensornumber,motornumber);
82  }
83 
84  /** @return Number of sensors the controller
85  was initialised with or 0 if not initialised */
86  virtual int getSensorNumber() const { return controller->getSensorNumber();}
87 
88  /** @return Number of motors the controller
89  was initialised with or 0 if not initialised */
90  virtual int getMotorNumber() const { return controller->getMotorNumber();}
91 
92  /** performs one step (includes learning).
93  Calculates motor commands from sensor inputs.
94  @param sensors sensors inputs scaled to [-1,1]
95  @param sensornumber length of the sensor array
96  @param motors motors outputs. MUST have enough space for motor values!
97  @param motornumber length of the provided motor array
98  */
99  virtual void step(const sensor* sensors, int sensornumber,
100  motor* motors, int motornumber) {
101  controller->step(sensors, sensornumber, motors, motornumber);
102  }
103 
104  /** performs one step without learning.
105  @see step
106  */
107  virtual void stepNoLearning(const sensor* sensors , int sensornumber,
108  motor* motors, int motornumber) {
109  controller->stepNoLearning(sensors,sensornumber,motors,motornumber);
110  }
111 
112  /****************************************************************************/
113  /* END methods of AbstractController */
114  /****************************************************************************/
115 
116  /****************************************************************************/
117  /* BEGIN methods of Configurable */
118  /****************************************************************************/
119 
120  // nothing needed to be overwrited
121 
122  /****************************************************************************/
123  /* END methods of Configurable */
124  /****************************************************************************/
125 
126  /****************************************************************************/
127  /* BEGIN methods of Inspectable */
128  /****************************************************************************/
129 
130  // nothing needed to be overwrited
131 
132  /****************************************************************************/
133  /* END methods of Inspectable */
134  /****************************************************************************/
135 
136  /****************************************************************************/
137  /* BEGIN methods of Storable */
138  /****************************************************************************/
139 
140  /********* STORABLE INTERFACE ******/
141  /// @see Storable
142  virtual bool store(FILE* f) const {
143  return controller->store(f);
144  }
145 
146  /// @see Storable
147  virtual bool restore(FILE* f) {
148  return controller->restore(f);
149  }
150 
151 
152  /****************************************************************************/
153  /* END methods of Storable */
154  /****************************************************************************/
155 
156 
157 protected:
158  AbstractController* controller; // the controller for the adapter to handle
159 
160 };
161 
162 #endif
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual void addConfigurable(Configurable *conf)
Adds a configurable as a child object.
Definition: configurable.cpp:427
Abstract adapter class (interface) for robot controller.
Definition: abstractcontrolleradapter.h:52
virtual bool store(FILE *f) const
Definition: abstractcontrolleradapter.h:142
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
AbstractController * controller
Definition: abstractcontrolleradapter.h:158
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must NORMALLY be called before us...
Definition: abstractcontrolleradapter.h:80
AbstractControllerAdapter(AbstractController *controller, const std::string &name, const std::string &revision)
Definition: abstractcontrolleradapter.h:55
double sensor
Definition: abstractcontroller.h:48
virtual void addInspectable(Inspectable *insp)
Adds an inspectable as a child object.
Definition: inspectable.cpp:128
virtual int getMotorNumber() const
Definition: abstractcontrolleradapter.h:90
virtual void stepNoLearning(const sensor *sensors, int sensornumber, motor *motors, int motornumber)
performs one step without learning.
Definition: abstractcontrolleradapter.h:107
virtual ~AbstractControllerAdapter()
Definition: abstractcontrolleradapter.h:63
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)=0
initialisation of the controller with the given sensor/ motornumber Must be called before use...
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)=0
performs one step without learning.
virtual bool restore(FILE *f)
Definition: abstractcontrolleradapter.h:147
virtual int getSensorNumber() const
Definition: abstractcontrolleradapter.h:86
virtual void step(const sensor *sensors, int sensornumber, motor *motors, int motornumber)=0
performs one step (includes learning).
virtual void step(const sensor *sensors, int sensornumber, motor *motors, int motornumber)
performs one step (includes learning).
Definition: abstractcontrolleradapter.h:99
double motor
Definition: abstractcontroller.h:49
virtual bool store(FILE *f) const =0
stores the object to the given file stream (ASCII preferred).
virtual bool restore(FILE *f)=0
loads the object from the given file stream (ASCII preferred).
virtual int getMotorNumber() const =0
virtual int getSensorNumber() const =0