Skip to content

Understanding ROS Time

Nick Charron edited this page Aug 19, 2021 · 4 revisions

The following are a compilation of notes that provide an overview of how ROS time works, as well as answers to commonly encountered issues when working with ROS time. The aim of these notes is to address the lack of documentation available online on the topic.

ROSCORE:

  • Launching roscore in a terminal when nothing else is running will not start ros time.
  • You can run roscore without worrying about initializing ros time at an incorrect time

Wall time vs. Sim time:

  • Wall time is the absolute time, or world time i.e. time on your watch
  • Sim time is the time set by your rosbag.

rosbag play and "--clock" argument:

  • When you run rosbag play /path/to/bag/bag_name.bag --clock, it will set the sim time to time that your rosbag recorded. This is usually the wall time that was recorded during your "rosbag record".

  • Your timetamps for all your messages recorded in that bag should have this sim time.

  • When playing back data and using this data for further processing, you want to make sure everything lines up with your timestamps of your bagged messages, so you want to use this sim time.

ROSPARAM /use_sim_time:

  • This ros parameter is used to set the ros time to either the simulation time or the wall time.

"Initializing ROS" vs "initialize ROS time" inside nodes:

There are two options that can be called inside nodes:

  • ros::init()
    • initializes ros and ros time, but does not start the clock
    • if you initialize ros without initializing the ros time, your clock will be zero or
  • ros::Time::init()
    • starts the clock

Possible Initialization Cases:

  1. /use_sim_time false:

    • ros::init() (with or without ros::Time::init())
      • This will initialize your clock with the wall time
      • Doesn't matter if the bag was started or not
    • set /use_sim_time true and rosbag play (with or without --clock)
      • ROS time remains at wall time

Summary: if you initialize ros or ros::Time with the sim time to false, your time will always be wall time until you stop ros (all nodes) and then set the sim time to true and re-init ROS

  1. /use_sim_time true:

    • ros::init()
      • Will not start your ros time (ros time will be 0)
      • ros time will not start until you launch a bag file with the --clock argument
    • ros::init() -> ros::Time::init()
      • Will start your ros time at wall time
      • Once you run your bag with the --clock argument, the ros time will change to the bag time
    • rosbag play --clock + ros::init()
      • Will start ros time and set it to the sim (bag) time
      • If you stop the bag, it will stop the ros time, and your node will stop EVERYTHING. Won't even keep outputting to the terminal
      • If you stop and rerun the bag it will restart the time to the start of the bag time as if you never ran anything before
    • rosbag play --clock + ros::init() + ros::Time::init()
      • Same exact thing as 2c
      • adding the ros::Time::init() did nothing
    • rosbag play -> ros::init()
      • Time will not start (ros time will be 0)
      • You can restart the bag with the --clock arg and it will set the ros time to the bag time.
    • rosbag play -> ros::init() -> ros::Time::init()
      • Time will start at the wall time
      • You can still set the ros time to the bag time by restarting the bag with the --clock argument

Summary: if you set /use_sim_time to true before launching anything, you need to make sure you start the bag with the --clock argument before running any node, or else it will start your ros time at zero (if you do not run ros::Time::init) or to the clock time (if you do set ros::Time::init). If you run another node with the ros::Time::init() that node will still be at the current bag time. This shouldn't mess anything up.

Displaying current ROS time:

If you are unsure of the current ROS time, or want to debug any issues that may be explained by the information above, there's two options to display the current ROS time:

  • From a node: Get the current ROS time in a node (preferable in a callback) using ros::Time::now(), then cout.
  • From the terminal: You can directly output the current ROS time from the terminal by using the rostopic pub to publish the time, then echo it in another terminal
rostopic pub -s -r 1 /current_ros_time std_msgs/Time 'now'

Author: Nick Charron

Last Edited: 18 Feb 2019

Clone this wiki locally