ROS2 Clock

Learning Objectives

In this example, we will

  • Have a brief discussion on the ROS2 Clock publisher and subscriber

  • Publish simulation time to ROS2 as a Clock message

  • Subscribe to a ROS2 Clock message

  • Add a Clock Action Graph using the menu shortcut

Getting Started

Important

Make sure to source your ROS 2 installation from the terminal before running Isaac Sim. If sourcing ROS 2 is a part of your bashrc then Isaac Sim can be run directly.

Prerequisite

  • Complete ROS and ROS 2 Installation.

  • Set the FASTRTPS_DEFAULT_PROFILES_FILE environment variable per instructions in ROS and ROS 2 Installation before launching Isaac Sim, as well as any terminal where ROS messages will be sent or received, and ROS2 Extension is enabled.

Note

In Windows 10 or 11, depending on your machine’s configuration, RViz2 may not open properly.

Simulation Time and Clock

In order for external ROS2 nodes to synchronize with simulation time, a clock topic is usually used. Many ROS2 nodes such as RViz2 use the parameter use_sim_time which if set to True will indicate to the node to begin subscribing to the /clock topic and synchronizing to the published simulation time.

You can either set this parameter in a ROS2 launch file or set the parameter using the following command in a new ROS2-sourced terminal:

ros2 param set /node_name use_sim_time true

Make sure to replace /node_name with whatever node you are currently running. If setting using the terminal, the node must already be running first before setting the parameter.

Running ROS 2 Clock Publisher

  1. Go to Create -> Visual Scripting -> Action Graph to create an Action graph.

  2. Add the following OmniGraph nodes into the Action graph:

  • On Playback Tick node to execute other graph nodes every simulation frame.

  • ROS2 Context node to create a context using either the given Domain ID or the ROS_DOMAIN_ID environment variable.

  • Isaac Read Simulation Time node to retrieve current simulation time. Note: By default the simulation time increases monotonically, meaning regardless of whether simulation is stopped and re-played, the time will continue incrementing. This is mainly to prevent issues that may aries with the time jumping back when simulation resets. You can set resetOnStop to True if you would like the clock to start from 0 every time simulation is reset.

  • ROS2 Publish Clock node to publish simulation time to the /clock topic.

    ROS2 Clock publisher
  1. Start RViz in a new ROS2-sourced terminal.

    ros2 run rviz2 rviz2
    

    Take note of the ROS Time and ROS Elapsed times listed in the bottom of the RViz window. These are currently displaying the wall time and should match the Wall Time and Wall Elapsed fields.

  2. In a new ROS2-sourced terminal set the use_sim_time parameter to true for the RViz node. Ensure that simulation is stopped in Isaac Sim.

    ros2 param set /rviz use_sim_time true
    

    Notice in RViz that ROS Time and ROS Elapsed are now both 0.

  3. In Isaac Sim click Play. In RViz, the ROS Time is now identical to the simulation time published from Isaac Sim over the /clock topic.

Publishing System Time

While publishing the simulation time is the most common workflow, there may be certain workflows that require certain messages to contain system time. To publish system time over the clock topic follow these steps:

  1. Go to Create -> Visual Scripting -> Action Graph to create an Action graph.

  2. Add the following OmniGraph nodes into the Action graph:

  • On Playback Tick node to execute other graph nodes every simulation frame.

  • ROS2 Context node to create a context using either the given Domain ID or the ROS_DOMAIN_ID environment variable.

  • Isaac Read System Time node to retrieve current system time.

  • ROS2 Publish Clock node to publish simulation time to the /clock topic.

    ROS2 Clock publisher with System Time
  1. In Isaac Sim click Play. To see the system timestamp published from Isaac Sim over the /clock topic run the following command in a ROS-sourced terminal:

    ros2 topic echo /clock
    

Camera Helper and RTX Lidar nodes

You have already seen the ROS2 Camera Helper node in a previous tutorial and you will see the ROS2 RTX Lidar Helper node in a following tutorial. As both of these nodes automatically generate a sensor publishing pipeline, to use system timestamps for their publishers, ensure that their useSystemTime input is set to True.

Running ROS 2 Clock Subscriber

  1. Open a new stage. Go to Create -> Visual Scripting -> Action Graph to create an Action graph.

  2. Add the following OmniGraph nodes into the Action graph:

  • On Playback Tick node to execute other graph nodes every simulation frame.

  • ROS2 Context node to create a context using either the given Domain ID or the ROS_DOMAIN_ID environment variable.

  • ROS2 Subscribe Clock node to subscribe to external timestamp data.

    ROS2 Clock subscriber
  1. Start simulation by clicking Play. Select the ROS2 Subscribe Clock node inside the action graph to view its timeStamp output in the Property window. The timestamp should be currently 0.

  2. In a new ROS2-sourced terminal run the following command to manually publish a clock message once:

    ros2 topic pub  -t 1 /clock rosgraph_msgs/Clock "clock: { sec: 1, nanosec: 200000000 }"
    

    The timeStamp value in the ROS2 Subscribe Clock OmniGraph node should change to 1.2.

  3. Change the previous command with different sec and nanosec values to see those values being reflected in the timeStamp field of the ROS2 Subscribe Clock OmniGraph node.

Graph Shortcut

We provide a menu shortcut to build a clock graph within just a few clicks. Go to Isaac Utils > Common Omnigraphs > ROS2 Clock. (If you don’t see any ROS2 graphs listed, you need to first enable the ROS2 bridge). A popup box will appear asking for the parameters needed to populate the graphs. Provide the graph path and click “OK”, a graph publishing the simulated clock should appear on the stage.

Summary

This tutorial covered

  1. Explanation for using the /clock topic and the use_sim_time ROS parameter for time synchronization.

  2. Creating and using ROS2 Clock Publisher and Subscriber nodes.

Next Steps

Continue on to the next tutorial in our ROS2 Tutorials series, RTX Lidar Sensors, to learn how to add a RTX Lidar sensor to the Turtlebot3.