subscriber_proxy.py
Go to the documentation of this file.
00001 #
00002 # License: BSD
00003 #   https://raw.github.com/robotics-in-concert/rocon_tools/license/LICENSE
00004 #
00005 ##############################################################################
00006 # Imports
00007 ##############################################################################
00008 
00009 import time
00010 import rospy
00011 
00012 ##############################################################################
00013 # Subscriber Proxy
00014 ##############################################################################
00015 
00016 class SubscriberProxy():
00017     '''
00018       Works like a service proxy, but using a subscriber instead.
00019     '''
00020     def __init__(self, topic, msg_type):
00021         '''
00022           @param topic : the topic name to subscriber to
00023           @type str
00024           @param msg_type : any ros message type (typical arg for the subscriber)
00025           @type msg
00026           @param timeout : timeout on the wait operation (None = /infty)
00027           @type rospy.Duration()
00028           @return msg type data or None
00029         '''
00030         self._subscriber = rospy.Subscriber(topic, msg_type, self._callback)
00031         self._data = None
00032 
00033     def __call__(self, timeout=None):
00034         '''
00035           Returns immediately with the latest data or waits for
00036           incoming data.
00037 
00038           @param timeout : time to wait for data, polling at 10Hz.
00039           @type rospy.Duration
00040           @return latest data or None
00041         '''
00042         if timeout:
00043             # everything in floating point calculations
00044             timeout_time = time.time() + timeout.to_sec()
00045         while not rospy.is_shutdown() and self._data == None:
00046             rospy.rostime.wallsleep(0.1)
00047             if timeout:
00048                 if time.time() > timeout_time:
00049                     return None
00050         return self._data
00051 
00052     def wait_for_next(self, timeout=None):
00053         '''
00054           Makes sure any current data is cleared and waits for new data.
00055         '''
00056         self._data = None
00057         return self.__call__(timeout)
00058 
00059     def wait_for_publishers(self):
00060         '''
00061           Blocks until publishers are seen.
00062 
00063           @raise rospy.exceptions.ROSInterruptException if we are in shutdown.
00064         '''
00065         r = rospy.Rate(10)
00066         while not rospy.is_shutdown():
00067             if self._subscriber.get_num_connections() != 0:
00068                 return
00069             else:
00070                 r.sleep()
00071         # we are shutting down
00072         raise rospy.exceptions.ROSInterruptException
00073 
00074     def _callback(self, data):
00075         self._data = data
00076 
00077     def unregister(self):
00078         '''
00079           Unregister the subscriber so future instantiations of this class can pull a
00080           fresh subscriber (important if the data is latched).
00081         '''
00082         self._subscriber.unregister()


rocon_python_comms
Author(s): Daniel Stonier
autogenerated on Fri May 2 2014 10:35:42