ROS 2 rclcpp + rcl - kilted  kilted
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() const;
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 
213  [[deprecated("Use ClockConditionalVariable")]]
214  RCLCPP_PUBLIC
215  void
217 
219  RCLCPP_PUBLIC
220  rcl_clock_t *
221  get_clock_handle() noexcept;
222 
223  RCLCPP_PUBLIC
225  get_clock_type() const noexcept;
226 
228  RCLCPP_PUBLIC
229  std::mutex &
230  get_clock_mutex() noexcept;
231 
233 
253  RCLCPP_PUBLIC
254  JumpHandler::SharedPtr
256  JumpHandler::pre_callback_t pre_callback,
257  JumpHandler::post_callback_t post_callback,
258  const rcl_jump_threshold_t & threshold);
259 
260 private:
261  // Invoke time jump callback
262  RCLCPP_PUBLIC
263  static void
264  on_time_jump(
265  const rcl_time_jump_t * time_jump,
266  bool before_jump,
267  void * user_data);
268 
270  class Impl;
271 
272  std::shared_ptr<Impl> impl_;
273 };
274 
285 {
286 private:
287  class ClockWaiterImpl;
288  std::unique_ptr<ClockWaiterImpl> impl_;
289 
290 public:
291  RCLCPP_SMART_PTR_DEFINITIONS(ClockWaiter)
292 
293  RCLCPP_PUBLIC
294  explicit ClockWaiter(const rclcpp::Clock::SharedPtr & clock);
295 
296  RCLCPP_PUBLIC
297  ~ClockWaiter();
298 
309  RCLCPP_PUBLIC
310  bool
311  wait_until(
312  std::unique_lock<std::mutex> & lock,
313  const rclcpp::Time & abs_time, const std::function<bool ()> & pred);
314 
320  RCLCPP_PUBLIC
321  void
322  notify_one();
323 };
324 
325 
335 {
336  class Impl;
337  std::unique_ptr<Impl> impl_;
338 
339 public:
340  RCLCPP_SMART_PTR_DEFINITIONS(ClockConditionalVariable)
341 
342  RCLCPP_PUBLIC
344  const rclcpp::Clock::SharedPtr & clock,
345  rclcpp::Context::SharedPtr context = rclcpp::contexts::get_global_default_context());
346  RCLCPP_PUBLIC
348 
362  RCLCPP_PUBLIC
363  bool
364  wait_until(
365  std::unique_lock<std::mutex> & lock, rclcpp::Time until,
366  const std::function<bool ()> & pred);
367 
372  RCLCPP_PUBLIC
373  void
374  notify_one();
375 
380  RCLCPP_PUBLIC
381  std::mutex &
382  mutex();
383 };
384 
385 
386 } // namespace rclcpp
387 
388 #endif // RCLCPP__CLOCK_HPP_
RCLCPP_PUBLIC void notify_one()
Definition: clock.cpp:592
RCLCPP_PUBLIC std::mutex & mutex()
Definition: clock.cpp:606
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, rclcpp::Time until, const std::function< bool()> &pred)
Definition: clock.cpp:598
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, const rclcpp::Time &abs_time, const std::function< bool()> &pred)
Definition: clock.cpp:507
RCLCPP_PUBLIC void notify_one()
Definition: clock.cpp:515
RCLCPP_PUBLIC rcl_clock_t * get_clock_handle() noexcept
Return the rcl_clock_t clock handle.
Definition: clock.cpp:295
RCLCPP_PUBLIC bool ros_time_is_active()
Definition: clock.cpp:278
RCLCPP_PUBLIC bool wait_until_started(Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:222
RCLCPP_PUBLIC Time now() const
Definition: clock.cpp:74
RCLCPP_PUBLIC bool started()
Definition: clock.cpp:213
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)
Add a callback to invoke if the jump threshold is exceeded.
Definition: clock.cpp:330
RCLCPP_PUBLIC Clock(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Default c'tor.
Definition: clock.cpp:68
RCLCPP_PUBLIC bool sleep_until(Time until, Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:97
RCLCPP_PUBLIC void cancel_sleep_or_wait()
Definition: clock.cpp:87
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
Definition: clock.cpp:307
RCLCPP_PUBLIC bool sleep_for(Duration rel_time, Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:207
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