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_clock_interface.hpp"
27 #include "rclcpp/node_interfaces/get_node_timers_interface.hpp"
28 #include "rclcpp/node_interfaces/node_base_interface.hpp"
29 #include "rclcpp/node_interfaces/node_clock_interface.hpp"
30 #include "rclcpp/node_interfaces/node_timers_interface.hpp"
45 template<
typename DurationRepT,
typename DurationT>
46 std::chrono::nanoseconds
47 safe_cast_to_period_in_ns(std::chrono::duration<DurationRepT, DurationT> period)
49 if (period < std::chrono::duration<DurationRepT, DurationT>::zero()) {
50 throw std::invalid_argument{
"timer period cannot be negative"};
55 constexpr
auto maximum_safe_cast_ns =
56 std::chrono::nanoseconds::max() - std::chrono::duration<DurationRepT, DurationT>(1);
65 constexpr
auto ns_max_as_double =
66 std::chrono::duration_cast<std::chrono::duration<double, std::chrono::nanoseconds::period>>(
67 maximum_safe_cast_ns);
68 if (period > ns_max_as_double) {
69 throw std::invalid_argument{
70 "timer period must be less than std::chrono::nanoseconds::max()"};
73 const auto period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(period);
74 if (period_ns < std::chrono::nanoseconds::zero()) {
75 throw std::runtime_error{
76 "Casting timer period to nanoseconds resulted in integer overflow."};
85 template<
typename CallbackT>
86 typename rclcpp::TimerBase::SharedPtr
88 std::shared_ptr<node_interfaces::NodeBaseInterface> node_base,
89 std::shared_ptr<node_interfaces::NodeTimersInterface> node_timers,
90 rclcpp::Clock::SharedPtr clock,
92 CallbackT && callback,
93 rclcpp::CallbackGroup::SharedPtr group =
nullptr,
94 bool autostart =
true)
98 period.
to_chrono<std::chrono::nanoseconds>(),
99 std::forward<CallbackT>(callback),
107 template<
typename NodeT,
typename CallbackT>
108 typename rclcpp::TimerBase::SharedPtr
111 rclcpp::Clock::SharedPtr clock,
113 CallbackT && callback,
114 rclcpp::CallbackGroup::SharedPtr group =
nullptr,
115 bool autostart =
true)
119 period.
to_chrono<std::chrono::nanoseconds>(),
120 std::forward<CallbackT>(callback),
122 rclcpp::node_interfaces::get_node_base_interface(node).get(),
123 rclcpp::node_interfaces::get_node_timers_interface(node).get(),
144 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
147 rclcpp::Clock::SharedPtr clock,
148 std::chrono::duration<DurationRepT, DurationT> period,
150 rclcpp::CallbackGroup::SharedPtr group,
153 bool autostart =
true)
155 if (clock ==
nullptr) {
156 throw std::invalid_argument{
"clock cannot be null"};
158 if (node_base ==
nullptr) {
159 throw std::invalid_argument{
"input node_base cannot be null"};
161 if (node_timers ==
nullptr) {
162 throw std::invalid_argument{
"input node_timers cannot be null"};
165 const std::chrono::nanoseconds period_ns = detail::safe_cast_to_period_in_ns(period);
169 std::move(clock), period_ns, std::move(callback), node_base->
get_context(), autostart);
189 template<
typename DurationRepT,
typename DurationT,
typename CallbackT>
192 std::chrono::duration<DurationRepT, DurationT> period,
194 rclcpp::CallbackGroup::SharedPtr group,
197 bool autostart =
true)
199 if (node_base ==
nullptr) {
200 throw std::invalid_argument{
"input node_base cannot be null"};
203 if (node_timers ==
nullptr) {
204 throw std::invalid_argument{
"input node_timers cannot be null"};
207 const std::chrono::nanoseconds period_ns = detail::safe_cast_to_period_in_ns(period);
211 period_ns, std::move(callback), node_base->
get_context(), autostart);
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, bool autostart=true)
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, bool autostart=true)
Convenience method to create a wall timer with node resources.