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

ROS spin and initialize

asked 2016-08-16 17:25:50 -0500

demi H gravatar image

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?

edit retag flag offensive close merge delete


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

ahendrix gravatar image ahendrix  ( 2016-08-16 21:03:15 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-08-17 01:14:10 -0500

Mark Rose gravatar image

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

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-08-16 17:25:50 -0500

Seen: 790 times

Last updated: Aug 17 '16