How to Remap Topics in ROS 2

In this tutorial, we will demonstrate how to remap a topic called /chatter from the command line using ROS 2. 

Remapping topics can be useful in several scenarios, such as avoiding naming conflicts when multiple nodes publish or subscribe to topics with the same name, or integrating third-party packages that use different topic names than your existing system.

We will use the built-in demo_nodes_cpp package, which contains a simple publisher (talker) and subscriber (listener) pair.

 The talker node publishes messages on the /chatter topic, and the listener node subscribes to the /chatter topic.

Prerequisites

  • You have ROS 2 installed.

Command Line

First, open a new terminal and source your ROS 2 installation.

Run the talker node without remapping:

ros2 run demo_nodes_cpp talker

Open another terminal, and run the listener node without remapping:

ros2 run demo_nodes_cpp listener

You should see the listener node receiving messages from the talker node on the /chatter topic.

In a new terminal, run ros2 topic list to see the available topics:

ros2 topic list

You should see the /chatter topic in the list.

1-ros2-topic-list

To verify that the talker node is publishing messages on the /chatter topic, run ros2 topic echo:

ros2 topic echo /chatter
2-ros2-topic-echo

You should see the messages being published by the talker node.

Run ros2 topic info to see the total number of publishers and subscribers for the /chatter topic:

ros2 topic info /chatter

You should see that there is one publisher and one subscriber for the /chatter topic.

3-ros2-topic-info

Now, let’s remap the talker node to publish on a new topic called /conversation.

Stop the talker node using CTRL + C, and type this:

ros2 run demo_nodes_cpp talker --ros-args -r /chatter:=/conversation

Check the listener node terminal. You will notice that it is no longer receiving messages because it is still subscribed to the /chatter topic.

Run ros2 topic list again:

ros2 topic list

You will see the new /conversation topic in the list, but the /chatter topic is no longer present.

4-ros2-topic-list-again

If you run ros2 topic echo /conversation, you will see the messages being published by the talker node on the new topic:

ros2 topic echo /conversation

Run ros2 topic info for the /conversation topic:

ros2 topic info /conversation

You should see that there is one publisher and no subscribers for the /conversation topic.

5-ros2-topic-info

To make the listener node receive messages from the talker node again, we need to remap its subscription from /chatter to /conversation. Stop the listener node (Ctrl+C) and run it with the remapping argument:

ros2 run demo_nodes_cpp listener --ros-args -r /chatter:=/conversation

Now, the listener node will receive messages from the talker node on the remapped /conversation topic.

Run ros2 topic info for the /conversation topic again:

ros2 topic info /conversation

You should now see that there is one publisher and one subscriber for the /conversation topic.

In a Launch File

To remap topics using a launch file in ROS 2, you can use the <remap> tag within the <node> tag.

Create a new launch file called remap_demo.launch.py:

#!/usr/bin/env python3
"""
ROS 2 Topic Remapping Launch File.

This launch file demonstrates how to remap topics in ROS 2 by launching a talker and listener
node pair with remapped topic names. It shows how to change the default topic '/chatter'
to '/conversation' for both nodes.

Launch File Nodes:
    * talker_node (demo_nodes_cpp/talker): Publishes messages on remapped topic
    * listener_node (demo_nodes_cpp/listener): Subscribes to messages on remapped topic

Topic Remappings:
    * /chatter -> /conversation: Remaps the default topic to a new name for both nodes

Example Usage:
    $ ros2 launch your_package remap_demo.launch.py

:author: Addison Sears-Collins
:date: November 26, 2024
"""

# Import the necessary ROS 2 launch modules
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    """
    Generate a launch description for topic remapping demonstration.

    This function creates and returns a LaunchDescription object that specifies how
    to launch and configure the talker and listener nodes with topic remapping.

    Returns
    -------
    LaunchDescription
        A launch description object containing the configured nodes
    """
    return LaunchDescription([
        # Create and configure the talker node
        # This node will publish messages on the remapped topic '/conversation'
        Node(
            package='demo_nodes_cpp',      # The package containing the node
            executable='talker',           # The name of the executable
            name='talker_node',           # A unique name for this node instance
            remappings=[
                # Remap the default '/chatter' topic to '/conversation'
                ('/chatter', '/conversation'),
            ],
            output='screen'               # Display node output in the terminal
        ),

        # Create and configure the listener node
        # This node will subscribe to messages on the remapped topic '/conversation'
        Node(
            package='demo_nodes_cpp',      # The package containing the node
            executable='listener',         # The name of the executable
            name='listener_node',         # A unique name for this node instance
            remappings=[
                # Remap the default '/chatter' topic to '/conversation'
                # This must match the talker's remapping to receive messages
                ('/chatter', '/conversation'),
            ],
            output='screen'               # Display node output in the terminal
        ),
    ])

In this launch file, we define two nodes: talker and listener. For each node, we specify the package, executable, and name. We also use the remappings parameter to remap the /chatter topic to /conversation for both nodes.

To run the launch file, open a new terminal, source your ROS 2 installation, and navigate to the package containing the remap_demo.launch.py file. Then, run the following command:

ros2 launch remap_demo.launch.py

You should see both the talker and listener nodes running, with the talker node publishing messages on the /conversation topic and the listener node subscribing to the /conversation topic.

In a new terminal, run ros2 topic list to see the available topics:

ros2 topic list

You should see the /conversation topic in the list.

To verify that the talker node is publishing messages on the /conversation topic, run ros2 topic echo:

