/
sensorTestGUI.py
475 lines (354 loc) · 13.5 KB
/
sensorTestGUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# IMPORTS
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QProgressBar, QLabel, QFrame
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtChart import QChart, QChartView, QValueAxis, QBarCategoryAxis, QBarSet, QBarSeries
from PyQt5.QtGui import QPainter, QFont
import sys
from datetime import datetime
import serial
class SENSOR_TEST(QWidget):
bars = None
series = None
chart = None
labelWidgets = []
comms = None
commsStatus = False
deviceID = "HARP"
def __init__(self):
QWidget.__init__(self)
# ADD GUI WIDGETS
self.setupLayout()
# LAUNCH GUI
self.show()
self.resize(800,600)
def setupLayout(self):
parentLayout = QVBoxLayout()
parentLayout.setContentsMargins(20,20,20,20)
buttonLayout = QHBoxLayout()
# CREATE SERIAL COMMUNICATION CONNECT BUTTON
self.connectButton = QPushButton("CONNECT")
self.connectButton.setFixedSize(150, 40)
self.connectButton.setCheckable(True)
self.connectButton.setStyleSheet("QPushButton {background-color: #66BB6A; color: black; border-radius: 20px}"
"QPushButton:hover {background-color: #4CAF50; color: black; border-radius: 20px}"
"QPushButton:checked {background-color: #EF5350; color: white; border-radius: 20px}"
"QPushButton:checked:hover {background-color: #F44336; color: white; border-radius: 20px}")
# CREATE SENSORS CALIBRATION BUTTON
self.calibrateButton = QPushButton("CALIBRATE")
self.calibrateButton.setFixedSize(150, 40)
self.calibrateButton.setStyleSheet("QPushButton {background-color: #E0E0E0; color: black; border-radius: 20px}"
"QPushButton:hover {background-color: #BDBDBD; color: black; border-radius: 20px}"
"QPushButton:pressed {background-color: #D5D5D5; color: black; border-radius: 20px}")
# ADD BUTTONS TO HORIZONTAL LAYOUT
buttonLayout.addWidget(self.connectButton, alignment = Qt.AlignLeft)
buttonLayout.addWidget(self.calibrateButton, alignment = Qt.AlignRight)
# CREATE BAR CHART TO DISPLAY SENSOR READINGS
self.bars = QBarSet('Sensor Readings')
self.bars.append([0 for i in range(5)])
self.chart = QChart()
self.chart.setBackgroundRoundness(20)
self.chart.layout().setContentsMargins(0, 0, 0, 0)
self.series = QBarSeries()
self.series.append(self.bars)
self.chart.addSeries(self.series)
self.chart.setTitle('Sensor Readings')
# CREATE X-AXIS AS SENSOR LABELS
xAxis = QBarCategoryAxis()
labels = ["Sensor {}".format(i+1) for i in range(5)]
xAxis.append(labels)
# CREATE Y-AXIS AND SENSOR READINGS
yAxis = QValueAxis()
yAxis.setRange(-100, 100)
yAxis.setTickCount(11)
yAxis.setTitleText("Pressure (%)")
# ADD AXES TO CHART
self.chart.addAxis(xAxis, Qt.AlignBottom)
self.chart.addAxis(yAxis, Qt.AlignLeft)
self.chart.legend().setVisible(False)
# ATTACH AXES TO SERIES
self.series.attachAxis(xAxis)
self.series.attachAxis(yAxis)
# ADD CHART TO A VIEW
chartView = QChartView(self.chart)
labelLayout = QFrame()
labelLayout.setStyleSheet("background-color: white; border-radius: 20px")
layout1 = QVBoxLayout()
labelLayout.setLayout(layout1)
layout2 = QHBoxLayout()
layout3 = QHBoxLayout()
for i in range(5):
label = QLabel("Sensor {}".format(i+1))
label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
label.setStyleSheet("font-weight: bold;")
layout2.addWidget(label)
value = QLabel("0 mV")
value.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.labelWidgets.append(value)
layout3.addWidget(value)
layout1.addLayout(layout2, 1)
layout1.addLayout(layout3, 1)
parentLayout.addLayout(buttonLayout)
parentLayout.addWidget(chartView, 5)
parentLayout.addWidget(labelLayout, 1)
# LINK WIDGETS
self.connectButton.clicked.connect(self.serialToggle)
self.calibrateButton.clicked.connect(self.sensorCalibrate)
self.setLayout(parentLayout)
def getSensorReadings(self):
"""
PURPOSE
Requests sensor readings from ROV and updates GUI.
INPUT
NONE
RETURNS
NONE
"""
# SENSOR POLLING RATE (HZ)
refreshRate = 100
# START QTIMER TO REPEATEDLY UPDATE SENSORS AT THE DESIRED POLLING RATE
self.timer = QTimer()
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.getSensorReadings)
self.timer.start(int(1000*1/refreshRate))
# STOP REQUESTING SENSOR VALUES IF ROV IS DISCONNECTED
if self.commsStatus == False:
self.timer.stop()
else:
# REQEST SINGLE READING
sensorReadings = self.getSensors()
# UPDATE VOLTAGE READINGS
self.updateVoltageReadings(sensorReadings)
# SCALE SENSOR READINGS
scaledReadings = self.mapPressureReadings(sensorReadings)
try:
# UPDATE GUI
self.series.remove(self.bars)
self.bars = QBarSet("")
self.bars.append([float(item) for item in scaledReadings])
self.series.append(self.bars)
except:
self.teensyDisconnect()
def mapPressureReadings(self, values):
mappedValues = []
for value in values:
try:
mappedValues.append((200/1023)*float(value) - 100)
except:
pass
return mappedValues
def updateVoltageReadings(self, values):
voltageValues = [round((3300/1023)*float(i)) for i in values]
for i, label in enumerate(self.labelWidgets):
label.setText(str(voltageValues[i]) + " mV")
def serialToggle(self, buttonState):
"""
PURPOSE
Determines whether to connect or disconnect from the ROV serial interface.
INPUT
- buttonState = the state of the button (checked or unchecked).
RETURNS
NONE
"""
# CONNECT
if buttonState:
self.teensyConnect()
# DISCONNECT
else:
self.teensyDisconnect()
def sensorCalibrate(self):
"""
PURPOSE
INPUT
RETURNS
"""
self.calibrateSensors()
########################
### SERIAL FUNCTIONS ###
########################
def teensyConnect(self):
"""
PURPOSE
Attempts to connect to the ROV using the comms library.
Changes the appearance of the connect buttons.
If connection is successful, the ROV startup procedure is initiated.
INPUT
NONE
RETURNS
NONE
"""
# DISABLE BUTTONS TO AVOID DOUBLE CLICKS
self.connectButton.setEnabled(False)
# FIND ALL AVAILABLE COM PORTS
availableComPorts, comPort, identity = self.findComPorts(115200, self.deviceID)
print(availableComPorts, comPort, identity)
# ATTEMPT CONNECTION TO ROV COM PORT
status, message = self.serialConnect(comPort, 115200)
print(status, message)
# IF CONNECTION IS SUCCESSFUL
if status == True:
# MODIFY BUTTON STYLE
self.connectButton.setText('DISCONNECT')
# START READING SENSOR VALUES
self.getSensorReadings()
# IF CONNECTION IS UNSUCCESSFUL
else:
self.teensyDisconnect()
# RE-ENABLE CONNECT BUTTONS
self.connectButton.setEnabled(True)
def teensyDisconnect(self):
"""
PURPOSE
Disconnects from the ROV using the comms library.
Changes the appearance of the connect buttons
INPUT
NONE
RETURNS
NONE
"""
# MODIFY BUTTON STYLE
self.connectButton.setText('CONNECT')
self.connectButton.setChecked(False)
# CLOSE COM PORT
if self.commsStatus:
self.comms.close()
self.commsStatus = False
def findComPorts(self, baudRate, identity):
"""
PURPOSE
Find all available COM ports and requests the devices identity.
INPUT
- menuObject = pointer to the drop down menu to display the available COM ports.
- baudRate = baud rate of the serial interface.
- identity = string containing the required device identity to connect to.
RETURNS
- availableComPorts = list of all the available COM ports.
- rovComPort = the COM port that belongs to the device.
- identity = the devices response from an identity request.
"""
# CREATE LIST OF ALL POSSIBLE COM PORTS
ports = ['COM%s' % (i + 1) for i in range(256)]
deviceIdentity = ""
comPort = None
availableComPorts = []
# CHECK WHICH COM PORTS ARE AVAILABLE
for port in ports:
try:
comms = serial.Serial(port, baudRate, timeout = 1)
availableComPorts.append(port)
# REQUEST IDENTITY FROM COM PORT
self.commsStatus = True
deviceIdentity = self.getIdentity(comms, identity)
comms.close()
self.commsStatus = False
# FIND WHICH COM PORT IS THE ROV
if deviceIdentity == identity:
comPort = port
break
# SKIP COM PORT IF UNAVAILABLE
except (OSError, serial.SerialException):
pass
return availableComPorts, comPort, deviceIdentity
def getIdentity(self, serialInterface, identity):
"""
PURPOSE
Request identity from a defined COM port.
INPUT
- serialInterface = pointer to the serial interface object.
- identity = the desired identity response from the device connected to the COM port.
RETURNS
- identity = the devices response.
"""
identity = ""
startTime = datetime.now()
elapsedTime = 0
# REPEATIDELY REQUEST IDENTIFICATION FROM DEVICE FOR UP TO 3 SECONDS
while (identity == "") and (elapsedTime < 3):
self.serialSend("?I", serialInterface)
identity = self.serialReceive(serialInterface)
elapsedTime = (datetime.now() - startTime).total_seconds()
return identity
def serialConnect(self, comPort, baudRate):
"""
PURPOSE
Attempts to initialise a serial communication interface with a desired COM port.
INPUT
- rovComPort = the COM port of the ROV.
- baudRate = the baud rate of the serial interface.
RETURNS
NONE
"""
self.commsStatus = False
if comPort != None:
try:
self.comms = serial.Serial(comPort, baudRate, timeout = 1)
message = "Connection to ROV successful."
self.commsStatus = True
except:
message = "Failed to connect to {}.".format(comPort)
else:
message = "Failed to recognise device identity."
return self.commsStatus, message
def serialSend(self, command, serialInterface):
"""
PURPOSE
Sends a string down the serial interface to the ROV.
INPUT
- command = the command to send.
- serialInterface = pointer to the serial interface object.
RETURNS
NONE
"""
if self.commsStatus:
try:
serialInterface.write((command + '\n').encode('ascii'))
except:
self.teensyDisconnect()
print("Failed to send command.")
def serialReceive(self, serialInterface):
"""
PURPOSE
Waits for data until a newline character is received.
INPUT
- serialInterface = pointer to the serial interface object.
RETURNS
NONE
"""
received = ""
try:
received = serialInterface.readline().decode('ascii').strip()
except:
self.teensyDisconnect()
print("Failed to receive data.")
return(received)
def getSensors(self):
"""
PURPOSE
Send request to device to return sensor readings.
INPUT
NONE
RETURNS
- results = array containing the sensor readings.
"""
results = []
# REQUEST SENSOR READINGS
command = "?S"
self.serialSend(command, self.comms)
# READ RESPONSE INTO AN ARRAY
results = self.serialReceive(self.comms).split(",")
print(results)
return results
def calibrateSensors(self):
"""
"""
# REQUEST SENSOR READINGS
command = "?C"
self.serialSend(command, self.comms)
def guiInitiate():
app = QApplication(sys.argv)
app.setStyle("Fusion")
app.setFont(QFont("Bahnschrift Light", 10))
program = SENSOR_TEST()
program.setWindowTitle("Sensor Testing Program")
app.exec()
if __name__ == '__main__':
guiInitiate()