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

Revision history [back]

click to hide/show revision 1
initial version

And I really have no idea how different between rospy.Subscribe and rospy.wait_for_message.

The difference is this:

rospy.Subscriber is a Python class which encapsulates a subscription to a topic. You create an instance of this class whenever you use rospy.Subscriber("<topic_name>", <topic_type>, <callback>). Provided something publishes to <topic_name>, the callback registered will be invoked for every message received.

rospy.wait_for_message(..) is a convenience function, which, as you wrote yourself: "receives one message from [a] topic" and then returns.

More precisely (and a copy of the documentation):

This will create a new subscription to the topic, receive one message, then unsubscribe.

So this function (in pseudo-code almost):

  1. creates a rospy.Subscriber
  2. initialises a flag to False
  3. waits for a single message, which
  4. triggers its callback to be called
  5. this callback sets the flag to True
  6. the True flag will cause the function to cancel its subscription, and
  7. it now returns the message it has received

You could do all of this yourself, but it's much more convenient to have rospy.wait_for_message(..) do this for you.

Note: what you should not do is use wait_for_message(..) to "sample" a topic, like so:

while not rospy.is_shutdown():
    msg = rospy.wait_for_message(..)
    ...

this is very inefficient and almost an anti-pattern.