[ROS2 in 5 mins] 005 – How to work with ros2 topics from the command line

[ROS2 in 5 mins] 005 - How to work with ros2 topics from the command line

Written by Bayode Aderinola

25/11/2018

Hello ROS2 developers!

In this post, we’ll see how to work (or play) with ROS2 topics from the command line. This is very useful for tinkering with ROS2 topics as wells as writing and debugging mission-critical ROS2 programs.

Let’s go, shall we?

Getting Ready for the Action

This post is 99.99% practical, so you need a functional ROS2 installation to follow along. We will use one of the awesome Docker images of ROS2 provided by the Open Source Robotics Foundation (OSRF), to keep it easy for everyone and make sure we can tinker with ROS2 without committing too much time to installing it (yet). Choose any of the following options:

  1. Spin a free ROS development environment at ROSDS. With this, you skip all installations; just a few clicks and you will have access a ROS-ready computer within your browser. This is the recommended option and the one used for this post. In this option, a “open a terminal” means you should pick the Shell app from the ROSDS Tools menu.
  2. You have docker installed on your local development machine. Please note that a ROS2 installation is not necessary since we’re using a docker image.

Open a terminal on your machine and spin up the ROS2 docker image:

user:~$ docker run -it osrf/ros2:nightly
Unable to find image 'osrf/ros2:nightly' locally
nightly: Pulling from osrf/ros2
...
root@759719bcb5a5:/#

Important: if the image tag specified is not available, please check out the available tags at https://hub.docker.com/r/osrf/ros2/tags.

Now open three other terminals and run the same command above. We’ll need to work with them in parallel. So we should have four terminals now, Terminals 1 – 4.

ROS2 topics – how you can work (or play) with them

Now we are getting down to it; let’s see how we can work or play with ROS2 topics.

See a list of commands available to work with ROS2

First, we want to see the operations we can do on ROS2 topics. Depending on the version of ROS2 in the image you are able to spin, you might see a slightly different output. The version used in this post is ROS2 Crystal, but the one in the video is ROS2 Bouncy. That said, we’ll be looking at basically the same operations regardless of the RO2 version.

To see what commands you can run against ROS2 topics, type in the command shown below.

In Terminal 1:

root@759719bcb5a5:/# ros2 topic
usage: ros2 topic [-h] [--include-hidden-topics]
                  Call `ros2 topic <command> -h` for more detailed usage. ...

Various topic related sub-commands

optional arguments:
  -h, --help            show this help message and exit
  --include-hidden-topics
                        Consider hidden topics as well

Commands:
  delay  Display delay of topic from timestamp in header
  echo   Output messages from a topic
  hz     Print the average publishing rate to screen
  info   Print information about a topic
  list   Output a list of available topics
  pub    Publish a message to a topic

  Call `ros2 topic <command> -h` for more detailed usage.

There you go! Now let’s try some of these commands.

See a list of available topics

Following the output of the command we ran above, let’s get this list of available topics.

In Terminal 1:

root@759719bcb5a5:/# ros2 topic list
/parameter_events
/rosout

ROS2 created the above topics by default; we didn’t create them. But that’ll change in a bit.

⇒ Take home: run the command ros2 topic list -h to see more options for this command, and try out some.

Create a topic and publish to it at the same time

Now we’ll create and publish to a new topic, /barbarians: run the command below.

In Terminal 1:

root@759719bcb5a5:/# ros2 topic pub /barbarians std_msgs/String "data: Hello World!"
publisher: beginning loop
publishing #1: std_msgs.msg.String(data='Hello World!')

publishing #2: std_msgs.msg.String(data='Hello World!')

publishing #3: std_msgs.msg.String(data='Hello World!')

publishing #4: std_msgs.msg.String(data='Hello World!')

Basically, to publish to a topic, you do ros2 topic pub [topic_name] [message_type] [message_in_right_structure]. For our case above:

  • Our desired topic name was /barbarians
  • The message type I wanted to publish is a String, so I just used the inbuilt message type std_msgs/String.You can get a list of inbuilt messages with ros2 msg list.
  • I “composed” the message according to the structure dictated by std_msgs/String.  I got the structure of this message type with:
root@dd56efdf3a28:/# ros2 msg show std_msgs/String
string data

And that’s it!

Also, by publishing to a topic that did not exist before, we got ROS2 to automatically create it. Let’s confirm that.

In Terminal 2:

root@ed2f3e495885:/# ros2 topic list
/barbarians
/parameter_events
/rosout
root@ed2f3e495885:/#

We could also publish to an existing topic, and we’ll see that shortly.

⇒ Take home: run the command ros2 topic pub -h to see more options for this command, and try out some.

View some pertinent info about a topic

Time to see some info about /barbarians

In Terminal 2:

root@dd56efdf3a28:/# ros2 topic info /barbarians
Topic: /barbarians
Publisher count: 1
Subscriber count: 0
root@dd56efdf3a28:/#

So we see that we have 1 node publishing to the topic, and zero nodes subscribed to it. Since it’s a “barbaric” topic, let’s have another node publishing to it:

In Terminal 3:

root@b1cf8a1224ce:/# ros2 topic pub /barbarians std_ms/String "data: Hello Barbs!"
publisher: beginning loop
publishing #1: std_msgs.msg.String(data='Hello Barbs!')

publishing #2: std_msgs.msg.String(data='Hello Barbs!')

publishing #3: std_msgs.msg.String(data='Hello Barbs!')

Now get back to Terminal 2 and re-run ros2 topic info /barbarians:

root@dd56efdf3a28:/# ros2 topic info /barbarians
Topic: /barbarians
Publisher count: 2
Subscriber count: 0
root@dd56efdf3a28:/#

We now have 2 publishers but, still, there are no subscribers. Grossly unfair, let’s change that narrative!

Subscribe to a topic

To ‘subscribe’ means to get the message(s) being published to a topic. Time to do that for /barbarians, so get set for some babbling.

In Terminal 4:

root@dd56efdf3a28:/# ros2 topic echo /barbarians
data: Hello Barbs!

data: Hello World!

data: Hello Barbs!

data: Hello World!

data: Hello Barbs!

data: Hello World!

So we see the two different messages being published to the topic.

Now, back to Terminal 2, let’s spy on the /barbarian topic once more:

root@ba621804eaa6:/# ros2 topic info /barbarians
Topic: /barbarians
Publisher count: 2
Subscriber count: 1

As expected, we now have 1 subscriber.

More commands to play with?

And that was all! Or maybe not.

We have seen how to use four different commands related to ROS2 topics: list, pub, info, echo. You may have seen that we didn’t cover every command listed. What to do with the ones we didn’t cover? Try them out on your own, of course ;).

The video version of this post

I hope you learned something useful from this post. If you prefer to watch the video version of this post, we have you covered. Please find the video below.

Feedback

Did you like this video? Whatever the case, please leave us some feedback in 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 in the comments area and we will do a video or post about it.

Thank you!

Topics: ROS Q&A | ros2
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

1 Comment

  1. eyeNeeli

    hello, how can i solve this problem with this page showing? eye

    Reply

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