unexpected delay in subscriber/ subscriber not getting latest message

OK, I seriously need help. I know similar topics have been discussed on this forum, but I believe my case is different. I’ll try to keep it succinct, but fully state my situation. I’m using rospy, kinetic, python2.7. All my testing is done on a PC.

I have a node that publishes to /camera/raw_image at 30Hz(it’s a live camera). I didn’t write this node and I don’t intend to largely modify it. The images are around 3.5MB each, and the bandwidth of this topic is roughly 100MB/s. I am writing a node that accomplishes two things: 1. Adjust the exposure setting of the camera. This portion reads a frame and see if it’s too dim/bright and adjust camera settings accordingly. This is very light work. I intend this function to run at 0.5Hz. 2. Detect a target in the image. The time this portion takes is unpredictable, as some images are more complex than others. I want this portion to run as frequently as the CPU allows, but I don’t want multiple frames to be processed at the same time, i.e. I want this function to process one image, and after it’s done, get the current frame and process it. It can run at 8-30Hz

My problem right now is on function 1. I talked about function 2 to illustrate why I can’t drop the publisher’s frame rate to 0.5Hz. The two functi onalities are implemented as static class methods, and they take only one argument which is the message. It is critical that whenever they are called, they are provided with the latest message available. I implement the speed control on function 1 by putting a rospy.rate.sleep() statement as I usually does with publisher. My first question is: Does ros spawn a new thread for a callback function whenever a new message is available in the topic? Does ros check if last callback has returned? Based on my testing it seems it does check this, but I want confirmation.

When I was working with the exposure setting callback. I found that this callback keeps getting old messages(frames), so it thought the adjustment it made was not enough, and it keeps over/under adjust. When the rate is 0.5 Hz, I have a constant 4s delay in receiving messages regardless of the publisher’s publish rate( I tried 30fps and 15 fps), correct number of messages are dropped(for 30fps ~59 frame dropped), but the whole thing is lagging behind. When I set the rate to be 0.25Hz, the delay becomes 8s.

My setting for this subscriber is buff_size=2**24, queue_size=1. Everyone’s problem seems to be fixed by setting the buffer size, but mine isn’t.

Why does this happen, how can I go around and fix it?

There's a lot of code, here's related ones:

###Subscriber

rospy.Subscriber("camera/image_raw ...
edit retag close merge delete