Skip to main content
Version: 1.5.0

ROS Time in ROS2

The clock object

Comparing times

rclcpp::Rate

rclcpp::Duration

Duration object storing an interal of 1 second:

rclcpp::Duration(std::chrono::duration<double>(1.0))

rclcpp::Time

  • creating the Time object out of a floating point time:
rclcpp::Duration(std::chrono::duration<double>(1.0))
  • incrementing time: you can add rclcpp::Duration to rclcpp::Clock , but it needs to have the same clock type:
last_time_ + rclcpp::Duration(secs_passed, clock_->get_clock_type());

or last_time_ + rclcpp::Duration(secs_passed, last_time_->get_clock_type());

Sleeping

What was in ROS1 done as:

ros::Duration(duration).sleep();

is in ROS2 done as:

clock_->sleep_for(std::chrono::duration<double>(duration));

Simulation time