ROS 2 rclcpp + rcl - rolling  rolling-b14af74a
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/experimental/executors/events_executor/events_executor.hpp"
24 #include "rclcpp/node.hpp"
25 #include "rclcpp/utilities.hpp"
26 #include "rclcpp/visibility_control.hpp"
27 
28 namespace rclcpp
29 {
30 
46 [[deprecated("use SingleThreadedExecutor::spin_all instead")]]
47 RCLCPP_PUBLIC
48 void
49 spin_all(
50  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
51  std::chrono::nanoseconds max_duration);
52 
68 [[deprecated("use SingleThreadedExecutor::spin_all instead")]]
69 RCLCPP_PUBLIC
70 void
71 spin_all(rclcpp::Node::SharedPtr node_ptr, std::chrono::nanoseconds max_duration);
72 
87 [[deprecated("use SingleThreadedExecutor::spin_some instead")]]
88 RCLCPP_PUBLIC
89 void
90 spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
91 
106 [[deprecated("use SingleThreadedExecutor::spin_some instead")]]
107 RCLCPP_PUBLIC
108 void
109 spin_some(rclcpp::Node::SharedPtr node_ptr);
110 
112 
113 RCLCPP_PUBLIC
114 void
115 spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
116 
117 RCLCPP_PUBLIC
118 void
119 spin(rclcpp::Node::SharedPtr node_ptr);
120 
121 namespace executors
122 {
123 
126 
128 
139 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
141 spin_node_until_future_complete(
142  rclcpp::Executor & executor,
143  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
144  const FutureT & future,
145  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
146 {
147  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
148  // inside a callback executed by an executor.
149  executor.add_node(node_ptr);
150  auto retcode = executor.spin_until_future_complete(future, timeout);
151  executor.remove_node(node_ptr);
152  return retcode;
153 }
154 
155 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
156  typename TimeT = std::milli>
158 spin_node_until_future_complete(
159  rclcpp::Executor & executor,
160  std::shared_ptr<NodeT> node_ptr,
161  const FutureT & future,
162  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
163 {
164  return rclcpp::executors::spin_node_until_future_complete(
165  executor,
166  node_ptr->get_node_base_interface(),
167  future,
168  timeout);
169 }
170 
171 } // namespace executors
172 
173 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
175 spin_until_future_complete(
176  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
177  const FutureT & future,
178  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
179 {
180  rclcpp::ExecutorOptions options;
181  options.context = node_ptr->get_context();
183  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
184 }
185 
186 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
187  typename TimeT = std::milli>
189 spin_until_future_complete(
190  std::shared_ptr<NodeT> node_ptr,
191  const FutureT & future,
192  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
193 {
194  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
195 }
196 
197 } // namespace rclcpp
198 
199 #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:380
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:230
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:189
Node is the single point of entry for creating publishers and subscribers.
Definition: node.hpp:81
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:39
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:19
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:30
Options to be passed to the executor constructor.