ROS 2 rclcpp + rcl - jazzy  jazzy
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 
209  RCLCPP_PUBLIC
210  void
212 
214  RCLCPP_PUBLIC
215  rcl_clock_t *
216  get_clock_handle() noexcept;
217 
218  RCLCPP_PUBLIC
220  get_clock_type() const noexcept;
221 
223  RCLCPP_PUBLIC
224  std::mutex &
225  get_clock_mutex() noexcept;
226 
227  // Add a callback to invoke if the jump threshold is exceeded.
248  RCLCPP_PUBLIC
249  JumpHandler::SharedPtr
251  JumpHandler::pre_callback_t pre_callback,
252  JumpHandler::post_callback_t post_callback,
253  const rcl_jump_threshold_t & threshold);
254 
255 private:
256  // Invoke time jump callback
257  RCLCPP_PUBLIC
258  static void
259  on_time_jump(
260  const rcl_time_jump_t * time_jump,
261  bool before_jump,
262  void * user_data);
263 
265  class Impl;
266 
267  std::shared_ptr<Impl> impl_;
268 };
269 
280 {
281 private:
282  class ClockWaiterImpl;
283  std::unique_ptr<ClockWaiterImpl> impl_;
284 
285 public:
286  RCLCPP_SMART_PTR_DEFINITIONS(ClockWaiter)
287 
288  RCLCPP_PUBLIC
289  explicit ClockWaiter(const rclcpp::Clock::SharedPtr & clock);
290 
291  RCLCPP_PUBLIC
292  ~ClockWaiter();
293 
304  RCLCPP_PUBLIC
305  bool
306  wait_until(
307  std::unique_lock<std::mutex> & lock,
308  const rclcpp::Time & abs_time, const std::function<bool ()> & pred);
309 
315  RCLCPP_PUBLIC
316  void
317  notify_one();
318 };
319 
320 
330 {
331  class Impl;
332  std::unique_ptr<Impl> impl_;
333 
334 public:
335  RCLCPP_SMART_PTR_DEFINITIONS(ClockConditionalVariable)
336 
337  RCLCPP_PUBLIC
339  const rclcpp::Clock::SharedPtr & clock,
340  rclcpp::Context::SharedPtr context = rclcpp::contexts::get_global_default_context());
341  RCLCPP_PUBLIC
343 
357  RCLCPP_PUBLIC
358  bool
359  wait_until(
360  std::unique_lock<std::mutex> & lock, rclcpp::Time until,
361  const std::function<bool ()> & pred);
362 
367  RCLCPP_PUBLIC
368  void
369  notify_one();
370 
375  RCLCPP_PUBLIC
376  std::mutex &
377  mutex();
378 };
379 
380 
381 } // namespace rclcpp
382 
383 #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)
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