rclcpp  master
C++ ROS Client Library API
synchronization_policy_common.hpp
Go to the documentation of this file.
1 // Copyright 2020 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__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
16 #define RCLCPP__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 
21 namespace rclcpp
22 {
23 namespace wait_set_policies
24 {
25 namespace detail
26 {
27 
30 {
31 protected:
32  SynchronizationPolicyCommon() = default;
33  ~SynchronizationPolicyCommon() = default;
34 
35  std::function<bool()>
37  std::chrono::nanoseconds time_to_wait_ns,
38  std::chrono::steady_clock::time_point start)
39  {
40  if (time_to_wait_ns >= std::chrono::nanoseconds(0)) {
41  // If time_to_wait_ns is >= 0 schedule against a deadline.
42  auto deadline = start + time_to_wait_ns;
43  return [deadline]() -> bool {return std::chrono::steady_clock::now() < deadline;};
44  } else {
45  // In the case of time_to_wait_ns < 0, just always return true to loop forever.
46  return []() -> bool {return true;};
47  }
48  }
49 
52  std::chrono::nanoseconds original_time_to_wait_ns,
53  std::chrono::steady_clock::time_point start)
54  {
55  std::chrono::nanoseconds time_left_to_wait;
56  if (original_time_to_wait_ns < std::chrono::nanoseconds(0)) {
57  time_left_to_wait = original_time_to_wait_ns;
58  } else {
59  time_left_to_wait = original_time_to_wait_ns - (std::chrono::steady_clock::now() - start);
60  if (time_left_to_wait < std::chrono::nanoseconds(0)) {
61  time_left_to_wait = std::chrono::nanoseconds(0);
62  }
63  }
64  return time_left_to_wait;
65  }
66 };
67 
68 } // namespace detail
69 } // namespace wait_set_policies
70 } // namespace rclcpp
71 
72 #endif // RCLCPP__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
rclcpp::wait_set_policies::detail::SynchronizationPolicyCommon::SynchronizationPolicyCommon
SynchronizationPolicyCommon()=default
rclcpp::wait_set_policies::detail::SynchronizationPolicyCommon::create_loop_predicate
std::function< bool()> create_loop_predicate(std::chrono::nanoseconds time_to_wait_ns, std::chrono::steady_clock::time_point start)
Definition: synchronization_policy_common.hpp:36
rclcpp::wait_set_policies::detail::SynchronizationPolicyCommon::calculate_time_left_to_wait
std::chrono::nanoseconds calculate_time_left_to_wait(std::chrono::nanoseconds original_time_to_wait_ns, std::chrono::steady_clock::time_point start)
Definition: synchronization_policy_common.hpp:51
std::chrono::nanoseconds
std::function
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::wait_set_policies::detail::SynchronizationPolicyCommon
Common structure for synchronization policies.
Definition: synchronization_policy_common.hpp:29
rclcpp::wait_set_policies::detail::SynchronizationPolicyCommon::~SynchronizationPolicyCommon
~SynchronizationPolicyCommon()=default
std::chrono::steady_clock::now
T now(T... args)