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 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 * * 00022 * $Log: derivativewiring.h,v $ 00023 * Revision 1.10 2009/08/05 22:32:21 martius 00024 * big change: 00025 * abstractwiring is responsable for providing sensors and motors 00026 * and noise to the inspectable interface. 00027 * external interface: unchanged except plotMode in constructor 00028 * internal interface: all subclasses have to overload 00029 * initIntern, wireSensorsIntern, wireMotorsIntern 00030 * All existing implementation are changed 00031 * 00032 * Revision 1.9 2008/04/17 14:54:45 martius 00033 * randomGen added, which is a random generator with long period and an 00034 * internal state. Each Agent has an instance and passed it to the controller 00035 * and the wiring. This is good for 00036 * a) repeatability on agent basis, 00037 * b) parallel execution as done in ode_robots 00038 * 00039 * Revision 1.8 2007/12/12 10:26:14 der 00040 * MI: calculation of H(X) and H(Y|X) added 00041 * 00042 * Revision 1.7 2006/12/21 11:44:17 martius 00043 * commenting style for doxygen //< -> ///< 00044 * FOREACH and FOREACHC are macros for collection iteration 00045 * 00046 * Revision 1.6 2006/12/12 09:43:40 martius 00047 * getDefaultConf1() 00048 * 00049 * Revision 1.5 2006/12/11 18:23:11 martius 00050 * changed order again: first all sensors, then all derivatives ... 00051 * noise is only added to first sensor set 00052 * now 2 functions for default configs 00053 * blind motors not as sets, but as direct number given 00054 * 00055 * Revision 1.4 2006/12/04 17:44:28 martius 00056 * unclear 00057 * 00058 * Revision 1.3 2006/12/04 16:05:10 der 00059 * under construction 00060 * 00061 * Revision 1.2 2006/07/14 12:24:02 martius 00062 * selforg becomes HEAD 00063 * 00064 * Revision 1.1.2.1 2005/11/16 11:24:28 martius 00065 * moved to selforg 00066 * 00067 * Revision 1.6 2005/10/28 12:05:27 martius 00068 * adapted time horizont for derivative 00069 * to quater of the time horizont of averaging 00070 * 00071 * Revision 1.5 2005/10/24 13:32:07 fhesse 00072 * comments adjusted and in doxygen style 00073 * 00074 * Revision 1.4 2005/10/24 11:06:33 fhesse 00075 * comments adjusted and in doxygen style 00076 * 00077 * Revision 1.3 2005/07/21 15:09:00 martius 00078 * blind motors 00079 * 00080 * Revision 1.2 2005/07/21 11:30:59 fhesse 00081 * started with blind motors 00082 * 00083 * Revision 1.1 2005/07/18 14:44:55 martius 00084 * wiring that supports derivatives 00085 * 00086 * * 00087 ***************************************************************************/ 00088 #ifndef __DERIVATIVEWIRING_H 00089 #define __DERIVATIVEWIRING_H 00090 00091 #include "abstractwiring.h" 00092 00093 /** Configuration Object for DerivativeWiring. 00094 If all boolean parametes are false, id is set to true (equivalent to One2OneWiring) 00095 */ 00096 typedef struct __DerivativeWiringConf { 00097 bool useId; //< include zeroth derivative 00098 bool useFirstD; ///< include first derivative 00099 bool useSecondD; ///< second include second derivative 00100 double eps; ///< update rate for floating average (0 -> no sensor variation, 1 -> no smoothing) 00101 double derivativeScale; ///< factor for the derivatives 00102 unsigned int blindMotors; ///< number of motors that are blind (not given to robot) 00103 } DerivativeWiringConf; 00104 00105 00106 /** Implements a wiring (between controller and robot) 00107 which includes the first and second derivative 00108 of the original robot sensor values 00109 */ 00110 class DerivativeWiring : public AbstractWiring{ 00111 public: 00112 /** constructor 00113 @param conf for giving the wished configuration of DerivativeWiring 00114 via \ref __DerivativeWiringConf "DerivativeWiringConf" 00115 @param noise NoiseGenerator that is used for adding noise to sensor values 00116 */ 00117 DerivativeWiring(const DerivativeWiringConf& conf, 00118 NoiseGenerator* noise); 00119 00120 /** destructor 00121 */ 00122 virtual ~DerivativeWiring(); 00123 00124 /** Providing default configuration for DerivativeWiring with first derivative. 00125 No smoothing and no scaling. ( as static method ) 00126 */ 00127 static DerivativeWiringConf getDefaultConf(){ 00128 DerivativeWiringConf c; 00129 c.useId = true; // use id 00130 c.useFirstD = false; // use first derivative 00131 c.useSecondD = false; // do not use secound derivative 00132 c.eps = 1; // no smoothing 00133 c.derivativeScale=1; // no scaleing 00134 c.blindMotors=0; // no blind motors used 00135 return c; 00136 }; 00137 00138 /** Providing default configuration for DerivativeWiring for only first derivative. 00139 smoothing over 4 steps and scale of 5. Use smaller noise! 00140 ( as static method ) 00141 */ 00142 static DerivativeWiringConf getDefaultConf1(){ 00143 DerivativeWiringConf c; 00144 c.useId = false; // do not use id 00145 c.useFirstD = true; // use first derivative 00146 c.useSecondD = false; // do not use secound derivative 00147 c.eps = 0.5; // smoothing over 2 steps 00148 c.derivativeScale=5; // scaling with 5 00149 c.blindMotors=0; // no blind motors used 00150 return c; 00151 }; 00152 00153 protected: 00154 00155 /** initializes the internal numbers of sensors and motors on robot side, calculate 00156 number of sensors and motors on controller side 00157 */ 00158 virtual bool initIntern(int robotsensornumber, int robotmotornumber, RandGen* randGen=0); 00159 00160 /** Realizes derivative wiring from robot sensors to controller sensors. 00161 @param rsensors pointer to array of sensorvalues from robot 00162 @param rsensornumber number of sensors from robot 00163 @param csensors pointer to array of sensorvalues for controller 00164 @param csensornumber number of sensors to controller 00165 @param noise size of the noise added to the sensors 00166 */ 00167 virtual bool wireSensorsIntern(const sensor* rsensors, int rsensornumber, 00168 sensor* csensors, int csensornumber, 00169 double noise); 00170 00171 /** Realizes wiring from controller motor outputs to robot motors. 00172 @param rmotors pointer to array of motorvalues for robot 00173 @param rmotornumber number of robot motors 00174 @param cmotors pointer to array of motorvalues from controller 00175 @param cmotornumber number of motorvalues from controller 00176 */ 00177 virtual bool wireMotorsIntern(motor* rmotors, int rmotornumber, 00178 const motor* cmotors, int cmotornumber); 00179 00180 protected: 00181 /** Calculate the first derivative of the sensorvalues given by the robot 00182 * f'(x) = (f(x+1) - f(x-1)) / 2 00183 * since we do not have f(x+1) we go one timestep in the past 00184 */ 00185 void calcFirstDerivative(); 00186 00187 /** Calculate the secound derivative of the sensorvalues given by the robot 00188 * f'(x) = f(x) - 2f(x-1) + f(x-2) 00189 */ 00190 void calcSecondDerivative(); 00191 00192 /// used configuration 00193 DerivativeWiringConf conf; 00194 static const int buffersize=40; 00195 int time; 00196 /// number timesteps the sensor values are delayed for calculation of the derivative 00197 // int delay; 00198 00199 00200 /// current and old smoothed sensor values of robot 00201 sensor* sensorbuffer[buffersize]; 00202 00203 /// current sensors (with noise) 00204 // sensor* id; 00205 00206 /// current first derivative 00207 sensor* first; 00208 00209 /// current second derivative 00210 sensor* second; 00211 00212 /// array that stored the values of the blind motors 00213 motor *blindMotors; 00214 00215 }; 00216 00217 #endif