mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
timer.hpp
1#ifndef MRS_LIB_EXPERIMENTAL_TIMER_HPP_
2#define MRS_LIB_EXPERIMENTAL_TIMER_HPP_
3
4#include <chrono>
5#include <functional>
6#include <memory>
7
8#include <rclcpp/callback_group.hpp>
9#include <rclcpp/create_timer.hpp>
10#include <rclcpp/node_interfaces/node_base_interface.hpp>
11#include <rclcpp/node_interfaces/node_clock_interface.hpp>
12#include <rclcpp/node_interfaces/node_interfaces.hpp>
13#include <rclcpp/node_interfaces/node_timers_interface.hpp>
14#include <rclcpp/timer.hpp>
15
17#include "utility/pimpl.hpp"
18
19namespace mrs_lib
20{
21
25 using TimerNodeInterfaces = rclcpp::node_interfaces::NodeInterfaces<rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeTimersInterface,
26 rclcpp::node_interfaces::NodeClockInterface>;
27
28 namespace internal
29 {
30
38 {
39 public:
43 TimerImplInterface() = default;
44
48 virtual ~TimerImplInterface() = default;
49
51 TimerImplInterface& operator=(const TimerImplInterface&) = delete;
53 TimerImplInterface& operator=(TimerImplInterface&&) = delete;
54
58 virtual void stop() = 0;
59
63 virtual void start_or_reset() = 0;
64
72 virtual void set_period(std::chrono::nanoseconds period) = 0;
73
79 [[nodiscard]] virtual bool is_running() const = 0;
80 };
81
82 } // namespace internal
83
92 {
97
101 bool autostart = true;
102
108 bool oneshot = false;
109
115 std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr;
116 };
117
125 {
130
134 bool autostart = true;
135
141 bool oneshot = false;
142 };
143
164 class Timer
165 {
166 public:
170 using EventCallback = std::function<void()>;
171
176
184 explicit Timer(const RosTimerOptions& options, std::chrono::nanoseconds period, EventCallback callback);
185
193 explicit Timer(const RosTimerOptions& options, std::chrono::nanoseconds period, EventCoroCallback callback);
194
202 explicit Timer(const ThreadTimerOptions& options, std::chrono::nanoseconds period, EventCallback callback);
203
211 explicit Timer(const ThreadTimerOptions& options, std::chrono::nanoseconds period, EventCoroCallback callback);
212
219 void stop();
220
227 void start_or_reset();
228
237 void set_period(std::chrono::nanoseconds period);
238
244 [[nodiscard]] bool is_running() const;
245
246 private:
248 };
249
250} // namespace mrs_lib
251
252#endif // MRS_LIB_EXPERIMENTAL_TIMER_HPP_
Base template for CoroCallback.
Definition callback.hpp:166
Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implem...
Definition pimpl.hpp:39
void stop()
Stop the timer.
Definition timer.cpp:257
std::function< void()> EventCallback
Type of the accepted callback.
Definition timer.hpp:170
bool is_running() const
Check whether the timer is currently running.
Definition timer.cpp:272
Timer(const RosTimerOptions &options, std::chrono::nanoseconds period, EventCallback callback)
Construct a ROS-based timer with a regular callback.
Definition timer.cpp:237
CoroCallback< void()> EventCoroCallback
Type of the accepted coroutine callback.
Definition timer.hpp:175
void set_period(std::chrono::nanoseconds period)
Set the timer period.
Definition timer.cpp:267
void start_or_reset()
Start or reset the timer.
Definition timer.cpp:262
virtual void start_or_reset()=0
Start the timer or reset its countdown.
virtual void set_period(std::chrono::nanoseconds period)=0
Change the timer period.
virtual ~TimerImplInterface()=default
Virtual destructor.
TimerImplInterface()=default
Default constructor.
virtual bool is_running() const =0
Check whether the timer is running.
virtual void stop()=0
Stop the timer.
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
rclcpp::node_interfaces::NodeInterfaces< rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeTimersInterface, rclcpp::node_interfaces::NodeClockInterface > TimerNodeInterfaces
ROS node interfaces required by the Timer.
Definition timer.hpp:25
Options for creating a ROS-based timer.
Definition timer.hpp:92
bool oneshot
Whether the timer should execute its callback only once.
Definition timer.hpp:108
std::shared_ptr< rclcpp::CallbackGroup > callback_group
Callback group in which the timer callback should be executed.
Definition timer.hpp:115
bool autostart
Whether the timer should start automatically after construction.
Definition timer.hpp:101
TimerNodeInterfaces node_interfaces
ROS node interfaces used by the timer.
Definition timer.hpp:96
Options for creating a thread-based timer.
Definition timer.hpp:125
TimerNodeInterfaces node_interfaces
ROS node interfaces used by the timer.
Definition timer.hpp:129
bool autostart
Whether the timer should start automatically after construction.
Definition timer.hpp:134
bool oneshot
Whether the timer should execute its callback only once.
Definition timer.hpp:141