simulation.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  *    frankguettler@gmx.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  *  simulation.h and simulation.cpp provide a generic ode-robot simulation *
00025  *  framework. It implements the initialisation, the simulation loop,      * 
00026  *  and a basic command line interface.                                    *
00027  *  Usage: call simulation_init(), simulation_start(), simulation_close()  *
00028  *         see template_onerobot/main.cpp for an example                   *
00029  *                                                                         *
00030  *   $Log: simulation.h,v $
00031  *   Revision 1.21  2006/08/04 15:07:46  martius
00032  *   documentation
00033  *
00034  *   Revision 1.20  2006/07/14 15:17:33  fhesse
00035  *   start option for intended simulation time added
00036  *   -simtime [min]
00037  *
00038  *   Revision 1.18.4.19  2006/06/29 16:31:47  robot3
00039  *   includes cleared up
00040  *
00041  *   Revision 1.18.4.18  2006/06/25 16:52:23  martius
00042  *   filelogging is done with a plotoption
00043  *
00044  *   Revision 1.18.4.17  2006/05/28 22:12:03  martius
00045  *   - noshadow cmdline flag
00046  *
00047  *   Revision 1.18.4.16  2006/05/15 13:07:48  robot3
00048  *   -handling of starting guilogger moved to simulation.cpp
00049  *   -CTRL-F now toggles logging to the file (controller stuff) on/off
00050  *   -CTRL-G now restarts the GuiLogger
00051  *
00052  *   Revision 1.18.4.15  2006/03/30 12:34:47  martius
00053  *   documentation updated
00054  *
00055  *   Revision 1.18.4.14  2006/03/06 16:54:05  robot3
00056  *   now ExtendedViewer is used because of the new getCurrentCameraManipulator(),
00057  *   code optimizations
00058  *
00059  *   Revision 1.18.4.13  2006/02/22 15:26:32  martius
00060  *   frame grabbing with osg works again
00061  *
00062  *   Revision 1.18.4.12  2006/02/20 10:50:20  martius
00063  *   pause, random, windowsize, Ctrl-keys
00064  *
00065  *   Revision 1.18.4.11  2006/02/14 17:36:14  martius
00066  *   much better time syncronisation
00067  *
00068  *   Revision 1.18.4.10  2006/01/17 17:01:53  martius
00069  *   *** empty log message ***
00070  *
00071  *   Revision 1.18.4.9  2006/01/12 22:33:23  martius
00072  *   key eventhandler integrated
00073  *
00074  *   Revision 1.18.4.8  2005/12/29 16:49:48  martius
00075  *   end is obsolete
00076  *   tidyUp is used for deletion
00077  *
00078  *   Revision 1.18.4.7  2005/12/29 12:54:09  martius
00079  *   multiple Tesselhints
00080  *
00081  *   Revision 1.18.4.6  2005/12/15 17:02:04  martius
00082  *   light is in sky and standart cams removed
00083  *   config has a default implentation now
00084  *
00085  *   Revision 1.18.4.5  2005/12/11 23:35:07  martius
00086  *   *** empty log message ***
00087  *
00088  *   Revision 1.18.4.4  2005/12/09 16:53:17  martius
00089  *   camera is working now
00090  *
00091  *   Revision 1.18.4.3  2005/12/06 17:38:13  martius
00092  *   *** empty log message ***
00093  *
00094  *   Revision 1.18.4.2  2005/12/06 10:13:23  martius
00095  *   openscenegraph integration started
00096  *
00097  *   Revision 1.18.4.1  2005/11/14 17:37:01  martius
00098  *   changed makefile structure to have and include directory
00099  *   mode to selforg
00100  *
00101  *   Revision 1.18  2005/10/06 17:11:26  martius
00102  *   switched to stl lists
00103  *
00104  *   Revision 1.17  2005/09/27 13:58:48  martius
00105  *   added drawLine
00106  *
00107  *   Revision 1.16  2005/09/22 13:17:11  martius
00108  *   OdeHandle and GlobalData finished
00109  *   doInternalStuff included
00110  *
00111  *   Revision 1.15  2005/09/22 11:21:57  martius
00112  *   removed global variables
00113  *   OdeHandle and GlobalData are used instead
00114  *   sensor prepared
00115  *
00116  *   Revision 1.14  2005/08/12 11:55:01  robot1
00117  *   camera module integrated
00118  *
00119  *   Revision 1.13  2005/08/03 20:33:30  martius
00120  *   changed signature of contains (but it stays compatible)
00121  *
00122  *   Revision 1.12  2005/07/27 13:23:16  martius
00123  *   new color and position construction
00124  *
00125  *   Revision 1.11  2005/07/18 08:35:27  martius
00126  *   drawcallback is additionalcallback now
00127  *
00128  *   Revision 1.10  2005/07/13 08:39:21  robot8
00129  *   added the possibility to use an additional command function, which handels special Inputs if the ODE simulation window has the focus
00130  *
00131  *   Revision 1.9  2005/07/08 10:14:05  martius
00132  *   added contains (helper for stringlist search)
00133  *
00134  *   Revision 1.8  2005/07/07 10:23:44  martius
00135  *   added user draw callback
00136  *
00137  *   Revision 1.7  2005/06/30 13:23:38  robot8
00138  *   completing the call of the dynamic collisionCallback-function for  standard collisions
00139  *
00140  *   Revision 1.6  2005/06/29 09:27:11  martius
00141  *   *** empty log message ***
00142  *
00143  *   Revision 1.5  2005/06/29 09:25:06  martius
00144  *   customized callback for collision
00145  *
00146  *   Revision 1.4  2005/06/17 08:42:01  martius
00147  *   documentation
00148  *
00149  *   Revision 1.3  2005/06/15 14:01:31  martius
00150  *   moved all general code from main to simulation
00151  *                                                                 *
00152  ***************************************************************************/
00153 #ifndef __SIMULATION_H
00154 #define __SIMULATION_H
00155 
00156 // include base classes of class Simulation
00157 #include "base.h"
00158 #include <osgGA/GUIEventHandler>
00159 #include <Producer/Camera>
00160 
00161 #include <math.h>
00162 #define PI M_PI // (3.14159265358979323846)
00163 #include <vector>
00164 #include <iterator>
00165 
00166 #include "globaldata.h"
00167 #include "grabframe.h"
00168 
00169 /***  some forward declarations  ***/
00170 class PlotOption; // selforg
00171 
00172 namespace lpzrobots {
00173   class ExtendedViewer;
00174 }
00175 /*** end of forward declarations ***/
00176 
00177 
00178 namespace lpzrobots {
00179 
00180   class Simulation : public Base, public osgGA::GUIEventHandler, public Producer::Camera::Callback {
00181   public:
00182 
00183     typedef enum SimulationState { none, initialised, running, closed };
00184     
00185     Simulation();
00186     virtual ~Simulation();
00187 
00188     /** starts the Simulation. Do not overload it. 
00189         This function returns of the simulation is terminated.
00190         @return: true if closed regulary, false on error
00191     */
00192     bool run(int argc, char** argv);
00193   
00194     // the following function have to be overloaded.
00195 
00196     /// start() is called at the start and should create all the object (obstacles, agents...).
00197     virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData) = 0;
00198 
00199     // the following functions have dummy default implementations
00200 
00201     /// end() is called at the end and should tidy up
00202     virtual void end(GlobalData& globalData);
00203     /** config() is called when the user presses Ctrl-C */
00204     virtual void config(GlobalData& globalData);
00205     /** is called if a key was pressed. 
00206         For keycodes see: osgGA::GUIEventAdapter
00207         @return true if the key was handled
00208     */
00209     virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData, 
00210                          int key, bool down) { return false; };
00211 
00212     /** this can be used to describe the key bindings used by command()     
00213      */
00214     virtual void bindingDescription(osg::ApplicationUsage & au) const {};
00215 
00216     /** collCallback() can be used to overload the standart collision handling.
00217         However it is called after the robots collision handling.       
00218         @return true if collision is treated, false otherwise
00219     */
00220     virtual bool collCallback(const OdeHandle&, void* data, dGeomID o1, dGeomID o2) { return false;};
00221 
00222     /// addCallback()  optional additional callback function.
00223     virtual void addCallback(GlobalData& globalData, bool draw, bool pause) {};
00224 
00225     ///////////////// Camera::Callback interface
00226     virtual void operator() (const Producer::Camera &);
00227 
00228   
00229 
00230   protected:
00231     // GUIEventHandler
00232     virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
00233     virtual void getUsage (osg::ApplicationUsage & au) const;
00234     virtual void accept(osgGA::GUIEventHandlerVisitor& v);
00235 
00236     virtual bool init(int argc, char** argv);
00237 
00238     /** define the home position and view orientation of the camera.
00239         view.x is the heading angle in degree. view.y is the tilt angle in degree (nick), 
00240         view.z is ignored
00241     */
00242     void setCameraHomePos(const osg::Vec3& eye, const osg::Vec3& view);
00243 
00244     static void nearCallback(void *data, dGeomID o1, dGeomID o2);
00245     bool control_c_pressed();
00246 
00247     // plotoptions is a list of possible online output, 
00248     // if the list is empty no online gnuplot windows and no logging to file occurs.
00249     // The list is modified with commandline options, see run() in simulation.cpp
00250     std::list<PlotOption> plotoptions;
00251 
00252 
00253   private:
00254     void processCmdLine(int argc, char** argv);
00255     void loop();
00256     /// clears obstacle and agents lists and delete entries
00257     void tidyUp(GlobalData& globalData);
00258 
00259     void resetSyncTimer();
00260     long timeOfDayinMS();
00261 
00262     static void control_c(int i);
00263     static void cmd_handler_exit();
00264     static void cmd_handler_init();
00265     static void cmd_begin_input();
00266     static void cmd_end_input();
00267 
00268     // Commandline interface stuff
00269     static void usage(const char* progname);
00270 
00271   protected:
00272     GlobalData globalData;
00273     VideoStream videostream;
00274 
00275     int nextLeakAnnounce;
00276     int leakAnnCounter;
00277     long realtimeoffset;
00278     long simtimeoffset;
00279 
00280     int windowWidth;
00281     int windowHeight;
00282     bool pause;
00283     bool useShadow;
00284     bool simulation_time_reached;
00285     long int simulation_time;
00286 
00287     long sim_step;
00288 
00289     int guiloggerinterval;
00290     int filelogginginterval;
00291     int neuronvizinterval;
00292 
00293     //  CameraType camType; // default is a non-moving and non-rotating camera
00294     //  OdeRobot* viewedRobot; // the robot who is viewed from the camera
00295 
00296   private:
00297     SimulationState state;
00298     osg::ArgumentParser* arguments;
00299     ExtendedViewer* viewer;
00300     Producer::Camera* cam;
00301     static int ctrl_C;
00302   };
00303 
00304   // /// initializes or resets the camera per user, if wanted
00305   // void camera_init(CameraType type, OdeRobot* robot);
00306 
00307   // /// starts the simulation.
00308   // void simulation_start(int argc, char** argv);
00309   // /// call this after the @simulation_start()@ has returned to tidy up.
00310   // void simulation_close();
00311 
00312   // Helper
00313   /// returns the index+1 if the list contains the given string or 0 if not
00314   int contains(char **list, int len,  const char *str);
00315 
00316   // Commandline interface stuff
00317   /// shows all parameters of all given configurable objects
00318   void showParams(const ConfigList& configs);
00319   /// offers the possibility to change parameter of all configurable objects
00320   void changeParams(ConfigList& configs);
00321   
00322   /// creates a new directory with the stem base, which is not yet there (using subsequent numbers)
00323   void createNewDir(const char* base, char *newdir);
00324 }
00325 
00326 #endif

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