0
$\begingroup$

Rosanswers logo

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?


Originally posted by Bill5785 on ROS Answers with karma: 141 on 2017-03-19

Post score: 8


Original comments

Comment by eRCaGuy on 2020-06-17:
This is helpful too: http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning.

$\endgroup$

3 Answers 3

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Airuno2L with karma: 3460 on 2017-03-20

This answer was ACCEPTED on the original site

Post score: 15


Original comments

Comment by Bill5785 on 2017-03-20:
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)

Comment by Airuno2L on 2017-03-20:
Just run a while loop and publish. Spin is not needed for publishers since there is no callback. Its mentioned in this tutorial that it is good practice

Comment by Bill5785 on 2017-03-20:
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?

Comment by Airuno2L on 2017-03-20:
sorry, having trouble editing the comment, I'm working on it.

Comment by Airuno2L on 2017-03-20:
Geez, not sure what's going on with the link, here it is in text: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(c%2B%2B)#roscpp_tutorials.2BAC8-Tutorials.2BAC8-WritingPublisherSubscriber.The_Code

Comment by Airuno2L on 2017-03-20:
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.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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).


Originally posted by kramer with karma: 1470 on 2017-03-20

This answer was NOT ACCEPTED on the original site

Post score: 4


Original comments

Comment by Bill5785 on 2017-03-20:
"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?

Comment by kramer on 2017-03-21:
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.

Comment by here2infinity on 2020-04-07:
+1 for showing how to implement it.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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


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


Originally posted by cascais with karma: 35 on 2020-08-06

This answer was NOT ACCEPTED on the original site

Post score: 3


Original comments

Comment by breigo on 2020-10-07:
Finally, an illustrated, comprehensible explanation of all the queues and worker-threads. Great!

Comment by gvdhoorn on 2020-10-08:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.