[ros-users] Problem synchronizing between nodes

Ken Conley kwc at willowgarage.com
Fri Dec 3 08:57:05 UTC 2010


Hi John,

On Thu, Dec 2, 2010 at 9:38 PM, John Daly <jmdaly at gmail.com> wrote:
> Hi everyone,
>
> I'm new to ROS, and having some trouble implementing a robot
> controller. So I was hoping someone could point me in the right
> direction! I've been through the tutorials on the ROS wiki, but am
> still having some trouble.
>
> At the moment we don't have access to the actual robot, so I've
> written a ROS node (this is all in Python using rospy) to simulate the
> robot dynamics (call it node A). This node publishes the robot state
> to a topic. Then I've written another node (node B) that subscribes to
> the topic where the state is published. This node computes the control
> law for the robot, and publishes that signal to a different topic.
> Node A subscribes to this topic, so that it gets the control signal,
> and applies it to the robot dynamics.
>
> I've written callbacks in each of the nodes that simply take the
> message coming in from the topic and assign it to a variable that is
> local to the node.
>
> In the main loop of each node, I have something similar to the following:
>
> rate = rospy.Rate(UPDATE_HZ)
>
> while not rospy.is_shutdown():
>    rate.sleep()
>
>    # Do the work. In node A, this is integrating dynamics. In node B,
> this is computing a control law.
>    # After the work is done, publish the suitable message (robot
> state in Node A, control signal in node B).
>
> The problem I'm having is that I don't think the two nodes are at all
> synchronized, and I think this is causing trouble with the values
> computed in each node. For example, node A might get executed a couple
> of times before node B does.

I don't write controllers, but the issue I think you're having is that
you're writing clock-driven code (i.e. rate.sleep()), when you really
want data-driven code.  A data-driven node receives a message, does
something to it, and publishes the result(s), w/o attempting to loop
at a particular rate -- the receipt of the message doubles as a clock.
You would still need a clock-driven code to determine the rate of the
data going into the system.

> So I'm wondering, is there an existing pattern within rospy for
> synchronizing among nodes that are passing messages back and forth? Or
> should I look to Python for synchronization mechanisms? If anyone can
> point me to some references on this, that would be great. Also, is
> each node run as its own process, or are all nodes run as threads in
> some other process?

Each node is its own process.  roscpp has nodelets, which allow you to
load multiple programs into one process.

 - Ken



More information about the ros-users mailing list