taskedsimulation.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 _TASKEDSIMULATION_H_
00025 #define _TASKEDSIMULATION_H_
00026 
00027 #include "simulation.h"
00028 #include "simulationtaskhandle.h"
00029 #include <string>
00030 #include <selforg/quickmp.h>
00031 
00032 namespace lpzrobots {
00033 
00034   class TaskedSimulation : public Simulation {
00035     public:
00036       TaskedSimulation() :
00037         Simulation(), taskId(0), simTaskHandle(0) {
00038         useOdeThread = false;
00039         useOsgThread = false;
00040         useQMPThreads = false;
00041         inTaskedMode = true;
00042       }
00043 
00044       virtual ~TaskedSimulation() {
00045       }
00046 
00047       /// start() is called at the first start of the cycles and should create all the object (obstacles, agents...).
00048       virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData,
00049           SimulationTaskHandle& simTaskHandle, int taskId) {
00050       }
00051 
00052       /**
00053        * restart() is called at the second and all following starts of the cylce
00054        * The end of a cycle is determined by (simulation_time_reached==true)
00055        * @param the odeHandle
00056        * @param the osgHandle
00057        * @param globalData
00058        * @return if the simulation should be restarted; this is false by default
00059        */
00060       virtual bool restart(const OdeHandle&, const OsgHandle&, GlobalData& globalData, SimulationTaskHandle&,
00061           int taskId) {
00062         return false;
00063       }
00064 
00065       /** optional additional callback function which is called every simulation step.
00066        Called between physical simulation step and drawing.
00067        @param draw indicates that objects are drawn in this timestep
00068        @param pause always false (only called of simulation is running)
00069        @param control indicates that robots have been controlled this timestep
00070        */
00071       virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control, SimulationTaskHandle&,
00072           int taskId) {
00073       }
00074 
00075       /** is called if a key was pressed.
00076        For keycodes see: osgGA::GUIEventAdapter
00077        @return true if the key was handled
00078        */
00079       virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData, int key, bool down,
00080           SimulationTaskHandle&, int taskId) {
00081         return false;
00082       }
00083 
00084       /**
00085        * Sets the taskId of the associated SimulationTask.
00086        * This method is called by the associated SimulationTask.
00087        * @param taskId of the associated SimulationTask
00088        */
00089       void setTaskId(int taskId) {
00090         this->taskId = taskId;
00091         if (taskId == 0)
00092           noGraphics = false;
00093         else
00094           noGraphics = true;
00095         // inform osg relevant stuff that no graphics is used
00096         //osgHandle.cfg->noGraphics=noGraphics;
00097       }
00098 
00099       void setTaskNameSuffix(std::string nameSuffix) {
00100         windowName.append(nameSuffix);
00101       }
00102 
00103       /**
00104        * Sets the global SimulationTaskHandle. This method is
00105        * called by the associated SimulationTask.
00106        * @param simTaskHandle
00107        */
00108       void setSimTaskHandle(SimulationTaskHandle& simTaskHandle) {
00109         this->simTaskHandle = &simTaskHandle;
00110       }
00111 
00112     private:
00113       /**
00114        * Overwrite the usage of threads for ODE and OSG.
00115        * @see Simulation::processCmdLine(int arg, char** argv)
00116        */
00117       bool processCmdLine(int argc, char** argv) {
00118         bool rv = Simulation::processCmdLine(argc, argv);
00119         useOdeThread = false;
00120         useOsgThread = false;
00121         useQMPThreads = false;
00122         inTaskedMode = true;
00123         if (taskId!=0) {
00124           noGraphics = true;
00125           // inform osg relevant stuff that no graphics is used
00126           osgHandle.cfg->noGraphics=noGraphics;
00127         }
00128         return rv;
00129       }
00130 
00131       /**
00132        * Overwrite to avoid thread conflicts while
00133        * accessing the same file. Just disable it.
00134        */
00135       virtual bool storeCfg(const char* filenamestem, const std::list<std::string>& comments = std::list<std::string>()) {
00136         return true;
00137       }
00138 
00139       /**
00140        * Overwrite the usage of threads for ODE and OSG.
00141        * @see Configurable::restoreCFG(int arg, char** argv)
00142        */
00143       bool restoreCfg(const char* filenamestem) {
00144         bool result = Simulation::restoreCfg(filenamestem);
00145         useOdeThread = false;
00146         useOsgThread = false;
00147         useQMPThreads = false;
00148         inTaskedMode = true;
00149         if (taskId!=0) {
00150           noGraphics = true;
00151           // inform osg relevant stuff that no graphics is used
00152           osgHandle.cfg->noGraphics=noGraphics;
00153         }
00154         return result;
00155       }
00156 
00157       int taskId;
00158       SimulationTaskHandle* simTaskHandle;
00159       std::string nameSuffix;
00160 
00161       void start(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData) {
00162         QMP_CRITICAL(61);
00163         start(odeHandle, osgHandle, globalData, *simTaskHandle, taskId);
00164         QMP_END_CRITICAL(61);
00165       }
00166 
00167       bool restart(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData) {
00168         bool doRestart;
00169         QMP_CRITICAL(62);
00170         doRestart = restart(odeHandle, osgHandle, globalData, *simTaskHandle, taskId);
00171         QMP_END_CRITICAL(62);
00172         return doRestart;
00173       }
00174 
00175       virtual void addCallback(GlobalData& globalData, bool draw, bool pause, bool control) {
00176         QMP_CRITICAL(63);
00177         addCallback(globalData, draw, pause, control, *simTaskHandle, taskId);
00178         QMP_END_CRITICAL(63);
00179       }
00180       ;
00181 
00182       bool command(const OdeHandle& odeHandle, const OsgHandle& osgHandle, GlobalData& globalData, int key, bool down) {
00183         return command(odeHandle, osgHandle, globalData, key, down, *simTaskHandle, taskId);
00184       }
00185 
00186   };
00187 
00188 }
00189 
00190 #endif /* _TASKEDSIMULATION_H_ */
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