rclcpp  master
C++ ROS Client Library API
static_single_threaded_executor.hpp
Go to the documentation of this file.
1 // Copyright 2019 Nobleo Technology
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__STATIC_SINGLE_THREADED_EXECUTOR_HPP_
16 #define RCLCPP__EXECUTORS__STATIC_SINGLE_THREADED_EXECUTOR_HPP_
17 
18 #include <cassert>
19 #include <cstdlib>
20 #include <memory>
21 #include <vector>
22 #include <string>
23 
24 #include "rmw/rmw.h"
25 
26 #include "rclcpp/executor.hpp"
29 #include "rclcpp/macros.hpp"
31 #include "rclcpp/node.hpp"
32 #include "rclcpp/rate.hpp"
33 #include "rclcpp/utilities.hpp"
35 
36 namespace rclcpp
37 {
38 namespace executors
39 {
40 
42 
58 {
59 public:
61 
62 
66 
70 
72 
78  void
79  spin() override;
80 
82 
92  void
93  add_node(
94  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
95  bool notify = true) override;
96 
98 
103  void
104  add_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true) override;
105 
107 
115  void
116  remove_node(
117  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
118  bool notify = true) override;
119 
121 
125  void
126  remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true) override;
127 
129 
146  template<typename ResponseT, typename TimeRepT = int64_t, typename TimeT = std::milli>
151  {
152  std::future_status status = future.wait_for(std::chrono::seconds(0));
153  if (status == std::future_status::ready) {
155  }
156 
157  auto end_time = std::chrono::steady_clock::now();
158  std::chrono::nanoseconds timeout_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
159  timeout);
160  if (timeout_ns > std::chrono::nanoseconds::zero()) {
161  end_time += timeout_ns;
162  }
163  std::chrono::nanoseconds timeout_left = timeout_ns;
164 
165  entities_collector_->init(&wait_set_, memory_strategy_, &interrupt_guard_condition_);
166 
167  while (rclcpp::ok(this->context_)) {
168  // Do one set of work.
169  entities_collector_->refresh_wait_set(timeout_left);
171  // Check if the future is set, return SUCCESS if it is.
172  status = future.wait_for(std::chrono::seconds(0));
173  if (status == std::future_status::ready) {
175  }
176  // If the original timeout is < 0, then this is blocking, never TIMEOUT.
177  if (timeout_ns < std::chrono::nanoseconds::zero()) {
178  continue;
179  }
180  // Otherwise check if we still have time to wait, return TIMEOUT if not.
181  auto now = std::chrono::steady_clock::now();
182  if (now >= end_time) {
184  }
185  // Subtract the elapsed time from the original timeout.
186  timeout_left = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - now);
187  }
188 
189  // The future did not complete before ok() returned false, return INTERRUPTED.
191  }
192 
193 protected:
195 
200  void
202 
203 private:
205 
206  StaticExecutorEntitiesCollector::SharedPtr entities_collector_;
207 };
208 
209 } // namespace executors
210 } // namespace rclcpp
211 
212 #endif // RCLCPP__EXECUTORS__STATIC_SINGLE_THREADED_EXECUTOR_HPP_
std::shared_future
rclcpp::executors::StaticSingleThreadedExecutor::spin_until_future_complete
rclcpp::FutureReturnCode spin_until_future_complete(std::shared_future< ResponseT > &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: static_single_threaded_executor.hpp:148
rclcpp::executors::StaticSingleThreadedExecutor::add_node
void add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true) override
Add a node to the executor.
rclcpp::FutureReturnCode
FutureReturnCode
Return codes to be used with spin_until_future_complete.
Definition: future_return_code.hpp:33
std::shared_ptr
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::Executor::interrupt_guard_condition_
rcl_guard_condition_t interrupt_guard_condition_
Guard condition for signaling the rmw layer to wake up for special events.
Definition: executor.hpp:329
rmw.h
rclcpp::Executor::memory_strategy_
memory_strategy::MemoryStrategy::SharedPtr memory_strategy_
The memory strategy: an interface for handling user-defined memory allocation strategies.
Definition: executor.hpp:338
node.hpp
rclcpp::executors::StaticSingleThreadedExecutor::~StaticSingleThreadedExecutor
virtual ~StaticSingleThreadedExecutor()
Default destrcutor.
std::chrono::duration
rclcpp::Executor
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:58
std::chrono::nanoseconds::zero
T zero(T... args)
executable_list.hpp
rclcpp::ok
bool ok(rclcpp::Context::SharedPtr context=nullptr)
Check rclcpp's status.
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::executors::StaticSingleThreadedExecutor
Static executor implementation.
Definition: static_single_threaded_executor.hpp:57
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
static_executor_entities_collector.hpp
rclcpp::ExecutorOptions
Options to be passed to the executor constructor.
Definition: executor_options.hpp:28
rclcpp::FutureReturnCode::SUCCESS
@ SUCCESS
rclcpp::executors::StaticSingleThreadedExecutor::remove_node
void remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true) override
Remove a node from the executor.
rclcpp::executors::StaticSingleThreadedExecutor::spin
void spin() override
Static executor implementation of spin.
rclcpp::FutureReturnCode::TIMEOUT
@ TIMEOUT
rate.hpp
rclcpp::FutureReturnCode::INTERRUPTED
@ INTERRUPTED
executor.hpp
rclcpp::executors::StaticSingleThreadedExecutor::StaticSingleThreadedExecutor
StaticSingleThreadedExecutor(const rclcpp::ExecutorOptions &options=rclcpp::ExecutorOptions())
Default constructor. See the default constructor for Executor.
std::shared_future::wait_for
T wait_for(T... args)
rclcpp::Executor::wait_set_
rcl_wait_set_t wait_set_
Wait set for managing entities that the rmw layer waits on.
Definition: executor.hpp:332
visibility_control.hpp
rclcpp::executors::StaticSingleThreadedExecutor::execute_ready_executables
void execute_ready_executables()
Check which executables in ExecutableList struct are ready from wait_set and execute them.
rclcpp::Executor::context_
std::shared_ptr< rclcpp::Context > context_
The context associated with this executor.
Definition: executor.hpp:341
memory_strategies.hpp
utilities.hpp
std::chrono::steady_clock::now
T now(T... args)