Metronome.h
Go to the documentation of this file.
1 #pragma once
2 
3 
8 
9 namespace 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  class Metronome
35  {
36 
37  public:
38  Metronome(const Frequency& targetFrequency, ClockType clockType = ClockType::Virtual);
39 
40  /**
41  * @brief Constructs a new rate limiter with given target period and clock type.
42  * @param targetPeriod Period to target for in loops etc.
43  * @param clockType Type of clock to use to assess time.
44  */
45  Metronome(const Duration& targetPeriod, ClockType clockType = ClockType::Virtual);
46 
47  /**
48  * @brief Constructs a new rate limiter with given target period and clock.
49  * @param targetPeriod Period to target for in loops etc.
50  * @param clock Clock to use to assess time.
51  */
52  Metronome(const Duration& targetPeriod, const Clock& clock);
53 
54  /**
55  * @brief Wait and block until the target period is met.
56  * @return The duration the rate limiter waited for. Can be negative if the next deadline
57  * was missed.
58  */
60 
61  /**
62  * @brief Resets the rate limiter so that the next targetted time point will be in the
63  * current time plus the target period.
64  */
65  void reset();
66 
67  private:
68  /**
69  * @brief Clock to use to assess time.
70  */
71  Clock _clock;
72 
73  /**
74  * @brief Next date/time to target in the next call to wait().
75  */
76  DateTime _nextCheckpoint;
77 
78  /**
79  * @brief Period to target.
80  */
81  const Duration _targetPeriod;
82  };
83 
84 } // namespace armarx::core::time
85 
86 namespace armarx
87 {
89 }
armarx::core::time::Metronome::Metronome
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual)
Definition: Metronome.cpp:8
DateTime.h
Frequency.h
Duration.h
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:16
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:25
Clock.h
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:9
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:27
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:34
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
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:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27