[ROS in 5 mins] 022 – What is a ROS publisher?

what is a publisher

Written by Bayode Aderinola

13/08/2018

In this post, we will see what a ROS publisher is all about in just about five minutes! We’ll also see how it relates to ROS nodes and topics.

Let’s go!

Step 1: Grab a copy of the Project (ROSject) on ROSDS

Get it here: http://www.rosject.io/l/18f83296-0e7e-4c5c-95a7-2d3e3d6430d4/.

Once you have copied the project, open it up.

Step 2: Run the obiwan_pub.py program

Pick a Shell from the Tools menu and run the following commands.

First, start the ROS master – we need this to run any ROS commands. We’re starting this program in the background:

user:~$ nohup roscore &
[1] 1501
user:~$ nohup: ignoring input and appending output to 'nohup.out'
user:~$

Before we proceed, let’s check the ROS topics currently available:

user:~$ rostopic list
/rosout
/rosout_agg

Now start the obiwan_pub.py program:

user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ ./obiwan_pub.py

Again, check the list of available ROS topics. Pick another Shell from the Tools menu and run:

user:~$ rostopic list
/help_msg
/rosout
/rosout_agg

Now we have a new topic /help_msg, obviously created by the obiwan_pub.py program. Running it is the only thing we have done since the last time we checked the list of messages, right?

Okay, do not take my word for it; let’s spy on the /help_msg topic to confirm:

user:~$ rostopic info /help_msg
Type: std_msgs/String

Publishers:
 * /sos_2 (http://rosdscomputer:38619/)

Subscribers: None

Now we know that that topic uses a message of type std_msgs/String and has a Publisher named /sos_2.

But what has this got to with the obiwan_pub node? Everything – let’s see that in the next step.

Step 3: Unravel the mysterious “Publisher” /sos_2

In the previous step, we saw that /sos_2 was listed as a Publisher on the topic /help_msg, and we suspected that this has to do with the obiwan_pub.py program. Now, let’s examine the obiwan_pub.py code to see if we have some clues.

obiwan_pub.py

#! /usr/bin/env python

import rospy
from std_msgs.msg import String

rospy.init_node("sos_2")
rate = rospy.Rate(2)
help_msg = String()
help_msg.data = "help me Obi-Wan Kenobi, you're my only hope"
pub = rospy.Publisher('/help_msg', String, queue_size = 1)
while not rospy.is_shutdown():
    pub.publish(help_msg)
    rate.sleep()

On line 6, we have the statement rospy.init_node("sos_2"), so we now know that:

  • /sos_2 was created by this program!
  • /sos_2 is a ROS node, because it says rospy.init_node...

Also on line 10, we have this statement: pub = rospy.Publisher('/help_msg', String, queue_size = 1), confirming that:

  • The code actually creates a Publisher on the /help_msg topic. Bingo!

Step 4: Master the Concept – What is a ROS Publisher?

Combining all the points in the previous steps, we can see that a ROS Publisher…

  • is a ROS node (program).
  • creates a particular type of ROS message. In this case, it’s std_msgs/String, as can be seen on lines 4,7-8 of the code.
  • sends (publishes) a message over a channel called “topic”, as can be seen on lines 10 then 12 of the code.

In short, a ROS publisher is a ROS node that publishes a specific type of ROS message over a given ROS topic. “Interested” nodes (Subscribers) can access messages so published.

And that was it!

Extra: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

  • A previous video about ROS nodes: [irp posts=”10305″ name=”ROS in 5 mins – 005 – What is a ROS Node?”]

If you are a ROS beginner and want to learn ROS basics fast, we recommend you take any of the following courses on Robot Ignite Academy:

Feedback

Did you like this post? Whatever the case, please leave a comment in 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 in the comments area and we will do a post or video about it.

Thank you!

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

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