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 /* 00025 ** Started on Mon Oct 22 10:50:47 2007 Georg Martius 00026 ** Last update Mon Oct 22 10:50:47 2007 Georg Martius 00027 */ 00028 #ifndef WIREDCONTROLLER_H_ 00029 #define WIREDCONTROLLER_H_ 00030 00031 #include "plotoptionengine.h" 00032 #include "backcaller.h" 00033 #include "types.h" 00034 #include "inspectable.h" 00035 #include "configurable.h" 00036 #include "randomgenerator.h" 00037 00038 #include <stdio.h> 00039 #include <list> 00040 #include <utility> 00041 #include <string> 00042 00043 00044 class AbstractController; 00045 class AbstractWiring; 00046 class Callbackable; 00047 class WiredController; 00048 00049 /** The WiredController contains a controller and a wiring, which 00050 connects the controller with the robot. 00051 Additionally there are some ways to keep track of internal information. 00052 You have the possibility to keep track of sensor values, 00053 motor values and internal parameters of the controller with PlotOptions. 00054 The name PlotOptions is a bit missleaded, it should be "OutputOptions", 00055 however you can write the data into a file or send it to 00056 visualisation tools like guilogger or neuronviz. 00057 */ 00058 class WiredController : public Inspectable, public Configurable { 00059 public: 00060 /** constructor. PlotOption as output setting. 00061 noisefactor is used to set the relative noise strength of this agent 00062 */ 00063 WiredController(const PlotOption& plotOption = PlotOption(NoPlot), double noisefactor = 1, const iparamkey& name = "WiredController", const paramkey& revision = "$ID"); 00064 /** constructor. A list of PlotOption can given. 00065 noisefactor is used to set the relative noise strength of this agent 00066 */ 00067 WiredController(const std::list<PlotOption>& plotOptions, double noisefactor = 1, const iparamkey& name = "WiredController", const paramkey& revision = "$ID"); 00068 00069 /** destructor 00070 */ 00071 virtual ~WiredController(); 00072 00073 /** initializes the object with the given controller and wiring 00074 and initializes the output options 00075 It is also possible to provide a random seed, 00076 if not given (0) rand() is used to create one 00077 */ 00078 virtual bool init(AbstractController* controller, AbstractWiring* wiring, 00079 int robotsensornumber, int robotmotornumber, RandGen* randGen=0); 00080 00081 /** Performs an step of the controller, which includes 00082 pushing sensor values through the wiring, 00083 controller step, 00084 pushing controller outputs (= motorcommands) back through the wiring 00085 @param sensors sensors inputs scaled to [-1,1] 00086 @param sensornumber length of the sensor array 00087 @param motors motors outputs. MUST have enough space for motor values! 00088 @param motornumber length of the provided motor array 00089 00090 @param noise Noise strength. 00091 @param time (optional) current simulation time (used for logging) 00092 */ 00093 virtual void step(const sensor* sensors, int sensornumber, 00094 motor* motors, int motornumber, 00095 double noise, double time=-1); 00096 00097 /** Enables the motor babbling mode for given number of steps (typically 1000). 00098 Optionally a controller can be 00099 given that is used for the babbling (default is MotorBabbler) (deleted automatically). 00100 During motor babbling the function motorbabbling of the normal controller is called instead of step. 00101 The parameter fixRobot is intended for simulations 00102 */ 00103 virtual void startMotorBabblingMode (int steps, AbstractController* babblecontroller = 0, 00104 bool fixRobot = true); 00105 00106 virtual AbstractController* getMotorBabbler() { return motorBabbler; } 00107 00108 /** stops the motor babbling mode. */ 00109 virtual void stopMotorBabblingMode () { motorBabblingSteps = 0; } 00110 /// returns true if in motorbabbling mode 00111 virtual bool getMotorBabblingMode() { return motorBabblingSteps > 0; } 00112 00113 00114 /** adds the PlotOptions to the list of plotoptions 00115 If a plotoption with the same Mode exists, then the old one is deleted first 00116 */ 00117 virtual PlotOption addPlotOption(PlotOption& plotoption); 00118 00119 /** adds a new PlotOption and initializes it 00120 @see addPlotOption 00121 */ 00122 virtual bool addAndInitPlotOption(PlotOption& plotOption); 00123 00124 /** removes the PlotOptions with the given type 00125 @return true if sucessful, false otherwise 00126 */ 00127 virtual bool removePlotOption(PlotMode mode); 00128 00129 /** 00130 write comment to output streams (PlotOptions). For instance changes in parameters. 00131 */ 00132 virtual void writePlotComment(const char* cmt); 00133 00134 /** Returns a pointer to the controller. 00135 */ 00136 virtual AbstractController* getController() { return controller;} 00137 virtual const AbstractController* getController() const { return controller;} 00138 00139 /** Returns a pointer to the wiring. 00140 */ 00141 virtual AbstractWiring* getWiring() { return wiring;} 00142 00143 00144 protected: 00145 /** 00146 * Plots controller sensor- and motorvalues and internal controller parameters. 00147 * @param time simulation time 00148 */ 00149 virtual void plot(double time); 00150 00151 00152 AbstractController* controller; 00153 AbstractWiring* wiring; 00154 00155 /// number of sensors of robot 00156 int rsensornumber; 00157 /// number of motors of robot 00158 int rmotornumber; 00159 /// number of sensors of comntroller 00160 int csensornumber; 00161 /// number of motors of comntroller 00162 int cmotornumber; 00163 00164 /// factor that is muliplied with noise stength 00165 double noisefactor; 00166 00167 motor *cmotors; 00168 sensor *csensors; 00169 00170 void internInit(); 00171 00172 protected: 00173 AbstractController* motorBabbler; 00174 int motorBabblingSteps; 00175 00176 PlotOptionEngine plotEngine; 00177 00178 bool initialised; 00179 00180 std::list<Callbackable* > callbackables; 00181 00182 long int t; 00183 }; 00184 00185 #endif /* !WIREDCONTROLLER_H_ */