15 #include "rclcpp/clock.hpp"
17 #include <condition_variable>
21 #include "rclcpp/exceptions.hpp"
22 #include "rclcpp/utilities.hpp"
24 #include "rcpputils/scope_exit.hpp"
25 #include "rcutils/logging_macros.h"
38 exceptions::throw_from_rcl_error(ret,
"failed to initialize rcl clock");
46 RCUTILS_LOG_ERROR(
"Failed to fini rcl clock.");
52 std::mutex clock_mutex_;
55 JumpHandler::JumpHandler(
56 pre_callback_t pre_callback,
57 post_callback_t post_callback,
59 : pre_callback(pre_callback),
60 post_callback(post_callback),
61 notice_threshold(threshold)
72 Time now(0, 0, impl_->rcl_clock_.type);
76 exceptions::throw_from_rcl_error(ret,
"could not get current time stamp");
85 if (!context || !context->is_valid()) {
86 throw std::runtime_error(
"context cannot be slept with because it's invalid");
88 const auto this_clock_type = get_clock_type();
90 throw std::runtime_error(
"until's clock type does not match this clock's type");
92 bool time_source_changed =
false;
94 std::condition_variable cv;
102 auto callback_remover = rcpputils::scope_exit(
103 [context, &shutdown_cb_handle]() {
104 context->remove_on_shutdown_callback(shutdown_cb_handle);
110 const std::chrono::steady_clock::time_point chrono_entry = std::chrono::steady_clock::now();
111 const Duration delta_t = until - rcl_entry;
112 const std::chrono::steady_clock::time_point chrono_until =
113 chrono_entry + std::chrono::nanoseconds(delta_t.
nanoseconds());
116 std::unique_lock lock(impl_->clock_mutex_);
117 while (
now() < until && context->is_valid()) {
118 cv.wait_until(lock, chrono_until);
121 auto system_time = std::chrono::system_clock::time_point(
123 std::chrono::duration_cast<std::chrono::system_clock::duration>(
127 std::unique_lock lock(impl_->clock_mutex_);
128 while (
now() < until && context->is_valid()) {
129 cv.wait_until(lock, system_time);
144 time_source_changed = true;
151 auto system_time = std::chrono::system_clock::time_point(
153 std::chrono::duration_cast<std::chrono::system_clock::duration>(
157 std::unique_lock lock(impl_->clock_mutex_);
158 while (
now() < until && context->is_valid() && !time_source_changed) {
159 cv.wait_until(lock, system_time);
165 std::unique_lock lock(impl_->clock_mutex_);
166 while (
now() < until && context->is_valid() && !time_source_changed) {
172 if (!context->is_valid() || time_source_changed) {
176 return now() >= until;
189 throw std::runtime_error(
"clock is not rcl_clock_valid");
197 if (!context || !context->is_valid()) {
198 throw std::runtime_error(
"context cannot be slept with because it's invalid");
201 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
215 Context::SharedPtr context,
218 if (!context || !context->is_valid()) {
219 throw std::runtime_error(
"context cannot be slept with because it's invalid");
222 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
226 Time start = timeout_clock.
now();
230 while (!
started() && context->is_valid()) {
231 if (timeout < wait_tick_ns) {
234 Duration time_left = start + timeout - timeout_clock.
now();
235 if (time_left > wait_tick_ns) {
242 if (timeout_clock.
now() - start > timeout) {
254 RCUTILS_LOG_ERROR(
"ROS time not valid!");
258 bool is_enabled =
false;
261 exceptions::throw_from_rcl_error(
262 ret,
"Failed to check ros_time_override_status");
270 return &impl_->rcl_clock_;
274 Clock::get_clock_type() const noexcept
276 return impl_->rcl_clock_.type;
282 return impl_->clock_mutex_;
291 const auto * handler =
static_cast<JumpHandler *
>(user_data);
292 if (
nullptr == handler) {
295 if (before_jump && handler->pre_callback) {
296 handler->pre_callback();
297 }
else if (!before_jump && handler->post_callback) {
298 handler->post_callback(*time_jump);
302 JumpHandler::SharedPtr
304 JumpHandler::pre_callback_t pre_callback,
305 JumpHandler::post_callback_t post_callback,
309 JumpHandler::UniquePtr handler(
new JumpHandler(pre_callback, post_callback, threshold));
310 if (
nullptr == handler) {
311 throw std::bad_alloc{};
315 std::lock_guard<std::mutex> clock_guard(impl_->clock_mutex_);
318 &impl_->rcl_clock_, threshold, Clock::on_time_jump,
321 exceptions::throw_from_rcl_error(ret,
"Failed to add time jump callback");
325 std::weak_ptr<Clock::Impl> weak_impl = impl_;
328 return JumpHandler::SharedPtr(handler.release(), [weak_impl](
JumpHandler * handler) noexcept {
329 auto shared_impl = weak_impl.lock();
331 std::lock_guard<std::mutex> clock_guard(shared_impl->clock_mutex_);
332 rcl_ret_t ret = rcl_clock_remove_jump_callback(&shared_impl->rcl_clock_,
333 Clock::on_time_jump, handler);
334 if (RCL_RET_OK != ret) {
335 RCUTILS_LOG_ERROR(
"Failed to remove time jump callback");
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
RCLCPP_PUBLIC rcl_clock_t * get_clock_handle() noexcept
Return the rcl_clock_t clock handle.
RCLCPP_PUBLIC bool ros_time_is_active()
RCLCPP_PUBLIC bool wait_until_started(Context::SharedPtr context=contexts::get_global_default_context())
RCLCPP_PUBLIC bool started()
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)
RCLCPP_PUBLIC Clock(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Default c'tor.
RCLCPP_PUBLIC bool sleep_until(Time until, Context::SharedPtr context=contexts::get_global_default_context())
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
RCLCPP_PUBLIC bool sleep_for(Duration rel_time, Context::SharedPtr context=contexts::get_global_default_context())
rcl_duration_value_t nanoseconds() const
Get duration in nanosecods.
RCLCPP_PUBLIC rcl_time_point_value_t nanoseconds() const
Get the nanoseconds since epoch.
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type() const
Get the clock type.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Encapsulation of a time source.
rcl_duration_value_t nanoseconds
Duration in nanoseconds and its source.
Describe the prerequisites for calling a time jump callback.
rcl_duration_t min_forward
bool on_clock_change
True to call callback when the clock type changes.
rcl_duration_t min_backward
Struct to describe a jump in time.
rcl_clock_change_t clock_change
Indicate whether or not the source of time changed.
rcl_time_point_value_t nanoseconds
Nanoseconds of the point in time.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_get_now(rcl_clock_t *clock, rcl_time_point_value_t *time_point_value)
Fill the time point value with the current value of the associated clock.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_fini(rcl_clock_t *clock)
Finalize a clock.
@ RCL_ROS_TIME_NO_CHANGE
The source before and after the jump is ROS_TIME.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_clock_time_started(rcl_clock_t *clock)
Check if the clock has started.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_add_jump_callback(rcl_clock_t *clock, rcl_jump_threshold_t threshold, rcl_jump_callback_t callback, void *user_data)
Add a callback to be called when a time jump exceeds a threshold.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_is_enabled_ros_time_override(rcl_clock_t *clock, bool *is_enabled)
Check if the RCL_ROS_TIME time source has the override enabled.
@ RCL_ROS_TIME
Use ROS time.
@ RCL_SYSTEM_TIME
Use system time.
@ RCL_STEADY_TIME
Use a steady clock time.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_init(rcl_clock_type_t clock_type, rcl_clock_t *clock, rcl_allocator_t *allocator)
Initialize a clock based on the passed type.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_clock_valid(rcl_clock_t *clock)
Check if the clock has valid values.
#define RCL_RET_OK
Success return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.