ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
3

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

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

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

3 Answers

Sort by ยป oldest newest most voted
9

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

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 imageBill5785 ( 2017-03-20 07:52:20 -0500 )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 imageAiruno2L ( 2017-03-20 09:27:50 -0500 )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 imageBill5785 ( 2017-03-20 09:33:18 -0500 )edit

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

Airuno2L gravatar imageAiruno2L ( 2017-03-20 09:38:34 -0500 )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 imageAiruno2L ( 2017-03-20 09:41:24 -0500 )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 imageAiruno2L ( 2017-03-20 09:43:17 -0500 )edit
3

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

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 imageBill5785 ( 2017-03-20 07:46:51 -0500 )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 imagekramer ( 2017-03-21 07:38:21 -0500 )edit
-1

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 11,674 times

Last updated: Mar 20 '17