ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

ros subscribe to the latest available message

asked 2014-03-18 23:58:21 -0500

hashmuke gravatar image

updated 2016-10-24 08:36:27 -0500

ngrennan gravatar image

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.

edit retag flag offensive close merge delete



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.

demmeln gravatar image demmeln  ( 2014-03-19 02:30:04 -0500 )edit

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.

demmeln gravatar image demmeln  ( 2014-03-19 02:31:51 -0500 )edit

Thanks @demmeln, I have tries to put things in properly.

hashmuke gravatar image hashmuke  ( 2014-03-19 03:36:53 -0500 )edit

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 gravatar image Wolf  ( 2014-03-19 04:26:31 -0500 )edit

@Wolf is it really possible to set queue_size to one in rospy.Publisher or do I have to use the other arguments?

hashmuke gravatar image hashmuke  ( 2014-03-19 12:39:40 -0500 )edit

@Wolf why do you say that? What happens when only subscriber has queue size 1?

demmeln gravatar image demmeln  ( 2014-03-19 14:26:51 -0500 )edit

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

Wolf gravatar image Wolf  ( 2014-03-19 21:11:31 -0500 )edit

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...

Wolf gravatar image Wolf  ( 2014-03-19 21:15:06 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-03-19 02:48:57 -0500

BennyRe gravatar image

+1 to @demmeln for his clarification about topic/message. Please review these terms.

I would spontaneously suggest a threaded solution. You do all your time consuming work in a separate thread and in your subscriber callback just pick the messages you want, discard all the others and provide the worker thread the correct messages.

edit flag offensive delete link more


@BennyRe thanks

hashmuke gravatar image hashmuke  ( 2014-03-19 03:46:36 -0500 )edit

answered 2017-12-29 21:06:12 -0500

nickzhang gravatar image

If I understand correctly, setting the queue_size of the subscriber to 1 would solve your problem.

edit flag offensive delete link more


Question Tools


Asked: 2014-03-18 23:58:21 -0500

Seen: 5,983 times

Last updated: Mar 19 '14