[ROS2 in 5 mins] 006 – What is a ROS2 message?

[ROS2 in 5 mins] 006 - What is a ROS2 message?

Written by Bayode Aderinola

02/12/2018

Hello ROS developers and enthusiasts!

In this short post, we’ll find out what a ROS2 message is and what ROS2 has provided to work with messages. Keeping up with our usual style, we’ll use a practical-centric approach to learning ROS, with some sort of twist here – a practical/theory sandwich!

Let’s go!

Setup for the practicals

To follow this post, you need access to a ROS2 installation. There are two options for this. I recommend option 1; I used it for this post.

  1. Avoid all headaches related to ROS(2) installation and embrace the future — spin a development environment within a few seconds and with a few clicks on ROSDS. If you select this option, to “open a terminal”, just select the Shell app on the ROSDS Tools menu.
  2. Keep all the headaches related with ROS(2) installation; you have good reasons like supporting the drug companies…kidding 🙂 — use a local ROS2 install or a PC with Docker installed.

Okay, you just made your choice; now, let’s shoot the first command. On a new terminal, spin up the ROS2 docker machine. You can skip this if you have ROS2 installed locally:

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

Good job; now we are ready for some action!

So, what is a ROS2 message? Here’s your sandwich!

Well, you had it coming; here you go!

What does ROS2 have to say about messages?

In your open shell, type this command at the shell:

root@9cd3dc8de69a:/# ros2 msg
usage: ros2 msg [-h] Call `ros2 msg <command> -h` for more detailed usage. ...

Various msg related sub-commands

optional arguments:
  -h, --help            show this help message and exit

Commands:
  list      Output a list of available message types
  package   Output a list of available message types within one package
  packages  Output a list of packages which contain messages
  show      Output the message definition

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

That’s about all you get to work with ROS2 messages in the current version.

The makers of ROS2 decided to save you some typing by shortening “message” to “msg”, but you might find yourself typing “ros2 message…” sometimes. Not to worry, it happens, and the shell will faithfully do its job of shocking you back to reality :). But I digress…

Back to the issue on ground: all that output above and not a word about what a ROS2 message is? The shell output is rather assuming, don’t you think? Anyways, time to use some theory. We’ll come back to the shell later on.

A picture speaks a thousand words

So they say. Since the shell couldn’t help us, maybe a picture will. Here is it:

ROS2 messages

Key points about the picture above:

  • Nodes need to communicate with one another.
  • They do so by sending and receiving messages over channels called topics.

These are the basics of ROS(2) messages. We’ll see more in the “tell and show” subsections that follow.

There are different kind of messages

Yes, depending on the goal. For instance, a message to move a robot wheel is different from a message to take a picture with its camera.

Let’s see what the shell has to say about this, using the first command (list) shown in our first shell output:

root@9cd3dc8de69a:/# ros2 msg list
...
geometry_msgs/Twist
...
geometry_msgs/Vector3
...
std_msgs/String
...
root@9cd3dc8de69a:/#

Whew, we have a bucket list there (output truncated)! Those are the message types bundled with ROS2. You can create your own messages, but we won’t talk about that here.

You now know that there are many message type, but there’s one more important thing you should know…

You need to know the structure of messages before you can use them effectively

Yes, that right:

  • Sending nodes need this to compose a valid message.
  • Receiving nodes need this to properly process the message received.

Let’s say we want to send a ‘string’ message “Hello World”. Looking at the list of bundled messages, std_msgs/String looks like a good candidate. How do we compose a message of that type? Here’s where the last command (show) proposed by our first shell output comes in:

root@9cd3dc8de69a:/# ros2 msg show std_msgs/String
string data

So we know from the output above that we need to compose a message object with a single variable data of string type. Now let’s send the message over the wire:

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

Remember we mentioned that we send messages over channels called topics? Here we’re sending out message over the /world channel. If you want to learn more about working with topics, please see this post.

Take home – make your own sandwich!

So far so good! Having taken the sandwich in the section above, I believe you have come of age and can make some yourself now 🙂  . The following were intentionally left out of this post, try them out:

  • Examine the structure of the geometry_msgs/Twist , geometry_msgs/Vector3 and any other message(s) you wish from the bucket list of message types.
  • Try out the message sub-commands shown in the first shell output: package and packages.

Video version of this post

One man’s meat is another man’s fish. If you prefer to learn about ROS2 messages by watching a short video, we have one for your below. Happy watching!

Feedback

Did you like this post or the video? Whatever the case, please leave us your thoughts 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 post or video about it.

Thank you!

You May Also Like…

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.

Share This