ROS - Tutorials - UnderstandingTopics - ROS Wiki
ROS - Tutorials - UnderstandingTopics - ROS Wiki
Note: This tutorial assumes that you have completed the previous tutorials: understanding ROS
nodes (/ROS/Tutorials/UnderstandingNodes).
Please ask about problems and questions regarding this tutorial on answers.ros.org
(http://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.
Contents
1. Setup
1. roscore
2. turtlesim
3. turtle keyboard teleoperation
2. ROS Topics
1. Using rqt_graph
2. Introducing rostopic
3. Using rostopic echo
4. Using rostopic list
3. ROS Messages
1. Using rostopic type
4. rostopic continued
1. Using rostopic pub
2. Using rostopic hz
5. Using rqt_plot
6. Video Tutorial
1. Setup
1.1 roscore
Let's start by making sure that we have roscore running, in a new terminal:
$ roscore
If you left roscore running from the last tutorial, you may get the error message:
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 1/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
1.2 turtlesim
For this tutorial we will also use turtlesim. Please run in a new terminal:
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.
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 2/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
Now that you can drive your turtle around, let's look at what's going on behind the scenes.
2. 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 turtlesim
subscribes to the same topic to receive the key strokes. Let's use rqt_graph (/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.
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 3/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
replacing <distro> with the name of your ROS distribution (/Distributions) (e.g. indigo, jade, kinetic,
lunar ...)
In a new terminal:
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.
You can use the help option to get the available sub-commands for rostopic
$ rostopic -h
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 4/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
$ rostopic
bw echo find hz info list pub type
Usage:
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:
For ROS Groovy and earlier, this data is published on the /turtle1/command_velocity topic. In
a new terminal, run:
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:
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 5/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
For ROS Groovy and earlier, you should now see the following when you press the up key:
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
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.
Let's figure out what argument the list sub-command needs. In a new terminal run:
$ rostopic list -h
Options:
-h, --help show this help message and exit
-b BAGFILE, --bag=BAGFILE
list topics in .bag file
-v, --verbose list full details about each topic
-p list only publishers
-s list only subscribers
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 6/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
$ rostopic list -v
This displays a verbose list of topics to publish to and subscribe to and their type.
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher
* /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher
* /rosout [rosgraph_msgs/Log] 2 publishers
* /rosout_agg [rosgraph_msgs/Log] 1 publisher
* /turtle1/pose [turtlesim/Pose] 1 publisher
Subscribed topics:
* /turtle1/cmd_vel [geometry_msgs/Twist] 1 subscriber
* /rosout [rosgraph_msgs/Log] 1 subscriber
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher
* /turtle1/command_velocity [turtlesim/Velocity] 1 publisher
* /rosout [roslib/Log] 2 publishers
* /rosout_agg [roslib/Log] 1 publisher
* /turtle1/pose [turtlesim/Pose] 1 publisher
Subscribed topics:
* /turtle1/command_velocity [turtlesim/Velocity] 1 subscriber
* /rosout [roslib/Log] 1 subscriber
3. 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.
Usage:
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 7/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
Try:
$ rostopic type /turtle1/cmd_vel
geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x
float64 y
float64 z
Try:
$ rostopic type /turtle1/command_velocity
float32 linear
float32 angular
Now that we know what type of message turtlesim expects, we can publish commands to our turtle.
4. rostopic continued
Now that we have learned about ROS messages, let's use rostopic with messages.
Usage:
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 8/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
The previous command will send a single message to turtlesim telling it to move with a linear velocity
of 2.0, and an angular velocity of 1.8 .
rostopic pub
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 9/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
This option (dash-one) causes rostopic to only publish one message then exit:
-1
/turtle1/cmd_vel
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 (/ROS/YAMLCommandLine).
rostopic pub
This option (dash-one) causes rostopic to only publish one message then exit:
-1
/turtle1/command_velocity
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.
--
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 10/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
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 (/ROS/YAMLCommandLine).
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:
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 11/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
We can also look at what is happening in rqt_graph. Press the refresh button in the upper-left. The
rostopic pub node (here in red) is communicating with the rostopic echo node (here in green):
As you can see the turtle is running in a continuous circle. In a new terminal, we can use
rostopic echo to see the data published by our turtlesim:
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 12/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
Usage:
rostopic hz [topic]
$ rostopic hz /turtle1/pose
subscribed to [/turtle1/pose]
average rate: 59.354
min: 0.005s max: 0.027s std dev: 0.00284s window: 58
average rate: 59.459
min: 0.005s max: 0.027s std dev: 0.00271s window: 118
average rate: 59.539
min: 0.004s max: 0.030s std dev: 0.00339s window: 177
average rate: 59.492
min: 0.004s max: 0.030s std dev: 0.00380s window: 237
average rate: 59.463
min: 0.004s max: 0.030s std dev: 0.00380s window: 290
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:
Now that we've examined the topics using rostopic let's use another tool to look at the data
published by our turtlesim:
5. 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
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
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 13/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
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.
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 14/15
06/11/2019 ROS/Tutorials/UnderstandingTopics - ROS Wiki
That's it for this section, use Ctrl-C to kill the rostopic terminals but keep your turtlesim running.
Now that you understand how ROS topics work, let's look at how services and parameters work
(/ROS/Tutorials/UnderstandingServicesParams).
6. Video Tutorial
The following video presents a small tutorial using turtlesim on ROS nodes and ROS topics
(http://www.osrfoundation.org)
wiki.ros.org/ROS/Tutorials/UnderstandingTopics 15/15