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

What is the actual meaning of ros::spin()

asked 2017-03-19 21:43:14 -0600

Bill5785 gravatar image

I know that ros::spin can make the subscriber and publisher run in loop to publish or receive data.

But I dont know which part in the program is running in loop or the whole program is running in loop?

int main(int argc, char ** argv )
{
    ros::init(argc,argv,"obstacle_finder");
    ros::NodeHandle n;
    ros::NodeHandle private_nh("~");

    ObjDet object_detector(n,private_nh);
    ros::spin();

    return 0;
}

Such as this example. Will the class ObjDet run constructor and destructor in every loop also? What is the range in the loop? Can I say all the part in main will run in every loop?

edit retag flag offensive close merge delete

Comments

eRCaGuy gravatar image eRCaGuy  ( 2020-06-17 17:46:55 -0600 )edit

4 Answers

Sort by ยป oldest newest most voted
15

answered 2017-03-20 07:18:54 -0600

Airuno2L gravatar image

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.

edit flag offensive delete link more

Comments

It seems that spin has no relationship to publishers, right? So how do publishers continuously publish data. (It cannot run in loop like subscribers by spin)

Bill5785 gravatar image Bill5785  ( 2017-03-20 07:52:20 -0600 )edit

Just run a while loop and publish. Spin is not needed for publishers since there is no callback. Its mentioned in this#roscpp_tutorials.2BAC8-Tutorials.2BAC8-WritingPublisherSubscriber.The_Code) tutorial that it is good practice

Airuno2L gravatar image Airuno2L  ( 2017-03-20 09:27:50 -0600 )edit

The link doesnt exist. I read other tutorials and as far as I understand, spin and spinOnce inside while loop are similar except that the latter one can assign loop rate. In this case, does it mean spin can also make publisher run continuously if there is data to publish?

Bill5785 gravatar image Bill5785  ( 2017-03-20 09:33:18 -0600 )edit

sorry, having trouble editing the comment, I'm working on it.

Airuno2L gravatar image Airuno2L  ( 2017-03-20 09:38:34 -0600 )edit

Geez, not sure what's going on with the link, here it is in text: http://wiki.ros.org/ROS/Tutorials/Wri...

Airuno2L gravatar image Airuno2L  ( 2017-03-20 09:41:24 -0600 )edit

Long story short, you don't need spin or spinOnce if you don't have any subscribers. Publishers don't need it because they don't use callbacks.

Airuno2L gravatar image Airuno2L  ( 2017-03-20 09:43:17 -0600 )edit
4

answered 2017-03-20 07:05:55 -0600

kramer gravatar image

ros::spin processes messages from the callback queue. From Section 1 of the Callback and Spinning Overview:

Implementing a spin() of our own is quite simple:

#include <ros/callback_queue.h>
ros::NodeHandle n;
while (ros::ok())
{
  ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0.1));
}

Briefly: the callback queue is associated with a NodeHandle; the NodeHandle is used to create your publishers and subscribers, which associates those pubs/subs with the callback queue; since you pass private_nh as a parameter to you ObjDet instance, I assume that's where your pubs/subs are created.

To your questions: no, the constructor/destructor will not be run each loop. It's not clear to me what you mean by "range in the loop". No, once spin is called, nothing after it is run until ros::ok() returns false (it's a non-returning call).

edit flag offensive delete link more

Comments

"range in the loop" is the same question so ingore it. I guess there is some mistake in your last sentence. Do you want to mean once spin is called, and ros::ok is true, things after it will always run? But when ros::ok is false, things after it will stop?

Bill5785 gravatar image Bill5785  ( 2017-03-20 07:46:51 -0600 )edit
1

No, I said what I meant. See if re-ordering helps: calling spin enters a loop that executes while ros::ok() is true. Once ros::ok() is false, the loop exits, control returns to the caller, and only then are lines after the spin executed.

kramer gravatar image kramer  ( 2017-03-21 07:38:21 -0600 )edit

+1 for showing how to implement it.

here2infinity gravatar image here2infinity  ( 2020-04-07 21:10:43 -0600 )edit
3

answered 2020-08-06 13:33:29 -0600

cascais gravatar image

updated 2020-10-08 02:44:00 -0600

gvdhoorn gravatar image

"ROS Spinning, Threading, Queuing Effective use of multi spinner threads, different queues in ROS" https://levelup.gitconnected.com/ros-...


Edit (moderator): as this is a link only answer, here is a screenshot of the article it links to, to avoid this answer going stale in the event the site ever goes down:

screenshot of article

edit flag offensive delete link more

Comments

Finally, an illustrated, comprehensible explanation of all the queues and worker-threads. Great!

breigo gravatar image breigo  ( 2020-10-07 12:20:58 -0600 )edit
1

As nice as the article is, link-only answers are not. They have a tendency to go stale after the linked page disappeared. I've included a screenshot of the article.

All credits to @cascais of course.

gvdhoorn gravatar image gvdhoorn  ( 2020-10-08 02:45:35 -0600 )edit
-1

answered 2017-03-20 01:24:14 -0600

duck-development gravatar image

your code is finish at ros::spin. the mein is never exited so your Obj Dec is never Free

in the case the spin is for the main an while (true) (simplefied view)

and the stuff is done by your handler functions. whits are called von inside of the ros spin.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2017-03-19 21:43:14 -0600

Seen: 38,824 times

Last updated: Oct 08 '20