ping.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os
00004 import sys
00005 
00006 import rospy
00007 import roslib
00008 
00009 roslib.load_manifest("hrpsys_gazebo_atlas")
00010 
00011 from std_srvs.srv import Empty
00012 from std_msgs.msg import Float64
00013 from qt_gui.plugin import Plugin
00014 from python_qt_binding import loadUi
00015 from python_qt_binding.QtGui import QWidget
00016 
00017 from python_qt_binding.QtGui import QLabel, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QCheckBox, QWidget, QToolBar, QLineEdit, QPushButton, QPixmap
00018 from python_qt_binding.QtCore import Qt, QTimer
00019 
00020 class RedirectStdStreams(object):
00021     def __init__(self, stdout=None, stderr=None):
00022         self._stdout = stdout or sys.stdout
00023         self._stderr = stderr or sys.stderr
00024     def __enter__(self):
00025         self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
00026         self.old_stdout.flush(); self.old_stderr.flush()
00027         sys.stdout, sys.stderr = self._stdout, self._stderr
00028     def __exit__(self, exc_type, exc_value, traceback):
00029         self._stdout.flush(); self._stderr.flush()
00030         sys.stdout = self.old_stdout
00031         sys.stderr = self.old_stderr
00032                                                                                                 
00033 
00034 class PingGUI(Plugin):
00035     def __init__(self, context):
00036         super(PingGUI, self).__init__(context)
00037         # Give QObjects reasonable names
00038         self.setObjectName('PingGUI')
00039         self.msg = None
00040         # Create a container widget and give it a layout
00041         self._container = QWidget()
00042         self._layout    = QVBoxLayout()
00043         self._container.setLayout(self._layout)
00044         self._label = QLabel("xx ms latency")
00045         p = self._label.palette()
00046         p.setColor(self._label.backgroundRole(), Qt.red)
00047         self._label.setPalette(p)
00048         self._layout.addWidget(self._label)
00049         self.set_bg_color(100, 100, 100)
00050         
00051         rospy.Subscriber("/ping/delay", Float64, self.ping_cb)
00052         context.add_widget(self._container)
00053         self._update_plot_timer = QTimer(self)
00054         self._update_plot_timer.timeout.connect(self.update_gui)
00055         self._update_plot_timer.start(1)
00056     def update_gui(self):
00057         if not self.msg:
00058             return
00059         msg = self.msg
00060         # msec 
00061         # 100 -> green, 1000 -> red
00062         # normalize within 1000 ~ 100
00063         orig_latency = msg.data
00064         msg_data = orig_latency
00065         if msg.data > 1000:
00066             msg_data = 1000
00067         elif msg.data < 100:
00068             msg_data = 100
00069         ratio = (msg_data - 100) / (1000 - 100)
00070         color_r = ratio * 255.0
00071         color_g = (1 - ratio) * 255.0
00072         # devnull = open(os.devnull, "w")
00073         # with RedirectStdStreams(stdout=devnull, stderr=devnull):
00074         self.set_bg_color(color_r, color_g, 0)
00075         self._label.setText("%d ms latency" % (orig_latency))
00076     def set_bg_color(self, r, g, b):
00077         self._label.setStyleSheet("QLabel { display: block; background-color: rgba(%d, %d, %d, 255); text-align: center; font-size: 30px;}" % (r, g, b))
00078     def ping_cb(self, msg):
00079         self.msg = msg
00080     def shutdown_plugin(self):
00081         pass
00082     def save_settings(self, plugin_settings, instance_settings):
00083         pass
00084     def restore_settings(self, plugin_settings, instance_settings):
00085         pass
00086 
00087     
00088 
00089     


hrpsys_gazebo_atlas
Author(s): Yohei Kakiuchi
autogenerated on Wed Sep 16 2015 10:53:59