Ask Your Question
0

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

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

chrisLB gravatar image

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

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 imageNEngelhard ( 2017-01-03 10:17:41 -0600 )edit

Thank you for the valuable hint! I answered below.

chrisLB gravatar imagechrisLB ( 2017-01-04 02:39:35 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

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

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 -0600

NickCitizen gravatar image

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

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 imagegvdhoorn ( 2018-04-26 11:33:12 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 755 times

Last updated: May 21