ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
And I really have no idea how different between
rospy.Subscribe
androspy.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):
rospy.Subscriber
False
True
True
flag will cause the function to cancel its subscription, andYou 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.