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
taskedsimulation.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 _TASKEDSIMULATION_H_
25 #define _TASKEDSIMULATION_H_
26 
27 #include "simulation.h"
28 #include "simulationtaskhandle.h"
29 #include <string>
30 #include <selforg/quickmp.h>
31 
32 namespace lpzrobots {
33 
34  class TaskedSimulation : public Simulation {
35  public:
37  Simulation(), taskId(0), simTaskHandle(0) {
38  useOdeThread = false;
39  useOsgThread = false;
40  useQMPThreads = false;
41  inTaskedMode = true;
42  }
43 
44  virtual ~TaskedSimulation() {
45  }
46 
47  /// start() is called at the first start of the cycles and should create all the object (obstacles, agents...).
48  virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData,
49  SimulationTaskHandle& simTaskHandle, int taskId) {
50  }
51 
52  /**
53  * restart() is called at the second and all following starts of the cylce
54  * The end of a cycle is determined by (simulation_time_reached==true)
55  * @param the odeHandle
56  * @param the osgHandle
57  * @param globalData
58  * @return if the simulation should be restarted; this is false by default
59  */
61  int taskId) {
62  return false;
63  }
64 
65  /** optional additional callback function which is called every simulation step.
66  Called between physical simulation step and drawing.
67  @param draw indicates that objects are drawn in this timestep
68  @param pause always false (only called of simulation is running)
69  @param control indicates that robots have been controlled this timestep
70  */
71  virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control, SimulationTaskHandle&,
72  int taskId) {
73  }
74 
75  /** is called if a key was pressed.
76  For keycodes see: osgGA::GUIEventAdapter
77  @return true if the key was handled
78  */
79  virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData, int key, bool down,
80  SimulationTaskHandle&, int taskId) {
81  return false;
82  }
83 
84  /**
85  * Sets the taskId of the associated SimulationTask.
86  * This method is called by the associated SimulationTask.
87  * @param taskId of the associated SimulationTask
88  */
89  void setTaskId(int taskId) {
90  this->taskId = taskId;
91  if (taskId == 0)
92  noGraphics = false;
93  else
94  noGraphics = true;
95  // inform osg relevant stuff that no graphics is used
96  //osgHandle.cfg->noGraphics=noGraphics;
97  }
98 
99  void setTaskNameSuffix(std::string nameSuffix) {
100  windowName.append(nameSuffix);
101  }
102 
103  /**
104  * Sets the global SimulationTaskHandle. This method is
105  * called by the associated SimulationTask.
106  * @param simTaskHandle
107  */
108  void setSimTaskHandle(SimulationTaskHandle& simTaskHandle) {
109  this->simTaskHandle = &simTaskHandle;
110  }
111 
112  private:
113  /**
114  * Overwrite the usage of threads for ODE and OSG.
115  * @see Simulation::processCmdLine(int arg, char** argv)
116  */
117  bool processCmdLine(int argc, char** argv) {
118  bool rv = Simulation::processCmdLine(argc, argv);
119  useOdeThread = false;
120  useOsgThread = false;
121  useQMPThreads = false;
122  inTaskedMode = true;
123  if (taskId!=0) {
124  noGraphics = true;
125  // inform osg relevant stuff that no graphics is used
127  }
128  return rv;
129  }
130 
131  /**
132  * Overwrite to avoid thread conflicts while
133  * accessing the same file. Just disable it.
134  */
135  virtual bool storeCfg(const char* filenamestem, const std::list<std::string>& comments = std::list<std::string>()) {
136  return true;
137  }
138 
139  /**
140  * Overwrite the usage of threads for ODE and OSG.
141  * @see Configurable::restoreCFG(int arg, char** argv)
142  */
143  bool restoreCfg(const char* filenamestem) {
144  bool result = Simulation::restoreCfg(filenamestem);
145  useOdeThread = false;
146  useOsgThread = false;
147  useQMPThreads = false;
148  inTaskedMode = true;
149  if (taskId!=0) {
150  noGraphics = true;
151  // inform osg relevant stuff that no graphics is used
153  }
154  return result;
155  }
156 
157  int taskId;
158  SimulationTaskHandle* simTaskHandle;
159  std::string nameSuffix;
160 
161  void start(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData) {
162  QMP_CRITICAL(61);
163  start(odeHandle, osgHandle, globalData, *simTaskHandle, taskId);
164  QMP_END_CRITICAL(61);
165  }
166 
167  bool restart(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData) {
168  bool doRestart;
169  QMP_CRITICAL(62);
170  doRestart = restart(odeHandle, osgHandle, globalData, *simTaskHandle, taskId);
171  QMP_END_CRITICAL(62);
172  return doRestart;
173  }
174 
175  virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control) {
176  QMP_CRITICAL(63);
177  addCallback(globalData, draw, pause, control, *simTaskHandle, taskId);
178  QMP_END_CRITICAL(63);
179  }
180  ;
181 
182  bool command(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData, int key, bool down) {
183  return command(odeHandle, osgHandle, globalData, key, down, *simTaskHandle, taskId);
184  }
185 
186  };
187 
188 }
189 
190 #endif /* _TASKEDSIMULATION_H_ */
virtual bool restoreCfg(const char *filenamestem)
restores the key values paires from the file : filenamestem.cfg
Definition: configurable.cpp:53
parambool useOsgThread
Definition: simulation.h:260
Data structure for accessing the ODE.
Definition: odehandle.h:44
void setSimTaskHandle(SimulationTaskHandle &simTaskHandle)
Sets the global SimulationTaskHandle.
Definition: taskedsimulation.h:108
bool noGraphics
Definition: osghandle.h:49
void setTaskId(int taskId)
Sets the taskId of the associated SimulationTask.
Definition: taskedsimulation.h:89
virtual bool processCmdLine(int argc, char **argv)
returns false if the program is to exit
Definition: simulation.cpp:1156
Definition: taskedsimulation.h:34
virtual void addCallback(GlobalData &globalData, bool draw, bool pause, bool control, SimulationTaskHandle &, int taskId)
optional additional callback function which is called every simulation step.
Definition: taskedsimulation.h:71
Data structure for accessing the OpenSceneGraph.
Definition: osghandle.h:79
std::string windowName
Definition: simulation.h:264
virtual void start(const OdeHandle &, const OsgHandle &, GlobalData &globalData, SimulationTaskHandle &simTaskHandle, int taskId)
start() is called at the first start of the cycles and should create all the object (obstacles...
Definition: taskedsimulation.h:48
parambool useQMPThreads
Definition: simulation.h:261
virtual bool command(const OdeHandle &, const OsgHandle &, GlobalData &globalData, int key, bool down, SimulationTaskHandle &, int taskId)
is called if a key was pressed.
Definition: taskedsimulation.h:79
Data structure holding all essential global information.
Definition: globaldata.h:57
parambool useOdeThread
Definition: simulation.h:259
struct which holds all structural data for the simulations.
Definition: simulationtaskhandle.h:37
Definition: simulation.h:58
void setTaskNameSuffix(std::string nameSuffix)
Definition: taskedsimulation.h:99
OdeHandle odeHandle
Definition: base.h:149
GlobalData globalData
Definition: simulation.h:219
parambool inTaskedMode
Definition: simulation.h:262
virtual bool restart(const OdeHandle &, const OsgHandle &, GlobalData &globalData, SimulationTaskHandle &, int taskId)
restart() is called at the second and all following starts of the cylce The end of a cycle is determi...
Definition: taskedsimulation.h:60
TaskedSimulation()
Definition: taskedsimulation.h:36
OsgHandle osgHandle
Definition: base.h:147
bool pause
Definition: simulation.h:233
#define QMP_END_CRITICAL
Defines the beginning of a critical section used for synchronization.
Definition: quickmp.h:138
OsgConfig * cfg
Definition: osghandle.h:98
bool noGraphics
Definition: simulation.h:236
virtual ~TaskedSimulation()
Definition: taskedsimulation.h:44
#define QMP_CRITICAL
Defines the beginning of a critical section used for synchronization.
Definition: quickmp.h:131