ROS 2 rclcpp + rcl - jazzy  jazzy
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/experimental/executors/events_executor/events_executor.hpp"
25 #include "rclcpp/executors/events_cbg_executor/events_cbg_executor.hpp"
26 #include "rclcpp/node.hpp"
27 #include "rclcpp/utilities.hpp"
28 #include "rclcpp/visibility_control.hpp"
29 
30 namespace rclcpp
31 {
32 
34 
35 RCLCPP_PUBLIC
36 void
37 spin_all(
38  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
39  std::chrono::nanoseconds max_duration);
40 
41 RCLCPP_PUBLIC
42 void
43 spin_all(rclcpp::Node::SharedPtr node_ptr, std::chrono::nanoseconds max_duration);
44 
46 
47 RCLCPP_PUBLIC
48 void
49 spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
50 
51 RCLCPP_PUBLIC
52 void
53 spin_some(rclcpp::Node::SharedPtr node_ptr);
54 
56 
57 RCLCPP_PUBLIC
58 void
59 spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
60 
61 RCLCPP_PUBLIC
62 void
63 spin(rclcpp::Node::SharedPtr node_ptr);
64 
65 namespace executors
66 {
67 
71 
73 
84 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
86 spin_node_until_future_complete(
87  rclcpp::Executor & executor,
88  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
89  const FutureT & future,
90  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
91 {
92  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
93  // inside a callback executed by an executor.
94  executor.add_node(node_ptr);
95  auto retcode = executor.spin_until_future_complete(future, timeout);
96  executor.remove_node(node_ptr);
97  return retcode;
98 }
99 
100 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
101  typename TimeT = std::milli>
103 spin_node_until_future_complete(
104  rclcpp::Executor & executor,
105  std::shared_ptr<NodeT> node_ptr,
106  const FutureT & future,
107  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
108 {
109  return rclcpp::executors::spin_node_until_future_complete(
110  executor,
111  node_ptr->get_node_base_interface(),
112  future,
113  timeout);
114 }
115 
116 } // namespace executors
117 
118 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
120 spin_until_future_complete(
121  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
122  const FutureT & future,
123  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
124 {
125  rclcpp::ExecutorOptions options;
126  options.context = node_ptr->get_context();
128  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
129 }
130 
131 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
132  typename TimeT = std::milli>
134 spin_until_future_complete(
135  std::shared_ptr<NodeT> node_ptr,
136  const FutureT & future,
137  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
138 {
139  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
140 }
141 
142 } // namespace rclcpp
143 
144 #endif // RCLCPP__EXECUTORS_HPP_
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:65
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:365
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:224
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:188
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:80
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:50
FutureReturnCode
Return codes to be used with spin_until_future_complete.
RCLCPP_PUBLIC void spin_all(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, std::chrono::nanoseconds max_duration)
Create a default single-threaded executor and execute all available work exhaustively.
Definition: executors.cpp:18
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:35
Options to be passed to the executor constructor.