00001 import os
00002 import rospy
00003 import rospkg
00004 import math
00005
00006 from PyQt4 import QtGui, QtCore
00007 from qt_gui.plugin import Plugin
00008 from python_qt_binding import loadUi
00009 from python_qt_binding.QtCore import QEvent, QModelIndex, QObject, Qt, QTimer, Signal, Slot, QTimer
00010 from python_qt_binding.QtGui import QShortcut, QTableWidgetItem, QWidget, QLCDNumber, QItemDelegate, QAbstractItemView, QPen, QBrush, QColor, QTransform
00011 from rospy import Time
00012 from std_msgs.msg import Bool
00013 from std_msgs.msg import Float64, Float32, Empty
00014
00015
00016 class TrackerBaseUi(QObject):
00017 _update_task_delegates = Signal()
00018
00019 def __init__(self, context):
00020 QObject.__init__(self, context)
00021 self.setObjectName('TrackerBaseUi')
00022
00023
00024 self._WhiteLightPublisher = rospy.Publisher('/light', Bool)
00025 self._BlueLightPublisher = rospy.Publisher('/bluelight', Bool)
00026 self._CalibFlipperPublisher = rospy.Publisher('/init_flipper', Empty)
00027 self._FlipperCommandPublisher = rospy.Publisher('/flipper_control/command/absolute', Float64)
00028
00029 self._FlipperStateSubsciber = rospy.Subscriber('/flipper_control/state', Float64, self._on_flipper_state)
00030
00031
00032 self._widget = QWidget()
00033 ui_file = os.path.join(rospkg.RosPack().get_path('tracker_base_ui'), 'lib', 'TrackerBaseUi.ui')
00034 loadUi(ui_file, self._widget)
00035 self._widget.setObjectName('TrackerBaseUi')
00036
00037
00038 self._widget.white_checkBox.stateChanged.connect(self._on_white_changed)
00039 self._widget.blue_checkBox.stateChanged.connect(self._on_blue_changed)
00040
00041
00042
00043
00044
00045
00046 if context.serial_number() > 1:
00047 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00048
00049 context.add_widget(self._widget)
00050
00051
00052 self._trackerSupplySubscriber = rospy.Subscriber("/supply_voltage", Float32, self._on_tracker_supply)
00053
00054
00055
00056 self._update_task_delegates.connect(self._on_update_task_delegates)
00057 self._widget.calib_flipper_button.pressed.connect(self._on_calib_flipper_button_pressed)
00058 self._widget.flipper_up_button.pressed.connect(self._flipper_up_button_pressed)
00059 self._widget.flipper_center_button.pressed.connect(self._flipper_center_button_pressed)
00060 self._widget.flipper_down_button.pressed.connect(self._flipper_down_button_pressed)
00061 self.supply = -1
00062 self._widget.supply_lineEdit.setText('unknown')
00063
00064
00065 self.flipper_scene = QtGui.QGraphicsScene(self)
00066
00067 self._widget.graphicsView_Flipper.setScene(self.flipper_scene)
00068 self.flipper_state = 0;
00069 self._widget.graphicsView_Flipper.setRenderHint(QtGui.QPainter.Antialiasing)
00070
00071 self._widget.graphicsView_Flipper.show()
00072
00073 self.timer = QTimer()
00074 self.timer.timeout.connect(self.draw_flipper_state)
00075 self.timer.start(500)
00076
00077 self.flipper_front_max_state = 0.79
00078 self.flipper_front_min_state = -0.2
00079
00080 self.flipper_diff = 0.1
00081
00082 def _on_tracker_supply(self, message):
00083 self.supply = message.data
00084 self._update_task_delegates.emit()
00085
00086 def _on_flipper_state(self, message):
00087 self.flipper_state = message.data
00088
00089 def _on_flipper_cmd(self, message):
00090 self.flipper_cmd +=0.1
00091
00092 def shutdown_plugin(self):
00093
00094 pass
00095
00096 def save_settings(self, plugin_settings, instance_settings):
00097
00098
00099 pass
00100
00101 def restore_settings(self, plugin_settings, instance_settings):
00102
00103
00104 pass
00105
00106 def _on_white_changed(self,value):
00107 self._WhiteLightPublisher.publish(value)
00108
00109 def _on_blue_changed(self,value):
00110 self._BlueLightPublisher.publish(value)
00111
00112 def _on_calib_flipper_button_pressed(self):
00113 self._CalibFlipperPublisher.publish(Empty())
00114
00115 def _flipper_up_button_pressed(self):
00116 self._FlipperCommandPublisher.publish(0.5)
00117 self.flipper_state += 0.05
00118
00119 def _flipper_center_button_pressed(self):
00120 self._FlipperCommandPublisher.publish(0)
00121 self.flipper_state = 0
00122
00123 def _flipper_down_button_pressed(self):
00124 self._FlipperCommandPublisher.publish(-0.2)
00125 self.flipper_state -= 0.05
00126
00127 def _on_update_task_delegates(self):
00128 if (self.supply == -1) :
00129 self._widget.supply_lineEdit.setText('unknown')
00130 else:
00131 self._widget.supply_lineEdit.setText(str(self.supply))
00132
00133 def compute_coordinates(self, angle):
00134
00135 length = 100
00136 y = length * math.cos(-angle)
00137 x = length * math.sin(-angle)
00138 return {'y':y, 'x':x }
00139
00140 def draw_flipper_max(self):
00141 coords = self.compute_coordinates(self.flipper_front_max_state)
00142
00143 self.flipper_scene.addLine(200, 50+coords['x'], 300, 50+coords['x'])
00144
00145 def draw_flipper_min(self):
00146 coords = self.compute_coordinates(self.flipper_front_min_state)
00147
00148 self.flipper_scene.addLine(200, 100+coords['x'], 300, 100+coords['x'])
00149
00150 def draw_flipper_state(self):
00151 length = 100
00152
00153 self._widget.flipper_front_current.setText(str(self.flipper_state))
00154
00155
00156
00157 if(self.flipper_state > self.flipper_front_max_state):
00158 self.flipper_diff *= -1
00159
00160
00161 if(self.flipper_state < self.flipper_front_min_state):
00162 self.flipper_diff *= -1
00163
00164
00165 self.flipper_scene.clear()
00166
00167 self.draw_flipper_max()
00168 self.draw_flipper_min()
00169
00170
00171 self.flipper_scene.addRect(65, 60, 145, 25, QPen(), QBrush(QColor(255,0,0)))
00172
00173
00174 self.flipper_scene.addEllipse(150, 50, 50, 50, QPen(), QBrush(QColor(0,0,0)))
00175
00176
00177 self.flipper_scene.addEllipse(50, 50, 50, 50, QPen(), QBrush(QColor(0,0,0)))
00178
00179
00180 transform = QTransform()
00181 transform.translate(155,70)
00182 transform.rotateRadians(-self.flipper_state)
00183 transform.translate(-25,-25)
00184
00185
00186 flipper_wheel = QtGui.QGraphicsEllipseItem(120,5, 50, 50)
00187 flipper_wheel.setTransform(transform)
00188 flipper_wheel.setBrush( QBrush(QColor(0,0,0)))
00189
00190 self.flipper_scene.addItem(flipper_wheel)
00191
00192
00193
00194 transform = QTransform()
00195 transform.translate(160,75)
00196 transform.rotateRadians(-self.flipper_state)
00197 transform.translate(-10,-10)
00198 flipper_link = QtGui.QGraphicsRectItem(0,0, 135, 20)
00199 flipper_link.setBrush( QBrush(QColor(163,163,163)))
00200
00201 flipper_link.setTransform(transform)
00202
00203 self.flipper_scene.addItem(flipper_link)
00204
00205
00206 self.flipper_scene.addEllipse(155, 70, 10, 10, QPen(), QBrush(QColor(0,0,0)))
00207
00208 transform = QTransform()
00209 transform.translate(160,75)
00210 transform.rotateRadians(-self.flipper_state)
00211 transform.translate(-10,-10)
00212
00213
00214 flipper_wheel_connection = QtGui.QGraphicsEllipseItem(120,5, 10, 10)
00215 flipper_wheel_connection.setTransform(transform)
00216 flipper_wheel_connection.setBrush( QBrush(QColor(0,0,0)))
00217
00218 self.flipper_scene.addItem(flipper_wheel_connection)
00219
00220
00221