2 #include "IMovableItemCallback.h"
3 #include "ISimGUICallback.h"
4 #include "IMotorizedJoint.h"
9 #include "IPhysicsMovableItem.h"
10 #include "IPhysicsBody.h"
12 #include "MovableItem.h"
34 #include "SimulationThread.h"
35 #include "SimulationMgr.h"
39 #include "RobotPartInterface.h"
44 SimulationThread::SimulationThread(
void)
47 m_bThreadProcessing =
false;
48 m_bNeedToStop =
false;
51 SimulationThread::~SimulationThread(
void)
65 {
Std_TraceMsg(0,
"Caught Error in desctructor of SimulationThread\r\n",
"", -1,
false,
true);}
68 bool SimulationThread::NeedToStopSim() {
return m_bNeedToStop;}
70 Simulator *SimulationThread::Sim() {
return m_lpSim;}
72 void SimulationThread::StartSimulation(std::string strSimFile,
bool bForceNoWindows)
84 m_SimThread = boost::thread(&SimulationThread::ProcessSimulation,
this);
88 boost::posix_time::ptime pt = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(10);
90 boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock(m_WaitForInitEndMutex);
91 bool bWaitRet = m_WaitForInitEndCond.timed_wait(lock, pt);
94 THROW_PARAM_ERROR(Al_Err_lTimedOutWaitingForSimToStop, Al_Err_strTimedOutWaitingForSimToStop,
"Sim ID", m_lpSim->
ID());
97 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
100 void SimulationThread::ProcessSimulation()
104 m_bThreadProcessing =
true;
110 m_WaitForInitEndCond.notify_all();
116 m_bThreadProcessing =
false;
120 m_bThreadProcessing =
false;
123 m_bThreadProcessing =
false;
126 void SimulationThread::Simulate(
float fltTime,
bool bBlocking,
float fltWaitTime)
128 if(m_lpSim && m_lpSim->
Paused())
141 if(bBlocking && fltTime > 0)
144 fltWaitTime = fltTime+120;
148 boost::posix_time::ptime pt = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(fltWaitTime);
150 boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock(m_WaitForSimEndMutex);
151 bool bWaitRet = m_WaitForSimEndCond.timed_wait(lock, pt);
156 THROW_PARAM_ERROR(Al_Err_lTimedOutWaitingForSimToStop, Al_Err_strTimedOutWaitingForSimToStop,
"Sim ID", m_lpSim->
ID());
160 void SimulationThread::PauseSimulation()
166 void SimulationThread::ResumeSimulation()
168 if(m_lpSim && m_lpSim->
Paused())
172 void SimulationThread::ResetSimulation()
180 void SimulationThread::StopSimulation()
192 THROW_PARAM_ERROR(Al_Err_lTimedOutWaitingForSimToStop, Al_Err_strTimedOutWaitingForSimToStop,
"SimID", m_lpSim->
ID());
196 void SimulationThread::ShutdownSimulation()
198 if(m_bThreadProcessing)
203 bool bTryJoin =
false;
204 #if (BOOST_VERSION >= 105000)
205 bTryJoin = m_SimThread.try_join_for(boost::chrono::seconds(10));
219 m_WaitForSimEndCond.notify_all();
224 std::cout <<
"Critical error has occured. Shutting down the simulation.\r\n";
225 std::cout << strError <<
"\r\n";
226 ShutdownSimulation();
231 std::cout <<
"Critical error has occured. Shutting down the simulation.\r\n";
232 std::cout << strError <<
"\r\n";
233 ShutdownSimulation();
virtual void HandleCriticalError(std::string strError)
Handle critical-errors in the GUI. A critical error will report the error and then shut the entire ap...
Base class file for all Animat simulation objects.
virtual void HandleNonCriticalError(std::string strError)
Handle a non-critical error in the GUI. Non-critical errors are simply reported and the simulation st...
Declares the nervous system class.
Declares the simulation recorder class.
Declares the Robot IO control interface base class.
Simulates the entire environment.
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
virtual bool Paused()
Gets whether the Simulation is paused.
Information about the standard error.
virtual std::string ID()
Gets the unique GUID ID of this object.
virtual bool WaitForSimulationBlock(long lTimeout=6000)
Wait for simulation block.
virtual bool PauseSimulation()=0
Pauses the simulation.
static Simulator * CreateSimulator(std::string strAnimatModule, std::string strSimulationFile, bool bForceNoWindows=false)
Creates a simulator from a simulator file.
Declares the link base class.
Declares the key frame class.
Declares the joint class.
Declares the organism class.
virtual bool SimRunning()
Gets whether the simulation is running.
virtual int VisualSelectionMode()
Gets the visual selection mode.
virtual bool StartSimulation()=0
Starts the simulation.
Declares the activated item class.
Declares a light manager object.
Declares the bounding box class.
virtual void Simulate()
Simulates the system.
virtual void NeedToStopSimulation()
Callback to inform that GUI that the currently running simulation needs to be stopped.
virtual void ResetSimulation()
Resets the the simulation to its orginal settings at time 0.
virtual void ToggleSimulation()=0
Toggles the simulation between running and paused.
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.
virtual void Load(std::string strFileName="")
Loads the simulation from an xml configuration file.
virtual void SimCallBack(ISimGUICallback *lpCallback)
Sets the calback object from the simulation.
virtual void Initialize(int argc, const char **argv)=0
Initializes this object.
Declares the data chart manager class.
virtual void ShutdownSimulation()=0
Shuts down the simulation.
Declares the rigid body class.
Declares the structure class.
Declares the odor type class.
Declares the robotics inerface for animatlab.
Declares the simulator class.
Declares the neural module class.
Declares the activated item manager class.
Declares the external stimuli manager class.
virtual void UnblockSimulation()
Unblock simulation.
Declares the receptive field class.
virtual float EndSimTime()
Gets the time at which to automatically end the simulation.