ROS 2 rclcpp + rcl - lyrical  lyrical
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/executors/events_cbg_executor/events_cbg_executor.hpp"
25 #include "rclcpp/node.hpp"
26 #include "rclcpp/utilities.hpp"
27 #include "rclcpp/visibility_control.hpp"
28 
29 namespace rclcpp
30 {
31 
47 [[deprecated("use SingleThreadedExecutor::spin_all instead")]]
48 RCLCPP_PUBLIC
49 void
50 spin_all(
51  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr & node_ptr,
52  std::chrono::nanoseconds max_duration);
53 
69 [[deprecated("use SingleThreadedExecutor::spin_all instead")]]
70 RCLCPP_PUBLIC
71 void
72 spin_all(const rclcpp::Node::SharedPtr & node_ptr, std::chrono::nanoseconds max_duration);
73 
88 [[deprecated("use SingleThreadedExecutor::spin_some instead")]]
89 RCLCPP_PUBLIC
90 void
91 spin_some(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr & node_ptr);
92 
107 [[deprecated("use SingleThreadedExecutor::spin_some instead")]]
108 RCLCPP_PUBLIC
109 void
110 spin_some(const rclcpp::Node::SharedPtr & node_ptr);
111 
113 
114 RCLCPP_PUBLIC
115 void
116 spin(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr & node_ptr);
117 
118 RCLCPP_PUBLIC
119 void
120 spin(const rclcpp::Node::SharedPtr & node_ptr);
121 
122 namespace executors
123 {
124 
128 
130 
141 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
143 spin_node_until_future_complete(
144  rclcpp::Executor & executor,
145  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr & node_ptr,
146  const FutureT & future,
147  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
148 {
149  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
150  // inside a callback executed by an executor.
151  executor.add_node(node_ptr);
152  auto retcode = executor.spin_until_future_complete(future, timeout);
153  executor.remove_node(node_ptr);
154  return retcode;
155 }
156 
157 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
158  typename TimeT = std::milli>
160 spin_node_until_future_complete(
161  rclcpp::Executor & executor,
162  const std::shared_ptr<NodeT> & node_ptr,
163  const FutureT & future,
164  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
165 {
166  return rclcpp::executors::spin_node_until_future_complete(
167  executor,
168  node_ptr->get_node_base_interface(),
169  future,
170  timeout);
171 }
172 
173 } // namespace executors
174 
175 template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli>
177 spin_until_future_complete(
178  const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr & node_ptr,
179  const FutureT & future,
180  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
181 {
182  rclcpp::ExecutorOptions options;
183  options.context = node_ptr->get_context();
185  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
186 }
187 
188 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeRepT = int64_t,
189  typename TimeT = std::milli>
191 spin_until_future_complete(
192  const std::shared_ptr<NodeT> & node_ptr,
193  const FutureT & future,
194  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
195 {
196  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
197 }
198 
199 } // namespace rclcpp
200 
201 #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:384
virtual RCLCPP_PUBLIC void remove_node(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr, bool notify=true)
Remove a node from the executor.
Definition: executor.cpp:232
virtual RCLCPP_PUBLIC void add_node(const 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_some(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr)
Create a default single-threaded executor and execute any immediately available work.
Definition: executors.cpp:30
FutureReturnCode
Return codes to be used with spin_until_future_complete.
RCLCPP_PUBLIC void spin(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr)
Create a default single-threaded executor and spin the specified node.
Definition: executors.cpp:39
RCLCPP_PUBLIC void spin_all(const 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
Options to be passed to the executor constructor.