proxy_publisher.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import roslib; roslib.load_manifest('flexbe_core')
4 import rospy
5 from flexbe_msgs.msg import BEStatus
6 
7 import time
8 import random
9 
10 
11 class ProxyPublisher(object):
12  """
13  A proxy for publishing topics.
14  """
15  _simulate_delay = False
16 
17  _topics = {}
18 
19  def __init__(self, topics = {}, _latch=False):
20  """
21  Initializes the proxy with optionally a given set of topics.
22  Automatically creates a publisher for sending status messages.
23 
24  @type topics: dictionary string - message class
25  @param topics: A dictionay containing a collection of topic - message type pairs.
26 
27  @type _latch: bool
28  @param: _latch: Defines if messages on the given topics should be latched.
29  """
30  for topic, msg_type in topics.iteritems():
31  self.createPublisher(topic, msg_type, _latch)
32 
33 
34  def createPublisher(self, topic, msg_type, _latch = False):
35  """
36  Adds a new publisher to the proxy.
37 
38  @type topic: string
39  @param topic: The topic to publish on.
40 
41  @type msg_type: a message class
42  @param msg_type: The type of messages of this topic.
43 
44  @type _latch: bool
45  @param: _latch: Defines if messages on the given topics should be latched.
46  """
47  if topic not in ProxyPublisher._topics:
48  pub = rospy.Publisher(topic, msg_type, latch = _latch, queue_size=100)
49  ProxyPublisher._topics[topic] = pub
50 
51 
52  def is_available(self, topic):
53  """
54  Checks if the publisher on the given topic is available.
55 
56  @type topic: string
57  @param topic: The topic of interest.
58  """
59  return topic in ProxyPublisher._topics
60 
61 
62  def publish(self, topic, msg):
63  """
64  Publishes a message on the specified topic.
65 
66  @type topic: string
67  @param topic: The topic to publish on.
68 
69  @type msg: message class (defined when created publisher)
70  @param msg: The message to publish.
71  """
72  if ProxyPublisher._simulate_delay:
73  time.sleep(max(0, random.gauss(1.5, 1))) # for simulating comms_bridge delay
74 
75  if topic not in ProxyPublisher._topics:
76  rospy.logwarn('ProxyPublisher: topic '+ topic +' not yet registered!')
77  return
78  try:
79  ProxyPublisher._topics[topic].publish(msg)
80  except Exception as e:
81  rospy.logwarn('Something went wrong when publishing to %s!\n%s', topic, str(e))
82 
83 
84  def wait_for_any(self, topic, timeout=5.0):
85  """
86  Blocks until there are any subscribers to the given topic.
87 
88  @type topic: string
89  @param topic: The topic to publish on.
90 
91  @type timeout: float
92  @param timeout: How many seconds should be the maximum blocked time.
93  """
94  pub = ProxyPublisher._topics[topic]
95  starting_time = rospy.get_rostime()
96  while pub.get_num_connections() < 1:
97  elapsed = rospy.get_rostime() - starting_time
98  if elapsed.to_sec() >= timeout:
99  return False
100  return True
def __init__(self, topics={}, _latch=False)
def createPublisher(self, topic, msg_type, _latch=False)


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:51:59