simulation.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 __SIMULATION_H
00025 #define __SIMULATION_H
00026 
00027 // include base classes of class Simulation
00028 #include "base.h"
00029 
00030 #include <osgViewer/Viewer>
00031 #include <osgViewer/ViewerEventHandlers>
00032 #include <osgGA/KeySwitchMatrixManipulator>
00033 #include <osg/Camera>
00034 
00035 
00036 #include <cmath>
00037 #define PI M_PI // (3.14159265358979323846)
00038 #include <vector>
00039 #include <iterator>
00040 #include <string>
00041 
00042 #include "globaldata.h"
00043 #include "grabframe.h"
00044 #include "pos.h"
00045 #include "camerahandle.h"
00046 
00047 /***  some forward declarations  ***/
00048 class PlotOption; // selforg
00049 
00050 namespace lpzrobots {
00051   class LPZViewer;
00052 }
00053 /*** end of forward declarations ***/
00054 
00055 
00056 namespace lpzrobots {
00057 
00058   class Simulation : public Base, public osgGA::GUIEventHandler
00059   {
00060   public:
00061 
00062     enum SimulationState { none, initialised, running, closed };
00063     enum CameraMode {Static=0, Follow, TV, Race};
00064 
00065     Simulation();
00066     virtual ~Simulation();
00067 
00068     /** starts the Simulation. Do not overload it.
00069         This function returns of the simulation is terminated.
00070         @return: true if closed regulary, false on error
00071     */
00072     bool run(int argc, char** argv);
00073 
00074     // the following function have to be overloaded.
00075 
00076     /// start() is called at the first start of the cycles and should create all the object (obstacles, agents...).
00077     virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData) = 0;    
00078 
00079     // the following functions have dummy default implementations
00080 
00081     /**
00082      * restart() is called at the second and all following starts of the cylce
00083      * The end of a cycle is determined by (simulation_time_reached==true)
00084      * @param the odeHandle
00085      * @param the osgHandle
00086      * @param globalData
00087      * @return if the simulation should be restarted; this is false by default
00088      */
00089     virtual bool restart(const OdeHandle&, const OsgHandle&, GlobalData& globalData);
00090 
00091     /// end() is called at the end and should tidy up
00092     virtual void end(GlobalData& globalData);
00093     /** config() is called when the user presses Ctrl-C
00094         @return false to exit program, true otherwise
00095     */
00096     virtual bool config(GlobalData& globalData);
00097     /** is called if a key was pressed.
00098         For keycodes see: osgGA::GUIEventAdapter
00099         @return true if the key was handled
00100     */
00101     virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData,
00102                          int key, bool down) { return false; };
00103 
00104     /** this can be used to describe the key bindings used by command()
00105      */
00106     virtual void bindingDescription(osg::ApplicationUsage & au) const {};
00107 
00108     /** this can be used to print additional usage information (cmd-line options)
00109      */
00110     virtual void usage() const {};
00111 
00112     /** collCallback() can be used to overload the standart collision handling.
00113         However it is called after the robots collision handling.
00114         @return true if collision is treated, false otherwise
00115     */
00116     virtual bool collCallback(const OdeHandle&, void* data, dGeomID o1, dGeomID o2) { return false;};
00117 
00118     /** optional additional callback function which is called every simulation step.
00119         Called between physical simulation step and drawing.
00120         @param draw indicates that objects are drawn in this timestep
00121         @param pause always false (only called of simulation is running)
00122         @param control indicates that robots have been controlled this timestep
00123      */
00124     virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control) {};
00125 
00126     /** adds a palette file to be loaded at initialization time
00127         Call this before run()!
00128      */
00129     virtual void addPaletteFile(const std::string& filename, bool verbose = false);
00130     /** adds a color alias file to be loaded at initialization time
00131         Call this before run()!                
00132      */
00133     virtual void addColorAliasFile(const std::string& filename, bool verbose = false);
00134 
00135     virtual void odeStep();
00136 
00137     virtual void osgStep();
00138 
00139   protected:    
00140     // GUIEventHandler
00141     virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
00142     virtual void getUsage (osg::ApplicationUsage & au) const;
00143     virtual void accept(osgGA::GUIEventHandlerVisitor& v);
00144     virtual bool init(int argc, char** argv);
00145 
00146     virtual void updateGraphics(); ///< update the graphics objects
00147 
00148     /** define the home position and view orientation of the camera.
00149         view.x is the heading angle in degree. view.y is the tilt angle in degree (nick),
00150         view.z is ignored
00151     */
00152     void setCameraHomePos(const osg::Vec3& eye, const osg::Vec3& view);
00153 
00154     /**
00155      * Sets the mode of the camera, the numbers are the same like the keys
00156      * @param mode see CameraMode 
00157      */
00158     void setCameraMode(CameraMode mode);
00159 
00160     /**
00161      * Sets the agent to be watched with the camera.
00162      * @param agent to set
00163      */
00164     void setWatchedAgent(OdeAgent* agent);
00165 
00166     static void nearCallback_TopLevel(void *data, dGeomID o1, dGeomID o2);
00167     static void nearCallback(void *data, dGeomID o1, dGeomID o2);
00168     bool control_c_pressed();
00169 
00170     // plotoptions is a list of possible online output,
00171     // if the list is empty no online gnuplot windows and no logging to file occurs.
00172     // The list is modified with commandline options, see run() in simulation.cpp
00173     std::list<PlotOption>& plotoptions; // points now to globaldata.plotoptions
00174 
00175 
00176     /**
00177      *  shows all parameters of all given configurable objects
00178      *  @deprecated this is handled by simulation itself, do not call this function anymore
00179      */
00180     __attribute__ ((deprecated)) void showParams(const ConfigList& configs) {}
00181 
00182   private:
00183     void insertCmdLineOption(int& argc,char**& argv);
00184     bool loop();
00185     /// clears obstacle and agents lists and delete entries
00186     void tidyUp(GlobalData& globalData);
00187 
00188   protected:
00189     /// returns false if the program is to exit
00190     virtual bool processCmdLine(int argc, char** argv); 
00191     void resetSyncTimer();
00192     long timeOfDayinMS();
00193 
00194   private:
00195     static void control_c(int i);
00196     static void cmd_handler_exit();
00197     static void cmd_handler_init();
00198     static void cmd_begin_input();
00199     static void cmd_end_input();
00200 
00201     // Commandline interface stuff
00202     static void main_usage(const char* progname);
00203 
00204     bool storeOdeRobotsCFG();
00205 
00206 
00207   protected:
00208     GlobalData globalData;
00209     osg::ref_ptr<VideoStream> videostream;
00210 
00211     int nextLeakAnnounce;
00212     int leakAnnCounter;
00213     long realtimeoffset;
00214     long simtimeoffset;
00215     double truerealtimefactor; // calculated true speed
00216     bool justresettimes;      // true if we just reset sync times
00217 
00218     paramint windowWidth;
00219     paramint windowHeight;
00220 
00221     paramint defaultFPS;      // default framerate
00222 
00223     bool pause;
00224     bool simulation_time_reached;
00225     long int simulation_time;
00226     bool noGraphics;
00227     bool useKeyHandler;
00228 
00229     // use globalData.sim_step instead
00230      //long sim_step;
00231 
00232     int guiloggerinterval;
00233     int filelogginginterval;
00234     int matrixvizinterval;
00235 
00236     char odeRobotsCfg[256]; /// < filename of config file
00237 
00238     //  CameraType camType; // default is a non-moving and non-rotating camera
00239     //  OdeRobot* viewedRobot; // the robot who is viewed from the camera
00240 
00241     /// the current cycle; the simulation restarts if restart() returns true
00242     int currentCycle;
00243 
00244     CameraHandle cameraHandle;
00245 
00246     parambool useOdeThread;
00247     parambool useOsgThread;
00248     parambool useQMPThreads; // decides if quick mp is used in this simulation
00249     parambool inTaskedMode;
00250     
00251     std::string windowName;
00252 
00253   protected:
00254     SimulationState state;
00255     osg::ArgumentParser* arguments;
00256     LPZViewer* viewer; // inherited from osgViewer::Viewer
00257     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
00258 
00259     static int ctrl_C;
00260     char** orig_argv;
00261 
00262     std::list<std::string> paletteFiles;
00263     std::list<std::string> colorAliasFiles;
00264     bool verboseColorLoading;
00265 
00266 
00267     // -conf detected in console arguments?
00268     bool startConfigurator;
00269 
00270     // multiprocessoring stuff
00271     pthread_t odeThread;
00272     pthread_t osgThread;
00273     bool odeThreadCreated;
00274     bool osgThreadCreated;
00275 
00276   };
00277 
00278   /** creates a new directory with the stem base, which is not yet there 
00279       (using subsequent numbers)  and returns its name in newdir */
00280   void createNewDir(const char* base, char *newdir);
00281 }
00282 
00283 #endif
Generated on Thu Jun 28 14:45:37 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3