Metronome.h
Go to the documentation of this file.
1#pragma once
2
3
8
9namespace armarx::core::time
10{
11
12 /**
13 * @brief Simple rate limiter for use in loops to maintain a certain frequency given a clock.
14 *
15 * In most cases, it is enough sufficient to construct a rate limiter with a given target
16 * duration (or target period). This will then use virtual time. In cases where a specific
17 * clock is required, a constructor taking a clock or clock type can be used.
18 *
19 * Code example:
20 *
21 * @code
22 * using namespace armarx;
23 *
24 * Metronome m{Frequeny::hertz(100)}; // For a 100Hz loop.
25 * while (condition)
26 * {
27 * operation();
28 *
29 * const Duration waitingTime = m.waitForNextTick();
30 * ARMARX_INFO << "We now waited " << waitingTime << ".";
31 * }
32 * @endcode
33 *
34 * Another example with a do-while loop where the metronome must be placed at the top of a loop
35 * because of continue etc.:
36 *
37 * @code
38 * using namespace armarx;
39 *
40 * Metronome m{Frequeny::hertz(100), ClockType::Virtual, Metronome::NextTickMode::tick_immediately}; // For a 100Hz loop.
41 * do
42 * {
43 * const Duration waitingTime = m.waitForNextTick(); // This will not wait in the first loop iteration.
44 * ARMARX_INFO << "We now waited " << waitingTime << ".";
45 *
46 * if (some_continue_condition)
47 * {
48 * continue;
49 * }
50 *
51 * operation();
52 * }
53 * while (condition);
54 * @endcode
55 */
57 {
58
59 public:
60 /**
61 * @brief Modes that influence how the reset method should function for the next
62 * waitForNextTick() method call.
63 */
64 enum class NextTickMode
65 {
66 /**
67 * If waitForNextTicK() is called shortly after reset() (or any constructor), the
68 * metronome will wait for the length of a period.
69 */
71
72 /**
73 * If waitForNextTicK() is called shortly after reset() (or any constructor), the
74 * metronome will not wait and continue immediately. This is useful, if the
75 * waitForNextTick() method must be called at the top of the scope of a loop and it is
76 * not desired for the metronome to block the first loop iteration.
77 */
79 };
80
81 Metronome(const Frequency& targetFrequency,
82 ClockType clockType = ClockType::Virtual,
84
85 /**
86 * @brief Constructs a new rate limiter with given target period and clock type.
87 * @param targetPeriod Period to target for in loops etc.
88 * @param clockType Type of clock to use to assess time.
89 */
90 Metronome(const Duration& targetPeriod, ClockType clockType = ClockType::Virtual);
91
92 /**
93 * @brief Constructs a new rate limiter with given target period and clock.
94 * @param targetPeriod Period to target for in loops etc.
95 * @param clock Clock to use to assess time.
96 */
97 Metronome(const Duration& targetPeriod, const Clock& clock);
98
99 /**
100 * @brief Wait and block until the target period is met.
101 * @return The duration the rate limiter waited for. Can be negative if the next deadline
102 * was missed.
103 */
105
106 /**
107 * @brief Resets the rate limiter so that the next targetted time point will be in the
108 * current time plus the target period.
109 */
111
112 private:
113 /**
114 * @brief Clock to use to assess time.
115 */
116 Clock _clock;
117
118 /**
119 * @brief Next date/time to target in the next call to wait().
120 */
121 mutable DateTime _nextCheckpoint;
122
123 /**
124 * @brief Period to target.
125 */
126 const Duration _targetPeriod;
127 };
128
129} // namespace armarx::core::time
130
131namespace armarx
132{
134}
Clock to get date/time from a specific clock type or wait for certain durations or until certain date...
Definition Clock.h:26
Represents a point in time.
Definition DateTime.h:25
Represents a duration.
Definition Duration.h:17
Represents a frequency.
Definition Frequency.h:17
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition Metronome.h:57
Duration waitForNextTick() const
Wait and block until the target period is met.
Definition Metronome.cpp:27
NextTickMode
Modes that influence how the reset method should function for the next waitForNextTick() method call.
Definition Metronome.h:65
@ wait_for_next_tick
If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will wait fo...
Definition Metronome.h:70
@ tick_immediately
If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will not wai...
Definition Metronome.h:78
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual, NextTickMode mode=NextTickMode::wait_for_next_tick)
Definition Metronome.cpp:8
void reset(NextTickMode mode=NextTickMode::wait_for_next_tick) const
Resets the rate limiter so that the next targetted time point will be in the current time plus the ta...
Definition Metronome.cpp:35
ClockType
Describes the type of clock.
Definition ClockType.h:10
@ Virtual
Time given by time server if configured, realtime otherwise.
Definition ClockType.h:16
This file offers overloads of toIce() and fromIce() functions for STL container types.