AnimatLab  2
Test
RbSimulator.cpp
1 // RbSimulator.cpp: implementation of the RbSimulator class.
2 //
4 
5 #include "StdAfx.h"
6 #include <stdarg.h>
7 #include "RbMovableItem.h"
8 #include "RbBody.h"
9 #include "RbJoint.h"
10 #include "RbMotorizedJoint.h"
11 #include "RbRigidBody.h"
12 #include "RbClassFactory.h"
13 #include "RbSimulator.h"
14 
15 namespace RoboticsAnimatSim
16 {
17 
19 // Construction/Destruction
21 
22 
23 RbSimulator::RbSimulator()
24 {
26  m_lStepTimeCount = 0;
28  m_lStepTimeCount = 0;
29  m_dblTotalVortexStepTime = 0;
30  m_lStepVortexTimeCount = 0;
31 
34 }
35 
36 RbSimulator::~RbSimulator()
37 {
38 
39 try
40 {
41  m_bShuttingDown = true;
42 
43  Reset();
44 }
45 catch(...)
46 {Std_TraceMsg(0, "Caught Error in desctructor of Simulator\r\n", "", -1, false, true);}
47 }
48 
49 #pragma region AccessorMutatorOverrides
50 
51 bool RbSimulator::InSimulation() {return false;}
52 
53 #pragma endregion
54 
55 
57 {
58  return NULL;
59 }
60 
62 {
63  Simulator::Reset();
64 
67 }
68 
70 {
71  Simulator::ResetSimulation();
72 
73  m_bSimRunning = false;
74 }
75 
76 void RbSimulator::SetSimulationStabilityParams()
77 {
78 
79 }
80 
81 //Timer Methods
82 unsigned long long RbSimulator::GetTimerTick()
83 {
84  m_lLastTickTaken = osg::Timer::instance()->tick();
85  return m_lLastTickTaken;
86 }
87 
88 double RbSimulator::TimerDiff_n(unsigned long long lStart, unsigned long long lEnd)
89 {return osg::Timer::instance()->delta_n(lStart, lEnd);}
90 
91 double RbSimulator::TimerDiff_u(unsigned long long lStart, unsigned long long lEnd)
92 {return osg::Timer::instance()->delta_u(lStart, lEnd);}
93 
94 double RbSimulator::TimerDiff_m(unsigned long long lStart, unsigned long long lEnd)
95 {return osg::Timer::instance()->delta_m(lStart, lEnd);}
96 
97 double RbSimulator::TimerDiff_s(unsigned long long lStart, unsigned long long lEnd)
98 {return osg::Timer::instance()->delta_s(lStart, lEnd);}
99 
100 void RbSimulator::MicroSleep(unsigned int iMicroTime)
101 {OpenThreads::Thread::microSleep(iMicroTime);}
102 
103 void RbSimulator::WriteToConsole(std::string strMessage)
104 {
105  std::cout << strMessage << "\r\n";
106 }
107 
108 //This function initializes the robotics related
109 //classes and the vortex viewer.
110 void RbSimulator::InitializeRobotics(int argc, const char **argv)
111 {
112 
113  //int iObjectCount = 100 + m_iPhysicsBodyCount;
114  //int iCollisionCount = iObjectCount*40;
115 
116  // m_lpCollisionConfiguration = new btDefaultCollisionConfiguration();
117  // m_lpDispatcher = new RbAnimatCollisionDispatcher(m_lpCollisionConfiguration, this);
118  // m_lpSolver = new btSequentialImpulseConstraintSolver;
119 
120  // btVector3 worldAabbMin( -10000, -10000, -10000 );
121  // btVector3 worldAabbMax( 10000, 10000, 10000 );
122  // m_lpBroadPhase = new btAxisSweep3( worldAabbMin, worldAabbMax, 1000 );
123 
124  // m_lpDynamicsWorld = new btDiscreteDynamicsWorld( m_lpDispatcher, m_lpBroadPhase, m_lpSolver, m_lpCollisionConfiguration );
125  // m_lpDynamicsWorld->setGravity( btVector3( 0, m_fltGravity, 0 ) );
126 
127  //m_lpDynamicsWorld->setInternalTickCallback(ProcessTickCallback, static_cast<void *>(this));
128 
129  // if(m_bDrawDebug)
130  // {
131  // this->OSGRoot()->addChild(m_dbgDraw.getSceneGraph());
132  // m_lpDynamicsWorld->setDebugDrawer( &m_dbgDraw );
133  // }
134 
136  // gContactProcessedCallback = &AnimatContactCallback;
137 }
138 
139 
140 void RbSimulator::Initialize(int argc, const char **argv)
141 {
142  InitializeRobotics(argc, argv);
143 
145 
146  m_oDataChartMgr.Initialize();
147  m_oExternalStimuliMgr.Initialize();
148  if(m_lpSimRecorder) m_lpSimRecorder->Initialize();
149 }
150 
152 {
154  //if(m_lTimeSlice > 10 && m_lTimeSlice < 5000 && !m_timePeriod.TimerStarted())
155  // m_timePeriod.StartTimer();
156 
157 
158  try
159  {
160  //step the frame and update the windows
161  if (!m_bPaused)
162  {
163  Simulator::StepSimulation();
164 
165  unsigned long long lStart = GetTimerTick();
166 
167  // if( m_bDrawDebug )
168  // m_dbgDraw.BeginDraw();
169 
170  // m_lpDynamicsWorld->stepSimulation(m_fltPhysicsTimeStep, m_iPhysicsSubsteps, m_fltPhysicsSubstepTime);
171 
172  // if( m_bDrawDebug )
173  // {
174  // m_lpDynamicsWorld->debugDrawWorld();
175  // m_dbgDraw.EndDraw();
176  // }
177 
178  double dblVal = TimerDiff_s(lStart, GetTimerTick());
179  m_fltPhysicsStepTime += dblVal;
180 
181  if(m_lTimeSlice > 10 && m_lTimeSlice < 5000)
182  {
183  m_dblTotalVortexStepTime += dblVal;
184  m_lStepVortexTimeCount++;
185  }
186  else if(m_lTimeSlice == 5000)
187  {
188  double dblAvgStepTime = m_dblTotalVortexStepTime/m_lStepVortexTimeCount;
189  //osg::notify(osg::NOTICE) << "Average step time: " << dblAvgStepTime << std::endl;
190  //osg::notify(osg::NOTICE) << "Total vortex step time: " << m_dblTotalVortexStepTime << std::endl;
191  //osg::notify(osg::NOTICE) << "Slice Time: " << m_lTimeSlice << std::endl;
192  //osg::notify(osg::NOTICE) << "Sim Time: " << Time() << std::endl;
193  }
194  }
195 
196  //PauseSimulation();
197 
198  }
199  catch(CStdErrorInfo oError)
200  {
201  std::string strError = "An error occurred while step the simulation.\nError: " + oError.m_strError;
202  HandleNonCriticalError(strError);
203 
204  //For the robotics system there are no non-critical errors. If we get an error while running the simulation
205  //we need to exit immediately.
207  }
208 
209 
210  //double dblVal2 = m_timeSimulationStep.StopTimer();
211  //if(m_lTimeSlice > 10 && m_lTimeSlice < 5000)
212  //{
213  // m_dblTotalStepTime += dblVal2;
214  // m_lStepTimeCount++;
215  //}
216  //else if(m_lTimeSlice == 5000)
217  //{
218  // double dblAvgStepTime = m_dblTotalStepTime/m_lStepTimeCount;
219  // cout << "Average step time: " << dblAvgStepTime << std::endl;
220  // cout << "Total step time: " << m_dblTotalStepTime << ", " << m_lStepTimeCount << std::endl;
221  // cout << "Period time: " << m_timePeriod.StopTimer() << std::endl;
222  // cout << "Slice Time: " << m_lTimeSlice << std::endl;
223  // cout << "Sim Time: " << Time() << std::endl;
224  //}
225 
226 }
227 
229 {
230  SimStopping();
231  m_bForceSimulationStop = true;
232 }
233 
235 {
236  SimPausing();
237  m_bPaused = true;
238  return true;
239 }
240 
242 {
243  m_lStartSimTick = GetTimerTick();
244 
245  SimStarting();
246  m_bSimRunning = true;
247  m_bPaused = false;
248  return true;
249 }
250 
252 {
253  if(m_bPaused)
254  SimStarting();
255  else
256  SimPausing();
257 
258  m_bPaused = !m_bPaused;
259 }
260 
262 {
263  SimStopping();
264  if(!m_bPaused)
266  m_bSimRunning = false;
267 }
268 
270 {
271  Reset();
272 }
273 
274 } //RoboticsAnimatSim
bool m_bPaused
true if the simulation is paused.
Definition: Simulator.h:143
float m_fltPhysicsStepTime
This is the time pers step for the physics engine.
Definition: Simulator.h:381
ExternalStimuliMgr m_oExternalStimuliMgr
Manager for external stimuli.
Definition: Simulator.h:103
virtual void ResetSimulation()
Resets the the simulation to its orginal settings at time 0.
Definition: RbSimulator.cpp:69
virtual void ShutdownSimulation()
Shuts down the simulation.
virtual AnimatSim::Recording::SimulationRecorder * CreateSimulationRecorder()
Creates the simulation recorder.
Definition: RbSimulator.cpp:56
virtual void StepSimulation()
Steps the entire simulation forward by one physics integration time step.
virtual void SimulateEnd()
Called at the end of the Simulate method.
virtual void InitializeStructures()
Initializes all of the structures of this simulation.
Definition: Simulator.cpp:1928
bool m_bShuttingDown
True if the simulation is shutting down. This is used by other objects in their destructor to know wh...
Definition: Simulator.h:158
bool m_bForceSimulationStop
Set to true to stop the simulation. This is a more forceful way of stopping the sim.
Definition: Simulator.h:137
long m_lTimeSlice
The current time slice. This a long value.
Definition: Simulator.h:251
virtual bool PauseSimulation()
Pauses the simulation.
virtual void ToggleSimulation()
Toggles the simulation between running and paused.
bool m_bSimRunning
true if the simulation is running
Definition: Simulator.h:149
unsigned long long m_lLastTickTaken
This is the last tick taken by a GetTickCount. It is used in debugging.
Definition: Simulator.h:378
DataChartMgr m_oDataChartMgr
Manager for data charts.
Definition: Simulator.h:100
SimulationRecorder * m_lpSimRecorder
The pointer to a simulation recorder.
Definition: Simulator.h:106
virtual bool InSimulation()
Used to determine if we are running in a simulation, or in a real control mode.
Definition: RbSimulator.cpp:51
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
long m_lStepTimeCount
This is a temp variable for measuring sim time for a set period for each simulation.
Definition: Simulator.h:420
unsigned long long m_lStartSimTick
The tick count for when the simulation first begins running.
Definition: Simulator.h:267
virtual void Initialize()
Initializes this object with no argc/argv params.
Definition: Simulator.cpp:5304
virtual void SimPausing()
Simulation pausing event.
Definition: Simulator.cpp:2739
double m_dblTotalStepTime
This is a temp variable for measuring sim time for a set period for each simulation.
Definition: Simulator.h:417
virtual void StopSimulation()
Stops the simulation and resets it.
virtual void SimStopping()
Simulation stopping event.
Definition: Simulator.cpp:2760
virtual bool StartSimulation()
Starts the simulation.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void SimStarting()
Simulation starting event.
Definition: Simulator.cpp:2718
virtual void Reset()
Resets all objects of the simulation to their unloaded state.
Definition: RbSimulator.cpp:61
IStdClassFactory * m_lpAnimatClassFactory
Definition: Simulator.h:78