Robotics StackExchange | Archived questions

ROS spin and initialize

Hi. I'm new to ROS and I'm working with ros cpp. I have a question.

I know that if I call ros::spin(), then it enter a loop, pumping callbacks. But there are certain function that I just want to call, only once in the whole execution time.(and probably at the beginning) Like initializing device(not ros::init), setting which device to use through stdin.

I know that i can use inifinite while loop, but I don't think that it's a good idea. Because ros::spin() is made for that purpose.

Is there any function to solve this problem?

Asked by demi H on 2016-08-16 17:25:50 UTC

Comments

Is there anything that prevents you from doing all of your initialization before you call ros::spin()?

Asked by ahendrix on 2016-08-16 21:03:15 UTC

Answers

@ahendrix is right, you can just perform initialization in your main function, either before or after initializing the node. You only call ros::spin() if all of your functionality is handled in subscriber callbacks. Often, nodes need to do things periodically, so they use an infinite loop, but don't execute continuously. Instead, they often set up a ros::Rate variable and use a combination of ros::spinOnce() and ros::Rate.sleep(). See callbacks and spinning for more info and other threading models. The other links on that page are also worth a look.

Asked by Mark Rose on 2016-08-17 01:14:10 UTC

Comments