simulation.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 * frankguettler@gmx.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 * simulation.h and simulation.cpp provide a generic ode-robot simulation * 00025 * framework. It implements the initialisation, the simulation loop, * 00026 * and a basic command line interface. * 00027 * Usage: call simulation_init(), simulation_start(), simulation_close() * 00028 * see template_onerobot/main.cpp for an example * 00029 * * 00030 * $Log: simulation.h,v $ 00031 * Revision 1.24 2006/12/13 09:13:03 martius 00032 * agents get comments about changed parameter for logfile 00033 * 00034 * Revision 1.23 2006/12/11 18:31:34 martius 00035 * list of configurables for agents 00036 * reference counting and memleaks fixed 00037 * onlycontrol used in steps where controller is not used 00038 * 00039 * Revision 1.22 2006/09/20 15:30:40 martius 00040 * shadowsize 00041 * 00042 * Revision 1.21 2006/08/04 15:07:46 martius 00043 * documentation 00044 * 00045 * Revision 1.20 2006/07/14 15:17:33 fhesse 00046 * start option for intended simulation time added 00047 * -simtime [min] 00048 * 00049 * Revision 1.18.4.19 2006/06/29 16:31:47 robot3 00050 * includes cleared up 00051 * 00052 * Revision 1.18.4.18 2006/06/25 16:52:23 martius 00053 * filelogging is done with a plotoption 00054 * 00055 * Revision 1.18.4.17 2006/05/28 22:12:03 martius 00056 * - noshadow cmdline flag 00057 * 00058 * Revision 1.18.4.16 2006/05/15 13:07:48 robot3 00059 * -handling of starting guilogger moved to simulation.cpp 00060 * -CTRL-F now toggles logging to the file (controller stuff) on/off 00061 * -CTRL-G now restarts the GuiLogger 00062 * 00063 * Revision 1.18.4.15 2006/03/30 12:34:47 martius 00064 * documentation updated 00065 * 00066 * Revision 1.18.4.14 2006/03/06 16:54:05 robot3 00067 * now ExtendedViewer is used because of the new getCurrentCameraManipulator(), 00068 * code optimizations 00069 * 00070 * Revision 1.18.4.13 2006/02/22 15:26:32 martius 00071 * frame grabbing with osg works again 00072 * 00073 * Revision 1.18.4.12 2006/02/20 10:50:20 martius 00074 * pause, random, windowsize, Ctrl-keys 00075 * 00076 * Revision 1.18.4.11 2006/02/14 17:36:14 martius 00077 * much better time syncronisation 00078 * 00079 * Revision 1.18.4.10 2006/01/17 17:01:53 martius 00080 * *** empty log message *** 00081 * 00082 * Revision 1.18.4.9 2006/01/12 22:33:23 martius 00083 * key eventhandler integrated 00084 * 00085 * Revision 1.18.4.8 2005/12/29 16:49:48 martius 00086 * end is obsolete 00087 * tidyUp is used for deletion 00088 * 00089 * Revision 1.18.4.7 2005/12/29 12:54:09 martius 00090 * multiple Tesselhints 00091 * 00092 * Revision 1.18.4.6 2005/12/15 17:02:04 martius 00093 * light is in sky and standart cams removed 00094 * config has a default implentation now 00095 * 00096 * Revision 1.18.4.5 2005/12/11 23:35:07 martius 00097 * *** empty log message *** 00098 * 00099 * Revision 1.18.4.4 2005/12/09 16:53:17 martius 00100 * camera is working now 00101 * 00102 * Revision 1.18.4.3 2005/12/06 17:38:13 martius 00103 * *** empty log message *** 00104 * 00105 * Revision 1.18.4.2 2005/12/06 10:13:23 martius 00106 * openscenegraph integration started 00107 * 00108 * Revision 1.18.4.1 2005/11/14 17:37:01 martius 00109 * changed makefile structure to have and include directory 00110 * mode to selforg 00111 * 00112 * Revision 1.18 2005/10/06 17:11:26 martius 00113 * switched to stl lists 00114 * 00115 * Revision 1.17 2005/09/27 13:58:48 martius 00116 * added drawLine 00117 * 00118 * Revision 1.16 2005/09/22 13:17:11 martius 00119 * OdeHandle and GlobalData finished 00120 * doInternalStuff included 00121 * 00122 * Revision 1.15 2005/09/22 11:21:57 martius 00123 * removed global variables 00124 * OdeHandle and GlobalData are used instead 00125 * sensor prepared 00126 * 00127 * Revision 1.14 2005/08/12 11:55:01 robot1 00128 * camera module integrated 00129 * 00130 * Revision 1.13 2005/08/03 20:33:30 martius 00131 * changed signature of contains (but it stays compatible) 00132 * 00133 * Revision 1.12 2005/07/27 13:23:16 martius 00134 * new color and position construction 00135 * 00136 * Revision 1.11 2005/07/18 08:35:27 martius 00137 * drawcallback is additionalcallback now 00138 * 00139 * Revision 1.10 2005/07/13 08:39:21 robot8 00140 * added the possibility to use an additional command function, which handels special Inputs if the ODE simulation window has the focus 00141 * 00142 * Revision 1.9 2005/07/08 10:14:05 martius 00143 * added contains (helper for stringlist search) 00144 * 00145 * Revision 1.8 2005/07/07 10:23:44 martius 00146 * added user draw callback 00147 * 00148 * Revision 1.7 2005/06/30 13:23:38 robot8 00149 * completing the call of the dynamic collisionCallback-function for standard collisions 00150 * 00151 * Revision 1.6 2005/06/29 09:27:11 martius 00152 * *** empty log message *** 00153 * 00154 * Revision 1.5 2005/06/29 09:25:06 martius 00155 * customized callback for collision 00156 * 00157 * Revision 1.4 2005/06/17 08:42:01 martius 00158 * documentation 00159 * 00160 * Revision 1.3 2005/06/15 14:01:31 martius 00161 * moved all general code from main to simulation 00162 * * 00163 ***************************************************************************/ 00164 #ifndef __SIMULATION_H 00165 #define __SIMULATION_H 00166 00167 // include base classes of class Simulation 00168 #include "base.h" 00169 #include <osgGA/GUIEventHandler> 00170 #include <Producer/Camera> 00171 00172 #include <math.h> 00173 #define PI M_PI // (3.14159265358979323846) 00174 #include <vector> 00175 #include <iterator> 00176 00177 #include "globaldata.h" 00178 #include "grabframe.h" 00179 00180 /*** some forward declarations ***/ 00181 class PlotOption; // selforg 00182 00183 namespace lpzrobots { 00184 class ExtendedViewer; 00185 } 00186 /*** end of forward declarations ***/ 00187 00188 00189 namespace lpzrobots { 00190 00191 class Simulation : public Base, public osgGA::GUIEventHandler, public Producer::Camera::Callback { 00192 public: 00193 00194 typedef enum SimulationState { none, initialised, running, closed }; 00195 00196 Simulation(); 00197 virtual ~Simulation(); 00198 00199 /** starts the Simulation. Do not overload it. 00200 This function returns of the simulation is terminated. 00201 @return: true if closed regulary, false on error 00202 */ 00203 bool run(int argc, char** argv); 00204 00205 // the following function have to be overloaded. 00206 00207 /// start() is called at the start and should create all the object (obstacles, agents...). 00208 virtual void start(const OdeHandle&, const OsgHandle&, GlobalData& globalData) = 0; 00209 00210 // the following functions have dummy default implementations 00211 00212 /// end() is called at the end and should tidy up 00213 virtual void end(GlobalData& globalData); 00214 /** config() is called when the user presses Ctrl-C */ 00215 virtual void config(GlobalData& globalData); 00216 /** is called if a key was pressed. 00217 For keycodes see: osgGA::GUIEventAdapter 00218 @return true if the key was handled 00219 */ 00220 virtual bool command(const OdeHandle&, const OsgHandle&, GlobalData& globalData, 00221 int key, bool down) { return false; }; 00222 00223 /** this can be used to describe the key bindings used by command() 00224 */ 00225 virtual void bindingDescription(osg::ApplicationUsage & au) const {}; 00226 00227 /** collCallback() can be used to overload the standart collision handling. 00228 However it is called after the robots collision handling. 00229 @return true if collision is treated, false otherwise 00230 */ 00231 virtual bool collCallback(const OdeHandle&, void* data, dGeomID o1, dGeomID o2) { return false;}; 00232 00233 /// addCallback() optional additional callback function. 00234 virtual void addCallback(GlobalData& globalData, bool draw, bool pause) {}; 00235 00236 ///////////////// Camera::Callback interface 00237 virtual void operator() (const Producer::Camera &); 00238 00239 00240 00241 protected: 00242 // GUIEventHandler 00243 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); 00244 virtual void getUsage (osg::ApplicationUsage & au) const; 00245 virtual void accept(osgGA::GUIEventHandlerVisitor& v); 00246 00247 virtual bool init(int argc, char** argv); 00248 00249 /** define the home position and view orientation of the camera. 00250 view.x is the heading angle in degree. view.y is the tilt angle in degree (nick), 00251 view.z is ignored 00252 */ 00253 void setCameraHomePos(const osg::Vec3& eye, const osg::Vec3& view); 00254 00255 static void nearCallback(void *data, dGeomID o1, dGeomID o2); 00256 bool control_c_pressed(); 00257 00258 // plotoptions is a list of possible online output, 00259 // if the list is empty no online gnuplot windows and no logging to file occurs. 00260 // The list is modified with commandline options, see run() in simulation.cpp 00261 std::list<PlotOption> plotoptions; 00262 /// this list contains by default only the odeconfig. This list os passed to the plotoptions. 00263 std::list<const Configurable*> globalconfigurables; 00264 00265 00266 private: 00267 void processCmdLine(int argc, char** argv); 00268 void loop(); 00269 /// clears obstacle and agents lists and delete entries 00270 void tidyUp(GlobalData& globalData); 00271 00272 void resetSyncTimer(); 00273 long timeOfDayinMS(); 00274 00275 static void control_c(int i); 00276 static void cmd_handler_exit(); 00277 static void cmd_handler_init(); 00278 static void cmd_begin_input(); 00279 static void cmd_end_input(); 00280 00281 // Commandline interface stuff 00282 static void usage(const char* progname); 00283 00284 protected: 00285 GlobalData globalData; 00286 VideoStream videostream; 00287 00288 int nextLeakAnnounce; 00289 int leakAnnCounter; 00290 long realtimeoffset; 00291 long simtimeoffset; 00292 00293 int windowWidth; 00294 int windowHeight; 00295 bool pause; 00296 bool simulation_time_reached; 00297 long int simulation_time; 00298 00299 long sim_step; 00300 00301 int guiloggerinterval; 00302 int filelogginginterval; 00303 int neuronvizinterval; 00304 00305 // CameraType camType; // default is a non-moving and non-rotating camera 00306 // OdeRobot* viewedRobot; // the robot who is viewed from the camera 00307 00308 private: 00309 SimulationState state; 00310 osg::ArgumentParser* arguments; 00311 ExtendedViewer* viewer; 00312 Producer::Camera* cam; 00313 static int ctrl_C; 00314 }; 00315 00316 // /// initializes or resets the camera per user, if wanted 00317 // void camera_init(CameraType type, OdeRobot* robot); 00318 00319 // /// starts the simulation. 00320 // void simulation_start(int argc, char** argv); 00321 // /// call this after the @simulation_start()@ has returned to tidy up. 00322 // void simulation_close(); 00323 00324 // Helper 00325 /// returns the index+1 if the list contains the given string or 0 if not 00326 int contains(char **list, int len, const char *str); 00327 00328 // Commandline interface stuff 00329 /// shows all parameters of all given configurable objects 00330 void showParams(const ConfigList& configs); 00331 /// offers the possibility to change parameter of all configurable objects in globalData. 00332 // also informs agents about changes 00333 void changeParams(GlobalData& globalData); 00334 00335 /// creates a new directory with the stem base, which is not yet there (using subsequent numbers) 00336 void createNewDir(const char* base, char *newdir); 00337 } 00338 00339 #endif

Generated on Tue Jan 16 02:14:38 2007 for Robotsystem of the Robot Group Leipzig by doxygen 1.3.8