Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simulation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __SIMULATION_H
25 #define __SIMULATION_H
26 
27 // include base classes of class Simulation
28 #include "base.h"
29 
30 #include <osgViewer/Viewer>
31 #include <osgViewer/ViewerEventHandlers>
32 #include <osgGA/KeySwitchMatrixManipulator>
33 #include <osg/Camera>
34 
35 
36 #include <cmath>
37 #define PI M_PI // (3.14159265358979323846)
38 #include <vector>
39 #include <iterator>
40 #include <string>
41 
42 #include "globaldata.h"
43 #include "grabframe.h"
44 #include "pos.h"
45 #include "camerahandle.h"
46 
47 /*** some forward declarations ***/
48 class PlotOption; // selforg
49 
50 namespace lpzrobots {
51  class LPZViewer;
52 }
53 /*** end of forward declarations ***/
54 
55 
56 namespace lpzrobots {
57 
58  class Simulation : public Base, public osgGA::GUIEventHandler, public Callbackable
59  {
60  public:
61 
64 
65  Simulation();
66  virtual ~Simulation();
67 
68  /** starts the Simulation. Do not overload it.
69  This function returns of the simulation is terminated.
70  @return: true if closed regulary, false on error
71  */
72  bool run(int argc, char** argv);
73 
74  // the following function have to be overloaded.
75 
76  /// start() is called at the first start of the cycles and should create all the object (obstacles, agents...).
77  virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData) = 0;
78 
79  // the following functions have dummy default implementations
80 
81  /**
82  * restart() is called at the second and all following starts of the cylce
83  * The end of a cycle is determined by (simulation_time_reached==true)
84  * @param the odeHandle
85  * @param the osgHandle
86  * @param globalData
87  * @return if the simulation should be restarted; this is false by default
88  */
89  virtual bool restart(const OdeHandle&, const OsgHandle&, GlobalData& globalData);
90 
91  /// end() is called at the end and should tidy up
92  virtual void end(GlobalData& globalData);
93  /** config() is called when the user presses Ctrl-C
94  @return false to exit program, true otherwise
95  */
96  virtual bool config(GlobalData& globalData);
97  /** is called if a key was pressed.
98  For keycodes see: osgGA::GUIEventAdapter
99  @return true if the key was handled
100  */
101  virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData,
102  int key, bool down) { return false; };
103 
104  /** this can be used to describe the key bindings used by command()
105  */
106  virtual void bindingDescription(osg::ApplicationUsage & au) const {};
107 
108  /** this can be used to print additional usage information (cmd-line options)
109  */
110  virtual void usage() const {};
111 
112  /** collCallback() can be used to overload the standart collision handling.
113  However it is called after the robots collision handling.
114  @return true if collision is treated, false otherwise
115  */
116  virtual bool collCallback(const OdeHandle&, void* data, dGeomID o1, dGeomID o2) { return false;};
117 
118  /** optional additional callback function which is called every simulation step.
119  Called between physical simulation step and drawing.
120  @param draw indicates that objects are drawn in this timestep
121  @param pause always false (only called of simulation is running)
122  @param control indicates that robots have been controlled this timestep
123  */
124  virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control) {};
125 
126  /** adds a palette file to be loaded at initialization time
127  Call this before run()!
128  */
129  virtual void addPaletteFile(const std::string& filename, bool verbose = false);
130  /** adds a color alias file to be loaded at initialization time
131  Call this before run()!
132  */
133  virtual void addColorAliasFile(const std::string& filename, bool verbose = false);
134 
135  virtual void odeStep();
136 
137  virtual void osgStep();
138 
140 
141  protected:
142  // GUIEventHandler
143  virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
144  virtual void getUsage (osg::ApplicationUsage & au) const;
145  virtual void accept(osgGA::GUIEventHandlerVisitor& v);
146  virtual bool init(int argc, char** argv);
147 
148  virtual void updateGraphics(); ///< update the graphics objects
149 
150  /** define the home position and view orientation of the camera.
151  view.x is the heading angle in degree. view.y is the tilt angle in degree (nick),
152  view.z is ignored
153  */
154  void setCameraHomePos(const osg::Vec3& eye, const osg::Vec3& view);
155 
156  /**
157  * Sets the mode of the camera, the numbers are the same like the keys
158  * @param mode see CameraMode
159  */
160  void setCameraMode(CameraMode mode);
161 
162  /// start video recording (write frames to name(XXX) folder)
163  bool startVideoRecording(const char* name);
164  /// stop video recording
165  bool stopVideoRecording();
166 
167  /**
168  * Sets the agent to be watched with the camera.
169  * @param agent to set
170  */
171  void setWatchedAgent(OdeAgent* agent);
172 
173  /// returns the watched agent (or 0)
174  OdeAgent* getWatchedAgent() const;
175 
176  static void nearCallback_TopLevel(void *data, dGeomID o1, dGeomID o2);
177  static void nearCallback(void *data, dGeomID o1, dGeomID o2);
178  bool control_c_pressed();
179 
180  // plotoptions is a list of possible online output,
181  // if the list is empty no online gnuplot windows and no logging to file occurs.
182  // The list is modified with commandline options, see run() in simulation.cpp
183  std::list<PlotOption>& plotoptions; // points now to globaldata.plotoptions
184 
185 
186  /**
187  * shows all parameters of all given configurable objects
188  * @deprecated this is handled by simulation itself, do not call this function anymore
189  */
190  __attribute__ ((deprecated)) void showParams(const ConfigList& configs) {}
191 
192  private:
193  void insertCmdLineOption(int& argc,char**& argv);
194  bool loop();
195  /// clears obstacle and agents lists and delete entries
196  void tidyUp(GlobalData& globalData);
197 
198  protected:
199  /// returns false if the program is to exit
200  virtual bool processCmdLine(int argc, char** argv);
201  void resetSyncTimer();
202  long timeOfDayinMS();
203 
204  std::list<std::pair<std::string, double> > parseKeyValuePairs(std::string kv);
205  private:
206  static void control_c(int i);
207  static void cmd_handler_exit();
208  static void cmd_handler_init();
209  static void cmd_begin_input();
210  static void cmd_end_input();
211 
212  // Commandline interface stuff
213  static void main_usage(const char* progname);
214 
215  bool storeOdeRobotsCFG();
216 
217 
218  protected:
220  osg::ref_ptr<VideoStream> videostream;
221 
224  double truerealtimefactor; // calculated true speed
225  bool justresettimes; // true if we just reset sync times
227 
230 
231  paramint defaultFPS; // default framerate
232 
233  bool pause;
235  long int simulation_time;
238 
239  // use globalData.sim_step instead
240  //long sim_step;
241 
245 
246  /// parameters for configurables set on commandline
247  std::string initConfParams;
248 
249  char odeRobotsCfg[256]; /// < filename of config file
250 
251  // CameraType camType; // default is a non-moving and non-rotating camera
252  // OdeRobot* viewedRobot; // the robot who is viewed from the camera
253 
254  /// the current cycle; the simulation restarts if restart() returns true
256 
258 
261  parambool useQMPThreads; // decides if quick mp is used in this simulation
263 
264  std::string windowName;
265 
266  protected:
268  osg::ArgumentParser* arguments;
269  LPZViewer* viewer; // inherited from osgViewer::Viewer
270  osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
271 
272  static int ctrl_C;
273  char** orig_argv;
274 
275  std::list<std::string> paletteFiles;
276  std::list<std::string> colorAliasFiles;
278 
279 
280  // -conf detected in console arguments?
282 
283  // multiprocessoring stuff
284  pthread_t odeThread;
285  pthread_t osgThread;
288 
289  private:
290  bool commandline_param_dummy;
291 
292  };
293 
294  /** creates a new directory with the stem base, which is not yet there
295  (using subsequent numbers) and returns its name in newdir */
296  void createNewDir(const char* base, char *newdir);
297 }
298 
299 #endif
Configurable::configurableList ConfigList
Definition: globaldata.h:47
bool parambool
Definition: configurable.h:93
virtual void doOnCallBack(BackCaller *src, BackCaller::CallbackableType type=BackCaller::DEFAULT_CALLBACKABLE_TYPE) override
This method is invoked when a callback is done from the class where this class is for callback regist...
Definition: simulation.cpp:1050
parambool useOsgThread
Definition: simulation.h:260
Interface class for a class which wants to be callback on a certain action.
Definition: callbackable.h:39
osg::ref_ptr< VideoStream > videostream
Definition: simulation.h:220
virtual bool command(const OdeHandle &, const OsgHandle &, GlobalData &globalData, int key, bool down)
is called if a key was pressed.
Definition: simulation.h:101
Data structure for accessing the ODE.
Definition: odehandle.h:44
virtual void accept(osgGA::GUIEventHandlerVisitor &v)
Definition: simulation.cpp:989
virtual bool collCallback(const OdeHandle &, void *data, dGeomID o1, dGeomID o2)
collCallback() can be used to overload the standart collision handling.
Definition: simulation.h:116
paramint windowHeight
Definition: simulation.h:229
static void nearCallback(void *data, dGeomID o1, dGeomID o2)
Definition: simulation.cpp:1352
static int ctrl_C
Definition: simulation.h:272
virtual bool init(int argc, char **argv)
Definition: simulation.cpp:209
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
Definition: simulation.cpp:789
CameraMode
Definition: simulation.h:63
Definition: simulation.h:63
virtual bool processCmdLine(int argc, char **argv)
returns false if the program is to exit
Definition: simulation.cpp:1156
int guiloggerinterval
Definition: simulation.h:242
Definition: simulation.h:63
SimulationState
Definition: simulation.h:62
char odeRobotsCfg[256]
Definition: simulation.h:249
std::list< std::string > paletteFiles
Definition: simulation.h:275
char ** orig_argv
Definition: simulation.h:273
void setCameraMode(CameraMode mode)
Sets the mode of the camera, the numbers are the same like the keys.
Definition: simulation.cpp:1548
bool osgThreadCreated
Definition: simulation.h:287
bool drawContacts
Definition: simulation.h:226
virtual void osgStep()
Definition: simulation.cpp:1612
Viewer holds a single view on to a single scene that supports the rendering of offscreen RRT (render ...
Definition: lpzviewer.h:37
Data structure for accessing the OpenSceneGraph.
Definition: osghandle.h:79
SimulationState state
Definition: simulation.h:267
std::string windowName
Definition: simulation.h:264
OdeAgent * getWatchedAgent() const
returns the watched agent (or 0)
Definition: simulation.cpp:1567
virtual void usage() const
this can be used to print additional usage information (cmd-line options)
Definition: simulation.h:110
static void nearCallback_TopLevel(void *data, dGeomID o1, dGeomID o2)
Definition: simulation.cpp:1333
bool run(int argc, char **argv)
starts the Simulation.
Definition: simulation.cpp:419
paramint defaultFPS
Definition: simulation.h:231
std::list< std::string > colorAliasFiles
Definition: simulation.h:276
Class which holds all data used by CameraManipulators.
Definition: camerahandle.h:54
Definition: base.h:64
parambool useQMPThreads
Definition: simulation.h:261
static const CallbackableType DEFAULT_CALLBACKABLE_TYPE
This is the default Callbackable type.
Definition: backcaller.h:51
unsigned long CallbackableType
Definition: backcaller.h:45
Definition: simulation.h:62
virtual void end(GlobalData &globalData)
end() is called at the end and should tidy up
Definition: simulation.cpp:534
void showParams(const ConfigList &configs, FILE *f, const char *lineprefix)
Shows the values of all parameters of the given configurable objects.
Definition: cmdline.cpp:9
Vec3f Vec3
Definition: osgforwarddecl.h:42
bool control_c_pressed()
Definition: simulation.cpp:1439
void createNewDir(const char *base, char *newdir)
creates a new directory with the stem base, which is not yet there (using subsequent numbers) and ret...
Definition: simulation.cpp:1578
LPZViewer * viewer
Definition: simulation.h:269
This class contains options for the use of an external plot utility like guilogger or neuronviz or ju...
Definition: plotoption.h:66
paramint windowWidth
Definition: simulation.h:228
double truerealtimefactor
Definition: simulation.h:224
std::list< PlotOption > & plotoptions
Definition: simulation.h:183
CameraHandle cameraHandle
Definition: simulation.h:257
bool startVideoRecording(const char *name)
start video recording (write frames to name(XXX) folder)
Definition: simulation.cpp:993
Data structure holding all essential global information.
Definition: globaldata.h:57
Specialised agent for ode robots.
Definition: odeagent.h:62
bool useKeyHandler
Definition: simulation.h:237
std::string initConfParams
parameters for configurables set on commandline
Definition: simulation.h:247
parambool useOdeThread
Definition: simulation.h:259
bool verboseColorLoading
Definition: simulation.h:277
void resetSyncTimer()
Definition: simulation.cpp:1457
long realtimeoffset
Definition: simulation.h:222
Simulation()
Definition: simulation.cpp:105
osgGA::KeySwitchMatrixManipulator * keyswitchManipulator
Definition: simulation.h:270
void setCameraHomePos(const osg::Vec3 &eye, const osg::Vec3 &view)
define the home position and view orientation of the camera.
Definition: simulation.cpp:1537
void setWatchedAgent(OdeAgent *agent)
Sets the agent to be watched with the camera.
Definition: simulation.cpp:1556
Definition: simulation.h:62
bool simulation_time_reached
Definition: simulation.h:234
bool odeThreadCreated
Definition: simulation.h:286
long simtimeoffset
Definition: simulation.h:223
bool stopVideoRecording()
stop video recording
Definition: simulation.cpp:1029
int paramint
Definition: configurable.h:98
Definition: simulation.h:58
virtual void odeStep()
Definition: simulation.cpp:1593
virtual void start(const OdeHandle &, const OsgHandle &, GlobalData &globalData)=0
start() is called at the first start of the cycles and should create all the object (obstacles...
Definition: simulation.h:62
int currentCycle
< filename of config file
Definition: simulation.h:255
virtual bool config(GlobalData &globalData)
config() is called when the user presses Ctrl-C
Definition: simulation.cpp:530
Definition: simulation.h:63
std::list< std::pair< std::string, double > > parseKeyValuePairs(std::string kv)
Definition: simulation.cpp:1134
Definition: simulation.h:63
virtual void updateGraphics()
update the graphics objects
Definition: simulation.cpp:776
virtual ~Simulation()
Definition: simulation.cpp:166
virtual void addCallback(GlobalData &globalData, bool draw, bool pause, bool control)
optional additional callback function which is called every simulation step.
Definition: simulation.h:124
osg::ArgumentParser * arguments
Definition: simulation.h:268
GlobalData globalData
Definition: simulation.h:219
parambool inTaskedMode
Definition: simulation.h:262
virtual bool restart(const OdeHandle &, const OsgHandle &, GlobalData &globalData)
restart() is called at the second and all following starts of the cylce The end of a cycle is determi...
Definition: simulation.cpp:1643
bool startConfigurator
Definition: simulation.h:281
bool pause
Definition: simulation.h:233
virtual void bindingDescription(osg::ApplicationUsage &au) const
this can be used to describe the key bindings used by command()
Definition: simulation.h:106
Class prototype which provides functions to handle callbackable classes.
Definition: backcaller.h:42
pthread_t odeThread
Definition: simulation.h:284
Definition: simulation.h:62
int filelogginginterval
Definition: simulation.h:243
int matrixvizinterval
Definition: simulation.h:244
pthread_t osgThread
Definition: simulation.h:285
virtual void getUsage(osg::ApplicationUsage &au) const
Definition: simulation.cpp:971
long int simulation_time
Definition: simulation.h:235
bool noGraphics
Definition: simulation.h:236
bool justresettimes
Definition: simulation.h:225
virtual void addPaletteFile(const std::string &filename, bool verbose=false)
adds a palette file to be loaded at initialization time Call this before run()!
Definition: simulation.cpp:199
virtual void addColorAliasFile(const std::string &filename, bool verbose=false)
adds a color alias file to be loaded at initialization time Call this before run()! ...
Definition: simulation.cpp:203
long timeOfDayinMS()
Definition: simulation.cpp:1451
__attribute__((deprecated)) void showParams(const ConfigList &configs)
shows all parameters of all given configurable objects
Definition: simulation.h:190