7.2.13. ROS 2 Python Custom Messages

7.2.13.1. Learning Objectives

In this example, we will learn how to

  • Use ROS 2 rclpy Python interface with Isaac Sim for using a custom message

7.2.13.2. Getting Started

Prerequisite

7.2.13.3. Using Custom Messages with Python

For using rclpy with Isaac Sim the packages must be built with Python3.10. You can create your own package and build it with the ROS 2 workspace For demonstrating the workflow, we will use a custom_message package which is a part of the Isaac Sim ROS Workspace repository. This repository contains a custom message under custom_message/msg/SampleMsg.msg with the following definition:

std_msgs/String my_string
int64 my_num

For Ubuntu 22, Python3.10 is the default version. Hence, packages built can be used directly with rclpy in Isaac Sim. Make sure ROS 2 Humble has been installed Running Native ROS.

  1. Clone Isaac Sim ROS Workspace

  2. Source your existing ROS 2 Humble Installation

    source /opt/ros/humble/setup.bash
    
  3. Build ROS 2 workspace

    cd IsaacSim-ros_workspaces/humble_ws
    
    colcon build
    
  4. Source built workspace and run Isaac Sim from the same terminal

    source install/setup.bash
    

Follow the same process for building your ROS 2 Humble custom message package. Ensure that you source the built workspace before running Isaac Sim from the same terminal.

We provide a sample dockerfile to build a ROS 2 workspace with Python3.10. The steps are similar to Building ROS 2 Workspaces with Python3.10, we will also source our custom_message package workspace at the end before running Isaac Sim.

We will use the minimal dockerfile to build our workspace with Python3.10. Ensure that docker is installed on your system (In Container Setup refer to #2 Install Docker)

  1. Clone Isaac Sim ROS Workspace

  2. Build dockerfile

    cd IsaacSim-ros_workspaces
    
    ./build_foxy.sh
    

    The minimal foxy_ws needed to run Isaac Sim is under build_ws/foxy/foxy_ws. Additional workspaces can also be created and built in this dockerfile.

  3. Now source the ROS 2 workspace

    source build_ws/foxy/foxy_ws/install/setup.bash
    
    source build_ws/foxy/isaac_sim_ros_ws/install/setup.bash
    

    Note

    If you see the warning: not found: "/workspace/foxy_ws/install/local_setup.bash", you can ignore it.

  4. Run Isaac Sim from the same terminal, the sourced workspace contains the minimal ROS 2 Foxy dependencies to enable the ROS 2 bridge and the custom_message package which contains our sample message.

For building your own ROS 2 Foxy custom message packages to use with Isaac Sim, you can place the package under foxy_ws/src in your Isaac Sim ROS Workspace folder. Then run ./build_foxy.sh and source your workspaces before running Isaac Sim.

We will use the minimal dockerfile to build our workspace with Python3.10. Ensure that docker is installed on your system (In Container Setup refer to #2 Install Docker)

  1. Clone Isaac Sim ROS Workspace

  2. Build dockerfile

    cd IsaacSim-ros_workspaces
    
    ./build_humble.sh
    

    The minimal humble_ws needed to run Isaac Sim is under build_ws/humble/humble_ws. Additional workspaces can also be created and built in this dockerfile.

  3. Now source the ROS 2 workspace

    source build_ws/humble/humble_ws/install/setup.bash
    
    source build_ws/humble/isaac_sim_ros_ws/install/setup.bash
    

    Note

    If you see the warning: not found: "/workspace/humble_ws/install/local_setup.bash", you can ignore it.

  4. Run Isaac Sim from the same terminal, the sourced workspace contains the minimal ROS 2 Humble dependencies needed to enable the ROS 2 bridge and the custom_message package which contains our sample message.

For building your own ROS 2 Humble custom message packages to use with Isaac Sim, you can place the package under humble_ws/src in your Isaac Sim ROS Workspace folder. Then run ./build_humble.sh and source your workspaces before running Isaac Sim.

Using the custom_message package with Python in Isaac Sim

Launch Isaac Sim from the sourced terminal containing the custom_message package, enable the ROS 2 Bridge (Window-Extensions-ROS2 Bridge)

Open the Script Editor (Window-Script Editor) and type

import rclpy
from custom_message.msg import SampleMsg

# Create message
sample_msg = SampleMsg()

# assign data in the string part and integer part of the message
sample_msg.my_string.data = "hello from Isaac Sim!"
sample_msg.my_num = 23

print("Message assignment completed!")

Press Run and the Message assignment completed should be logged on your console, indicating that the message import and interaction was successful.

You can use standalone a Python script to enable the ROS 2 bridge and import your custom_message. Navigate to your Isaac Sim installation directory and create a file named ros2_custom_message.py, paste the following content into it

import carb
from omni.isaac.kit import SimulationApp

simulation_app = SimulationApp({"renderer": "RayTracedLighting", "headless": True})

import omni
from omni.isaac.core.utils.extensions import enable_extension

# enable ROS2 bridge extension
enable_extension("omni.isaac.ros2_bridge")

# Make the rclpy imports
import rclpy
from custom_message.msg import SampleMsg

# Create message
sample_msg = SampleMsg()

# assign data in the string part and integer part of the message
sample_msg.my_string.data = "hello from Isaac Sim!"
sample_msg.my_num = 23

print("Message assignment completed!")

Run this script with (make sure your ROS 2 workspace with the custom_message package is sourced in the same terminal before running the script and you are in the right directory which contains ./python.sh)

./python.sh ros2_custom_message.py

Message assignment completed should be logged on your console, indicating that the message import and interaction was successful.

7.2.13.4. Summary

This tutorial covered the following topics:

  1. Building a ROS 2 custom message package with Python3.10

  2. Using the custom message with rclpy in Isaac Sim

  3. Overview of steps to build and use your own custom message package with rclpy and Isaac Sim

7.2.13.4.1. Next Steps

Continue on to the next tutorial in our ROS2 Tutorials series, ROS 2 Navigation with Block World Generator.