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

Which data is thrown away in queue if publish and spinonce rate is different?

asked 2016-08-12 03:38:25 -0500

alienmon gravatar image

updated 2016-08-14 21:16:22 -0500

I read that SpinOnce does not only make callback, but it will also clean the queue if possible. is that right?

Let's say publisher publish alphabets (a,b,c,...) at 9hz, and subscriber call Spinonce at 3hz.

So publisher will publish a,b,c to queue. Then subscriber does spinonce, getting a (because queue is First in first out) and empty the queue.Now the queue is empty. Next, publisher will publish d,e,f to queue. Then subscriber does spinonce, getting d and empty the queue.Now the queue is empty. Next, publisher will publish g,h,i to queue. Then subscriber does spinonce, getting g and empty the queue.Now the queue is empty.

So at the end what the subscriber gets is a,d,g,...

Is that right? Please explain

EDIT (additional question) Where should we call ros::spin() or ros::spinOnce()? Is it in the publisher code, or subscriber code? and why is so?

In this tutorial of publisher-subscriber It calls ros::spinOnce() in the publisher code, and it also calls ros::spin() in subscriber code. Why is that so? please explain..

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2016-08-12 04:29:55 -0500

Marcin Bogdanski gravatar image

updated 2016-08-12 04:39:05 -0500

No

SpinOnce can call multiple callbacks if necessary. spinOnce will call callback for each message in a queue separately, and only then remove it from a queue. Thus you will get callback for each one. Callbacks will be one after another as fast as possible.

Your receiver will get: a,b,c ...pause... d,e,f.... pause... g,h,i

This is assuming your queue length is sufficient. if queue gets full, you will start loosing messages.

edit flag offensive delete link more

Comments

"If necessary". When is it necessary? Is it when there is still stuffs on the queue after the SpinOnce is called?

alienmon gravatar image alienmon  ( 2016-08-12 05:47:45 -0500 )edit

"If necessary" == If there is more than one message on the queue.

SpinOnce will call a callback once for each message on the queue before removing it. And through that it will clear the whole queue. As far as I know it will never remove message without calling a callback.

Marcin Bogdanski gravatar image Marcin Bogdanski  ( 2016-08-14 13:55:16 -0500 )edit

So anyway we will still get all of the datas/messages?(assuming the queue size is big enough).

alienmon gravatar image alienmon  ( 2016-08-14 20:29:28 -0500 )edit

Also, If I use ros::spin instead of spinOnce, I will just need to call it once, and it will callback automatically everytime a message arrives. Is that right?

alienmon gravatar image alienmon  ( 2016-08-14 20:33:48 -0500 )edit

I have additional question, I edited the question already. Could you please help to asnwer? Thankss

alienmon gravatar image alienmon  ( 2016-08-14 21:17:27 -0500 )edit
0

answered 2016-08-15 07:03:59 -0500

kramer gravatar image

For your added edit question: the publisher and subscriber from the tutorial are in different nodes, thus they each have their own spin call.

Generally speaking, every ROS node has a spin call. ros.spin() is a blocking call that doesn't return until stopped, using that thread of execution to process messages from the callback queue. ros.spinOnce() processes one message in the callback queue then returns; typically, it is placed within a loop that throttles how often it is called.

edit flag offensive delete link more

Comments

They are in different nodes, but they are dealing with the same message. let's say when the publisher publish a message, then the spin in publisher code does callback to handle the message. Then what does the spin in subsriber node do? what's the role of each spin? please explain

alienmon gravatar image alienmon  ( 2016-08-17 21:13:06 -0500 )edit

can you explain more what happen chronologically when a message being published? what does each spin do? let's say for the case of the tutorial. cause I don't understand why we need 2 spin for the same message. @kramer

alienmon gravatar image alienmon  ( 2016-08-17 21:14:07 -0500 )edit

@Mark Rose please help to answer my additional quest

alienmon gravatar image alienmon  ( 2016-08-17 22:22:43 -0500 )edit

You need to understand: a node is, in this case, a process (or job, or executable). A message is merely data. Read the tutorials, particularly Understanding ROS Nodes.

kramer gravatar image kramer  ( 2016-08-18 00:08:52 -0500 )edit

Succinctly: each node has (at least) one callback queue. Every node containing a subscriber to a topic receives a copy of a message published on that topic (in its callback queue). Message processing results from a spin; a spinOnce processes one message from the node's callback queue.

kramer gravatar image kramer  ( 2016-08-18 00:20:17 -0500 )edit

@kramer (I have read the tutorial previously, but I am a bit confused.). SO every node has its own callback queue?

alienmon gravatar image alienmon  ( 2016-08-18 03:01:12 -0500 )edit

@kramer a subscriber node's callback queue is for receiving message. The message received will be handled once a spin is performed in the subscriber node... Then what is a publisher node's callback queue for, what is the callback funct for? And what is being performed when a publisher does a spin??

alienmon gravatar image alienmon  ( 2016-08-18 03:04:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-12 03:38:25 -0500

Seen: 947 times

Last updated: Aug 15 '16