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