float32_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 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("float32_to_overlay_text")
50  text_interface = OverlayTextInterface("~text")
51  multi_topics = rospy.get_param("~multi_topics", [])
52  g_format = rospy.get_param("~format", "value: {0}")
53  if multi_topics:
54  subs = []
55  for topic in multi_topics:
56  callback = MultiTopicCallback(topic)
57  subs.append(rospy.Subscriber(topic, Float32, callback.callback))
58  rospy.Timer(rospy.Duration(0.1), publish_text_multi)
59  else:
60  sub = rospy.Subscriber("~input", Float32, callback)
61  rospy.Timer(rospy.Duration(0.1), publish_text)
62  rospy.spin()
def config_callback(config, level)


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Sat Mar 20 2021 03:03:18