Go to the documentation of this file.00001
00002
00003 import roslib; roslib.load_manifest('flexbe_core')
00004 import rospy
00005 from flexbe_msgs.msg import BEStatus
00006
00007 import time
00008 import random
00009
00010
00011 class ProxyPublisher(object):
00012 """
00013 A proxy for publishing topics.
00014 """
00015 _simulate_delay = False
00016
00017 _topics = {}
00018
00019 def __init__(self, topics = {}, _latch=False):
00020 """
00021 Initializes the proxy with optionally a given set of topics.
00022 Automatically creates a publisher for sending status messages.
00023
00024 @type topics: dictionary string - message class
00025 @param topics: A dictionay containing a collection of topic - message type pairs.
00026
00027 @type _latch: bool
00028 @param: _latch: Defines if messages on the given topics should be latched.
00029 """
00030 for topic, msg_type in topics.iteritems():
00031 self.createPublisher(topic, msg_type, _latch)
00032
00033
00034 def createPublisher(self, topic, msg_type, _latch = False):
00035 """
00036 Adds a new publisher to the proxy.
00037
00038 @type topic: string
00039 @param topic: The topic to publish on.
00040
00041 @type msg_type: a message class
00042 @param msg_type: The type of messages of this topic.
00043
00044 @type _latch: bool
00045 @param: _latch: Defines if messages on the given topics should be latched.
00046 """
00047 if topic not in ProxyPublisher._topics:
00048 pub = rospy.Publisher(topic, msg_type, latch = _latch, queue_size=100)
00049 ProxyPublisher._topics[topic] = pub
00050
00051
00052 def is_available(self, topic):
00053 """
00054 Checks if the publisher on the given topic is available.
00055
00056 @type topic: string
00057 @param topic: The topic of interest.
00058 """
00059 return topic in ProxyPublisher._topics
00060
00061
00062 def publish(self, topic, msg):
00063 """
00064 Publishes a message on the specified topic.
00065
00066 @type topic: string
00067 @param topic: The topic to publish on.
00068
00069 @type msg: message class (defined when created publisher)
00070 @param msg: The message to publish.
00071 """
00072 if ProxyPublisher._simulate_delay:
00073 time.sleep(max(0, random.gauss(1.5, 1)))
00074
00075 if topic not in ProxyPublisher._topics:
00076 rospy.logwarn('ProxyPublisher: topic '+ topic +' not yet registered!')
00077 return
00078 try:
00079 ProxyPublisher._topics[topic].publish(msg)
00080 except Exception as e:
00081 rospy.logwarn('Something went wrong when publishing to %s!\n%s', topic, str(e))
00082
00083
00084 def wait_for_any(self, topic, timeout=5.0):
00085 """
00086 Blocks until there are any subscribers to the given topic.
00087
00088 @type topic: string
00089 @param topic: The topic to publish on.
00090
00091 @type timeout: float
00092 @param timeout: How many seconds should be the maximum blocked time.
00093 """
00094 pub = ProxyPublisher._topics[topic]
00095 starting_time = rospy.get_rostime()
00096 while pub.get_num_connections() < 1:
00097 elapsed = rospy.get_rostime() - starting_time
00098 if elapsed.to_sec() >= timeout:
00099 return False
00100 return True