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):
- creates a
rospy.Subscriber
- initialises a flag to
False
- waits for a single message, which
- triggers its callback to be called
- this callback sets the flag to
True
- the
True
flag will cause the function to cancel its subscription, and - 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.