rclcpp  master
C++ ROS Client Library API
create_timer.hpp
Go to the documentation of this file.
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__CREATE_TIMER_HPP_
16 #define RCLCPP__CREATE_TIMER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "rclcpp/duration.hpp"
27 
28 namespace rclcpp
29 {
32 template<typename CallbackT>
38  rclcpp::Duration period,
39  CallbackT && callback,
40  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr)
41 {
43  clock,
45  std::forward<CallbackT>(callback),
46  node_base->get_context());
47 
48  node_timers->add_timer(timer, group);
49  return timer;
50 }
51 
53 template<typename NodeT, typename CallbackT>
56  NodeT node,
58  rclcpp::Duration period,
59  CallbackT && callback,
60  rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr)
61 {
62  return create_timer(
65  clock,
66  period,
67  std::forward<CallbackT>(callback),
68  group);
69 }
70 
71 } // namespace rclcpp
72 
73 #endif // RCLCPP__CREATE_TIMER_HPP_
virtual rclcpp::Context::SharedPtr get_context()=0
Return the context of the node.
DurationT to_chrono() const
Definition: duration.hpp:105
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::node_interfaces::NodeTimersInterface * get_node_timers_interface(NodeType node_pointer)
Get the NodeTimersInterface as a pointer from a pointer to a "Node like" object.
Definition: get_node_timers_interface.hpp:126
virtual void add_timer(rclcpp::TimerBase::SharedPtr timer, rclcpp::callback_group::CallbackGroup::SharedPtr callback_group)=0
Add a timer to the node.
Generic timer. Periodically executes a user-specified callback.
Definition: timer.hpp:121
rclcpp::TimerBase::SharedPtr create_timer(node_interfaces::NodeBaseInterface *node_base, node_interfaces::NodeTimersInterface *node_timers, rclcpp::Clock::SharedPtr clock, rclcpp::Duration period, CallbackT &&callback, rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr)
Definition: create_timer.hpp:34
rclcpp::node_interfaces::NodeBaseInterface * get_node_base_interface(NodeType node_pointer)
Get the NodeBaseInterface as a pointer from a pointer to a "Node like" object.
Definition: get_node_base_interface.hpp:126
Definition: duration.hpp:26
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
Pure virtual interface class for the NodeTimers part of the Node API.
Definition: node_timers_interface.hpp:29
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>