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
38 spin_some(rclcpp::Node::SharedPtr node_ptr);
39 
41 
43 void
44 spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
45 
47 void
48 spin(rclcpp::Node::SharedPtr node_ptr);
49 
50 namespace executors
51 {
52 
55 
57 
68 template<typename ResponseT, 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 TimeT = std::milli>
87  rclcpp::executor::Executor & executor,
88  std::shared_ptr<NodeT> node_ptr,
91 {
93  executor,
94  node_ptr->get_node_base_interface(),
95  future,
96  timeout);
97 }
98 
99 } // namespace executors
100 
101 template<typename FutureT, typename TimeT = std::milli>
104  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
107 {
109  return executors::spin_node_until_future_complete<FutureT>(executor, node_ptr, future, timeout);
110 }
111 
112 template<typename NodeT = rclcpp::Node, typename FutureT, typename TimeT = std::milli>
115  std::shared_ptr<NodeT> node_ptr,
118 {
119  return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout);
120 }
121 
122 } // namespace rclcpp
123 
124 #endif // RCLCPP__EXECUTORS_HPP_
Single-threaded executor implementation.
Definition: single_threaded_executor.hpp:40
Definition: allocator_common.hpp:24
rclcpp::executor::FutureReturnCode spin_until_future_complete(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, std::shared_future< FutureT > &future, std::chrono::duration< int64_t, TimeT > timeout=std::chrono::duration< int64_t, TimeT >(-1))
Definition: executors.hpp:103
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:90
void spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and execute any immediately available work.
FutureReturnCode
Return codes to be used with spin_until_future_complete.
Definition: executor.hpp:52
FutureReturnCode spin_until_future_complete(std::shared_future< ResponseT > &future, std::chrono::duration< int64_t, TimeT > timeout=std::chrono::duration< int64_t, TimeT >(-1))
Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
Definition: executor.hpp:214
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Definition: multi_threaded_executor.hpp:32
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< int64_t, TimeT > timeout=std::chrono::duration< int64_t, TimeT >(-1))
Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
Definition: executors.hpp:70
void spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and spin the specified node.