ros subscribe to the latest available message
I am using rospy but I don't mind getting a solution in roscpp.
I have a publisher that publish message at a rate of 60Hz, and let say my subscriber is capable of handling messages at 6Hz. I am planning to sample the messages from the subscriber side and take the last available message from the publisher for subscription. Setting the queue_size to one, I can possibly sample the message. However, the new message that I am processing is turn out to be the one available just after the subscriber node finish processing the previous topic.
To clarify more, let say the publisher publish message at time t t = [0, 1, 2, 3, ..., 9, 10, 11, ...] sec Assuming the first subscription to occur at 0.5 Sec What I'm getting is the next subscription on the message topic occurring at time t = 11sec, what I want is to use the the message at time t=10Sec.
I hope I explain myself well.
There seems to be some confusion of terms. The publisher advertises a topic (once) and the subscriber subscribers to the topic (once). After that, what occurs repeatedly is the publisher publishing messages (not topcis) and the subscriber recieving/processing messages.
Having said that, I'm not sure I understand your question exactly. Do you want to process always the latest (newest) received message in your 6Hz subscriber? I.e on average every 10th message would be used? Providing some code of your subscriber and publisher could help.
Thanks @demmeln, I have tries to put things in properly.
I would like to add that both, publisher and subscriber have a queue size. I assume both have to be at 1 for getting the behaviour you are looking for...
@Wolf is it really possible to set queue_size to one in rospy.Publisher or do I have to use the other arguments?
@Wolf why do you say that? What happens when only subscriber has queue size 1?
Advertise of publisher has also arg queue_size. C++ signature: template <class m=""> Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false); not familiar with python. Doc of queue size says:Maximum number of outgoing messages to be queued for delivery to subscribers
I assume - have not tested that - that the publisher also uses this as FIFO queue if the subscriber is not able to subscribe at full rate. I. e. if subscriber queue size is 1 and publisher queue 10 you would still always get the "10 messages ago" message if publisher queue is full...