(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Subscriber Proxy

Description: A useful way to use a subscriber and it's callback in a similar way to service callbacks with empty requests.

Keywords: rocon service

Tutorial Level: BEGINNER

Overview

Quite often you just want to tune into a publisher for one message, not a stream. This will often be the case when tuning into a latched publisher (e.g. it may be serving a map). To do that takes several lines of code and gets annoying to repeat.

The SubscriberProxy class takes care of that for you with a similar syntax to the existing ServiceProxy.

Example

The infamous talker-listener example. The subscriber proxy provides an alternate way of listening to the topic.

   1 import rocon_utilities
   2 
   3 chatter_proxy = rocon_utilities.SubscriberProxy('chatter', String)
   4 print("Heard: %s" % chatter_proxy())

Breakdown

The proxy internally constructs a subscriber and callback. When you call the proxy itself, it merely returns the latest data, or if it hasn't received any, waits for the first data to arrive.

Timeouts

The call function can be made with a timeout to make sure it doesn't hang:

   1 print("Heard: %s" % chatter_proxy(rospy.Duration(0.5)))

New Data

If you aren't interested in the last data that arrived, you can use (timeouts work for this as well):

   1 print("Heard: %s" % chatter_proxy.wait_for_next(rospy.Duration(0.5)))

One Shot

If you don't want to use it more than once, call it with the constructor and be sure to let it go out of scope so that it doesn't keep processing callbacks internally.

   1 chatter_proxy = rocon_utilities.SubscriberProxy('chatter', String)()

Wiki: rocon_utilities/Tutorials/groovy/Subscriber Proxy (last edited 2013-04-21 23:42:08 by DanielStonier)