rclpy.timer module

class rclpy.timer.Rate(timer: Timer, *, context: Context)

Bases: object

A utility for sleeping at a fixed rate.

Create a Rate.

Warning

Users should not create a rate with this constructor, instead they should call Node.create_rate().

destroy() None

Destroy a container for a ROS rate.

Warning

Users should not destroy a rate with this method, instead they should call Node.destroy_rate().

sleep() None

Block until timer triggers.

Care should be taken when calling this from a callback. This may block forever if called in a callback in a SingleThreadedExecutor.

class rclpy.timer.Timer(callback: Callable[[], None] | Callable[[TimerInfo], None] | None, callback_group: CallbackGroup, timer_period_ns: int, clock: rclpy.clock.Clock, *, context: Context | None = None, autostart: bool = True)

Bases: object

Create a Timer.

If autostart is True (the default), the timer will be started and every timer_period_sec number of seconds the provided callback function will be called. If autostart is False, the timer will be created but not started; it can then be started by calling reset() on the timer object.

Warning

Users should not create a timer with this constructor, instead they should call Node.create_timer().

Parameters:
  • callback – A user-defined callback function that is called when the timer expires.

  • callback_group – The callback group for the timer. If None, then the default callback group for the node is used.

  • timer_period_ns – The period (in nanoseconds) of the timer.

  • clock – The clock which the timer gets time from.

  • context – The context to be associated with.

  • autostart – Whether to automatically start the timer after creation; defaults to True.

cancel() None
property clock: rclpy.clock.Clock
destroy() None

Destroy a container for a ROS timer.

Warning

Users should not destroy a timer with this method, instead they should call Node.destroy_timer().

property handle: rpyutils.import_c_library.Timer
is_canceled() bool
is_ready() bool
reset() None
time_since_last_call() int
time_until_next_call() int | None
property timer_period_ns: int
class rclpy.timer.TimerInfo(*, expected_call_time: int = 0, actual_call_time: int = 0, clock_type: ClockType = rpyutils.import_c_library.ClockType.SYSTEM_TIME)

Bases: object

Represents a timer call information.

A TimerInfo object encapsulate the timer information when called.

property actual_call_time: Time
Returns:

the actual_call_time.

property expected_call_time: Time
Returns:

the expected_call_time.