rate.cpp
Go to the documentation of this file.
1 #include "rate.h"
2 #include <argos3/core/utility/logging/argos_log.h>
3 
4 using namespace argos;
5 
6 /****************************************/
7 /****************************************/
8 
9 CRate::CRate(Real f_rate) {
10  SetRate(f_rate);
11  ::gettimeofday(&m_tPast, NULL);
12 }
13 
14 /****************************************/
15 /****************************************/
16 
17 void CRate::Sleep() {
18  /* Get current time */
19  ::gettimeofday(&m_tNow, NULL);
20  /* Calculate difference between past call and now */
21  timersub(&m_tNow, &m_tPast, &m_tDiff);
22  UInt64 unMicroSecDiff =
23  m_tDiff.tv_sec * 1000000 +
24  m_tDiff.tv_usec;
25  /* Sleep if necessary */
26  if(unMicroSecDiff < m_unNominalPeriod) {
27  m_tSleepPeriod.tv_sec = (m_unNominalPeriod - unMicroSecDiff) / 1e6;
28  m_tSleepPeriod.tv_nsec = (m_unNominalPeriod - unMicroSecDiff) * 1000;
29  ::nanosleep(&m_tSleepPeriod, NULL);
30  }
31  else {
32  LOGERR << "[WARNING] Nominal rate "
33  << m_fNominalRate
34  << " loops per sec delayed by "
35  << (unMicroSecDiff - m_unNominalPeriod)
36  << " microseconds"
37  << std::endl;
38  }
39  /* Update past for next call */
40  ::gettimeofday(&m_tPast, NULL);
41 }
42 
43 /****************************************/
44 /****************************************/
45 
46 void CRate::SetRate(Real f_rate) {
47  m_fNominalRate = Abs(f_rate);
48  m_unNominalPeriod = 1e6 / m_fNominalRate;
49 }
50 
51 /****************************************/
52 /****************************************/
CRate(Real f_rate)
Definition: rate.cpp:9
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
T Abs(const T &t_v)
Returns the absolute value of the passed argument.
Definition: general.h:25
void SetRate(Real f_rate)
Definition: rate.cpp:46
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
Definition: argos_log.h:180
unsigned long long UInt64
64-bit unsigned integer.
Definition: datatypes.h:107
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
void Sleep()
Definition: rate.cpp:17