TrackerBaseUi.py
Go to the documentation of this file.
1 import os
2 import rospy
3 import rospkg
4 import math
5 
6 from PyQt4 import QtGui, QtCore
7 from qt_gui.plugin import Plugin
8 from python_qt_binding import loadUi
9 from python_qt_binding.QtCore import QEvent, QModelIndex, QObject, Qt, QTimer, Signal, Slot, QTimer
10 from python_qt_binding.QtGui import QShortcut, QTableWidgetItem, QWidget, QLCDNumber, QItemDelegate, QAbstractItemView, QPen, QBrush, QColor, QTransform
11 from rospy import Time
12 from std_msgs.msg import Bool
13 from std_msgs.msg import Float64, Float32, Empty
14 
15 
16 class TrackerBaseUi(QObject):
17  _update_task_delegates = Signal()
18 
19  def __init__(self, context):
20  QObject.__init__(self, context)
21  self.setObjectName('TrackerBaseUi')
22 
23  # setup publisher
24  self._WhiteLightPublisher = rospy.Publisher('/light', Bool)
25  self._BlueLightPublisher = rospy.Publisher('/bluelight', Bool)
26  self._CalibFlipperPublisher = rospy.Publisher('/init_flipper', Empty)
27  self._FlipperCommandPublisher = rospy.Publisher('/flipper_control/command/absolute', Float64)
28 
29  self._FlipperStateSubsciber = rospy.Subscriber('/flipper_control/state', Float64, self._on_flipper_state)
30 
31  # setup main widget
32  self._widget = QWidget()
33  ui_file = os.path.join(rospkg.RosPack().get_path('tracker_base_ui'), 'lib', 'TrackerBaseUi.ui')
34  loadUi(ui_file, self._widget)
35  self._widget.setObjectName('TrackerBaseUi')
36 
37  #set connections
38  self._widget.white_checkBox.stateChanged.connect(self._on_white_changed)
39  self._widget.blue_checkBox.stateChanged.connect(self._on_blue_changed)
40 
41  # Show _widget.windowTitle on left-top of each plugin (when
42  # it's set in _widget). This is useful when you open multiple
43  # plugins at once. Also if you open multiple instances of your
44  # plugin at once, these lines add number to make it easy to
45  # tell from pane to pane.
46  if context.serial_number() > 1:
47  self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
48  # Add widget to the user interface
49  context.add_widget(self._widget)
50 
51  # setup subscribers
52  self._trackerSupplySubscriber = rospy.Subscriber("/supply_voltage", Float32, self._on_tracker_supply)
53 
54 
55  # connect Signal Slot
56  self._update_task_delegates.connect(self._on_update_task_delegates)
57  self._widget.calib_flipper_button.pressed.connect(self._on_calib_flipper_button_pressed)
58  self._widget.flipper_up_button.pressed.connect(self._flipper_up_button_pressed)
59  self._widget.flipper_center_button.pressed.connect(self._flipper_center_button_pressed)
60  self._widget.flipper_down_button.pressed.connect(self._flipper_down_button_pressed)
61  self.supply = -1
62  self._widget.supply_lineEdit.setText('unknown')
63  # init percept model
64 
65  self.flipper_scene = QtGui.QGraphicsScene(self)
66 
67  self._widget.graphicsView_Flipper.setScene(self.flipper_scene)
68  self.flipper_state = 0;
69  self._widget.graphicsView_Flipper.setRenderHint(QtGui.QPainter.Antialiasing)
70 
71  self._widget.graphicsView_Flipper.show()
72 
73  self.timer = QTimer()
74  self.timer.timeout.connect(self.draw_flipper_state)
75  self.timer.start(500)
76 
79 
80  self.flipper_diff = 0.1
81 
82  def _on_tracker_supply(self, message):
83  self.supply = message.data
84  self._update_task_delegates.emit()
85 
86  def _on_flipper_state(self, message):
87  self.flipper_state = message.data
88 
89  def _on_flipper_cmd(self, message):
90  self.flipper_cmd +=0.1 #TODO
91 
92  def shutdown_plugin(self):
93  # TODO unregister all publishers here
94  pass
95 
96  def save_settings(self, plugin_settings, instance_settings):
97  # TODO save intrinsic configuration, usually using:
98  # instance_settings.set_value(k, v)
99  pass
100 
101  def restore_settings(self, plugin_settings, instance_settings):
102  # TODO restore intrinsic configuration, usually using:
103  # v = instance_settings.value(k)
104  pass
105 
106  def _on_white_changed(self,value):
107  self._WhiteLightPublisher.publish(value)
108 
109  def _on_blue_changed(self,value):
110  self._BlueLightPublisher.publish(value)
111 
113  self._CalibFlipperPublisher.publish(Empty())
114 
116  self._FlipperCommandPublisher.publish(0.5)
117  self.flipper_state += 0.05
118 
120  self._FlipperCommandPublisher.publish(0)
121  self.flipper_state = 0
122 
124  self._FlipperCommandPublisher.publish(-0.2)
125  self.flipper_state -= 0.05
126 
128  if (self.supply == -1) :
129  self._widget.supply_lineEdit.setText('unknown')
130  else:
131  self._widget.supply_lineEdit.setText(str(self.supply))
132 
133  def compute_coordinates(self, angle):
134 
135  length = 100
136  y = length * math.cos(-angle)
137  x = length * math.sin(-angle)
138  return {'y':y, 'x':x }
139 
140  def draw_flipper_max(self):
141  coords = self.compute_coordinates(self.flipper_front_max_state)
142 
143  self.flipper_scene.addLine(200, 50+coords['x'], 300, 50+coords['x'])
144 
145  def draw_flipper_min(self):
146  coords = self.compute_coordinates(self.flipper_front_min_state)
147 
148  self.flipper_scene.addLine(200, 100+coords['x'], 300, 100+coords['x'])
149 
151  length = 100
152 
153  self._widget.flipper_front_current.setText(str(self.flipper_state))
154 
155  #self.flipper_state += self.flipper_diff;
156 
157  if(self.flipper_state > self.flipper_front_max_state):
158  self.flipper_diff *= -1
159  # self.flipper_state = self.flipper_front_max_state
160 
161  if(self.flipper_state < self.flipper_front_min_state):
162  self.flipper_diff *= -1
163  # self.flipper_state = self.flipper_front_min_state
164 
165  self.flipper_scene.clear()
166 
167  self.draw_flipper_max()
168  self.draw_flipper_min()
169 
170  #red chassi
171  self.flipper_scene.addRect(65, 60, 145, 25, QPen(), QBrush(QColor(255,0,0)))
172 
173  #right wheel
174  self.flipper_scene.addEllipse(150, 50, 50, 50, QPen(), QBrush(QColor(0,0,0)))
175 
176  #left wheel
177  self.flipper_scene.addEllipse(50, 50, 50, 50, QPen(), QBrush(QColor(0,0,0)))
178 
179  #flipper wheel
180  transform = QTransform()
181  transform.translate(155,70)
182  transform.rotateRadians(-self.flipper_state)
183  transform.translate(-25,-25)
184 
185  #flipper wheel connection
186  flipper_wheel = QtGui.QGraphicsEllipseItem(120,5, 50, 50)
187  flipper_wheel.setTransform(transform)
188  flipper_wheel.setBrush( QBrush(QColor(0,0,0)))
189 
190  self.flipper_scene.addItem(flipper_wheel)
191  #self.flipper_scene.addEllipse(250, 50, 50, 50, QPen(), QBrush(QColor(0,0,0)))
192 
193 
194  transform = QTransform()
195  transform.translate(160,75)
196  transform.rotateRadians(-self.flipper_state)
197  transform.translate(-10,-10)
198  flipper_link = QtGui.QGraphicsRectItem(0,0, 135, 20)
199  flipper_link.setBrush( QBrush(QColor(163,163,163)))
200 
201  flipper_link.setTransform(transform)
202 
203  self.flipper_scene.addItem(flipper_link)
204 
205  #front connection
206  self.flipper_scene.addEllipse(155, 70, 10, 10, QPen(), QBrush(QColor(0,0,0)))
207 
208  transform = QTransform()
209  transform.translate(160,75)
210  transform.rotateRadians(-self.flipper_state)
211  transform.translate(-10,-10)
212 
213  #flipper wheel connection
214  flipper_wheel_connection = QtGui.QGraphicsEllipseItem(120,5, 10, 10)
215  flipper_wheel_connection.setTransform(transform)
216  flipper_wheel_connection.setBrush( QBrush(QColor(0,0,0)))
217 
218  self.flipper_scene.addItem(flipper_wheel_connection)
219 
220 # self.flipper_scene.addEllipse(270, 70, 10, 10, QPen(), QBrush(QColor(255,255,255)))
221 
def _on_white_changed(self, value)
def _on_tracker_supply(self, message)
def save_settings(self, plugin_settings, instance_settings)
def _on_calib_flipper_button_pressed(self)
def _flipper_center_button_pressed(self)
def _on_flipper_cmd(self, message)
def __init__(self, context)
def restore_settings(self, plugin_settings, instance_settings)
def compute_coordinates(self, angle)
def _on_flipper_state(self, message)
def _on_blue_changed(self, value)


tracker_base_ui
Author(s):
autogenerated on Mon Jun 10 2019 13:36:44