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 Member Functions

 Metronome (const Duration &targetPeriod, ClockType clockType=ClockType::Virtual)
 Constructs a new rate limiter with given target period and clock type. More...
 
 Metronome (const Duration &targetPeriod, const Clock &clock)
 Constructs a new rate limiter with given target period and clock. More...
 
 Metronome (const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual)
 
void reset ()
 Resets the rate limiter so that the next targetted time point will be in the current time plus the target period. More...
 
Duration waitForNextTick ()
 Wait and block until the target period is met. More...
 

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 << ".";
}

Definition at line 35 of file Metronome.h.

Constructor & Destructor Documentation

◆ Metronome() [1/3]

Metronome ( const Frequency targetFrequency,
ClockType  clockType = ClockType::Virtual 
)

Definition at line 9 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 16 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 23 of file Metronome.cpp.

Member Function Documentation

◆ reset()

void reset ( )

Resets the rate limiter so that the next targetted time point will be in the current time plus the target period.

Definition at line 40 of file Metronome.cpp.

+ Here is the call graph for this function:

◆ waitForNextTick()

Duration waitForNextTick ( )

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 31 of file Metronome.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files:
armarx::core::time::Metronome::Metronome
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual)
Definition: Metronome.cpp:9
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::Duration
armarx::core::time::Duration Duration
Definition: forward_declarations.h:14
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28