mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
mrs_lib::Timer Class Reference

Timer wrapper supporting ROS and thread-based implementations. More...

#include <timer.hpp>

Public Types

using EventCallback = std::function<void()>
 Type of the accepted callback.
using EventCoroCallback = CoroCallback<void()>
 Type of the accepted coroutine callback.

Public Member Functions

 Timer (const RosTimerOptions &options, std::chrono::nanoseconds period, EventCallback callback)
 Construct a ROS-based timer with a regular callback.
 Timer (const RosTimerOptions &options, std::chrono::nanoseconds period, EventCoroCallback callback)
 Construct a ROS-based timer with a coroutine callback.
 Timer (const ThreadTimerOptions &options, std::chrono::nanoseconds period, EventCallback callback)
 Construct a thread-based timer with a regular callback.
 Timer (const ThreadTimerOptions &options, std::chrono::nanoseconds period, EventCoroCallback callback)
 Construct a thread-based timer with a coroutine callback.
void stop ()
 Stop the timer.
void start_or_reset ()
 Start or reset the timer.
void set_period (std::chrono::nanoseconds period)
 Set the timer period.
bool is_running () const
 Check whether the timer is currently running.

Detailed Description

Timer wrapper supporting ROS and thread-based implementations.

You can chose the timer implementation by calling the constructor with the respective config type (RosTimerOptions, ThreadTimerOptions).

Note
Although the thread timer implementation tries to be as close as possible to the ROS timer, there are some important differences:
  • Clocks
    • Ros timer uses the clock provided by the node interfaces.
    • Thread timer uses standard library clock (does not handle sim time!).
  • Callback Groups - Thread timer does not handle rclcpp callback groups. This means that code relying on thread timers MUST manually ensure thread safe access to everything accessed by the timer callback. Using the ROS backend uses the callback groups as expected.
  • Callback Reentrancy - Thread timer runs all callbacks on a single thread and thus is unable to start multiple callbacks in parallel like the ROS timer.

Constructor & Destructor Documentation

◆ Timer() [1/4]

mrs_lib::Timer::Timer ( const RosTimerOptions & options,
std::chrono::nanoseconds period,
EventCallback callback )
explicit

Construct a ROS-based timer with a regular callback.

Parameters
optionsTimer configuration options.
periodTimer period.
callbackCallback invoked when the timer expires.

◆ Timer() [2/4]

mrs_lib::Timer::Timer ( const RosTimerOptions & options,
std::chrono::nanoseconds period,
EventCoroCallback callback )
explicit

Construct a ROS-based timer with a coroutine callback.

Parameters
optionsTimer configuration options.
periodTimer period.
callbackCoroutine callback invoked when the timer expires.

◆ Timer() [3/4]

mrs_lib::Timer::Timer ( const ThreadTimerOptions & options,
std::chrono::nanoseconds period,
EventCallback callback )
explicit

Construct a thread-based timer with a regular callback.

Parameters
optionsTimer configuration options.
periodTimer period.
callbackCallback invoked when the timer expires.

◆ Timer() [4/4]

mrs_lib::Timer::Timer ( const ThreadTimerOptions & options,
std::chrono::nanoseconds period,
EventCoroCallback callback )
explicit

Construct a thread-based timer with a coroutine callback.

Parameters
optionsTimer configuration options.
periodTimer period.
callbackCoroutine callback invoked when the timer expires.

Member Function Documentation

◆ is_running()

bool mrs_lib::Timer::is_running ( ) const
nodiscard

Check whether the timer is currently running.

Returns
true if the timer is running, otherwise false.

◆ set_period()

void mrs_lib::Timer::set_period ( std::chrono::nanoseconds period)

Set the timer period.

Parameters
periodNew timer period.

Changing the period also resets the timer countdown and starts the timer if it was previously stopped.

◆ start_or_reset()

void mrs_lib::Timer::start_or_reset ( )

Start or reset the timer.

If the timer is stopped, this starts it. If it is already running, its countdown is reset using the currently configured period.

◆ stop()

void mrs_lib::Timer::stop ( )

Stop the timer.

After calling this method, the timer will not invoke its callback until start_or_reset is called.


The documentation for this class was generated from the following files:
  • /home/runner/work/mrs_lib/mrs_lib/include/mrs_lib/timer.hpp
  • /home/runner/work/mrs_lib/mrs_lib/src/timer/timer.cpp