overlay_menu_bridge.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Author: Yuki Furuta <me@furushchev.ru>
4 
5 
6 import rospy
7 from jsk_rviz_plugins.msg import OverlayMenu
8 
9 
10 class OverlayMenuBridge(object):
11  def __init__(self):
12  super(OverlayMenuBridge, self).__init__()
13 
14  if OverlayMenu._md5sum == 'fed3c7e9788f7ee37908107a2597b619':
15  rospy.logwarn('This script is not necessary since md5sum of OverlayMenu is the same as old one.')
16 
17  self.queue_size = rospy.get_param('~queue_size', 10)
18  self.remap_suffix = rospy.get_param('~remap_suffix', 'kinetic')
19  self.publishers = {}
20  self.subscribers = {}
21 
22  poll_rate = rospy.get_param('~poll_rate', 1.0)
23  self.poll_timer = rospy.Timer(rospy.Duration(1.0 / poll_rate), self.timerCallback)
24 
25  def remap(self, topic):
26  return topic + '/' + self.remap_suffix
27 
28  def messageCallback(self, msg, topic):
29  try:
30  self.publishers[self.remap(topic)].publish(msg)
31  except Exception as exc:
32  rospy.logerr('Error on publishing to {}: {}'.format(topic, exc))
33 
34  def timerCallback(self, event):
35  topics = [i[0] for i in rospy.get_published_topics() if i[1] == OverlayMenu._type]
36  subscribed_topics = self.subscribers.keys()
37  managed_topics = subscribed_topics + self.publishers.keys()
38  for topic in topics:
39  if topic not in managed_topics:
40  self.publishers[self.remap(topic)] = rospy.Publisher(
41  self.remap(topic), OverlayMenu, queue_size=self.queue_size)
42  self.subscribers[topic] = rospy.Subscriber(
43  topic, rospy.AnyMsg, self.messageCallback, topic,
44  queue_size=self.queue_size)
45 
46  rospy.loginfo('Remapped {} -> {}'.format(topic, self.remap(topic)))
47 
48  for topic in subscribed_topics:
49  if topic not in topics:
50  sub = self.subscribers.pop(topic)
51  sub.unregister()
52  pub = self.publishers.pop(self.remap(topic))
53  pub.unregister()
54 
55  rospy.loginfo('Stopped Remap {} -> {}'.format(topic, self.remap(topic)))
56 
57 
58 if __name__ == '__main__':
59  rospy.init_node('overlay_menu_bridge')
61  rospy.spin()
overlay_menu_bridge.OverlayMenuBridge.publishers
publishers
Definition: overlay_menu_bridge.py:19
overlay_menu_bridge.OverlayMenuBridge
Definition: overlay_menu_bridge.py:10
msg
overlay_menu_bridge.OverlayMenuBridge.messageCallback
def messageCallback(self, msg, topic)
Definition: overlay_menu_bridge.py:28
overlay_menu_bridge.OverlayMenuBridge.remap
def remap(self, topic)
Definition: overlay_menu_bridge.py:25
overlay_menu_bridge.OverlayMenuBridge.queue_size
queue_size
Definition: overlay_menu_bridge.py:17
overlay_menu_bridge.OverlayMenuBridge.timerCallback
def timerCallback(self, event)
Definition: overlay_menu_bridge.py:34
overlay_menu_bridge.OverlayMenuBridge.remap_suffix
remap_suffix
Definition: overlay_menu_bridge.py:18
overlay_menu_bridge.OverlayMenuBridge.__init__
def __init__(self)
Definition: overlay_menu_bridge.py:11
overlay_menu_bridge.OverlayMenuBridge.poll_timer
poll_timer
Definition: overlay_menu_bridge.py:23
overlay_menu_bridge.OverlayMenuBridge.subscribers
subscribers
Definition: overlay_menu_bridge.py:20


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Fri Aug 2 2024 08:50:14