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?
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.
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 :)
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
The reason for the visible lag is most probably that your callback function takes up a lot of time. If at all possible, try to fix that. Setting the queue size to 1 essentially means asking ROS to process whatever frames it can hold on to.
Set your queue size to a larger number, say 5 or 10. This way, (hopefully, if the processing delay isn't too much) all of your frames will be processed, but they'll be lagging behind in time. That is, the video processing will run a few steps behind but without any "jerks" and missing frames in between.
来源:https://stackoverflow.com/questions/26415699/ros-subscriber-not-up-to-date