Skip to content
  • Home
  • About the Blog
  • About the Author
  • Sitemap

Abdur Rosyid's Blog

Just a few notes on mechanical engineering and robotics

ROS1 Topic, Publisher, and Subscriber

July 9, 2021 by Abdur Rosyid

In ROS, a node can publish a topic and subscribe (listen) to a topic. A topic is basically a unidirectional message in a certain data type containing some information. It is like a radio broadcast. The radio station is the topic publisher whereas the radio receiver is the topic subscriber. The topic is the radio stream. The unidirectional characteristics means that a node can publish a topic regardless of whether another node will subscribe to it or not. On the other hand, a node can subscribe to a topic without asking permission from the topic publisher.

The publishing and subscribing mechanism is very important in robotics. For example, a camera node publishes the stream of images and at the same time an object detection node subscribes to that stream of images in order to process the images and perform the object detection process. After the object detection node detects an object of interest, it publishes the pose of the object. A navigation node subscribes to the pose of the object and perform a motion planning and subsequently move the robot to approach the object.

The name of a topic in ROS accepts namespaces indicated by slash (/) mark. A namespace is very useful to differentiate two or more different topics with the same name. For example, Camera1 and Camera2 may publish a topic called image. To make them different, the one published by Camera1 is named camera1/image whereas the one published by Camera2 is called camera2/image.

  • A node can publish one or more topics.
  • A node can listen to one or more topics.
  • A topic being published is not necessarily to be subscribed.
  • A node can publish and subscribe (listen).

To publish a topic, simply run the publisher node. Similarly, to subscribe to a topic, simply run the subscriber node. To run a node, type the following in the terminal:

1
rosrun [PACKAGE_NAME] [NODE_NAME]

A topic subscriber typically performs two tasks:

  • listening to a published topic.
  • executing a callback upon receiving the published topic.

Of course listening without doing anything is useless in robotics. Therefore, a callback function should be defined in a subscriber to indicate what to be done upon receiving a published topic.

A topic is published/subscribed all the time at a certain rate as long as the publisher/subscriber node is running. To stop publishing/subscribing, simply kill the publishing/subscribing node using the following command:

1
rosnode kill [NODE_NAME]

A complete list of commands regarding a node can be found here: http://wiki.ros.org/rosnode

How to write a simple ROS1 publisher and subscriber in C++: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29

How to write a simple ROS1 publisher and subscriber in Python: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

In order to show the stream of a published topic, type in the terminal:

1
rostopic echo [TOPIC_NAME]

In order to show all the active topics, i.e. all the topic currently published and subscribed to, type in the terminal

1
rostopic list

This command will return a list of currently published topics and a list of currently subscribed topics.

In order to query the data type of a specific topic, type in the terminal:

1
rostopic type [TOPIC_NAME]

In order to publish a certain value of a currently published topic, type in the terminal:

1
rostopic pub [TOPIC_NAME] [MESSAGE_DATA_TYPE] [ARGS]

The complete list of commands regarding ROS topic can be found here: http://wiki.ros.org/rostopic

And here is an explanation about ROS topic by using Turtlebot as example: http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics

We can see there that a topic is illustrated by an arrow line in a ROS Graph given by “rqt_graph” command. The direction of the arrow indicates which node is publishing and which node is subscribing.

Post navigation

Previous Post:

Package in ROS1

Next Post:

ROS Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • STEM 101
  • Robotics
  • Kinematics
  • Dynamics
  • Control
  • Robot Operating System (ROS)
  • Robot Operating System (ROS2)
  • Software Development
  • Mechanics of Materials
  • Finite Element Analysis
  • Fluid Mechanics
  • Thermodynamics

Recent Posts

  • Pull Request on Github
  • Basics of Git and Github
  • Conda vs Docker
  • A Conda Cheat Sheet
  • Installing NVIDIA GPU Driver on Ubuntu

Archives

  • June 2025
  • July 2021
  • June 2021
  • March 2021
  • September 2020
  • April 2020
  • January 2015
  • April 2014
  • March 2014
  • March 2012
  • February 2012
  • June 2011
  • March 2008
© 2025 Abdur Rosyid's Blog | WordPress Theme by Superbthemes