7.2.5. ROS2 Clock

7.2.5.1. 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

7.2.5.2. 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 & ROS 2 Installation.

  • Set the FASTRTPS_DEFAULT_PROFILES_FILE environment variable per instructions in ROS & 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.

7.2.5.3. 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.

7.2.5.4. 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.

7.2.5.5. 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.

7.2.5.6. 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.

7.2.5.6.1. 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.