ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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 
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 
218 
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 
270 {
271 private:
272  class ClockWaiterImpl;
273  std::unique_ptr<ClockWaiterImpl> impl_;
274 
275 public:
276  RCLCPP_SMART_PTR_DEFINITIONS(ClockWaiter)
277 
278  RCLCPP_PUBLIC
279  explicit ClockWaiter(const rclcpp::Clock::SharedPtr & clock);
280 
281  RCLCPP_PUBLIC
282  ~ClockWaiter();
283 
294  RCLCPP_PUBLIC
295  bool
296  wait_until(
297  std::unique_lock<std::mutex> & lock,
298  const rclcpp::Time & abs_time, const std::function<bool ()> & pred);
299 
305  RCLCPP_PUBLIC
306  void
307  notify_one();
308 };
309 
310 
320 {
321  class Impl;
322  std::unique_ptr<Impl> impl_;
323 
324 public:
325  RCLCPP_SMART_PTR_DEFINITIONS(ClockConditionalVariable)
326 
327  RCLCPP_PUBLIC
329  const rclcpp::Clock::SharedPtr & clock,
330  rclcpp::Context::SharedPtr context = rclcpp::contexts::get_global_default_context());
331  RCLCPP_PUBLIC
333 
347  RCLCPP_PUBLIC
348  bool
349  wait_until(
350  std::unique_lock<std::mutex> & lock, rclcpp::Time until,
351  const std::function<bool ()> & pred);
352 
357  RCLCPP_PUBLIC
358  void
359  notify_one();
360 
365  RCLCPP_PUBLIC
366  std::mutex &
367  mutex();
368 };
369 
370 
371 } // namespace rclcpp
372 
373 #endif // RCLCPP__CLOCK_HPP_
RCLCPP_PUBLIC void notify_one()
Definition: clock.cpp:582
RCLCPP_PUBLIC std::mutex & mutex()
Definition: clock.cpp:596
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, rclcpp::Time until, const std::function< bool()> &pred)
Definition: clock.cpp:588
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, const rclcpp::Time &abs_time, const std::function< bool()> &pred)
Definition: clock.cpp:497
RCLCPP_PUBLIC void notify_one()
Definition: clock.cpp:505
RCLCPP_PUBLIC rcl_clock_t * get_clock_handle() noexcept
Return the rcl_clock_t clock handle.
Definition: clock.cpp:285
RCLCPP_PUBLIC bool ros_time_is_active()
Definition: clock.cpp:268
RCLCPP_PUBLIC bool wait_until_started(Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:212
RCLCPP_PUBLIC Time now() const
Definition: clock.cpp:74
RCLCPP_PUBLIC bool started()
Definition: clock.cpp:203
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:320
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:87
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
Definition: clock.cpp:297
RCLCPP_PUBLIC bool sleep_for(Duration rel_time, Context::SharedPtr context=contexts::get_global_default_context())
Definition: clock.cpp:197
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