default_visualization.cpp
Go to the documentation of this file.
1 
7 #include <argos3/core/simulator/visualization/default_visualization.h>
8 #include <argos3/core/simulator/space/space.h>
9 #include <argos3/core/simulator/loop_functions.h>
10 
11 #include <unistd.h>
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
18  static Real TVTimeToHumanReadable(::timeval& t_time) {
19  return
20  static_cast<Real>(t_time.tv_sec) +
21  static_cast<Real>(t_time.tv_usec * 10e-6);
22  }
23 
24  /****************************************/
25  /****************************************/
26 
28  /* Set the pointer to the step function */
30  /* Use real-time clock and set time structures */
31  m_tStepFunction = &CDefaultVisualization::RealTimeStep;
32  timerclear(&m_tStepClockTime);
33  m_tStepClockTime.tv_usec = 1e6 * CPhysicsEngine::GetSimulationClockTick();
34  ::gettimeofday(&m_tStepStartTime, NULL);
35  }
36  else {
37  /* Use normal clock */
38  m_tStepFunction = &CDefaultVisualization::NormalStep;
39  }
40  }
41 
42  /****************************************/
43  /****************************************/
44 
46  /* Main cycle */
48  (this->*m_tStepFunction)();
49  }
50  /* The experiment is finished */
52  LOG.Flush();
53  LOGERR.Flush();
54  }
55 
56  /****************************************/
57  /****************************************/
58 
59  void CDefaultVisualization::NormalStep() {
61  }
62 
63  /****************************************/
64  /****************************************/
65 
66  void CDefaultVisualization::RealTimeStep() {
67  /* m_tStepStartTime has already been set */
69  /* Take the time now */
70  ::gettimeofday(&m_tStepEndTime, NULL);
71  /* Calculate the elapsed time */
72  timersub(&m_tStepEndTime, &m_tStepStartTime, &m_tStepElapsedTime);
73  /* If the elapsed time is lower than the tick length, wait */
74  if(!timercmp(&m_tStepElapsedTime, &m_tStepClockTime, >)) {
75  /* Calculate the waiting time */
76  timersub(&m_tStepClockTime, &m_tStepElapsedTime, &m_tStepWaitTime);
77  /* Wait */
78  ::usleep(m_tStepWaitTime.tv_sec * 1e6 + m_tStepWaitTime.tv_usec);
79  /* Get the new step end */
80  ::gettimeofday(&m_tStepEndTime, NULL);
81  }
82  else {
83  LOGERR << "[WARNING] Clock tick took "
84  << TVTimeToHumanReadable(m_tStepElapsedTime)
85  << " sec, more than the expected "
86  << TVTimeToHumanReadable(m_tStepClockTime)
87  << " sec."
88  << std::endl;
89  }
90  /* Set the step start time to whatever the step end time is */
91  m_tStepStartTime.tv_sec = m_tStepEndTime.tv_sec;
92  m_tStepStartTime.tv_usec = m_tStepEndTime.tv_usec;
93  }
94 
95  /****************************************/
96  /****************************************/
97 
98 }
CSimulator & m_cSimulator
A reference to the simulator.
Definition: visualization.h:45
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
Definition: argos_log.h:179
CLoopFunctions & GetLoopFunctions()
Returns a reference to the loop functions associated to the current experiment.
Definition: simulator.h:236
bool IsExperimentFinished() const
Returns true if the experiment has finished.
Definition: simulator.cpp:288
void UpdateSpace()
Performs an update step of the space.
Definition: simulator.cpp:280
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
Definition: argos_log.h:180
virtual void PostExperiment()
Executes user-defined logic when the experiment finishes.
static Real GetSimulationClockTick()
Returns the simulation clock tick.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
bool IsRealTimeClock() const
Returns true if the clock tick follows the real time.
Definition: simulator.h:268