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

Execute loops in multiple nodes at the same time?

asked 2012-02-07 12:01:08 -0500

gavinmachine gravatar image


I have a system that has two ROS nodes, each with a while loop that waits for a certain amount of time to pass. However, when the first node enters its loop, the rest of the system freezes waiting for the loop to complete.

The question: Is there a way to execute my ROS system so that several nodes can run at the same time, even with loops running within? Perhaps the solution lies with threading or parallelism?


edit retag flag offensive close merge delete


The only thing I've thought of is to use the ros::Rate loop_rate to slow the loops down. This seems to work, but I'm not sure if it's an optimal solution.
gavinmachine gravatar image gavinmachine  ( 2012-02-07 12:47:07 -0500 )edit
What is "the rest of the system"? Is the node that's locking up expected to execute any callbacks while it's waiting? Why not use one of the system sleep functions instead of a while loop?
ahendrix gravatar image ahendrix  ( 2012-02-07 18:08:52 -0500 )edit

3 Answers

Sort by » oldest newest most voted

answered 2012-02-07 17:12:01 -0500

Each ROS node is a single process (see first sentence in the Nodes documentation), so what you describe should not happen. Have you actually tried what you describe or is it just a description of what you assume would happen? Being two separate processes, two nodes already run in parallel and one will not block the other (apart from some special cases on could probably construct somehow).

edit flag offensive delete link more

answered 2012-02-08 01:06:12 -0500

DimitriProsser gravatar image

If you're trying to wait a certain amount of time before proceeding, I would suggest that you do it asynchronously so that you don't block any loops. This can be achieved in ROS through timers.

Once a timer expires, it throws an event that is handled in a callback function. In this way, you can simply have a loop that runs all of your updates, but when the timer expires, command will jump to handle the callback function, and then will immediately return to your main loop. This way, you're not blocking your main loop while you wait for a certain period of time. You can see here for more information on event-driven programming.

edit flag offensive delete link more

answered 2012-02-07 18:34:53 -0500

I agree with Stefan answer. In any case if (as you said) are considering the thread level parallelism then you should consider use the nodelets:

edit flag offensive delete link more

Question Tools


Asked: 2012-02-07 12:01:08 -0500

Seen: 2,318 times

Last updated: Feb 08 '12