-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempSensor.py
More file actions
454 lines (359 loc) · 16.8 KB
/
Copy pathTempSensor.py
File metadata and controls
454 lines (359 loc) · 16.8 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 5 15:49:42 2019
@author: Benjamin Smith
"""
import sys
from PyQt5 import QtGui, uic
from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot, QTimer
from PyQt5.QtWidgets import QVBoxLayout, QApplication, QMainWindow, \
QMessageBox, QTableWidgetItem, QFileDialog
from PyQt5.QtGui import QColor
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import serial
import csv
import datetime as dt
import time
class Collector(QObject):
'''
Collector that is responsible for reading and signalling the temperature
data from the Arduino serial port. This object is instantiated and placed
on a designated thread so as to not block the GUI loop.
'''
measured = pyqtSignal(list) #maybe a python array
def __init__(self, running, port, parent=None):
super(Collector, self).__init__()
self.running = running
self.BAUD = 9600 # serial port parameters
self.BITLENGTH = 8
self.ser = serial.Serial(port, self.BAUD, self.BITLENGTH) # open serial
def read_serial(self):
'''Parse serial text. A serial line has the format `#ch_num: xx.xx`;
for example: `#2: 23.64`.'''
line = self.ser.readline().decode("utf-8")
chan = float(line.split(':')[0][1])
temp = float(line.split(' ')[1].split('\r')[0])
if temp == 512.50:
temp = np.nan
return chan, temp
@pyqtSlot()
def read(self):
''' While loop to continually read from serial. Sends a signal
`measured` with the data list to the main GUI loop.
'''
while self.running:
ch, t = self.read_serial()
if ch == 1:
time = dt.datetime.now()
temps = []
chans = []
temps.append(t)
chans.append(ch)
for i in range(7):
ch, t = self.read_serial()
temps.append(t)
chans.append(ch)
data = [time] + temps
self.measured.emit(data)
else:
continue
class Channel(object):
'''
Container class to hold channel information:
- Number
- Name
- QTableWidget header item object
- QLineEdit box for channel names
- RGB color for channel name text and plotted points/lines
'''
def __init__(self, number, name, table, nameBox, rgb):
self.num = number
self.name = name
self.color = rgb
# self.column = table.column(self.num)
self.col_header = table.horizontalHeaderItem(self.num)
self.col_header.setText(name)
self.nameBox = nameBox
self.nameBox.setText(name)
self.nameBox.setStyleSheet('color: ' + self.color + ';')
class TempSensorWindow(QMainWindow):
'''Main GUI class'''
def __init__(self):
super(TempSensorWindow, self).__init__()
self.initUI()
self.allData = []
self.begtime = None
self.chan_fname = './channel_names.csv'
self.csv_filter = 'Comma-separated values (*.csv)'
def initUI(self):
'''Configures the UI and connects the signals with the slot functions.'''
uic.loadUi('TempSensorUI.ui', self) # Load the .ui file
# Add the main matplotlib plot
self.fig1 = plt.Figure()
self.canvas1 = FigureCanvas(self.fig1)
self.plot_layout1 = QVBoxLayout()
self.plot_layout1.addWidget(self.canvas1)
self.gridLayout.addLayout(self.plot_layout1, 0, 1, 1, 1)
self.ax1 = self.fig1.add_subplot(111)
self.ax1.set_xlabel('Time')
self.ax1.set_ylabel('Temperature [$^\\circ$C]')
self.toolbar1 = NavigationToolbar(self.canvas1, self)
self.toolbar1.setFixedHeight(24)
self.plot_layout1.addWidget(self.toolbar1)
self.setWindowTitle('Temperature Array Logger')
self.statusbar = self.statusBar()
self.statusBar().showMessage('Connect to a port...')
self.connectButton.setStyleSheet("background-color: green")
# Configure the list of channels with their properties
self.channels = [Channel(1, 'Ch. 1', self.dataTable, self.ch1_name, 'rgb(31,119,180)'),
Channel(2, 'Ch. 2', self.dataTable, self.ch2_name, 'rgb(255,127,14)'),
Channel(3, 'Ch. 3', self.dataTable, self.ch3_name, 'rgb(44,160,44)'),
Channel(4, 'Ch. 4', self.dataTable, self.ch4_name, 'rgb(214,39,40)'),
Channel(5, 'Ch. 5', self.dataTable, self.ch5_name, 'rgb(148,103,189)'),
Channel(6, 'Ch. 6', self.dataTable, self.ch6_name, 'rgb(140,86,75)'),
Channel(7, 'Ch. 7', self.dataTable, self.ch7_name, 'rgb(227,119,194)'),
Channel(8, 'Ch. 8', self.dataTable, self.ch8_name, 'rgb(127,127,127)')]
self.startButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.resetButton.setEnabled(False)
self.durationLineEdit.setReadOnly(True)
self.connectButton.clicked.connect(self.startThread)
self.startButton.clicked.connect(self.startCollection)
self.stopButton.clicked.connect(self.stopCollection)
self.resetButton.clicked.connect(self.clearData)
self.saveAllData.clicked.connect(self.saveData)
self.saveChannels.clicked.connect(self.saveChannelNames)
self.loadChannels.clicked.connect(self.loadChannelNames)
self.ch1_name.textChanged.connect(lambda text: self.editChannelName(text, 1))
self.ch2_name.textChanged.connect(lambda text: self.editChannelName(text, 2))
self.ch3_name.textChanged.connect(lambda text: self.editChannelName(text, 3))
self.ch4_name.textChanged.connect(lambda text: self.editChannelName(text, 4))
self.ch5_name.textChanged.connect(lambda text: self.editChannelName(text, 5))
self.ch6_name.textChanged.connect(lambda text: self.editChannelName(text, 6))
self.ch7_name.textChanged.connect(lambda text: self.editChannelName(text, 7))
self.ch8_name.textChanged.connect(lambda text: self.editChannelName(text, 8))
self.show()
def editChannelName(self, text, ch_num):
'''Modifies the corresponding table header when the channel name
is changed.
'''
channel = self.channels[ch_num-1]
channel.name = text
channel.col_header.setText(text)
def startThread(self):
'''Instantiates a Collector object and places it on a designated
thread. Does not start collecting data yet.
'''
self.running = False
try:
self.Collector = Collector(self.running, self.portName.text())
# 1 - Create a thread object, no parent.
self.thread = QThread()
# 2 - Connect Worker`s Signals to form method slots to post data.
self.Collector.measured.connect(self.updateData)
self.startButton.clicked.connect(self.Collector.read)
# 3 - Move the Worker object to the Thread object
self.Collector.moveToThread(self.thread)
# 4 - Connect Thread started signal to Worker operational slot method
self.thread.started.connect(self.Collector.read)
# 5 - Start the thread
self.thread.start()
self.startButton.setEnabled(True)
self.resetButton.setEnabled(False)
self.statusBar().showMessage('Connected.')
self.connectButton.setStyleSheet("")
self.startButton.setStyleSheet("background-color: green")
except:
print('Bad Port')
self.portErrorMessage()
def updateData(self, data):
'''Recieves the signal from the Collector object when the data is read.
'''
if self.running:
self.statusBar().showMessage('Collecting...')
self.allData.append(data)
self.row_ind = len(self.allData)-1 # The number of rows [zero-indexed]
self.dataTable.insertRow(self.row_ind)
active = np.invert(np.isnan(np.array(data[1:]))) # which channels are active
# For the given row, set the time:
self.dataTable.setItem(self.row_ind,0, QTableWidgetItem(str(data[0].time()).split('.')[0]))
for i, chan in enumerate(self.channels):
chan.nameBox.setEnabled(active[i])
val = str(data[i+1])
if val != 'nan':
# For the given row, set each channel's temperature values:
self.dataTable.setItem(self.row_ind, i+1, QTableWidgetItem(val))
self.drawPlot()
self.duration = dt.datetime.now() - self.begtime
self.timerClock()
# Save all data to a backup file, in case of program crashes
with open('backup_data.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
time.sleep(0.5)
self.statusBar().showMessage('Collecting')
def timerClock(self):
'''Updates the 'Running Duration' time box.'''
allseconds = self.duration.seconds
allminutes = int(self.duration.seconds / 60)
seconds = int(allseconds - allminutes*60)
hours = int(allseconds / 3600)
minutes = int(allminutes - hours*60)
days = int(self.duration.days)
ssec = self.stringTime(seconds)
smin = self.stringTime(minutes)
shour = self.stringTime(hours)
sday = self.stringTime(days)
duration_string = f'{sday}:{shour}:{smin}:{ssec}'
self.durationLineEdit.setText(duration_string)
def stringTime(self, time):
'''Formats the time properly to always have two digits.
Returns time as a string.
'''
if time == 0:
stime = '00'
elif time < 10:
stime = '0' + str(time)
else:
stime = str(time)
return stime
def drawPlot(self):
'''Updates the matplotlib plot'''
data = np.array(self.allData)
times = data[:,0]
temps = data[:, 1:]
self.ax1.clear()
self.ax1.plot(times, temps, marker='o', lw=1, markersize=4)
self.ax1.set_xlabel('Time')
self.ax1.set_ylabel('Temperature [$^\\circ$C]')
self.canvas1.draw()
def saveChannelNames(self):
'''Saves the channel names as they currently are to a .csv file.'''
title = 'Save Channel File'
fname, selectedFilter = QFileDialog.getSaveFileName(self, title, self.chan_fname, self.csv_filter)
if fname is not None:
channel_names = [ch.name for ch in self.channels]
with open(fname, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(channel_names)
def loadChannelNames(self):
'''Loads channel names from a .csv file.'''
title = 'Open Channel File'
fname, selectedFilter = QFileDialog.getOpenFileName(self, title, self.chan_fname, self.csv_filter)
with open(fname, 'r', newline='') as f:
reader = csv.reader(f)
loaded_names = next(reader)
if len(loaded_names) == 8:
for i, chan in enumerate(self.channels): # Updates channel name boxes
chan.name = loaded_names[i]
chan.nameBox.setText(chan.name)
def startCollection(self):
'''Slot function for the `Start` button'''
print('Reading...')
if self.begtime == None:
self.begtime = dt.datetime.now()
self.running = True
self.Collector.running = self.running # set the Collector running
self.Collector.ser.reset_input_buffer()
self.statusBar().showMessage('Collecting')
self.startButton.setStyleSheet("")
self.startButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.stopButton.setStyleSheet("background-color: red")
self.resetButton.setEnabled(False)
# Write header to backup file
channel_header = [ch.name for ch in self.channels]
all_headers = ['Time'] + channel_header
with open('backup_data.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(all_headers)
def stopCollection(self):
'''Slot function for the `Stop` button'''
print('Stopped')
self.running = False
self.Collector.running = self.running # stop the Collector
self.statusBar().showMessage('Stopped')
self.stopButton.setStyleSheet("")
self.startButton.setStyleSheet("background-color: green")
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
self.resetButton.setEnabled(True)
def clearData(self):
'''Slot function for the `Reset` button'''
retval = self.clearConfirmation() # Check if user want to clear data
if retval == QMessageBox.Yes:
print('Reset')
self.running = False
self.Collector.running = self.running # stop the Collector
self.allData = [] # clear the data
self.ax1.clear() # clear the graph
self.ax1.set_xlabel('Time')
self.ax1.set_ylabel('Temperature [$^\\circ$C]')
self.canvas1.draw()
self.statusBar().showMessage('Ready')
self.duration = dt.timedelta()
self.timerClock()
# Remove the rows from the table
for i in range(self.row_ind, 0, -1):
self.dataTable.removeRow(i)
self.dataTable.setRowCount(0)
# Clear the backup file
with open("backup_data.csv", "w") as f:
f.truncate()
self.resetButton.setEnabled(False)
self.begtime = None
def saveData(self):
'''Function to save the data in a .csv file in the directory of choice'''
title = 'Save File'
fname, selectedFilter = QFileDialog.getSaveFileName(self, title, '.', self.csv_filter)
if fname is not None:
channel_header = [ch.name for ch in self.channels]
all_headers = ['Time'] + channel_header
with open(fname, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(all_headers) # Write channel headers
writer.writerows(self.allData) # Write data rows
def portErrorMessage(self):
'''Dialog box to indicate there is a problem connecting to the serial
port.
'''
message = "There is an issue with the serial port you selected."
label= "\"Houston, we have a problem.\""
badPort = QMessageBox()
badPort.setIcon(QMessageBox.Critical)
badPort.setWindowTitle(label)
badPort.setText(message)
badPort.setStandardButtons(QMessageBox.Ok)
badPort.exec_()
return
def clearConfirmation(self):
'''Dialog box to verify clearing the data.
'''
question = "Are you sure you want to clear the data?"
label= "Clearing"
Confirmation = QMessageBox()
Confirmation.setIcon(QMessageBox.Warning)
Confirmation.setWindowTitle(label)
Confirmation.setText(question)
Confirmation.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
Confirmation.setDefaultButton(QMessageBox.No)
retval = Confirmation.exec_()
return retval
if __name__ == '__main__':
if QApplication.instance(): # Checks if app is already created
app = QApplication.instance()
else:
app = QApplication(sys.argv)
window = TempSensorWindow()
this = app.exec_()
try:
window.Collector.running = False
window.Collector.ser.close()
finally:
sys.exit(this)