Page

User

Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Understanding ROS Topics

Description: This tutorial introduces ROS topics as well as using the rostopic and rqt_plot commandline tools.

Now you can use the arrow keys of the keyboard to drive the turtle around. If you can not drive the turtle select the terminal window of the turtle_teleop_key to make sure that the keys that you type are recorded.

Now that you can drive your turtle around, let's look at what's going on behind the scenes.

ROS Topics

The turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesimsubscribes to the same topic to receive the key strokes. Let's use rqt_graph which shows the nodes and topics currently running.

Note: If you're using electric or earlier, rqt is not available. Use rxgraph instead.

Using rqt_graph

rqt_graph creates a dynamic graph of what's going on in the system. rqt_graph is part of the rqt package. Unless you already have it installed, run:

replacing <distro> with the name of your ROS distribution (indigo, jade, kinetic)

In a new terminal:

$ rosrun rqt_graph rqt_graph

You will see something similar to:

If you place your mouse over /turtle1/command_velocity it will highlight the ROS nodes (here blue and green) and topics (here red). As you can see, the turtlesim_node and the turtle_teleop_key nodes are communicating on the topic named /turtle1/command_velocity.

Introducing rostopic

The rostopic tool allows you to get information about ROS topics.

You can use the help option to get the available sub-commands for rostopic

Using rostopic echo

Let's look at the command velocity data published by the turtle_teleop_key node.

For ROS Hydro and later, this data is published on the /turtle1/cmd_vel topic. In a new terminal, run:

$ rostopic echo /turtle1/cmd_vel

For ROS Groovy and earlier, this data is published on the /turtle1/command_velocity topic. In a new terminal, run:

$ rostopic echo /turtle1/command_velocity

You probably won't see anything happen because no data is being published on the topic. Let's make turtle_teleop_key publish data by pressing the arrow keys. Remember if the turtle isn't moving you need to select the turtle_teleop_key terminal again.

For ROS Hydro and later, you should now see the following when you press the up key:

Now let's look at rqt_graph again. Press the refresh button in the upper-left to show the new node. As you can see rostopic echo, shown here in red, is now also subscribed to the turtle1/command_velocity topic.

Using rostopic list

rostopic list returns a list of all topics currently subscribed to and published.

Let's figure out what argument the list sub-command needs. In a new terminal run:

ROS Messages

Communication on topics happens by sending ROS messages between nodes. For the publisher (turtle_teleop_key) and subscriber (turtlesim_node) to communicate, the publisher and subscriber must send and receive the same type of message. This means that a topic type is defined by the message type published on it. The type of the message sent on a topic can be determined using rostopic type.

The previous command will send a single message to turtlesim telling it to move with an linear velocity of 2.0, and an angular velocity of 1.8 .

This is a pretty complicated example, so lets look at each argument in detail.

For ROS Hydro and later,

This command will publish messages to a given topic:

rostopic pub

This option (dash-one) causes rostopic to only publish one message then exit:

-1

This is the name of the topic to publish to:

/turtle1/cmd_vel

This is the message type to use when publishing to the topic:

geometry_msgs/Twist

This option (double-dash) tells the option parser that none of the following arguments is an option. This is required in cases where your arguments have a leading dash -, like negative numbers.

--

As noted before, a geometry_msgs/Twist msg has two vectors of three floating point elements each: linear and angular. In this case, '[2.0, 0.0, 0.0]' becomes the linear value with x=2.0, y=0.0, and z=0.0, and '[0.0, 0.0, 1.8]' is the angular value with x=0.0, y=0.0, and z=1.8. These arguments are actually in YAML syntax, which is described more in the YAML command line documentation.

'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

For ROS Groovy and earlier,

This command will publish messages to a given topic:

rostopic pub

This option (dash-one) causes rostopic to only publish one message then exit:

-1

This is the name of the topic to publish to:

/turtle1/command_velocity

This is the message type to use when publishing to the topic:

turtlesim/Velocity

This option (double-dash) tells the option parser that none of the following arguments is an option. This is required in cases where your arguments have a leading dash -, like negative numbers.

--

As noted before, a turtlesim/Velocity msg has two floating point elements : linear and angular. In this case, 2.0 becomes the linear value, and 1.8 is the angular value. These arguments are actually in YAML syntax, which is described more in the YAML command line documentation.

2.0 1.8

You may have noticed that the turtle has stopped moving; this is because the turtle requires a steady stream of commands at 1 Hz to keep moving. We can publish a steady stream of commands using rostopic pub -r command:

Now we can tell that the turtlesim is publishing data about our turtle at the rate of 60 Hz. We can also use rostopic type in conjunction with rosmsg show to get in depth information about a topic:

For ROS Hydro and later,

$ rostopic type /turtle1/cmd_vel | rosmsg show

For ROS Groovy and earlier,

$ rostopic type /turtle1/command_velocity | rosmsg show

Now that we've examined the topics using rostopic let's use another tool to look at the data published by our turtlesim:

Using rqt_plot

Note: If you're using electric or earlier, rqt is not available. Use rxplot instead.

rqt_plot displays a scrolling time plot of the data published on topics. Here we'll use rqt_plot to plot the data being published on the /turtle1/pose topic. First, start rqt_plot by typing

$ rosrun rqt_plot rqt_plot

in a new terminal. In the new window that should pop up, a text box in the upper left corner gives you the ability to add any topic to the plot. Typing /turtle1/pose/x will highlight the plus button, previously disabled. Press it and repeat the same procedure with the topic /turtle1/pose/y. You will now see the turtle's x-y location plotted in the graph.

Pressing the minus button shows a menu that allows you to hide the specified topic from the plot. Hiding both the topics you just added and adding /turtle1/pose/theta will result in the plot shown in the next figure.

That's it for this section, use Ctrl-C to kill the rostopic terminals but keep your turtlesim running.