How to Publish & Subscribe to a ROS2 Topic Using Command line

ROS2 Tutorials #2 How to Publish & Subscribe to a ROS2 Topic [UPDATED]

Written by Alberto Ezquerro

05/07/2019

In this video you will learn how to publish and subscribe to a topic in ROS2 using the command line.

COMMANDS TO USE:

  • ros2 topic list
  • ros2 msg list
  • ros2 msg show std_msgs/String
  • ros2 topic pub /chatter std_msgs/String "data: Hello ROS Developers"
  • ros2 topic echo /chatter

RELATED ROS2 RESOURCES:

  • ROS2 Course for Beginners: Introduce you to the basic concepts that you need to know in order to start working with ROS2.
  • ROS Development Studio: Develop your first ROS2 project online (Ready online including Gazebo simulations + ros1_bridge)

The following steps take through the development as shown in the video:

  • Open ROS Development Studio and create a new project (use a suitable name for the project).
  • Activate the docker image for ROS2 by executing the following command in a SHELL
    $ docker run -it osrf/ros2:ardent-basic

 

  • The first command to know is ros2 topic list. This command is same as the rostopic list command in ros1.
  • The command for publishing a topic is ros2 topic pub. This command takes the topic name and message just like the rostopic pub command in ros1.
  • In ros2, we can use the command ros2 msg list to see all the supported message types

  • Now let’s see the usage with a String type message. We can see the structure of the String message with the following command
    $ ros2 msg show std_msgs/String

The output of this command will give us the information about the content of the message type String. To publish the message we will use the following command

$ ros2 topic pub /chatter std_msgs/String "data: Hello ROS Developers"

  • Once we execute the above command, a message will start publishing to the /chatter topic.

  • Next, we can check the published message by using ros2 topic echo command. This should show us the message on /chatter topic

    $ ros2 topic echo /chatter

The output of the above command should be the message that we encoded in the last command.

 

FEEDBACK:

Did you like this video? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.


ros2-basics-course

Topics: ros2 | ros2 topic
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This