Metronome Class Reference

Simple rate limiter for use in loops to maintain a certain frequency given a clock. More...

#include <ArmarXCore/core/time/Metronome.h>

Public Types

enum class  NextTickMode { wait_for_next_tick , tick_immediately }
 Modes that influence how the reset method should function for the next waitForNextTick() method call. More...
 

Public Member Functions

 Metronome (const Duration &targetPeriod, ClockType clockType=ClockType::Virtual)
 Constructs a new rate limiter with given target period and clock type.
 
 Metronome (const Duration &targetPeriod, const Clock &clock)
 Constructs a new rate limiter with given target period and clock.
 
 Metronome (const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual, NextTickMode mode=NextTickMode::wait_for_next_tick)
 
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 target period.
 
Duration waitForNextTick () const
 Wait and block until the target period is met.
 

Detailed Description

Simple rate limiter for use in loops to maintain a certain frequency given a clock.

In most cases, it is enough sufficient to construct a rate limiter with a given target duration (or target period). This will then use virtual time. In cases where a specific clock is required, a constructor taking a clock or clock type can be used.

Code example:

using namespace armarx;
Metronome m{Frequeny::hertz(100)}; // For a 100Hz loop.
while (condition)
{
operation();
const Duration waitingTime = m.waitForNextTick();
ARMARX_INFO << "We now waited " << waitingTime << ".";
}
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual, NextTickMode mode=NextTickMode::wait_for_next_tick)
Definition Metronome.cpp:8
Represents a duration.
Definition Duration.h:17
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.

Another example with a do-while loop where the metronome must be placed at the top of a loop because of continue etc.:

using namespace armarx;
Metronome m{Frequeny::hertz(100), ClockType::Virtual, Metronome::NextTickMode::tick_immediately}; // For a 100Hz loop.
do
{
const Duration waitingTime = m.waitForNextTick(); // This will not wait in the first loop iteration.
ARMARX_INFO << "We now waited " << waitingTime << ".";
if (some_continue_condition)
{
continue;
}
operation();
}
while (condition);
@ tick_immediately
If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will not wai...
Definition Metronome.h:78
@ Virtual
Time given by time server if configured, realtime otherwise.
Definition ClockType.h:16

Definition at line 56 of file Metronome.h.

Member Enumeration Documentation

◆ NextTickMode

enum class NextTickMode
strong

Modes that influence how the reset method should function for the next waitForNextTick() method call.

Enumerator
wait_for_next_tick 

If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will wait for the length of a period.

tick_immediately 

If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will not wait and continue immediately.

This is useful, if the waitForNextTick() method must be called at the top of the scope of a loop and it is not desired for the metronome to block the first loop iteration.

Definition at line 64 of file Metronome.h.

Constructor & Destructor Documentation

◆ Metronome() [1/3]

Definition at line 8 of file Metronome.cpp.

◆ Metronome() [2/3]

Metronome ( const Duration & targetPeriod,
ClockType clockType = ClockType::Virtual )

Constructs a new rate limiter with given target period and clock type.

Parameters
targetPeriodPeriod to target for in loops etc.
clockTypeType of clock to use to assess time.

Definition at line 14 of file Metronome.cpp.

◆ Metronome() [3/3]

Metronome ( const Duration & targetPeriod,
const Clock & clock )

Constructs a new rate limiter with given target period and clock.

Parameters
targetPeriodPeriod to target for in loops etc.
clockClock to use to assess time.

Definition at line 20 of file Metronome.cpp.

Member Function Documentation

◆ reset()

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 target period.

Definition at line 35 of file Metronome.cpp.

◆ waitForNextTick()

Duration waitForNextTick ( ) const

Wait and block until the target period is met.

Returns
The duration the rate limiter waited for. Can be negative if the next deadline was missed.

Definition at line 27 of file Metronome.cpp.


The documentation for this class was generated from the following files: