ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
executors.hpp
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 
21 #include "rclcpp/executors/multi_threaded_executor.hpp"
22 #include "rclcpp/executors/single_threaded_executor.hpp"
23 #include "rclcpp/executors/static_single_threaded_executor.hpp"
24 #include "rclcpp/node.hpp"
25 #include "rclcpp/utilities.hpp"
26 #include "rclcpp/visibility_control.hpp"
27 
28 namespace rclcpp
29 {
30 
32 
33 RCLCPP_PUBLIC
34 void
35 spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
36 
37 RCLCPP_PUBLIC
38 void
39 spin_some(rclcpp::Node::SharedPtr node_ptr);
40 
42 
43 RCLCPP_PUBLIC
44 void
45 spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
46 
47 RCLCPP_PUBLIC
48 void
49 spin(rclcpp::Node::SharedPtr node_ptr);
50 
51 namespace executors
52 {
53 
56 
58 
69 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
71 spin_node_until_future_complete(
72  rclcpp::Executor & executor,
73  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
74  const FutureT & future,
75  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
76 {
77  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
78  // inside a callback executed by an executor.
79  executor.add_node(node_ptr);
80  auto retcode = executor.spin_until_future_complete(future, timeout);
81  executor.remove_node(node_ptr);
82  return retcode;
83 }
84 
85 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
86  typename TimeT = std::milli>
88 spin_node_until_future_complete(
89  rclcpp::Executor & executor,
90  std::shared_ptr<NodeT> node_ptr,
91  const FutureT & future,
92  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
93 {
94  return rclcpp::executors::spin_node_until_future_complete(
95  executor,
96  node_ptr->get_node_base_interface(),
97  future,
98  timeout);
99 }
100 
101 } // namespace executors
102 
103 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
105 spin_until_future_complete(
106  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
107  const FutureT & future,
108  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
109 {
110  rclcpp::ExecutorOptions options;
111  options.context = node_ptr->get_context();
113  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
114 }
115 
116 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
117  typename TimeT = std::milli>
119 spin_until_future_complete(
120  std::shared_ptr<NodeT> node_ptr,
121  const FutureT & future,
122  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
123 {
124  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
125 }
126 
127 } // namespace rclcpp
128 
129 #endif // RCLCPP__EXECUTORS_HPP_
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:66
FutureReturnCode spin_until_future_complete(const FutureT &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:334
virtual RCLCPP_PUBLIC void remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true)
Remove a node from the executor.
Definition: executor.cpp:342
virtual RCLCPP_PUBLIC void add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true)
Add a node to the executor.
Definition: executor.cpp:253
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:78
Single-threaded executor implementation.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC void spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and spin the specified node.
Definition: executors.cpp:33
FutureReturnCode
Return codes to be used with spin_until_future_complete.
RCLCPP_PUBLIC void spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and execute any immediately available work.
Definition: executors.cpp:18
Options to be passed to the executor constructor.