I have written a ROS subscriber to one of the image topics and I have set my buffer to 1 using:
subscriber =rospy.Subscriber("/camera/rgb/image_mono/compressed",CompressedImage, callback, queue_size=1)
However my subscriber still lags behind. Any idea what might be causing this? Am I setting the queue size correctly?
I was having the exact same problem (it wasnt the choppy framerate that was the issue, it was the actual lag). When I would kill whatever image source was publishing (rosbag, camera driver, etc.), my node would still process ~5-10 frames even after the source was killed (and I was sure I had queue_size=1
)
Here is the github issue I made and it was resolved. It turns out there are multiple queues involved (not just the one that you set queue_size to 1). In my case, the default buff_size
was smaller than my images, and so my node wasn't processing them fast enough, and there always was a number of images backed up in some queue.
TL;DR
increase the buff_size
for your subscriber, as I did here. It worked for me :)
The queue is for queueing incoming messages. This means, if your callback takes longer to proccess than new messages arrive, only queue size
is kept, the others are not processed by your node.
I would suggest to print out a message in the publisher node before publishing and a message at the top of your callback method. Then you can exactly measure the time it takes for ros to handle your messages. All other timing issues will be possibly caused by your callback method.
A supplement to why buff_size matters.
Also the official document can help explain another reason that causes lag from the perspective of pub.publish.
publish() in rospy is synchronous by default (for backward compatibility reasons) which means that the invocation is blocking until:
the messages has been serialized into a buffer and that buffer has been written to the transport of every current subscriber
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With