[ROS Q&A] 128 – Can’t access messages of the ROS topic Publisher


Written by Alberto Ezquerro


In this video we are going to see why a user can’t access the message that he is publishing into a topic using a ROS Publisher.

This is a video trying to answer the following question posted at the ROS answers forum: https://answers.ros.org/question/293898/init_node-need-locating-after-publisher/

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

We can do any ROS development we want easily, without setting up environment locally in ROSDS and that’s the reason why we will use ROSDS for this tutorial. If you haven’t had an account yet. You can create one here for free now. After logging in, let’s begin our journey by clicking on the create new project and call it pub_example.

Step 2. Create a new package for testing

Now, let’s create a new package for testing with the following command

cd ~/catkin_ws/src/
catkin_create_pkg pub_example rospy

Then we create a new file called pub_ex.py under /pub_example/src with copying and pasting the following code.

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)    
    hello_str = "hello world %s" % rospy.get_time()

if __name__ == '__main__':

NOTICE: We no longer run ROS master for you in the backend, please launch it with the command roscore in a new terminal.

Now you can run it with the command rosrun pub_example pub_ex.py

You will get the output hello world, however, if you check with rostopic list , there is no chatter topic.

It turns out the talker function only publish once and was killed. If we want to access it, we have to keep publishing it. Let’s change our code.

def talker():
    rate = rospy.Rate(1) # this defines the publish rate
    pub = rospy.Publisher('chatter', String, queue_size=10)    
    hello_str = "hello world %s" % rospy.get_time()
    while not rospy.is_shutdown(): 
        # put everything in a while loop to keep publishiing topic
        rate.sleep() # this make sure that the topic is publishing at 1 Hz

When you run it again, you can see that the topic is publishing every 1 second and you can access it from other node.



Edit by: Tony Huang



▸ Original question: https://answers.ros.org/question/293898/init_node-need-locating-after-publisher/
Robot Ignite Academy
ROS Basics in 5 days Course (Python)
ROS Development Studio (RDS)


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


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