3 from threading
import Timer
10 A proxy for publishing topics. 14 def __init__(self, topics={}, _latch=False, _queue_size=100):
16 Initializes the proxy with optionally a given set of topics. 17 Automatically creates a publisher for sending status messages. 19 @type topics: dictionary string - message class 20 @param topics: A dictionay containing a collection of topic - message type pairs. 23 @param: _latch: Defines if messages on the given topics should be latched. 25 @type _queue_size: int 26 @param: _queue_size: Defines the queue size of the new publishers. 28 for topic, msg_type
in topics.items():
33 Adds a new publisher to the proxy. 36 @param topic: The topic to publish on. 38 @type msg_type: a message class 39 @param msg_type: The type of messages of this topic. 42 @param: _latch: Defines if messages on the given topics should be latched. 44 @type _queue_size: int 45 @param: _queue_size: Defines the queue size of the publisher. 47 if topic
not in ProxyPublisher._topics:
48 ProxyPublisher._topics[topic] = rospy.Publisher(topic, msg_type, latch=_latch, queue_size=_queue_size)
52 Checks if the publisher on the given topic is available. 55 @param topic: The topic of interest. 57 return topic
in ProxyPublisher._topics
61 Publishes a message on the specified topic. 64 @param topic: The topic to publish on. 66 @type msg: message class (defined when created publisher) 67 @param msg: The message to publish. 69 if topic
not in ProxyPublisher._topics:
70 Logger.logwarn(
'ProxyPublisher: topic %s not yet registered!' % topic)
73 ProxyPublisher._topics[topic].
publish(msg)
74 except Exception
as e:
75 Logger.logwarn(
'Something went wrong when publishing to %s!\n%s' % (topic, str(e)))
79 Blocks until there are any subscribers to the given topic. 82 @param topic: The topic to publish on. 85 @param timeout: How many seconds should be the maximum blocked time. 87 pub = ProxyPublisher._topics.get(topic)
89 Logger.logerr(
"Publisher %s not yet registered, need to add it first!" % topic)
102 Logger.logerr(
"Waiting for subscribers on %s timed out!" % topic)
106 Logger.loginfo(
"Finally found subscriber on %s..." % (topic))
110 Logger.logwarn(
"Waiting for subscribers on %s..." % (topic))
113 starting_time = rospy.get_rostime()
114 rate = rospy.Rate(100)
115 while not rospy.is_shutdown():
116 elapsed = rospy.get_rostime() - starting_time
117 if elapsed.to_sec() >= timeout:
119 if pub.get_num_connections() >= 1:
def __init__(self, topics={}, _latch=False, _queue_size=100)
def createPublisher(self, topic, msg_type, _latch=False, _queue_size=100)
def is_available(self, topic)
def publish(self, topic, msg)
def wait_for_any(self, topic, timeout=5.0)
def _wait_for_subscribers(self, pub, timeout=5.0)
def _print_wait_warning(self, topic)