Robot Simulator of the Robotics Group for Self-Organization of Control: braitenberg.h Source File

# braitenberg.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 __BRAITENBERG_H
    00025 #define __BRAITENBERG_H
    00026
    00027
    00028 #include <stdio.h>
    00029 #include <selforg/abstractcontroller.h>
    00030
    00031 /**
    00032  * simple braitenberg controler type 2 a and b (Aggressive,Cowardly)
    00033  *
    00034  * assumes a linecamera (see LineImgProc), left and right sensor are specified in the constructor
    00035  */
    00036 class Braitenberg : public AbstractController {
    00037 public:
    00038   enum Type {Aggressive, Cowardly};
    00039
    00040   /**
    00041      @param type Braitenberg type
    00042      @param leftsensor index of left light sensor
    00043      @param rightsensor index of right light sensor
    00044      @param leftmotor index of motor of left wheel
    00045      @param rightmotor index of motor of right wheel
    00046    */
    00047   Braitenberg(Type type, int leftsensor, int rightsensor,
    00048               int leftmotor=0, int rightmotor=1)
    00049     : AbstractController("Braitenberg", "0.1"), type(type),
    00050       leftsensor(leftsensor), rightsensor(rightsensor),
    00051       leftmotor(leftmotor), rightmotor(rightmotor) {
    00052
    00053     addParameterDef("strength", &strength,0.6);
    00054     addParameterDef("offset", &offset,0.0);
    00055   }
    00056
    00057   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0){
    00058     number_sensors=sensornumber;
    00059     number_motors=motornumber;
    00060     assert(sensornumber>=2 && sensornumber>=leftsensor && sensornumber>=rightsensor);
    00061     assert(motornumber>=2  && motornumber>=leftmotor   && motornumber>=rightmotor);
    00062   }
    00063
    00064   virtual int getSensorNumber() const {return number_sensors;}
    00065
    00066   virtual int getMotorNumber() const {return number_motors;}
    00067
    00068   virtual void step(const sensor* sensors, int sensornumber,
    00069                     motor* motors, int motornumber) {
    00070     stepNoLearning(sensors, sensornumber, motors , motornumber);
    00071   }
    00072
    00073   virtual void stepNoLearning(const sensor* sensors, int sensornumber,
    00074                               motor* motors, int motornumber){
    00075     switch(type){
    00076     case Aggressive:
    00077       motors[leftmotor] = sensors[rightsensor] * strength + offset;
    00078       motors[rightmotor] = sensors[leftsensor] * strength + offset;
    00079       break;
    00080     case Cowardly:
    00081       motors[leftmotor] = sensors[leftsensor] * strength + offset;
    00082       motors[rightmotor] = sensors[rightsensor] * strength + offset;
    00083       break;
    00084     }
    00085   }
    00086
    00087
    00088   /********* STORABLE INTERFACE ******/
    00089   /// @see Storable
    00090   virtual bool store(FILE* f) const {
    00091     Configurable::print(f,"");
    00092     return true;
    00093   }
    00094
    00095   /// @see Storable
    00096   virtual bool restore(FILE* f) {
    00097     Configurable::parse(f);
    00098     return true;
    00099   }
    00100
    00101 protected:
    00102
    00103   Type type;
    00104   int leftsensor;
    00105   int rightsensor;
    00106   int leftmotor;
    00107   int rightmotor;
    00108   int number_sensors;
    00109   int number_motors;
    00110   paramval strength;
    00111   paramval offset;
    00112 };
    00113
    00114 #endif