rate.h
Go to the documentation of this file.
1 #ifndef RATE_H
2 #define RATE_H
3 
4 #include <time.h>
5 #include <sys/time.h>
6 #include <argos3/core/utility/math/general.h>
7 
8 namespace argos {
9 
10  /*
11  * Helper to keep a constant rate in loops.
12  *
13  * This class is a simple helper that allows you to keep a constant rate in a
14  * loop such as a robot control loop. Its usage is simple:
15  *
16  * // Set the number of "ticks" per second
17  * CRate cRate(10.0);
18  * while(1) {
19  * // do stuff
20  * // ...
21  * // Sleep enough time to complete the tick period
22  * // If the tick period was execeed before this call,
23  * // don't sleep and write a warning message
24  * cRate.Sleep();
25  * }
26  */
27  class CRate {
28 
29  public:
30 
35  CRate(Real f_rate);
36 
40  ~CRate() {}
41 
45  UInt64 ElapsedUS() const;
46 
50  Real ElapsedS() const;
51 
57  void Sleep();
58 
62  inline Real GetRate() const {
63  return m_fNominalRate;
64  }
65 
70  void SetRate(Real f_rate);
71 
72  private:
73 
74  Real m_fNominalRate;
75  UInt64 m_unNominalPeriod;
76  ::timeval m_tPast;
77 
78  };
79 
80 }
81 
82 #endif // RATE_H
unsigned long long UInt64
64-bit unsigned integer.
Definition: datatypes.h:107
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CRate(Real f_rate)
Class constructor.
Definition: rate.cpp:9
UInt64 ElapsedUS() const
Returns the time elapsed since the last sleep in microseconds.
Definition: rate.cpp:16
void Sleep()
Sleeps for the appropriate time to complete the period.
Definition: rate.cpp:38
~CRate()
Class destructor.
Definition: rate.h:40
void SetRate(Real f_rate)
Sets the rate and resets the internal clock.
Definition: rate.cpp:62
Real GetRate() const
Returns the rate.
Definition: rate.h:62
Real ElapsedS() const
Returns the time elapsed since the last sleep in seconds.
Definition: rate.cpp:29