rclcpp  master
C++ ROS Client Library API
executors.hpp
Go to the documentation of this file.
1 // Copyright 2014 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__EXECUTORS_HPP_
16 #define RCLCPP__EXECUTORS_HPP_
17 
18 #include <future>
19 #include <memory>
20 
23 #include "rclcpp/node.hpp"
24 #include "rclcpp/utilities.hpp"
26 
27 namespace rclcpp
28 {
29 
31 
33 void
34 spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
35 
37 void
39 
41 
43 void
44 spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
45 
47 void
49 
50 namespace executors
51 {
52 
55 
57 
68 template<typename ResponseT, typename TimeRepT = int64_t, typename TimeT = std::milli>
71  rclcpp::executor::Executor & executor,
72  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
75 {
76  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
77  // inside a callback executed by an executor.
78  executor.add_node(node_ptr);
79  auto retcode = executor.spin_until_future_complete(future, timeout);
80  executor.remove_node(node_ptr);
81  return retcode;
82 }
83 
84 template<typename NodeT = rclcpp::Node, typename ResponseT, typename TimeRepT = int64_t,
85  typename TimeT = std::milli>
88  rclcpp::executor::Executor & executor,
89  std::shared_ptr<NodeT> node_ptr,
92 {
94  executor,
95  node_ptr->get_node_base_interface(),
96  future,
97  timeout);
98 }
99 
100 } // namespace executors
101 
102 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
105  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
108 {
110  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
111 }
112 
113 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
114  typename TimeT = std::milli>
117  std::shared_ptr<NodeT> node_ptr,
120 {
121  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
122 }
123 
124 } // namespace rclcpp
125 
126 #endif // RCLCPP__EXECUTORS_HPP_
rclcpp::executor::FutureReturnCode spin_node_until_future_complete(rclcpp::executor::Executor &executor, rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, std::shared_future< ResponseT > &future, std::chrono::duration< TimeRepT, TimeT > timeout=std::chrono::duration< TimeRepT, TimeT >(-1))
Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
Definition: executors.hpp:70
FutureReturnCode spin_until_future_complete(std::shared_future< ResponseT > &future, std::chrono::duration< TimeRepT, TimeT > timeout=std::chrono::duration< TimeRepT, TimeT >(-1))
Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
Definition: executor.hpp:225
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
virtual void remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true)
Remove a node from the executor.
virtual void add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true)
Add a node to the executor.
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:97
void spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and execute any immediately available work.
Definition: multi_threaded_executor.hpp:35
FutureReturnCode
Return codes to be used with spin_until_future_complete.
Definition: executor.hpp:55
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
Single-threaded executor implementation.
Definition: single_threaded_executor.hpp:40
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:72
void spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and spin the specified node.
rclcpp::executor::FutureReturnCode spin_until_future_complete(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, std::shared_future< FutureT > &future, std::chrono::duration< TimeRepT, TimeT > timeout=std::chrono::duration< TimeRepT, TimeT >(-1))
Definition: executors.hpp:104