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 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 *************************************************************************** 00022 * * 00023 * simulation.h and simulation.cpp provide a generic ode-robot simulation * 00024 * framework. It implements the initialisation, the simulation loop, * 00025 * and a basic command line interface. * 00026 * Usage: call simulation_init(), simulation_start(), simulation_close() * 00027 * see template_onerobot/main.cpp for an example * 00028 * * 00029 * $Log: simulation.h,v $ 00030 * Revision 1.18.4.15 2006/03/30 12:34:47 martius 00031 * documentation updated 00032 * 00033 * Revision 1.18.4.14 2006/03/06 16:54:05 robot3 00034 * now ExtendedViewer is used because of the new getCurrentCameraManipulator(), 00035 * code optimizations 00036 * 00037 * Revision 1.18.4.13 2006/02/22 15:26:32 martius 00038 * frame grabbing with osg works again 00039 * 00040 * Revision 1.18.4.12 2006/02/20 10:50:20 martius 00041 * pause, random, windowsize, Ctrl-keys 00042 * 00043 * Revision 1.18.4.11 2006/02/14 17:36:14 martius 00044 * much better time syncronisation 00045 * 00046 * Revision 1.18.4.10 2006/01/17 17:01:53 martius 00047 * *** empty log message *** 00048 * 00049 * Revision 1.18.4.9 2006/01/12 22:33:23 martius 00050 * key eventhandler integrated 00051 * 00052 * Revision 1.18.4.8 2005/12/29 16:49:48 martius 00053 * end is obsolete 00054 * tidyUp is used for deletion 00055 * 00056 * Revision 1.18.4.7 2005/12/29 12:54:09 martius 00057 * multiple Tesselhints 00058 * 00059 * Revision 1.18.4.6 2005/12/15 17:02:04 martius 00060 * light is in sky and standart cams removed 00061 * config has a default implentation now 00062 * 00063 * Revision 1.18.4.5 2005/12/11 23:35:07 martius 00064 * *** empty log message *** 00065 * 00066 * Revision 1.18.4.4 2005/12/09 16:53:17 martius 00067 * camera is working now 00068 * 00069 * Revision 1.18.4.3 2005/12/06 17:38:13 martius 00070 * *** empty log message *** 00071 * 00072 * Revision 1.18.4.2 2005/12/06 10:13:23 martius 00073 * openscenegraph integration started 00074 * 00075 * Revision 1.18.4.1 2005/11/14 17:37:01 martius 00076 * changed makefile structure to have and include directory 00077 * mode to selforg 00078 * 00079 * Revision 1.18 2005/10/06 17:11:26 martius 00080 * switched to stl lists 00081 * 00082 * Revision 1.17 2005/09/27 13:58:48 martius 00083 * added drawLine 00084 * 00085 * Revision 1.16 2005/09/22 13:17:11 martius 00086 * OdeHandle and GlobalData finished 00087 * doInternalStuff included 00088 * 00089 * Revision 1.15 2005/09/22 11:21:57 martius 00090 * removed global variables 00091 * OdeHandle and GlobalData are used instead 00092 * sensor prepared 00093 * 00094 * Revision 1.14 2005/08/12 11:55:01 robot1 00095 * camera module integrated 00096 * 00097 * Revision 1.13 2005/08/03 20:33:30 martius 00098 * changed signature of contains (but it stays compatible) 00099 * 00100 * Revision 1.12 2005/07/27 13:23:16 martius 00101 * new color and position construction 00102 * 00103 * Revision 1.11 2005/07/18 08:35:27 martius 00104 * drawcallback is additionalcallback now 00105 * 00106 * Revision 1.10 2005/07/13 08:39:21 robot8 00107 * added the possibility to use an additional command function, which handels special Inputs if the ODE simulation window has the focus 00108 * 00109 * Revision 1.9 2005/07/08 10:14:05 martius 00110 * added contains (helper for stringlist search) 00111 * 00112 * Revision 1.8 2005/07/07 10:23:44 martius 00113 * added user draw callback 00114 * 00115 * Revision 1.7 2005/06/30 13:23:38 robot8 00116 * completing the call of the dynamic collisionCallback-function for standard collisions 00117 * 00118 * Revision 1.6 2005/06/29 09:27:11 martius 00119 * *** empty log message *** 00120 * 00121 * Revision 1.5 2005/06/29 09:25:06 martius 00122 * customized callback for collision 00123 * 00124 * Revision 1.4 2005/06/17 08:42:01 martius 00125 * documentation 00126 * 00127 * Revision 1.3 2005/06/15 14:01:31 martius 00128 * moved all general code from main to simulation 00129 * * 00130 ***************************************************************************/ 00131 #ifndef __SIMULATION_H 00132 #define __SIMULATION_H 00133 00134 #include <math.h> 00135 #define PI M_PI // (3.14159265358979323846) 00136 #include <osgGA/GUIEventHandler> 00137 #include <Producer/Camera> 00138 00139 #include <vector> 00140 #include <iterator> 00141 00142 #include "odeconfig.h" 00143 #include "camera.h" 00144 #include "grabframe.h" 00145 00146 #include "globaldata.h" 00147 #include "base.h" 00148 00149 #include "extendedViewer.h" 00150 00151 namespace osg{ 00152 class ArgumentParser; 00153 } 00154 00155 //namespace osgProducer{ 00156 // class Viewer; 00157 //} 00158 00159 namespace lpzrobots { 00160 00161 class Simulation : public Base, public osgGA::GUIEventHandler, public Producer::Camera::Callback { 00162 public: 00163 typedef enum SimulationState { none, initialised, running, closed }; 00164 00165 Simulation(); 00166 virtual ~Simulation(); 00167 00168 /** starts the Simulation. Do not overload it. 00169 This function returns of the simulation is terminated. 00170 @return: true if closed regulary, false on error 00171 */ 00172 bool run(int argc, char** argv); 00173 00174 // the following function have to be overloaded. 00175 00176 /// start() is called at the start and should create all the object (obstacles, agents...). 00177 virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData) = 0; 00178 00179 // the following functions have dummy default implementations 00180 00181 /// end() is called at the end and should tidy up 00182 virtual void end(GlobalData& globalData); 00183 /** config() is called when the user presses Ctrl-C. 00184 Default: Call \ref changeParams(globalData.configs) */ 00185 virtual void config(GlobalData& globalData); 00186 /** command() is called if a key was pressed 00187 keycodes see: osgGA::GUIEventAdapter 00188 return true if the key was handled 00189 */ 00190 virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData, 00191 int key, bool down) { return false; }; 00192 00193 /** this can be used to describe the key bindings used by command() 00194 */ 00195 virtual void bindingDescription(osg::ApplicationUsage & au) const {}; 00196 00197 /** collCallback() can be used to overload the standart collision handling. 00198 However it is called after the robots collision handling. 00199 @return true if collision is treated, false otherwise 00200 */ 00201 virtual bool collCallback(const OdeHandle&, void* data, dGeomID o1, dGeomID o2) { return false;}; 00202 /// addCallback() optional additional callback function. 00203 virtual void addCallback(GlobalData& globalData, bool draw, bool pause) {}; 00204 00205 ///////////////// Camera::Callback interface 00206 virtual void operator() (const Producer::Camera &); 00207 00208 protected: 00209 // GUIEventHandler 00210 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); 00211 virtual void getUsage (osg::ApplicationUsage & au) const; 00212 virtual void accept(osgGA::GUIEventHandlerVisitor& v); 00213 00214 virtual bool init(int argc, char** argv); 00215 00216 /** define the home position and view orientation of the camera. 00217 view.x is the heading angle in degree. view.y is the tilt angle in degree (nick), 00218 view.z is ignored 00219 */ 00220 void setCameraHomePos(const osg::Vec3& eye, const osg::Vec3& view); 00221 00222 static void nearCallback(void *data, dGeomID o1, dGeomID o2); 00223 bool control_c_pressed(); 00224 00225 private: 00226 void processCmdLine(int argc, char** argv); 00227 void loop(); 00228 /// clears obstacle and agents lists and delete entries 00229 void tidyUp(GlobalData& globalData); 00230 00231 void resetSyncTimer(); 00232 long timeOfDayinMS(); 00233 00234 static void control_c(int i); 00235 static void cmd_handler_exit(); 00236 static void cmd_handler_init(); 00237 static void cmd_begin_input(); 00238 static void cmd_end_input(); 00239 00240 // Commandline interface stuff 00241 static void usage(const char* progname); 00242 00243 protected: 00244 GlobalData globalData; 00245 VideoStream videostream; 00246 00247 int nextLeakAnnounce; 00248 int leakAnnCounter; 00249 long realtimeoffset; 00250 long simtimeoffset; 00251 00252 int windowWidth; 00253 int windowHeight; 00254 bool pause; 00255 00256 long sim_step; 00257 00258 // CameraType camType; // default is a non-moving and non-rotating camera 00259 // OdeRobot* viewedRobot; // the robot who is viewed from the camera 00260 00261 private: 00262 SimulationState state; 00263 osg::ArgumentParser* arguments; 00264 ExtendedViewer* viewer; 00265 Producer::Camera* cam; 00266 static int ctrl_C; 00267 }; 00268 00269 // /// initializes or resets the camera per user, if wanted 00270 // void camera_init(CameraType type, OdeRobot* robot); 00271 00272 // /// starts the simulation. 00273 // void simulation_start(int argc, char** argv); 00274 // /// call this after the @simulation_start()@ has returned to tidy up. 00275 // void simulation_close(); 00276 00277 // Helper 00278 /// returns the index+1 if the list contains the given string or 0 if not 00279 int contains(char **list, int len, const char *str); 00280 00281 // Commandline interface stuff 00282 /// shows all parameters of all given configurable objects 00283 void showParams(const ConfigList& configs); 00284 /// offers the possibility to change parameter of all configurable objects 00285 void changeParams(ConfigList& configs); 00286 00287 /// creates a new directory with the stem base, which is not yet there (using subsequent numbers) 00288 void createNewDir(const char* base, char *newdir); 00289 } 00290 00291 #endif