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

Revision history [back]

click to hide/show revision 1
initial version

Here is a page that goes into more detail. But spin() just lets all the callbacks get called for your subscribers. Your example code doesn't have a subscriber so you wouldn't need spin() in that case.

If you did setup a subscriber and didn't have spin(), your program would just start, setup subscribers, then close without the subscriber callbacks ever getting called.

But when you do have a subscriber, all of your code runs then you put a spin() at the end to keep the program from just exiting when it reaches the end of main(). Instead of exiting, a loop continuously runs to allow the callbacks to be called when a new message arrives. To be clear, ROS will not process any callback until spin() is called.

Here is a related question with great answers.