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

Does every ROS node need to spin()?

asked 2016-06-21 14:06:50 -0500

powerstorm42 gravatar image

I have been using ROS for a short time now and learned about callbacks & spinning. My question is, since be default all callbacks are added to the global callback queue, does ros::spin() in node A also process callbacks for subscribers in node B?

For a specific example, in MAVROS there is a 4 thread asynchronous spinner. I built a ROS node that uses MAVROS. In my node I also use an asynchronous spinner to process callbacks; however if they both use the global callback queue then is my asynchronous spinner redundant?


edit retag flag offensive close merge delete



I can't comment on your specific use of MAVROS in your node, but in general, a spin in one node will not cause a callback to process in a different node. I know that from personal experience so have no cite for you. It's easy to check. Remove and see what happens.

billy gravatar image billy  ( 2016-06-21 14:40:41 -0500 )edit

The term "global callback queue" means "global" within the context of the node process, not between different processes. You can either create an asynchronous spinner, call spin() or call spinOnce().

Martin Günther gravatar image Martin Günther  ( 2016-06-23 02:43:18 -0500 )edit

Thanks everyone! I understand it know, especially Martin's explanation that the queue is only global within the node, not across all nodes.

powerstorm42 gravatar image powerstorm42  ( 2016-06-23 08:24:31 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2016-06-22 13:14:53 -0500

kramer gravatar image

updated 2016-06-22 13:40:32 -0500

Yes, every ROS node needs to spin, with the stipulation that your terminology matches conventional usage.

"ROS node" is conventionally used to refer to a process (or standalone executable, disregarding nodelets). (Edit: upon thinking about it, a more correct definition would also require use of a ROS callback queue. For instance, one might only access the master API in their executable; such a process would probably still be considered a ROS node, but it would have no need for a spin.) Threads, such as those used by an asynchronous spinner, are computational structures within a process. In the case of an asynchronous spinner, that are all able to use the global callback queue (thus, only one spin is necessary).

As to your use of MAVROS, it depends on what you mean by I built a node that uses MAVROS. If you run MAVROS as a node separate and distinct from your own node, then both need spin calls. If you've simply modified the MAVROS source, adding your own code to it that compiles into a single executable, then a single spin is sufficient.

Note that there are cases where it is helpful to use multiple spin calls (typically, multiple spinOnce calls with multiple threads) within a single node, and it is also sometimes useful to set up multiple callback queues within a single node. But these are beyond your question; any (communicating, functioning) ROS node must have at least one callback queue and one spin call.

edit flag offensive delete link more

Question Tools



Asked: 2016-06-21 14:06:50 -0500

Seen: 1,294 times

Last updated: Jun 22 '16