ros2 topic echo /conversation

You should see the messages being published by the talker node.

Run ros2 topic info for the /conversation topic:

ros2 topic info /conversation
7-conversation-topic-launch

You should see that there is one publisher and one subscriber for the /conversation topic.

6-one-publisher-one-subcriber

Using a launch file to remap topics has several advantages:

  1. It allows you to centralize the configuration of your nodes and their remappings in a single file.
  2. It makes it easier to manage complex systems with multiple nodes and remappings.
  3. It enables you to create reusable launch files that can be shared across different projects or with other users.

That’s it! Keep building!

How to Run ROS 2 on Multiple Machines

In this tutorial, we will learn how to run ROS 2 on different machines using the ROS_DOMAIN_ID environment variable. 

We will use the built-in demo_nodes_cpp package as an example, running the talker node on one machine to broadcast “Hello World” messages over a topic called /chatter. We will run the listener node on another machine.

Prerequisites

  • You have ROS 2 installed on multiple computers.

Directions

First, ensure both computers are on the same network.

  • Connect both machines to the same WiFi network or connect them using an Ethernet cable.

We will now set the ROS_DOMAIN_ID on the first machine. 

The ROS_DOMAIN_ID is like a unique channel number that allows ROS 2 nodes to communicate with each other when they are set to the same value, preventing interference from other ROS 2 systems on the same network. The default ROS_DOMAIN_ID is 0, and safe values range from 0 to 101, inclusive.

Open a terminal on the first machine.

Set the ROS_DOMAIN_ID to a valid integer value, e.g., 5:

export ROS_DOMAIN_ID=5

Run the talker node:

ros2 run demo_nodes_cpp talker
1-demo-nodes-cpp-talker-ros2

Set the ROS_DOMAIN_ID on the second machine (listener).

Open a terminal on the second machine.

Set the ROS_DOMAIN_ID to the same value as the first machine:

export ROS_DOMAIN_ID=5

Run the listener node:

ros2 run demo_nodes_cpp listener

Observe the communication. The listener node on the second machine should receive messages from the talker node on the first machine.

2-demo-nodes-listener-other-machine

Press CTRL + C on the second machine (the one with the listener node).

Experiment with different ROS_DOMAIN_ID values.

Set a different ROS_DOMAIN_ID on the second machine, e.g., 8:

export ROS_DOMAIN_ID=8

Run the listener node again:

ros2 run demo_nodes_cpp listener

Observe that the listener node no longer receives messages from the talker node because they are on different ROS domains.

3-nothing-received

Make the ROS_DOMAIN_ID change permanent

Open the .bashrc file in a text editor:

gedit ~/.bashrc

Add the following line at the end of the file:

export ROS_DOMAIN_ID=5

Save the file and exit the text editor.

Source the .bashrc file or open a new terminal for the changes to take effect:

source ~/.bashrc

You can now rerun the listener.

ros2 run demo_nodes_cpp listener

Everything is working again.

By following these steps, you can run ROS 2 nodes on different machines using the ROS_DOMAIN_ID environment variable to ensure they communicate on the same ROS domain.

That’s it! Keep building!

Using ros2 doctor to Identify Issues – ROS 2 Jazzy

In this tutorial, we will explore how to use the ros2 doctor command to identify and diagnose issues in your ROS 2 system.

The purpose of ros2 doctor is to check the health and configuration of your ROS 2 setup. It analyzes various aspects such as environment variables, network configuration, and running systems to identify potential issues or warnings.

Real-World Applications

ros2 doctor is useful when:

  • Setting up a new ROS 2 environment to ensure proper configuration
  • Troubleshooting issues or errors in your ROS 2 system
  • Collaborating on ROS 2 projects to ensure consistent setups across team members

Prerequisites

All my code for this project is located here on GitHub.

Check Your Setup

Open a new terminal window.

Run the following command:

ros2 doctor

Review the report for any warnings or errors.

If ros2 doctor finds no warnings, you’ll see “All <n> checks passed”

You don’t have to worry about UserWarnings. UserWarnings indicate non-ideal configurations, but the setup is still usable.

What you do need to pay attention to are errors. If any errors are found, you will see “UserWarning: ERROR:”. In this case, the check is considered failed, and you should address the errors.

Check a System

Let’s use ros2 doctor to analyze an actual system.

We will launch the robot and use ros2 doctor to analyze the running system.

Open a terminal window, and launch your robot.

I will type the following command:

x3 

or

bash ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_bringup/scripts/rosmaster_x3_gazebo.sh

Open another terminal window, and move the robot around:

ros2 run yahboom_rosmaster_system_tests square_mecanum_controller

In a separate terminal, display the messages that are broadcasting over the /cmd_vel topic:

ros2 topic echo /mecanum_drive_controller/cmd_vel

While the robot is running, open another terminal and run:

ros2 doctor

Review the report to see if there are any warnings or errors related to the running nodes, topics, or services.

Get a Full Report

To get a more detailed report from ros2 doctor, use the –report argument.

Open a terminal window, and run the following command:

ros2 doctor --report

The report will provide detailed information categorized into five groups:

  • NETWORK CONFIGURATION
  • PLATFORM INFORMATION
  • RMW MIDDLEWARE
  • ROS 2 INFORMATION
  • TOPIC LIST

Cross-reference the information in the report with the warnings from running ros2 doctor to gain deeper insights into the issues.

That’s it. Keep building!