agent.h

Go to the documentation of this file.
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: agent.h,v $
00023  *   Revision 1.4  2006/08/04 15:16:13  martius
00024  *   documentation
00025  *
00026  *   Revision 1.3  2006/08/02 09:35:09  martius
00027  *   LastPlot as a dummy option added
00028  *
00029  *   Revision 1.2  2006/07/14 12:23:57  martius
00030  *   selforg becomes HEAD
00031  *
00032  *   Revision 1.1.2.7  2006/06/25 16:51:35  martius
00033  *   configureable has name and revision
00034  *   a robot is configureable by default
00035  *
00036  *   Revision 1.1.2.6  2006/05/23 21:13:08  martius
00037  *   - add virtual destructor
00038  *
00039  *   Revision 1.1.2.5  2006/05/15 13:08:34  robot3
00040  *   -handling of starting guilogger moved to simulation.cpp
00041  *   -CTRL-F now toggles logging to the file (controller stuff) on/off
00042  *   -CTRL-G now restarts the GuiLogger
00043  *
00044  *   Revision 1.1.2.4  2006/03/30 12:33:53  fhesse
00045  *   trackrobot now protected to give OdeAgent access
00046  *
00047  *   Revision 1.1.2.3  2006/01/31 16:45:18  martius
00048  *   neuronviz plotoption
00049  *
00050  *   Revision 1.1.2.2  2005/11/15 12:30:26  martius
00051  *   new selforg structure and OdeAgent, OdeRobot ...
00052  *
00053  *   Revision 1.1.2.1  2005/11/14 17:37:56  martius
00054  *   moved to selforg
00055  *
00056  *   Revision 1.9  2005/11/07 17:03:30  martius
00057  *   class PlotOption added
00058  *   agent can be constructed with a list of PlotOptions
00059  *   tracking file gets controller parameters as well
00060  *
00061  *   Revision 1.8  2005/10/24 13:32:07  fhesse
00062  *   comments adjusted and in doxygen style
00063  *
00064  *   Revision 1.7  2005/09/22 12:24:36  martius
00065  *   removed global variables
00066  *   OdeHandle and GlobalData are used instead
00067  *   sensor prepared
00068  *
00069  *   Revision 1.6  2005/08/03 20:34:58  martius
00070  *   use if Inspectable interface
00071  *
00072  *   Revision 1.5  2005/07/26 17:01:47  martius
00073  *   flushing every 10
00074  *   guilogger is opened with nice -2
00075  *
00076  *   Revision 1.4  2005/07/18 10:13:46  martius
00077  *   noise moved to wiring
00078  *
00079  *   Revision 1.3  2005/07/14 15:57:53  fhesse
00080  *   now agent contains controller, robot and wiring, plotting ability included, therefore plotagent can be removed; ono2onewiring replaces one2oneagent
00081  *
00082  *   Revision 1.2  2005/06/15 14:02:47  martius
00083  *   revised and basicly tested
00084  *                                                                 *
00085  ***************************************************************************/
00086 #ifndef __AGENT_H
00087 #define __AGENT_H
00088 
00089 #include <stdio.h>
00090 #include <list>
00091 #include <utility>
00092 #include <string>
00093 
00094 
00095 class AbstractRobot;
00096 class AbstractController;
00097 class AbstractWiring;
00098 
00099 #include "types.h"
00100 #include "trackrobots.h"
00101 
00102 class Agent;
00103 
00104 /** Output mode for agent.
00105  */
00106 enum PlotMode {  
00107   /// dummy (does nothing) is there for compatibility, might be removed later
00108   NoPlot, 
00109   /// write into file
00110   File, 
00111   /// plotting with guilogger (gnuplot)
00112   GuiLogger, 
00113   /// plotting with guiscreen (gnuplot) in file logging mode
00114   GuiLogger_File,
00115   /// net visualiser
00116   NeuronViz,
00117   /// dummy used for upper bound of plotmode type
00118   LastPlot
00119 };
00120 
00121 /** Output either sensors from robot or from controller 
00122     (there can be a difference depending on the used wiring)
00123  */
00124 enum PlotSensors {Robot, Controller};
00125 
00126 /** This class contains options for the use of an external plot utility like guilogger or neuronviz
00127     or just simply file output
00128  */
00129 class PlotOption {
00130 public:
00131   friend class Agent;
00132   
00133   PlotOption(){ mode=NoPlot; whichSensors=Controller; interval=1; pipe=0; }
00134   PlotOption( PlotMode mode, PlotSensors whichSensors = Controller, int interval = 1)
00135     : mode(mode), whichSensors(whichSensors), interval(interval) {  pipe=0;}
00136 
00137   virtual ~PlotOption(){}
00138 
00139   /// nice predicate function for finding by mode
00140   struct matchMode : public std::unary_function<const PlotOption&, bool> {
00141     matchMode(PlotMode mode) : mode(mode) {}
00142     int mode;
00143     bool operator()(const PlotOption& m) { return m.mode == mode; }
00144   };
00145 
00146 private:
00147  
00148   bool open(); //< opens the connections to the plot tool 
00149   void close();//< closes the connections to the plot tool
00150 
00151   FILE* pipe;
00152   long t;
00153   std::string name;
00154 
00155   PlotMode mode;
00156   PlotSensors whichSensors;
00157   int interval;  
00158 };
00159 
00160 
00161 /** The Agent contains a controller, a robot and a wiring, which connects robot and controller.
00162     Additionally there are some ways to keep track of internal information.
00163     You have the possibility to keep track of sensor values, 
00164      motor values and internal parameters of the controller with PlotOptions. 
00165     The name PlotOptions is a bit missleaded, it should be "OutputOptions", 
00166      however you can write the data into a file or send it to visialisation tools like
00167      guilogger or neuronviz.
00168 
00169     If want to log the position, speed and orienation of your robot you can use setTrackOptions().
00170  */
00171 class Agent {
00172 public:
00173   /** constructor. PlotOption an output setting.
00174    */
00175   Agent(const PlotOption& plotOption);
00176   /** constructor. A list of PlotOption can begin. A PlotOption is an output setting.
00177    */
00178   Agent(const std::list<PlotOption>& plotOptions);
00179 
00180   /** destructor
00181    */
00182   virtual ~Agent();  
00183 
00184   /** initializes the object with the given controller, robot and wiring
00185       and initializes the output options
00186   */
00187   virtual bool init(AbstractController* controller, AbstractRobot* robot, AbstractWiring* wiring);
00188 
00189 
00190   /** Performs an step of the agent, including sensor reading, pushing sensor values through wiring, 
00191       controller step, pushing controller outputs (= motorcommands) back through wiring and sent 
00192       resulting motorcommands to robot.
00193       @param noise Noise strength.
00194   */
00195   virtual void step(double noise);
00196 
00197   /** Returns a pointer to the controller.
00198    */
00199   virtual AbstractController* getController() { return controller;}
00200 
00201   /** Returns a pointer to the robot.
00202    */
00203   virtual AbstractRobot* getRobot() { return robot;}
00204 
00205   /** Returns a pointer to the wiring.
00206    */
00207   virtual AbstractWiring* getWiring() { return wiring;}
00208 
00209   /// sets the trackoptions which enable tracking of a robot
00210   virtual void setTrackOptions(const TrackRobot& trackrobot);
00211 
00212 
00213   /** adds the PlotOptions to the list of plotoptions
00214       If a plotoption with the same Mode exists, then the old one is deleted first
00215    */
00216   virtual void addPlotOption(const PlotOption& plotoption);
00217 
00218   /** removes the PlotOptions with the given type 
00219       @return true if sucessful, false otherwise
00220    */
00221   virtual bool removePlotOption(PlotMode mode);
00222   
00223 protected:
00224 
00225   /**
00226    * Plots controller sensor- and motorvalues and internal controller parameters.
00227    * @param rx actual sensorvalues from robot (used for generation of motorcommand in actual timestep)
00228    * @param rsensornumber length of rx   
00229    * @param cx actual sensorvalues which are passed to controller (used for generation of motorcommand in actual timestep)
00230    * @param csensornumber length of cx   
00231    * @param y actual motorcommand (generated in the actual timestep)
00232    * @param motornumber length of y 
00233    */
00234   virtual void plot(const sensor* rx, int rsensornumber, const sensor* cx, int csensornumber, 
00235                     const motor* y, int motornumber);
00236   
00237 
00238   AbstractController* controller;
00239   AbstractRobot* robot;
00240   AbstractWiring* wiring;
00241 
00242   /// number of sensors of robot
00243   int rsensornumber;
00244   /// number of motors of robot
00245   int rmotornumber;
00246   /// number of sensors of comntroller
00247   int csensornumber;
00248   /// number of motors of comntroller
00249   int cmotornumber;
00250 
00251   sensor *rsensors;
00252   motor  *rmotors;
00253   sensor *csensors;
00254   motor  *cmotors;
00255 
00256   void internInit();
00257 
00258  protected:
00259   TrackRobot trackrobot;
00260 
00261  private:
00262   std::list<PlotOption> plotOptions;
00263 
00264   int t;
00265 
00266 };
00267 
00268 #endif

Generated on Mon Aug 7 16:40:14 2006 for Robotsystem of the Robot Group Leipzig by  doxygen 1.4.7