15 #ifndef RCLCPP__CREATE_TIMER_HPP_
16 #define RCLCPP__CREATE_TIMER_HPP_
24 #include "rclcpp/duration.hpp"
25 #include "rclcpp/node_interfaces/get_node_base_interface.hpp"
26 #include "rclcpp/node_interfaces/get_node_timers_interface.hpp"
27 #include "rclcpp/node_interfaces/node_base_interface.hpp"
28 #include "rclcpp/node_interfaces/node_timers_interface.hpp"
34 template<
typename CallbackT>
35 typename rclcpp::TimerBase::SharedPtr
37 std::shared_ptr<node_interfaces::NodeBaseInterface> node_base,
38 std::shared_ptr<node_interfaces::NodeTimersInterface> node_timers,
39 rclcpp::Clock::SharedPtr clock,
41 CallbackT && callback,
42 rclcpp::CallbackGroup::SharedPtr group =
nullptr)
46 period.
to_chrono<std::chrono::nanoseconds>(),
47 std::forward<CallbackT>(callback),
48 node_base->get_context());
50 node_timers->add_timer(timer, group);
55 template<
typename NodeT,
typename CallbackT>
56 typename rclcpp::TimerBase::SharedPtr
59 rclcpp::Clock::SharedPtr clock,
61 CallbackT && callback,
62 rclcpp::CallbackGroup::SharedPtr group =
nullptr)
65 rclcpp::node_interfaces::get_node_base_interface(node),
66 rclcpp::node_interfaces::get_node_timers_interface(node),
69 std::forward<CallbackT>(callback),
88 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
91 std::chrono::duration<DurationRepT, DurationT> period,
93 rclcpp::CallbackGroup::SharedPtr group,
97 if (node_base ==
nullptr) {
98 throw std::invalid_argument{
"input node_base cannot be null"};
101 if (node_timers ==
nullptr) {
102 throw std::invalid_argument{
"input node_timers cannot be null"};
105 if (period < std::chrono::duration<DurationRepT, DurationT>::zero()) {
106 throw std::invalid_argument{
"timer period cannot be negative"};
111 constexpr
auto maximum_safe_cast_ns =
112 std::chrono::nanoseconds::max() - std::chrono::duration<DurationRepT, DurationT>(1);
121 constexpr
auto ns_max_as_double =
122 std::chrono::duration_cast<std::chrono::duration<double, std::chrono::nanoseconds::period>>(
123 maximum_safe_cast_ns);
124 if (period > ns_max_as_double) {
125 throw std::invalid_argument{
126 "timer period must be less than std::chrono::nanoseconds::max()"};
129 const auto period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(period);
130 if (period_ns < std::chrono::nanoseconds::zero()) {
131 throw std::runtime_error{
132 "Casting timer period to nanoseconds resulted in integer overflow."};
136 period_ns, std::move(callback), node_base->
get_context());
DurationT to_chrono() const
Convert Duration into a std::chrono::Duration.
Generic timer. Periodically executes a user-specified callback.
Pure virtual interface class for the NodeBase part of the Node API.
virtual RCLCPP_PUBLIC rclcpp::Context::SharedPtr get_context()=0
Return the context of the node.
Pure virtual interface class for the NodeTimers part of the Node API.
virtual RCLCPP_PUBLIC void add_timer(rclcpp::TimerBase::SharedPtr timer, rclcpp::CallbackGroup::SharedPtr callback_group)=0
Add a timer to the node.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
rclcpp::TimerBase::SharedPtr create_timer(std::shared_ptr< node_interfaces::NodeBaseInterface > node_base, std::shared_ptr< node_interfaces::NodeTimersInterface > node_timers, rclcpp::Clock::SharedPtr clock, rclcpp::Duration period, CallbackT &&callback, rclcpp::CallbackGroup::SharedPtr group=nullptr)
rclcpp::WallTimer< CallbackT >::SharedPtr create_wall_timer(std::chrono::duration< DurationRepT, DurationT > period, CallbackT callback, rclcpp::CallbackGroup::SharedPtr group, node_interfaces::NodeBaseInterface *node_base, node_interfaces::NodeTimersInterface *node_timers)
Convenience method to create a timer with node resources.