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

Calling ros::spinOnce() in nodelets?

asked 2012-07-04 01:31:45 -0500

ipso gravatar image

updated 2012-07-04 02:17:38 -0500

Is it possible to integrate a while(true){ros::spinOnce(); ..} loop into a nodelet? I've based mine on the velodyne_driver nodelet, which runs a separate thread which polls the device object.

That while loop never sleeps though, and as the camera driver is a publisher, it doesn't have any callbacks to process, so there is no ros::spinOnce() either. I was wondering if that could be added there (to be able to handle callbacks) or if that would clash with the nodelet's manager?


EDIT: I know the manager handles the spin logic, it actually calls spinOnce() after a 1e6 us usleep (which is equal to what ros::spin() does). What I'm really after is a way to reduce that sleep time. I'm guessing that is not possible without changing and recompiling the nodelet manager, but hoping I'm wrong.

EDIT2: ^ all wrong. See @Lorenz's answer.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2012-07-04 01:55:39 -0500

Lorenz gravatar image

updated 2012-07-04 01:57:18 -0500

The nodelet manager doesn't call spinOnce but spin as you can see in nodelet/src/nodelet.cpp, line 296, at least in Fuerte. Spin doesn't do any sleeps but calls callAvailable on the global callback queue in a loop. callAvailable doesn't sleep on a timeout either. It blocks on a condition variable until a message is received on the queue and executes all callbacks. That means that you shouldn't need to do any special handling or call spin or spinOnce by hand. Message callbacks should be executed at once, i.e. as soon as the main thread gets computation time by the OS scheduler.

edit flag offensive delete link more

Comments

yep, you're right. I was looking under the command == "load" case, which is different from the manager. CallbackQueue::callAvailable() waits on timeout or a new event, so no sleeping. Basically, there is no explicit sleeping, only wait on timeout or event in the whole event loop (AFAICT).

ipso gravatar image ipso  ( 2012-07-04 02:16:50 -0500 )edit

Exactly. However, the timout is only there to stop if ros::ok becomes false, i.e. the manager is shut down.

Lorenz gravatar image Lorenz  ( 2012-07-04 02:18:28 -0500 )edit

And just to be absolutely clear: the 'no sleeping' also means that -- by default -- ROS tries to handle callbacks 'as fast as possible', to the abilities of the machine on which it runs.

ipso gravatar image ipso  ( 2012-07-04 02:43:16 -0500 )edit

Yes. I think so.

Lorenz gravatar image Lorenz  ( 2012-07-04 03:48:12 -0500 )edit

Question Tools

Stats

Asked: 2012-07-04 01:31:45 -0500

Seen: 2,538 times

Last updated: Jul 04 '12