ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
Classes | Public Member Functions | List of all members
rclcpp::Clock Class Reference

Classes

class  Impl
 

Public Member Functions

RCLCPP_PUBLIC Clock (rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
 Default c'tor. More...
 
RCLCPP_PUBLIC Time now () const
 
RCLCPP_PUBLIC bool sleep_until (Time until, Context::SharedPtr context=contexts::get_global_default_context())
 
RCLCPP_PUBLIC bool sleep_for (Duration rel_time, Context::SharedPtr context=contexts::get_global_default_context())
 
RCLCPP_PUBLIC bool started ()
 
RCLCPP_PUBLIC bool wait_until_started (Context::SharedPtr context=contexts::get_global_default_context())
 
RCLCPP_PUBLIC bool wait_until_started (const rclcpp::Duration &timeout, Context::SharedPtr context=contexts::get_global_default_context(), const rclcpp::Duration &wait_tick_ns=rclcpp::Duration(0, static_cast< uint32_t >(1e7)))
 
RCLCPP_PUBLIC bool ros_time_is_active ()
 
RCLCPP_PUBLIC rcl_clock_tget_clock_handle () noexcept
 Return the rcl_clock_t clock handle.
 
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type () const noexcept
 
RCLCPP_PUBLIC std::mutex & get_clock_mutex () noexcept
 Get the clock's mutex.
 
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. More...
 

Detailed Description

Definition at line 54 of file clock.hpp.

Constructor & Destructor Documentation

◆ Clock()

rclcpp::Clock::Clock ( rcl_clock_type_t  clock_type = RCL_SYSTEM_TIME)
explicit

Default c'tor.

Initializes the clock instance with the given clock_type.

WARNING Don't instantiate a clock using RCL_ROS_TIME directly, unless you really know what you are doing. By default no TimeSource is attached to a new clock. This will lead to the unexpected behavior, that your RCL_ROS_TIME will run always on system time. If you want a RCL_ROS_TIME use Node::get_clock(), or make sure to attach a TimeSource yourself.

Parameters
clock_typetype of the clock.
Exceptions
anythingrclcpp::exceptions::throw_from_rcl_error can throw.

Definition at line 68 of file clock.cpp.

Referenced by wait_until_started().

Here is the caller graph for this function:

Member Function Documentation

◆ create_jump_callback()

JumpHandler::SharedPtr rclcpp::Clock::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.

These callback functions must remain valid as long as the returned shared pointer is valid.

Function will register callbacks to the callback queue. On time jump all callbacks will be executed whose threshold is greater than the time jump; The logic will first call selected pre_callbacks and then all selected post_callbacks.

Function is only applicable if the clock_type is RCL_ROS_TIME

Parameters
pre_callbackMust be non-throwing
post_callbackMust be non-throwing.
thresholdCallbacks will be triggered if the time jump is greater than the threshold.
Exceptions
anythingrclcpp::exceptions::throw_from_rcl_error can throw.
std::bad_allocif the allocation of the JumpHandler fails.
Warning
the instance of the clock must remain valid as long as any created JumpHandler.

Definition at line 320 of file clock.cpp.

References rcl_clock_add_jump_callback(), and RCL_RET_OK.

Referenced by sleep_until().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ now()

Time rclcpp::Clock::now ( ) const

Returns current time from the time source specified by clock_type.

Returns
current time.
Exceptions
anythingrclcpp::exceptions::throw_from_rcl_error can throw.

Definition at line 74 of file clock.cpp.

References rcl_time_point_s::nanoseconds, rcl_clock_get_now(), and RCL_RET_OK.

Referenced by sleep_for(), sleep_until(), and wait_until_started().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ros_time_is_active()

bool rclcpp::Clock::ros_time_is_active ( )

Returns the clock of the type RCL_ROS_TIME is active.

Returns
true if the clock is active
Exceptions
anythingrclcpp::exceptions::throw_from_rcl_error can throw if the current clock does not have the clock_type RCL_ROS_TIME.

Definition at line 268 of file clock.cpp.

References rcl_clock_valid(), rcl_is_enabled_ros_time_override(), and RCL_RET_OK.

Referenced by sleep_until().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sleep_for()

bool rclcpp::Clock::sleep_for ( Duration  rel_time,
Context::SharedPtr  context = contexts::get_global_default_context() 
)

Sleep for a specified Duration.

Equivalent to

clock->sleep_until(clock->now() + rel_time, context)

The function will return immediately if rel_time is zero or negative.

Parameters
rel_timethe length of time to sleep for.
contextthe rclcpp context the clock should use to check that ROS is still initialized.
Returns
true when the end time is reached
false if time cannot be reached reliably, for example from shutdown or a change of time source.
Exceptions
std::runtime_errorif the context is invalid

Definition at line 197 of file clock.cpp.

References now(), and sleep_until().

Referenced by wait_until_started().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sleep_until()

bool rclcpp::Clock::sleep_until ( Time  until,
Context::SharedPtr  context = contexts::get_global_default_context() 
)

Sleep until a specified Time, according to clock type.

Notes for RCL_ROS_TIME clock type:

  • Can sleep forever if ros time is active and received clock never reaches until
  • If ROS time enabled state changes during the sleep, this method will immediately return false. There is not a consistent choice of sleeping time when the time source changes, so this is up to the caller to call again if needed.
Warning
When using gcc < 10 or when using gcc >= 10 and pthreads lacks the function pthread_cond_clockwait, steady clocks may sleep using the system clock. If so, steady clock sleep times can be affected by system clock time jumps. Depending on the steady clock's epoch and resolution in comparison to the system clock's, an overflow when converting steady clock durations to system clock times may cause undefined behavior. For more info see these issues: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=41861 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58931
Parameters
untilabsolute time according to current clock type to sleep until.
contextthe rclcpp context the clock should use to check that ROS is still initialized.
Returns
true immediately if until is in the past
true when the time until is reached
false if time cannot be reached reliably, for example from shutdown or a change of time source.
Exceptions
std::runtime_errorif the context is invalid
std::runtime_errorif until has a different clock type from this clock

Definition at line 87 of file clock.cpp.

References rcl_time_jump_s::clock_change, create_jump_callback(), rclcpp::Time::get_clock_type(), rcl_jump_threshold_s::min_backward, rcl_jump_threshold_s::min_forward, rcl_duration_s::nanoseconds, rclcpp::Duration::nanoseconds(), rclcpp::Time::nanoseconds(), now(), rcl_jump_threshold_s::on_clock_change, RCL_ROS_TIME, RCL_ROS_TIME_NO_CHANGE, RCL_STEADY_TIME, RCL_SYSTEM_TIME, and ros_time_is_active().

Referenced by sleep_for(), and wait_until_started().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ started()

bool rclcpp::Clock::started ( )

Check if the clock is started.

A started clock is a clock that reflects non-zero time. Typically a clock will be unstarted if it is using RCL_ROS_TIME with ROS time and nothing has been published on the clock topic yet.

Returns
true if clock is started
Exceptions
std::runtime_errorif the clock is not rcl_clock_valid

Definition at line 203 of file clock.cpp.

References get_clock_handle(), rcl_clock_time_started(), and rcl_clock_valid().

Referenced by wait_until_started().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait_until_started() [1/2]

bool rclcpp::Clock::wait_until_started ( const rclcpp::Duration timeout,
Context::SharedPtr  context = contexts::get_global_default_context(),
const rclcpp::Duration wait_tick_ns = rclcpp::Duration(0, static_cast<uint32_t>(1e7)) 
)

Wait for clock to start, with timeout.

The timeout is waited in steady time.

\rclcpp::Clock::started

Parameters
timeoutthe maximum time to wait for.
contextthe context to wait in.
wait_tick_nsthe time to wait between each iteration of the wait loop (in nanoseconds).
Returns
true if clock was or became valid
Exceptions
std::runtime_errorif the context is invalid or clock is not rcl_clock_valid

Definition at line 230 of file clock.cpp.

References Clock(), get_clock_handle(), now(), rcl_clock_valid(), RCL_STEADY_TIME, sleep_for(), and started().

Here is the call graph for this function:

◆ wait_until_started() [2/2]

bool rclcpp::Clock::wait_until_started ( Context::SharedPtr  context = contexts::get_global_default_context())

Wait until clock to start.

\rclcpp::Clock::started

Parameters
contextthe context to wait in
Returns
true if clock was already started or became started
Exceptions
std::runtime_errorif the context is invalid or clock is not rcl_clock_valid

Definition at line 212 of file clock.cpp.

References get_clock_handle(), rcl_clock_valid(), sleep_until(), and started().

Here is the call graph for this function:

The documentation for this class was generated from the following files: