ROS 2 breaks complex systems down into many modular nodes. Topics are a vital element of the ROS graph that act as a bus for nodes to exchange messages.
A node may publish data to any number of topics and simultaneously have subscriptions to any number of topics. Topics are one of the main ways in which data is moved between nodes and therefore between different parts of the system.
Commands
ros2 topic list
Running the ros2 topic list
command will return a list of all the topics currently active in the system, such as
/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
And ros2 topic list -t
will return the same list of topics, this time with the topic type appended in brackets:
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]
/turtle1/cmd_vel [geometry_msgs/msg/Twist]
/turtle1/color_sensor [turtlesim/msg/Color]
/turtle1/pose [turtlesim/msg/Pose]
These attributes, particularly the type, are how nodes know they're talking about the same information as it moves over topics.
ros2 topic echo
To see the data being published on a topic, use:
ros2 topic echo <topic_name>
For example, the following command
ros2 topic echo /turtle1/cmd_vel
may return
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
ros2 topic info
Topics don't have to only be one-to-one communication; they can be one-to-many, many-to-one, or many-to-many.
Another way to look at this is running:
ros2 topic info /turtle1/cmd_vel
Which will return:
Type: geometry_msgs/msg/Twist
Publisher count: 1
Subscription count: 2
ros2 interface show
Nodes send data over topics using messages. Publishers and subscribers must send and receive the same type of message to communicate.
The topic types we saw earlier after running ros2 topic list -t
let us know what message type is used on each topic. Recall that the cmd_vel
topic has the type:
geometry_msgs/msg/Twist
This means that in the package geometry_msgs
there is a msg
called Twist
.
Now we can run ros2 interface show <msg type>
on this type to learn its details. Specifically, what structure of data the message expects.
ros2 interface show geometry_msgs/msg/Twist
For the message type from above it yields:
# This expresses velocity in free space broken into its linear and angular parts.
Vector3 linear
float64 x
float64 y
float64 z
Vector3 angular
float64 x
float64 y
float64 z
ros2 topic pub
Now that you have the message structure, you can publish data to a topic directly from the command line using:
ros2 topic pub <topic_name> <msg_type> '<args>'
The '<args>'
argument is the actual data you'll pass to the topic, in the structure you just discovered in the previous section.
It's important to note that this argument needs to be input in YAML syntax. Input the full command like so:
ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
--once
is an optional argument meaning "publish one message then exit".
However, the turtle (and commonly the real robots which it is meant to emulate) require a steady stream of commands to operate continuously. So, to get the turtle to keep moving, you can run:
ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
The difference here is the removal of the --once
option and the addition of the --rate 1
option, which tells ros2 topic pub
to publish the command in a steady stream at 1 Hz.
When publishing messages with timestamps, pub
has two methods to automatically fill them out with the current time. For messages with a std_msgs/msg/Header
, the header field can be set to auto
to fill out the stamp
field.
ros2 topic pub /pose geometry_msgs/msg/PoseStamped '{header: "auto", pose: {position: {x: 1.0, y: 2.0, z: 3.0}}}'
If the message does not use a full header, but just has a field with type builtin_interfaces/msg/Time
, that can be set to the value now
.
ros2 topic pub /reference sensor_msgs/msg/TimeReference '{header: "auto", time_ref: "now", source: "dumy"}'
ros2 topic hz
One can view the rate at which data is published using:
ros2 topic hz /turtle1/pose
It will return data on the rate at which the /turtlesim
node is publishing data to the pose
topic.
average rate: 59.354
min: 0.005s max: 0.027s std dev: 0.00284s window: 58