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

Rospy subscriber needs sleep (some time) until the first message is received

asked 2017-01-03 09:28:35 -0500

chrisLB gravatar image

updated 2017-01-03 09:29:09 -0500

Hi guys, I noticed following somehow strange behaviour. A topic subscriber is not ready to receive messages directly after its creation and needs some time until the first message can be received. The minimal example below illustrates this.

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo("I heard %s",data.data)

rospy.init_node('node_name')

pub = rospy.Publisher('topic_name', String, queue_size=10)

sub = rospy.Subscriber("topic_name", String, callback)

pub.publish("no delay")

rospy.sleep(1)

pub.publish("with delay")

rospy.spin()

Results in:

[INFO] [1483456752.613187]: I heard with delay

I would expect:

[INFO] [1483457135.695501]: I heard no delay
[INFO] [1483457135.696547]: I heard with delay

Is this behaviour correct, as it is somehow understandable that it needs time to initialize the callback thread for the subscriber, or is it actually a bug? Does exist a better way to solve this issue instead of using rospy.sleep?

edit retag flag offensive close merge delete

Comments

4

It's the normal behaviour. You can check for a listener with getNumSubscribers/Publishers. Could you also have a look at latched topics.

NEngelhard gravatar image NEngelhard  ( 2017-01-03 10:17:41 -0500 )edit

Thank you for the valuable hint! I answered below.

chrisLB gravatar image chrisLB  ( 2017-01-04 02:39:35 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-01-04 02:39:04 -0500

chrisLB gravatar image

The hint from @NEngelhard was correct, using the option latch=True results in what I had expected before. Thanks a lot!

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo("I heard %s",data.data)

rospy.init_node('node_name')

pub = rospy.Publisher('topic_name', String, queue_size=10, latch=True) # working as expected
#pub = rospy.Publisher('topic_name', String, queue_size=10) # not working as expected

sub = rospy.Subscriber("topic_name", String, callback)

pub.publish("no delay")

rospy.sleep(1)

pub.publish("with delay")

rospy.spin()
edit flag offensive delete link more
0

answered 2018-04-26 11:30:05 -0500

NickCitizen gravatar image

updated 2019-05-21 17:11:31 -0500

Edit: Suboptimal answer. Please see comments by @NEngelhard and @gvdhoorn

Original Answer:

Latching works, but in case you do not want latching messages, a short wait between the Publisher declaration and the actual publishing of the message will do the trick, for example:

pub = rospy.Publisher('topic_name', String, queue_size=10)
rospy.sleep(0.5)
pub.publish("no delay")

I mention this because your question seems to imply that the wait is needed for the message to be received, when it seems to me that it is actually needed for it to be published. in other words, messages do not seem to get published during the half second immediately following the publisher declaration.

edit flag offensive delete link more

Comments

2

Please try to avoid sleeping in these situations. The getNumSubscribers() and getNumPublishers() methods mentioned by @NEngelhard were specifically created for this.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-26 11:33:12 -0500 )edit

According my experience, @NickCitizen is correct: it takes about half a second until the publisher starts publishing and therefore a sleep is needed... It would be better if there would be a publisher state that we could verify before for publishing.

mhallak gravatar image mhallak  ( 2020-04-22 03:34:16 -0500 )edit

Do you mean "the subscriber"? Because there should not be any requirement to subscribe after a publisher exists.

Publishers could be publishing, but if there are no subscribers, then the msgs end up in /dev/null. That's what getNumSubscribers() is for. That would be state based.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-22 03:42:15 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-01-03 09:28:35 -0500

Seen: 2,910 times

Last updated: May 21 '19