Topic not being published

Hello dear community !

I joined here just recently. I searched for topics similar to mine but it didn’t solve my issue.
In the ROS Basics (5 days) course, in example 3.1, I run the following command as told:

rosrun my_examples_pkg simple_topic_publisher.py

This gives the following output:

/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py:842:UserWarning: ’ /counter’ is not a legal ROS graph resource name. This may cause problems with other ROS tools
super(Publisher, self).init(name, data_class, Registration.PUB)
[FATAL] [1647509354.474772, 0.000000]: unable to register publication [/ /counter] with master: ERROR: parameter [topic] contains illegal chars

And also, to see if i have created a topic, i run (in second webshell) :

rostopic list | grep ‘/counter’

But output shows blank. As per the tutorial text, output SHOULD BE :

user ~ $ rostopic list | grep ‘/counter’
/counter

Where am I doing wrong?

simple_topic_publisher.py contains the following code:

#! /usr/bin/env python

import rospy
from std_msgs.msg import Int32

rospy.init_node(‘topic_publisher’)
pub = rospy.Publisher(’ /counter’, Int32, queue_size=1)
rate = rospy.Rate(2)
count = Int32()
count.data = 0

while not rospy.is_shutdown():
pub.publish(count)
count.data += 1
rate.sleep()

Hi ,

user ~ $ rostopic list | grep ‘/counter’
/counter

It should be without ’ ', that is:

user ~ $ rostopic list | grep /counter
/counter

And in future, use image, from the menu to copy code, making it easy to comprehend with colors and all.

Inorder to see the output:

user ~ $ rostopic echo /counter

Let me know if it worked out

Thank you sir for the kind reply.
It didn’t work.

error1

In second webshell, i run the following:
error2

remove the space before /counter, as shown below. Spaces are not allowed when naming.

pub = rospy.Publisher(’/counter’, Int32, queue_size=1)```

Oh thank you so much sir…!!!
I found two errors to rectify it…first as told by you, i removed the space before /counter. Secondly, during compiling the workspace using catkin build, i was getting error that the workspace is already compiled using catkin_make. So i deleted the devel and build folders in catkin_ws and again compiled using catkin build.

it worked!

Hi @arjun2312 ,
Glad it worked out. Also, go through this link about ROS graph resource names Names - ROS Wiki, basically rules to be followed while nameing topics, classes etc in ROS.

This is not part of the ROS curriculum but shouldn’t take more than 15mins to go through it.

All the best.
-Joseph

1 Like