Metronome.h
Go to the documentation of this file.
1 #pragma once
2 
3 
8 
9 
10 namespace armarx::core::time
11 {
12 
13  /**
14  * @brief Simple rate limiter for use in loops to maintain a certain frequency given a clock.
15  *
16  * In most cases, it is enough sufficient to construct a rate limiter with a given target
17  * duration (or target period). This will then use virtual time. In cases where a specific
18  * clock is required, a constructor taking a clock or clock type can be used.
19  *
20  * Code example:
21  *
22  * @code
23  * using namespace armarx;
24  *
25  * Metronome m{Frequeny::hertz(100)}; // For a 100Hz loop.
26  * while (condition)
27  * {
28  * operation();
29  *
30  * const Duration waitingTime = m.waitForNextTick();
31  * ARMARX_INFO << "We now waited " << waitingTime << ".";
32  * }
33  * @endcode
34  */
35  class Metronome
36  {
37 
38  public:
39  Metronome(const Frequency& targetFrequency, ClockType clockType = ClockType::Virtual);
40 
41  /**
42  * @brief Constructs a new rate limiter with given target period and clock type.
43  * @param targetPeriod Period to target for in loops etc.
44  * @param clockType Type of clock to use to assess time.
45  */
46  Metronome(const Duration& targetPeriod, ClockType clockType = ClockType::Virtual);
47 
48  /**
49  * @brief Constructs a new rate limiter with given target period and clock.
50  * @param targetPeriod Period to target for in loops etc.
51  * @param clock Clock to use to assess time.
52  */
53  Metronome(const Duration& targetPeriod, const Clock& clock);
54 
55  /**
56  * @brief Wait and block until the target period is met.
57  * @return The duration the rate limiter waited for. Can be negative if the next deadline
58  * was missed.
59  */
61 
62  /**
63  * @brief Resets the rate limiter so that the next targetted time point will be in the
64  * current time plus the target period.
65  */
66  void reset();
67 
68  private:
69  /**
70  * @brief Clock to use to assess time.
71  */
72  Clock _clock;
73 
74  /**
75  * @brief Next date/time to target in the next call to wait().
76  */
77  DateTime _nextCheckpoint;
78 
79  /**
80  * @brief Period to target.
81  */
82  const Duration _targetPeriod;
83  };
84 
85 } // namespace armarx::core::time
86 
87 
88 namespace armarx
89 {
91 }
armarx::core::time::Metronome::Metronome
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual)
Definition: Metronome.cpp:9
DateTime.h
Frequency.h
Duration.h
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:17
armarx::core::time::Clock
Clock to get date/time from a specific clock type or wait for certain durations or until certain date...
Definition: Clock.h:26
Clock.h
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:10
armarx::core::time::ClockType::Virtual
@ Virtual
Time given by time server if configured, realtime otherwise.
armarx::core::time::Metronome::waitForNextTick
Duration waitForNextTick()
Wait and block until the target period is met.
Definition: Metronome.cpp:31
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::core::time::Metronome::reset
void reset()
Resets the rate limiter so that the next targetted time point will be in the current time plus the ta...
Definition: Metronome.cpp:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28