[ROS Q&A] 144 – What is the difference between ROS topics and messages?

ros q&a What is the difference between ROS topics and messages

Written by Marco Arruda

30/07/2018

In this video we are going to explain and show in a practical way the difference between ROS topics and messages.

We show two nodes running, one publishing to a given topic and another subscribing to this same topic. RQT graph is used to see the big picture of the environment.

 

Step 0. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it ros_q_a_topics_messages.

Step 1. Difference between ros topic and ros message

We’ll start by creating a package for the code in catkin_ws

cd catkin_ws/src
catkin_create_pkg my_pkg rospy std_msgs

Let’s also create a scripts folder for our scripts. Then two script called my_subscriber.py and my_publisher.py file in it with the following content.

#!/usr/bin/env python

import rospy
from std_msgs.msg import String

def main():
    pub = rospy.Publisher('my_topic', String, queue_size=10)
    rospy.init_node('my_publisher')

    rate = rospy.Rate(1)
    
    while not rospy.is_shutdown():
        hello_str = 'hello world %s' % rospy.get_time()

        rospy.loginfo(hello_str)

        pub.publish(hello_str)

        rate.sleep()

if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptExeception:
        pass

Basically, a message is data through a channel called topic. In this code, the message is String type and the topic is called my_topic.

#!/usr/bin/env python

import rospy
from std_msgs import String

def main():
    pub = rospy.Publisher('my_topic', String, queue_size = 10)

    rospy.init_node('my_publisher')

    rate = rospy.Rate(1)

    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()

        rospy.loginfo(hello_str)

        pub.publish(hello_str)

        rate.sleep()

if __name__ == '__main__' :
    try:
        main()
    except rospy.ROSInterruptExeception:
        pass

This script established a subscriber for the message through the topic my_topic.

To run the code, we have to give the scripts permission to execute with chmod +x my_publisher.py and chmod +x my_subscriber.py

Then we can run it with

cd catkin_ws
catkin_make
source devel/setup.bash
rosrun my_pkg my_publisher.py

You should see the publisher is publishing message into the topic.

Open another shell and run

rosrun my_pkg my_subscriber.py

Open the third shell and type rqt_graph , then go to Tools->graphical tool to open the GUI.

You should see the nodes are marked with circles. You can find the /my_publisher and /my_subscriber nodes in the graph.

The topic is marked with the square. In this example, the topic is called my_topic. The two node is communicating though this topic.

Want to learn more?

If you are interested in learning ROS, please check our ROS Basics Python course.

 

Edit by: Tony Huang


RELATED LINKS

▸ Original question: https://answers.ros.org/question/63511/what-is-the-difference-between-a-topic-and-a-message/
ROS Development Studio (ROSDS)
Robot Ignite Academy
ROS Basics Python
ROS Basics C++


Feedback

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This