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

What does `rospy.wait_for_message('/scan', LaserScan)` mean?

asked 2020-03-03 00:47:07 -0600

kane_choigo gravatar image

Hello, I'm new in ROS melodic of Ubuntu 18.04.

As far as I know, the only way to get some topic is to declare rospy.Subscribe.

However, I've seen

data = rospy.wait_for_message('/scan', LaserScan, timeout=5)

According to here, rospy.wait_for_message receives one message from topic.

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

If anyone who is familiar with this, would you please give me some advice?

Thanks in advance. :)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-03-03 03:38:50 -0600

gvdhoorn gravatar image

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.

edit flag offensive delete link more

Comments

Thanks for the quick reply. So, you recommended I not using wait_for_message(..) to just sample a topic, and when would you advise me to consider select that? I've planned to use it right just

data = rospy.wait_for_message('/scan', LaserScan, timeout=5)

as you wrote. Would you please tell me when is the most appropriate time to use rospy.wait_for_message? Thanks :)

kane_choigo gravatar image kane_choigo  ( 2020-03-03 07:32:03 -0600 )edit

when would you advise me to consider select that

If you need to receive the data from a topic only once (like maybe a topic publishing a configuration or info about something that isn't changing during the execution) then it's fine to use wait_for_message.

Your example is about the /scan topic, which is a topic that is published by your sensor very frequently (generally speaking) so unless you just need your sensor data once (which is odd) you would want to have a permanent subscriber to the /scan topic to always get the latest sensor informations.

Delb gravatar image Delb  ( 2020-03-06 10:03:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-03 00:47:07 -0600

Seen: 10,128 times

Last updated: Mar 03 '20