Subscriber - perform callback on 'most current' data received
Hey everybody,
I've been messing a bit with a subscriber node and i'm getting stuck.
Little bit of background: I have a topic where a camera publishes images with a frequency of 10 hz (ish). I have another node subscribed to this topic. There the image is processed using open CV. The processing takes about 1 second. This means for every 10 images, 1 image is processed.
Now when i look at the images received by the subscriber i see that the images received are delayed, meaning that the next callback image 2 is processed, which was made about 0.9 second ago. The 3rd callback, image 3 is processed, leading to a delay of 1.8 s. etc... I think this has something to do with the queue size. I tried messing around with the basic chatter nodes from the tutorial to understand the problem a bit better and i made a simplified version of the problem:
publisher
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String
def talker():
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(1) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
subscriber:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
rospy.sleep(2)
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
print("done with callback")
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()
Now, when i run both nodes, the publisher publisher at 1 message / s The subscriber receives the first message, waits 2 seconds, then receives the second message, etc. When i stop the publisher, the subscriber keeps receiving messages(probably since they are stored in a buffer)
Then set the queue size to 1, and ran the nodes again. This gave me the same result.
I want the subscriber to do something with the most up to date message, so deleting or not caring about all the messages received before. The reason for this is because i'm using this for a vision application, so i need the processing to process the latest image.
So, my question:
Is there a way to tell my subscriber (or publisher) to delete all stored messages or to start callback on the latest received image?
Kind regards, Jan Tromp.
I'm sure you've already looked for solutions to your problem before posting here.
Could you add what you've tried and what didn't work?
It might also help if you could link to Q&As here on ROS Answers that you've found, so that we don't suggest the same things.
I did look around for options to solve this problem, but apperantly i was searching in the wrong direction. I found a solution and explained it in my answer.