-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
397 lines (348 loc) · 17.7 KB
/
main.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
from PyQt5 import QtWidgets, QtCore, QtGui
from accessory_widgets.bootloader.bootloader import Bootloader
from accessory_widgets.frame_viewer import FrameViewer
from accessory_widgets.car_viewer import CarViewer
from accessory_widgets.cell_viewer import CellViewer
from accessory_widgets.charge_viewer import ChargeViewer
from accessory_widgets.faultViewer import FaultViewer
from accessory_widgets.log_export import LogExporter
from accessory_widgets.log_import import LogImporter
from accessory_widgets.motor_viewer import MotorViewer
from accessory_widgets.preferences_editor import PreferencesEditor
from accessory_widgets.variable_editor import VariableEditor
from accessory_widgets.file_viewer import FileViewer
from accessory_widgets.sensor_viewer import SensorViewer
from accessory_widgets.system_monitor import SystemMonitor
from display_widgets.plot_widget import PlotWidget
from display_widgets import plot_widget
from communication.can_bus import CanBus
from communication.daq_protocol import DaqProtocol
from display_widgets.lcd_widget import LcdDisplay
from display_widgets.widget_deleter import WidgetDeleter
from ui.mainWindow import Ui_MainWindow
import webbrowser
import qdarkstyle
import utils
import json
import time
import sys
import os
# File location changes when converted to exe
if getattr(sys, 'frozen', False):
CurrentPath = sys._MEIPASS
else:
CurrentPath = os.path.dirname(__file__)
CONFIG_FILE_PATH = os.path.join(os.getcwd(), "dashboard.json")
class Main(QtWidgets.QMainWindow):
def __init__(self, config):
super(Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
print(sys.platform)
# Load Configurations (dictionaries)
firmware_base = config['firmware_path']
self.daq_config = utils.load_json_config(os.path.join(firmware_base, 'common/daq/daq_config.json'), os.path.join(firmware_base, 'common/daq/daq_schema.json'))
self.can_config = utils.load_json_config(os.path.join(firmware_base, 'common/daq/can_config.json'), os.path.join(firmware_base, 'common/daq/can_schema.json'))
self.fault_config = utils.load_json_config(os.path.join(firmware_base, 'common/faults/fault_config.json'), os.path.join(firmware_base, 'common/faults/fault_schema.json'))
self.fault_config = DaqProtocol.create_ids(self, self.fault_config)
# Can Bus Initialization
self.can_bus = CanBus(os.path.join(firmware_base, 'common/daq/per_dbc.dbc'), config['default_ip'], self.can_config)
self.can_bus.connect_sig.connect(self.updateConnectionStatus)
self.can_bus.write_sig.connect(self.updateWriteConnectionStatus)
self.can_bus.bus_load_sig.connect(self.updateBusLoad)
self.daq_protocol = DaqProtocol(self.can_bus, self.daq_config)
# Status Bar
self.ui.comlbl = QtWidgets.QLabel()
self.ui.loadlbl = QtWidgets.QLabel()
self.ui.loadlbl.setStyleSheet("font:16px;")
self.ui.eventTextEdit = QtWidgets.QLineEdit(text='Event')
self.ui.eventButton = QtWidgets.QPushButton('Log Event')
self.ui.loginButton = QtWidgets.QPushButton('Write to Car')
self.ui.logoutButton = QtWidgets.QPushButton('Stop Writing to Car')
self.ui.logEnableBtn = QtWidgets.QPushButton('Start Logging')
self.ui.logDisableBtn = QtWidgets.QPushButton('Stop Logging')
self.ui.eventButton.setStyleSheet("border-color: black; border-style: outset; border-width: 2px;")
self.ui.writelbl = QtWidgets.QLabel()
self.ui.statusbar.addWidget(self.ui.comlbl)
self.ui.statusbar.addWidget(self.ui.loadlbl)
self.ui.statusbar.addWidget(self.ui.eventTextEdit)
self.ui.statusbar.addWidget(self.ui.eventButton)
self.ui.statusbar.addWidget(self.ui.loginButton)
self.ui.statusbar.addWidget(self.ui.logoutButton)
self.ui.statusbar.addWidget(self.ui.writelbl)
self.ui.statusbar.addWidget(self.ui.logEnableBtn)
self.ui.statusbar.addWidget(self.ui.logDisableBtn)
self.ui.eventButton.clicked.connect(self.logEvent)
self.ui.loginButton.clicked.connect(self.loginEvent)
self.ui.logoutButton.clicked.connect(self.logoutEvent)
self.ui.logEnableBtn.clicked.connect(self.logEvent)
self.ui.logDisableBtn.clicked.connect(self.logDisableEvent)
# Menu Bar Tools
self.ui.play_icon = self.style().standardIcon(getattr(QtWidgets.QStyle, 'SP_MediaPlay'))
self.ui.pause_icon = self.style().standardIcon(getattr(QtWidgets.QStyle, 'SP_MediaPause'))
self.ui.clear_icon = self.style().standardIcon(getattr(QtWidgets.QStyle, 'SP_DialogResetButton'))
self.ui.reconnect_icon = self.style().standardIcon(getattr(QtWidgets.QStyle, 'SP_BrowserReload'))
self.ui.actionPlayPause = QtWidgets.QAction(self.ui.play_icon, "play_pause", self.ui.menubar)
self.ui.actionClear = QtWidgets.QAction(self.ui.clear_icon, "clear", self.ui.menubar)
self.ui.actionReconnect = QtWidgets.QAction(self.ui.reconnect_icon, "reconnect", self.ui.menubar)
self.recording = False
self.ui.menubar.addAction(self.ui.actionPlayPause)
self.ui.menubar.addAction(self.ui.actionClear)
self.ui.menubar.addAction(self.ui.actionReconnect)
# Accessory Layout
self.ui.accessoryLayout = QtWidgets.QVBoxLayout()
self.ui.accessoryLayoutWidget = QtWidgets.QWidget()
self.ui.accessoryLayoutWidget.setLayout(self.ui.accessoryLayout)
self.ui.varEdit = VariableEditor(self.daq_protocol)
self.ui.fileViewer = FileViewer()
self.ui.frameViewer = FrameViewer(self.can_bus, self.ui.centralwidget)
self.ui.accessoryLayout.addWidget(self.ui.frameViewer)
self.ui.cellViewer = CellViewer(self.can_bus)
self.ui.logExporter = LogExporter(self.can_bus)
self.ui.bootloader = Bootloader(self.can_bus, config['firmware_path'])
self.ui.chargeViewer = ChargeViewer(self.can_bus)
self.ui.faultViewer = FaultViewer(self.can_bus, self.fault_config, self.daq_protocol)
self.ui.sensorViewer = SensorViewer(self.can_bus)
self.ui.systemMonitor = SystemMonitor(self.can_bus)
self.ui.motorViewer = MotorViewer(self.can_bus)
self.ui.carViewer = CarViewer(self.can_bus)
# Dashboard Layout
self.ui.dashboardLayout = QtWidgets.QGridLayout()
self.ui.dashboardLayoutWidget = QtWidgets.QWidget()
self.ui.dashboardLayoutWidget.setLayout(self.ui.dashboardLayout)
# Horizontally Split Accessory and Dashboard Layouts
self.ui.splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
self.ui.splitter.addWidget(self.ui.accessoryLayoutWidget)
self.ui.splitter.addWidget(self.ui.dashboardLayoutWidget)
self.ui.horizontalLayout.addWidget(self.ui.splitter)
# Dashboard grid tracking
self.ui.display_widgets = []
self.curr_loc = [0, 0]
self.max_rows = 1
self.max_cols = 1
# Menu Action Connections
self.ui.actionImport_log.triggered.connect(lambda : LogImporter.importLog(self.can_bus, self))
self.ui.actionVariable_Editor.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.varEdit))
self.ui.actionFile_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.fileViewer))
self.ui.actionFrame_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.frameViewer))
self.ui.actionCell_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.cellViewer))
self.ui.actionLog_Exporter.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.logExporter))
self.ui.actionBootloader.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.bootloader))
self.ui.actionCharge_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.chargeViewer))
self.ui.actionSensor_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.sensorViewer))
self.ui.actionSystem_Monitor.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.systemMonitor))
self.ui.actionFault_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.faultViewer))
self.ui.actionMotor_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.motorViewer))
self.ui.actionCar_Viewer.triggered.connect(lambda _visible: self.viewWidget(_visible, self.ui.carViewer))
self.ui.actionLCD.triggered.connect(self.newLCD)
self.ui.actionPlot.triggered.connect(self.newPlot)
self.ui.actionRemoveWidget.triggered.connect(self.removeDisplayWidget)
self.ui.actionLoad_View.triggered.connect(self.loadView)
self.ui.actionSave_View.triggered.connect(self.saveView)
self.ui.actionPreferences.triggered.connect(lambda : PreferencesEditor.editPreferences(self.ui.varEdit, parent=self))
self.ui.actionPlayPause.triggered.connect(self.playPause)
self.ui.actionClear.triggered.connect(self.clearData)
self.ui.actionReconnect.triggered.connect(self.can_bus.reconnect)
self.ui.actionAbout.triggered.connect(lambda: webbrowser.open(
'https://wiki.itap.purdue.edu/display/PER22/Data+Acquisition'))
self.updateWriteConnectionStatus(0)
self.can_bus.connect()
self.can_bus.start()
# self.can_bus.reconnect()
self.show()
def updateConnectionStatus(self, connected: bool):
""" Updates the connection status label when connect status changes """
if connected:
self.ui.comlbl.setStyleSheet("color: green; font: 18px bold;")
self.ui.comlbl.setText("Connected")
else:
self.ui.comlbl.setStyleSheet("color: red; font: 18px bold;")
self.ui.comlbl.setText("Disconnected")
self.ui.varEdit.setDisabled(not connected)
self.ui.frameViewer.setDisabled(not connected)
def updateWriteConnectionStatus(self, tcpconnected: int):
""" Updates the connection status label when connect status changes """
if tcpconnected == 2:
self.ui.writelbl.setStyleSheet("color: green; font: 18px bold;")
self.ui.writelbl.setText("Connected")
elif tcpconnected == 1:
self.ui.writelbl.setStyleSheet("color: yellow; font: 18px bold;")
self.ui.writelbl.setText("Connecting...")
elif tcpconnected == 0:
self.ui.writelbl.setStyleSheet("color: red; font: 18px bold;")
self.ui.writelbl.setText("Disconnected")
def updateBusLoad(self, load: float):
""" Updates the bus load label """
self.ui.loadlbl.setText(f"Bus Load: {load:.2f}%")
def viewWidget(self, is_visible: bool, widget):
""" Hides or shows a widget """
if is_visible:
self.ui.accessoryLayout.addWidget(widget)
widget.show()
else:
self.ui.accessoryLayout.removeWidget(widget)
widget.hide()
def newLCD(self):
""" Adds a new LCD dashboard widget """
self.ui.display_widgets.append(LcdDisplay(utils.signals, self))
self.addDashWidget(self.ui.display_widgets[-1])
def newPlot(self):
""" Adds a new Plot dashboard widget """
self.ui.display_widgets.append(PlotWidget(utils.signals, self))
self.addDashWidget(self.ui.display_widgets[-1])
def addDashWidget(self, widget):
""" Adds a widget to the dashboard and calcualtes the next grid location """
self.ui.dashboardLayout.addWidget(widget, *self.curr_loc)
# add new column, then new row until a square is reached
if self.curr_loc[0] == self.curr_loc[1] == (self.max_rows - 1) == (self.max_cols - 1):
self.max_cols += 1
self.curr_loc[0] = 0
if self.curr_loc[1] == (self.max_cols - 1):
self.curr_loc[0] += 1
if self.curr_loc[0] > (self.max_rows - 1):
self.curr_loc[1] = 0
self.max_rows += 1
else:
self.curr_loc[1] += 1
def removeDisplayWidget(self):
""" removes a display widget and resets the grid locations """
for w in self.ui.display_widgets:
self.ui.dashboardLayout.removeWidget(w)
widgets_to_delete = WidgetDeleter.getWidgets(self.ui.display_widgets)
for w in widgets_to_delete:
w.destroy()
self.ui.display_widgets.remove(w)
# Reset axis link (in case the plot that all the
# axes were linked to was deleted)
plot_widget.view_box_link = None
for w in self.ui.display_widgets:
w.signalsChanged()
# redo grid layout
self.curr_loc = [0, 0]
self.max_rows = 1
self.max_cols = 1
for w in self.ui.display_widgets:
self.addDashWidget(w)
def loadView(self):
""" Loads a widget display from a file, clears the current view"""
# Load file
file_loc, _ = QtWidgets.QFileDialog.getOpenFileName(self, filter="*.daqview")
# Check no file
if not file_loc: return
config = None
with open(file_loc, 'r') as f:
config = f.read()
if not config:
utils.log_error(f"Invalid view file: {file_loc}")
return
# Parse
new_display_widgets = []
widgets = config.split(';')
for w in widgets[:-1]:
name, sigs = w.split(':')
sig_def = json.loads(sigs)
chosen_sigs = []
for sig in sig_def[0]:
if sig[0] != utils.b_str:
utils.log_warning(f"Adding signal on bus {sig[0]}, but currently on bus {utils.b_str}")
try:
chosen_sigs.append(utils.signals[sig[0]][sig[1]][sig[2]][sig[3]])
# set color
chosen_sigs[-1].color = QtGui.QColor(*sig[4])
except KeyError:
utils.log_error(f"Unrecognized signal key for {sig[3]}")
return
if 'PlotWidget' in name:
new_display_widgets.append(PlotWidget(utils.signals, parent=self, selected_signals=chosen_sigs))
elif 'LcdDisplay' in name:
new_display_widgets.append(LcdDisplay(utils.signals, parent=self, selected_signals=chosen_sigs))
else:
utils.log_error(f"Invalid widget type {name}")
return
# Clear current view
for w in self.ui.display_widgets:
self.ui.dashboardLayout.removeWidget(w)
w.destroy()
self.ui.display_widgets.remove(w)
# Reset axis link (in case the plot that all the
# axes were linked to was deleted)
plot_widget.view_box_link = None
self.curr_loc = [0, 0]
self.max_rows = 1
self.max_cols = 1
# add
for w in new_display_widgets:
self.ui.display_widgets.append(w)
self.addDashWidget(self.ui.display_widgets[-1])
"""
PlotWidget:[(bus, node, message, sig, (r,g,b,a)),
(bus, node, message, sig, (r,g,b,a))];
PlotWidget:[(bus, node, message, sig, (r,g,b,a)),];
"""
def saveView(self):
""" Saves the current widget display"""
output_data = ""
for w in self.ui.display_widgets:
w_str = ""
if type(w) is PlotWidget:
w_str = "PlotWidget:["
elif type(w) is LcdDisplay:
w_str = "LcdDisplay:["
else:
pass
sigs = [[sig.bus_name,sig.node_name, sig.message_name, sig.signal_name, list(sig.color.getRgb())] for sig in w.current_signals]
w_str += str(sigs).replace("'","\"") + "];\n"
output_data += w_str
file_location, _ = QtWidgets.QFileDialog.getSaveFileName(self, filter="*.daqview")
with open(file_location, 'w') as f:
f.write(output_data)
def playPause(self):
""" Start recording signal values """
self.recording = not self.recording
if self.recording:
self.ui.actionPlayPause.setIcon(self.ui.pause_icon)
else:
self.ui.actionPlayPause.setIcon(self.ui.play_icon)
self.can_bus.pause(not self.recording)
def clearData(self):
""" Clears recorded signal values """
utils.clearDictItems(utils.signals)
utils.clearEvents()
self.can_bus.start_time_bus = -1
def logEvent(self):
""" Logs and event, recording the timestamp """
t = time.time() - self.can_bus.start_time_cmp
utils.logEvent(t, self.ui.eventTextEdit.text())
def loginEvent(self):
""" Attempts to connect to TCP server """
self.can_bus.connect_tcp()
def logoutEvent(self):
"""Attempts to disconnect from TCP server"""
self.can_bus.disconnect_tcp()
def logEvent(self):
"""Attempts to start PI Logger"""
self.can_bus.sendLogCmd(True)
def logDisableEvent(self):
"""Attempts to stop the PI logger"""
self.can_bus.sendLogCmd(False)
def closeEvent(self, event):
""" Called on exit of main window """
if self.can_bus.connected:
self.can_bus.connected = False
while(not self.can_bus.isFinished()):
# wait for bus receive to finish
pass
self.can_bus.disconnect_bus()
event.accept()
if __name__ == "__main__":
utils.initGlobals()
config = json.load(open(CONFIG_FILE_PATH))
app = QtWidgets.QApplication(sys.argv)
# Style Configuration
if config['dark_mode']:
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
app.setWindowIcon(QtGui.QIcon(os.path.join(CurrentPath, "ui/logo.png")))
utils.debug_mode = config['debug']
utils.dark_mode = config['dark_mode']
window = Main(config)
app.exec_()