Ros loses messages

Let’s take the simplest example (eg, Python’s) and update it slightly to our needs:

rospy.init_node('test')
pub = rospy.Publisher('hello', String, queue_size=10)
rate = rospy.Rate(250) # 250hz
for i in range(10):
    pub.publish("hello world"+str(i))
    rate.sleep()

And it seems to be clear what to expect in the subscriber (lets just output to the console that what came), but we can see:

But we do not see anything. What? How? Why? We check a hundred times the name of the topic – there is nowhere else to suspect a fake. Damn it, we increase range(10) to range(500) and start seeing the long-awaited hello world X, but X starts only somewhere with 200 (and up to 500 as it should). Ok, it’s getting better but where are the previous 200 messages gone? Trying to mess with queue_size, but it’s clear that this is not it. Okay, let us put another experiment – rate down to 10hz. And yes, we see literally starting with the 2nd. But where is the first?

Finally – everything is simple. In the second line the publisher has not been created yet and when the implementation reaches step “publish” – it publishes in nowhere. If you simply insert time.sleep(0.1) between the creation of the publisher and its use, the subscriber will see all the messages starting with the first (in this case from zero of course).

P.S. In the C++ – exactly the same thing, maybe even more is lost, since it is more speedy. But why on the rate (10) it loses only 1-2 messages, and on the rate (250) up to 200 – I think it’s not difficult to guess.

Add comment

avatar