ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Using Rospy to check on empty topic

asked 2020-08-31 23:46:12 -0600

loguna gravatar image

updated 2022-06-12 19:44:06 -0600

lucasw gravatar image

Hi,

I want to code a simple publisher and subscriber. However, I realized that whenever Ros is subscribing to a topic, e.g.:

rospy.subscriber("/chatter",string,callback)

it will always wait for the topic in the callback function. In this example, lets assume there is no '/chatter' topic published and the user himself had type the wrong topic. I wish to create a some kind of warning to users that warn them regarding "chatter" has not yet been publish or empty. How can I do that?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-09-01 01:10:53 -0600

mgruhler gravatar image

There is a bit of a misunderstanding here.

it will always wait for the topic in the callback function

The code is not "waiting" in the callback function. The callback function is fired once a message is received, otherwise it will not be called at all.

Also note that you will get a warning on the terminal when you launch a node that is subscribing to a (published) topic which has another topic type. if by "user himself had typed the wrong topic" you mean this is really just a typo, the python interpreter will throw an exception, if the type does not exist

About your question:

I wish to create a some kind of warning to users that warn them regarding "chatter" has not yet been publish

see #q237789

or empty.

If by "empty" you mean that a std_msgs/String message is published to the chatter topic, you should add this check in the callback:

if len(msg.data) == 0:
    rospy.logwarn("Message empty")
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-08-31 23:46:12 -0600

Seen: 1,787 times

Last updated: Sep 01 '20