Understanding Topic Communication - Part 1
In this section, we learn more about topic communication with tangible examples. Before starting, run the previously used two ROS nodes following here.
Topic Interface
Examining Messages Being Published
A publisher of a topic sends the message stream in a defined way. For example, a
publisher of sensor data periodically sends out the messages. In our example,
/turtlesim
keeps sending messages for /turtle1/pose
. This can be checked by:
rosmarry@rosmarry:~$ ros2 node info /turtlesim # ros2 node info <node_name>
/turtlesim
Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/turtle1/cmd_vel: geometry_msgs/msg/Twist
Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
/turtle1/color_sensor: turtlesim/msg/Color
/turtle1/pose: turtlesim/msg/Pose
Service Servers:
...
Service Clients:
Action Servers:
...
Action Clients:
From the registrations, we focus on the topic publishing /turtle1/pose
and
print the message stream being published:
rosmarry@rosmarry:~$ ros2 topic echo /turtle1/pose # ros topic echo <topic_name>
x: 5.544444561004639
y: 5.544444561004639
theta: 0.0
linear_velocity: 0.0
angular_velocity: 0.0
---
x: 5.544444561004639
y: 5.544444561004639
theta: 0.0
linear_velocity: 0.0
angular_velocity: 0.0
---
...
Try the below command to get periodicity:
rosmarry@rosmarry:~$ ros2 topic hz /turtle1/pose # ros topic hz <topic_name>
average rate: 62.545 # being published at around 60Hz
min: 0.015s max: 0.017s std dev: 0.00046s window: 64
average rate: 62.480
min: 0.015s max: 0.017s std dev: 0.00045s window: 127
This periodical publishing is not always the case. The below command prints nothing.
ros2 topic echo /turtle1/cmd_vel
If you press the upper-arrow key on the keyboard after focusing the terminal
where /teleop_turtle
is running, now a print appears.
rosmarry@rosmarry:~$ ros2 topic echo /turtle1/cmd_vel
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
The topic /turtle1/cmd_vel
seems to be printed only when keyboard event is
received. You will note that the turtle in the GUI has moved. You may try
sending more messages with keyboard and observe the turtle's movement. π
Message Types
In ROS 2, message structures are described using interfaces. The interfaces are defined in packages. These interfaces are composed of multiple fields, where each field's data type can be derived from primitive types like integers, floats, strings, and more (opens in a new tab). This allows developers to reuse or define the structure of the data being communicated between nodes.
To get the type name of a topic, we first use the following command:
rosmarry@rosmarry:~$ ros2 topic info /turtle1/pose # ros2 topic info <topic_name>
Type: turtlesim/msg/Pose
Publisher count: 1
Subscription count: 0
The message associated with /turtle1/pose
has the interface
turtlesim/msg/Pose
, which consists of the below field:
rosmarry@rosmarry:~$ ros2 interface show turtlesim/msg/Pose # ros2 interface show <type_name>
float32 x
float32 y
float32 theta
float32 linear_velocity
float32 angular_velocity
This is why printing /turtle1/pose
gives us
this output. Type name turtlesim/msg/Pose
implies that
package turtlesim
holds the interface definition. This package has several
interfaces as this command shows:
rosmarry@rosmarry:~$ ros2 interface package turtlesim # ros2 interface package <package_name>
turtlesim/action/RotateAbsolute
turtlesim/msg/Color
turtlesim/srv/SetPen
turtlesim/srv/Spawn
turtlesim/srv/TeleportRelative
turtlesim/srv/TeleportAbsolute
turtlesim/srv/Kill
turtlesim/msg/Pose
Publishing Topic
In the previous section, we observed that the turtle can be moved by your
keyboard input, as /turtlesim
is subscribing topic /turtle1/cmd_vel
published from /teleop_turtle
. This means the turtle will move even if another
node is publishing messages on the same topic βοΈ
Open another terminal and run the command:
rosmarry@rosmarry:~$ ros2 topic pub /turtle1/cmd_vel -r 10 geometry_msgs/msg/Twist "{linear: {x: 1.0, y: 0, z: 0}, angular: {x: 0, y: 0, z: 1}}" --print 0
publisher: beginning loop
The command breaks down as:
ros2 topic pub /turtle1/cmd_vel
: publishing on the topic/turtle1/cmd_vel
,-r 10
: at rate 10 Hz,geometry_msgs/msg/Twist
: the interface is this type,"{linear: {x: 1.0, y: 0, z: 0}, angular: {x: 0, y: 0, z: 1}}"
: and the message contents be like this.--print 0
: do not fill the terminal.
This will move the turtle as below:

Using GUI tools
Although tab completion helps you, it might not be handy to type those commands every time. In contrast to CLI (opens in a new tab) (commands in terminal), rqt_gui (opens in a new tab) provides graphical UIs. Running the commands will open a qt window.
ros2 run rqt_gui rqt_gui
In the GUI, follow the below video.