ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
clock.hpp
1 // Copyright 2017 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__CLOCK_HPP_
16 #define RCLCPP__CLOCK_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <mutex>
21 
22 #include "rclcpp/contexts/default_context.hpp"
23 #include "rclcpp/macros.hpp"
24 #include "rclcpp/time.hpp"
25 #include "rclcpp/visibility_control.hpp"
26 
27 #include "rcl/time.h"
28 #include "rcutils/time.h"
29 #include "rcutils/types/rcutils_ret.h"
30 
31 namespace rclcpp
32 {
33 
34 class TimeSource;
35 
37 {
38 public:
39  RCLCPP_SMART_PTR_DEFINITIONS(JumpHandler)
40 
41  using pre_callback_t = std::function<void ()>;
42  using post_callback_t = std::function<void (const rcl_time_jump_t &)>;
43 
45  pre_callback_t pre_callback,
46  post_callback_t post_callback,
47  const rcl_jump_threshold_t & threshold);
48 
49  pre_callback_t pre_callback;
50  post_callback_t post_callback;
51  rcl_jump_threshold_t notice_threshold;
52 };
53 
54 class Clock
55 {
56 public:
57  RCLCPP_SMART_PTR_DEFINITIONS(Clock)
58 
59 
73  RCLCPP_PUBLIC
74  explicit Clock(rcl_clock_type_t clock_type = RCL_SYSTEM_TIME);
75 
76  RCLCPP_PUBLIC
77  ~Clock();
78 
85  RCLCPP_PUBLIC
86  Time
87  now();
88 
117  RCLCPP_PUBLIC
118  bool
119  sleep_until(
120  Time until,
121  Context::SharedPtr context = contexts::get_global_default_context());
122 
141  RCLCPP_PUBLIC
142  bool
143  sleep_for(
144  Duration rel_time,
145  Context::SharedPtr context = contexts::get_global_default_context());
146 
157  RCLCPP_PUBLIC
158  bool
159  started();
160 
169  RCLCPP_PUBLIC
170  bool
171  wait_until_started(Context::SharedPtr context = contexts::get_global_default_context());
172 
185  RCLCPP_PUBLIC
186  bool
188  const rclcpp::Duration & timeout,
189  Context::SharedPtr context = contexts::get_global_default_context(),
190  const rclcpp::Duration & wait_tick_ns = rclcpp::Duration(0, static_cast<uint32_t>(1e7)));
191 
199  RCLCPP_PUBLIC
200  bool
202 
204  RCLCPP_PUBLIC
205  rcl_clock_t *
206  get_clock_handle() noexcept;
207 
208  RCLCPP_PUBLIC
210  get_clock_type() const noexcept;
211 
213  RCLCPP_PUBLIC
214  std::mutex &
215  get_clock_mutex() noexcept;
216 
217  // Add a callback to invoke if the jump threshold is exceeded.
238  RCLCPP_PUBLIC
239  JumpHandler::SharedPtr
241  JumpHandler::pre_callback_t pre_callback,
242  JumpHandler::post_callback_t post_callback,
243  const rcl_jump_threshold_t & threshold);
244 
245 private:
246  // Invoke time jump callback
247  RCLCPP_PUBLIC
248  static void
249  on_time_jump(
250  const rcl_time_jump_t * time_jump,
251  bool before_jump,
252  void * user_data);
253 
255  class Impl;
256 
257  std::shared_ptr<Impl> impl_;
258 };
259 
260 } // namespace rclcpp
261 
262 #endif // RCLCPP__CLOCK_HPP_
RCLCPP_PUBLIC rcl_clock_t * get_clock_handle() noexcept
Return the rcl_clock_t clock handle.
Definition: clock.cpp:268
RCLCPP_PUBLIC bool ros_time_is_active()
Definition: clock.cpp:251
RCLCPP_PUBLIC bool wait_until_started(Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:195
RCLCPP_PUBLIC bool started()
Definition: clock.cpp:186
RCLCPP_PUBLIC Time now()
Definition: clock.cpp:70
RCLCPP_PUBLIC JumpHandler::SharedPtr create_jump_callback(JumpHandler::pre_callback_t pre_callback, JumpHandler::post_callback_t post_callback, const rcl_jump_threshold_t &threshold)
Definition: clock.cpp:303
RCLCPP_PUBLIC Clock(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Default c'tor.
Definition: clock.cpp:64
RCLCPP_PUBLIC bool sleep_until(Time until, Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:83
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
Definition: clock.cpp:280
RCLCPP_PUBLIC bool sleep_for(Duration rel_time, Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:180
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Encapsulation of a time source.
Definition: time.h:138
Describe the prerequisites for calling a time jump callback.
Definition: time.h:114
Struct to describe a jump in time.
Definition: time.h:95
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
@ RCL_SYSTEM_TIME
Use system time.
Definition: time.h:68