float32_to_overlay_text.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 
00005 from std_msgs.msg import Float32
00006 from threading import Lock
00007 #from jsk_rviz_plugins.msg import OverlayText
00008 from jsk_rviz_plugins.overlay_text_interface import OverlayTextInterface
00009 
00010 g_lock = Lock()
00011 g_msg = None
00012 
00013 def callback(msg):
00014     global g_msg, g_lock
00015     with g_lock:
00016         g_msg = msg
00017 
00018 def config_callback(config, level):
00019     global g_format
00020     g_format = config.format
00021     return config
00022 
00023 def publish_text(event):
00024     global g_lock, g_msg, g_format
00025     with g_lock:
00026         if not g_msg:
00027             return
00028         text_interface.publish(g_format.format(g_msg.data))
00029 
00030 def publish_text_multi(event):
00031     global g_lock, multi_topic_msgs, g_format
00032     with g_lock:
00033         if all([msg for topic, msg in multi_topic_msgs.items()]):
00034             text_interface.publish(g_format.format(sum([msg.data for topic, msg in multi_topic_msgs.items()])))
00035         
00036 multi_topic_msgs = dict()
00037         
00038 class MultiTopicCallback():
00039     def __init__(self, topic):
00040         self.topic = topic
00041         global multi_topic_msgs
00042         multi_topic_msgs[self.topic] = None
00043     def callback(self, msg):
00044         global multi_topic_msgs
00045         with g_lock:
00046             multi_topic_msgs[self.topic] = msg
00047         
00048 if __name__ == "__main__":
00049     rospy.init_node("float32_to_overlay_text")
00050     text_interface = OverlayTextInterface("~text")
00051     multi_topics = rospy.get_param("~multi_topics", [])
00052     g_format = rospy.get_param("~format", "value: {0}")
00053     if multi_topics:
00054         subs = []
00055         for topic in multi_topics:
00056             callback = MultiTopicCallback(topic)
00057             subs.append(rospy.Subscriber(topic, Float32, callback.callback))
00058         rospy.Timer(rospy.Duration(0.1), publish_text_multi)
00059     else:
00060         sub = rospy.Subscriber("~input", Float32, callback)
00061         rospy.Timer(rospy.Duration(0.1), publish_text)
00062     rospy.spin()


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Wed May 1 2019 02:40:22