Robot Simulator of the Robotics Group for Self-Organization of Control: taskedsimulation.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages
- Examples

- Search for |

- File List
- File Members

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