ARGoS  3
A parallel, multi-engine simulator for swarm robotics
visualization.cpp
Go to the documentation of this file.
1 
7 #include <argos3/core/simulator/visualization/visualization.h>
8 #include <argos3/core/simulator/space/space.h>
9 
10 #include <unistd.h>
11 
12 namespace argos {
13 
14  /****************************************/
15  /****************************************/
16 
17  static Real TVTimeToHumanReadable(::timeval& t_time) {
18  return
19  static_cast<Real>(t_time.tv_sec) +
20  static_cast<Real>(t_time.tv_usec * 10e-6);
21  }
22 
23  /****************************************/
24  /****************************************/
25 
27  /* Get visualization id from the XML */
28  //GetNodeAttribute(t_tree, "id", m_strId);
29  /* Set the pointer to the step function */
31  /* Use real-time clock and set time structures */
32  m_tStepFunction = &CDefaultVisualization::RealTimeStep;
33  timerclear(&m_tStepClockTime);
34  m_tStepClockTime.tv_usec = 1e6 * CPhysicsEngine::GetSimulationClockTick();
35  ::gettimeofday(&m_tStepStartTime, NULL);
36  }
37  else {
38  /* Use normal clock */
39  m_tStepFunction = &CDefaultVisualization::NormalStep;
40  }
41  }
42 
43  /****************************************/
44  /****************************************/
45 
47  /* Main cycle */
49  (this->*m_tStepFunction)();
50  }
51  }
52 
53  /****************************************/
54  /****************************************/
55 
56  void CDefaultVisualization::NormalStep() {
58  }
59 
60  /****************************************/
61  /****************************************/
62 
63  void CDefaultVisualization::RealTimeStep() {
64  /* m_tStepStartTime has already been set */
66  /* Take the time now */
67  ::gettimeofday(&m_tStepEndTime, NULL);
68  /* Calculate the elapsed time */
69  timersub(&m_tStepEndTime, &m_tStepStartTime, &m_tStepElapsedTime);
70  /* If the elapsed time is lower than the tick length, wait */
71  if(!timercmp(&m_tStepElapsedTime, &m_tStepClockTime, >)) {
72  /* Calculate the waiting time */
73  timersub(&m_tStepClockTime, &m_tStepElapsedTime, &m_tStepWaitTime);
74  /* Wait */
75  ::usleep(m_tStepWaitTime.tv_sec * 1e6 + m_tStepWaitTime.tv_usec);
76  /* Get the new step end */
77  ::gettimeofday(&m_tStepEndTime, NULL);
78  }
79  else {
80  LOGERR << "[WARNING] Clock tick took "
81  << TVTimeToHumanReadable(m_tStepElapsedTime)
82  << " sec, more than the expected "
83  << TVTimeToHumanReadable(m_tStepClockTime)
84  << " sec."
85  << std::endl;
86  }
87  /* Set the step start time to whatever the step end time is */
88  m_tStepStartTime.tv_sec = m_tStepEndTime.tv_sec;
89  m_tStepStartTime.tv_usec = m_tStepEndTime.tv_usec;
90  }
91 
92  /****************************************/
93  /****************************************/
94 
95 }
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CSimulator::IsRealTimeClock
bool IsRealTimeClock() const
Returns true if the clock tick follows the real time.
Definition: simulator.h:268
argos::CDefaultVisualization::Execute
virtual void Execute()
Definition: default_visualization.cpp:45
argos::LOGERR
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
Definition: argos_log.h:180
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CDefaultVisualization::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
Definition: default_visualization.h:29
argos::CVisualization::m_cSimulator
CSimulator & m_cSimulator
A reference to the simulator.
Definition: visualization.h:45
argos::CSimulator::IsExperimentFinished
bool IsExperimentFinished() const
Returns true if the experiment has finished.
Definition: simulator.cpp:284
argos::CPhysicsEngine::GetSimulationClockTick
static Real GetSimulationClockTick()
Returns the simulation clock tick.
Definition: physics_engine.cpp:264
argos::CSimulator::UpdateSpace
void UpdateSpace()
Performs an update step of the space.
Definition: simulator.cpp:276
Real
float Real
Collects all ARGoS code.
Definition: datatypes.h:39