string_to_overlay_text.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 from std_msgs.msg import String, Float32
6 from threading import Lock
7 #from jsk_rviz_plugins.msg import OverlayText
8 from jsk_rviz_plugins.overlay_text_interface import OverlayTextInterface
9 
10 g_lock = Lock()
11 g_msg = None
12 
13 def callback(msg):
14  global g_msg, g_lock
15  with g_lock:
16  g_msg = msg
17 
18 def config_callback(config, level):
19  global g_format
20  g_format = config.format
21  return config
22 
23 def publish_text(event):
24  global g_lock, g_msg, g_format
25  with g_lock:
26  if not g_msg:
27  return
28  text_interface.publish(g_format.format(g_msg.data))
29 
30 def publish_text_multi(event):
31  global g_lock, multi_topic_msgs, g_format
32  with g_lock:
33  if all([msg for topic, msg in multi_topic_msgs.items()]):
34  text_interface.publish(g_format.format(sum([msg.data for topic, msg in multi_topic_msgs.items()])))
35 
36 multi_topic_msgs = dict()
37 
39  def __init__(self, topic):
40  self.topic = topic
41  global multi_topic_msgs
42  multi_topic_msgs[self.topic] = None
43  def callback(self, msg):
44  global multi_topic_msgs
45  with g_lock:
46  multi_topic_msgs[self.topic] = msg
47 
48 if __name__ == "__main__":
49  rospy.init_node("string_to_overlay_text")
50  text_interface = OverlayTextInterface("~output")
51  multi_topics = rospy.get_param("~multi_topics", [])
52  g_format = rospy.get_param("~format", "{0}")
53  if multi_topics:
54  subs = []
55  for topic in multi_topics:
56  callback = MultiTopicCallback(topic)
57  subs.append(rospy.Subscriber(topic, String, callback.callback))
58  rospy.Timer(rospy.Duration(0.1), publish_text_multi)
59  else:
60  sub = rospy.Subscriber("~input", String, callback)
61  rospy.Timer(rospy.Duration(0.1), publish_text)
62  rospy.spin()
string_to_overlay_text.MultiTopicCallback.__init__
def __init__(self, topic)
Definition: string_to_overlay_text.py:39
string_to_overlay_text.MultiTopicCallback.callback
def callback(self, msg)
Definition: string_to_overlay_text.py:43
string_to_overlay_text.config_callback
def config_callback(config, level)
Definition: string_to_overlay_text.py:18
string_to_overlay_text.callback
callback
Definition: string_to_overlay_text.py:56
string_to_overlay_text.publish_text
def publish_text(event)
Definition: string_to_overlay_text.py:23
jsk_rviz_plugins.overlay_text_interface
Definition: overlay_text_interface.py:1
string_to_overlay_text.MultiTopicCallback
Definition: string_to_overlay_text.py:38
jsk_rviz_plugins.overlay_text_interface.OverlayTextInterface
Definition: overlay_text_interface.py:8
string_to_overlay_text.publish_text_multi
def publish_text_multi(event)
Definition: string_to_overlay_text.py:30
string_to_overlay_text.MultiTopicCallback.topic
topic
Definition: string_to_overlay_text.py:40


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Fri Dec 13 2024 03:49:57