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 __ABSTRACTWIRING_H 00025 #define __ABSTRACTWIRING_H 00026 00027 //#include "abstractrobot.h" 00028 #include "matrix.h" 00029 #include "noisegenerator.h" 00030 #include "inspectable.h" 00031 #include "randomgenerator.h" 00032 00033 00034 /** Abstract wiring-object between controller and robot. 00035 * Implements wiring of robot sensors to inputs of the controller and 00036 * controller outputs to robot motors. 00037 */ 00038 class AbstractWiring : public Inspectable { 00039 public: 00040 typedef double sensor; 00041 typedef double motor; 00042 00043 enum PlotTypes {Nothing=0, Robot=1, Controller=4, Noise=8}; 00044 00045 00046 /** constructor 00047 * @param noise NoiseGenerator that is used for adding noise to sensor values 00048 */ 00049 AbstractWiring(NoiseGenerator* noise, int plotMode=Controller, const std::string& name = "AbstractWiring") 00050 : Inspectable(name), plotMode(plotMode) { 00051 rsensornumber = 0; 00052 rmotornumber = 0; 00053 csensornumber = 0; 00054 cmotornumber = 0; 00055 noiseGenerator = noise; 00056 noisevals=0; 00057 initialised = false; 00058 } 00059 00060 /** destructor 00061 */ 00062 virtual ~AbstractWiring(){ 00063 if(noiseGenerator) delete noiseGenerator; 00064 } 00065 00066 /** Initializes the number of sensors and motors from robot 00067 * (to be precise the internal parameters rsensornumber and rmotornumber!), 00068 * calculates the number of sensors and motors on controller side. 00069 * The internal version initIntern() is called from here and 00070 * be overloaded to calculate and provide the appropriate numbers 00071 * controllersensornumber (csensornumber), controllermotornumber (cmotornumber) 00072 * The number of noise channels (noisenumber) can also be changed. 00073 * @param randGen pointer to random generator, if not given then a new one is created 00074 * @return returns false on error, otherwise true 00075 */ 00076 virtual bool init(int robotsensornumber, int robotmotornumber, RandGen* randGen=0); 00077 00078 /** Realizes wiring from robot sensors to controller sensors. 00079 * The internal version wireSensorsIntern() is called from here and 00080 * must be overloaded in order to implement the appropriate mapping. 00081 * Noise values of the right size are then accessible via the noisevals array. 00082 * @param rsensors pointer to array of sensorvalues from robot 00083 * @param rsensornumber number of sensors from robot 00084 * @param csensors pointer to array of sensorvalues for controller 00085 * @param csensornumber number of sensors to controller 00086 * @param noiseStrength size of the noise added to the sensors 00087 * @return returns false on error, otherwise true 00088 */ 00089 virtual bool wireSensors(const sensor* rsensors, int rsensornumber, 00090 sensor* csensors, int csensornumber, 00091 double noiseStrength); 00092 00093 /** Realizes wiring from controller motor outputs to robot motors. 00094 * The internal version wireMotorsIntern() is called from here and 00095 * must be overloaded in order to implement the appropriate mapping. 00096 * @param rmotors pointer to array of motorvalues for robot 00097 * @param rmotornumber number of robot motors 00098 * @param cmotors pointer to array of motorvalues from controller 00099 * @param cmotornumber number of motorvalues from controller 00100 * @return returns false if error, else true 00101 */ 00102 virtual bool wireMotors(motor* rmotors, int rmotornumber, 00103 const motor* cmotors, int cmotornumber); 00104 00105 /** Returns the number of sensors on robot side. 00106 */ 00107 virtual int getRobotSensornumber(){return rsensornumber;} 00108 00109 /** Returns the number of motors on robot side. 00110 */ 00111 virtual int getRobotMotornumber() {return rmotornumber;} 00112 00113 /** Returns the number of sensors on controller side. 00114 */ 00115 virtual int getControllerSensornumber(){return csensornumber;} 00116 00117 /** Returns the number of motors on controller side. 00118 */ 00119 virtual int getControllerMotornumber() {return cmotornumber;} 00120 00121 /// reset internal state 00122 virtual void reset() {} 00123 00124 protected: 00125 /** to be overloaded by subclasses 00126 The rsensornumber and rmotornumber are already stored 00127 in the member variables. The random values are to be accessed 00128 via the noiseGenerator. 00129 @see init() 00130 */ 00131 virtual bool initIntern() = 0; 00132 00133 /** to be overloaded by subclasses 00134 @see wireSensors() 00135 */ 00136 virtual bool wireSensorsIntern(const sensor* rsensors, int rsensornumber, 00137 sensor* csensors, int csensornumber, 00138 double noiseStrength) = 0; 00139 00140 /** to be overloaded by subclasses 00141 @see wireMotors() 00142 */ 00143 virtual bool wireMotorsIntern(motor* rmotors, int rmotornumber, 00144 const motor* cmotors, int cmotornumber) = 0; 00145 00146 00147 00148 /// using plotTypes this variables defines what is plotted 00149 int plotMode; 00150 00151 /// for storing the noise values 00152 matrix::Matrix mNoise; 00153 sensor* noisevals; // pointer to the noisevalues stored in the matrix 00154 // size of the noise vector 00155 int noisenumber; 00156 00157 /// number of sensors at robot side 00158 int rsensornumber; 00159 /// copy of the last robot sensors 00160 matrix::Matrix mRsensors; 00161 00162 /// number of motors at robot side 00163 int rmotornumber; 00164 /// copy of the last robot motors 00165 matrix::Matrix mRmotors; 00166 00167 /// number of sensors at controller side 00168 int csensornumber; 00169 /// copy of the last controller sensors 00170 matrix::Matrix mCsensors; 00171 00172 /// number of motors at controller side 00173 int cmotornumber; 00174 /// copy of the last controller motors 00175 matrix::Matrix mCmotors; 00176 00177 /// noise generator 00178 NoiseGenerator* noiseGenerator; 00179 00180 /// random generator used in NoiseGenerator (in case it is needed by subclasses) 00181 RandGen* randGen; 00182 00183 bool initialised; 00184 }; 00185 00186 #endif