-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_app.py
229 lines (193 loc) · 8.46 KB
/
run_app.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
import sys
# import pyximport; pyximport.install()
from PySide2 import QtWidgets
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from MainWindow import Ui_MainWindow
import traceback, sys
import serial
import time
import sys
import time
import numpy as np
import views
import data
import pandas as pd
import protocols
import measurements
import threading
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.nsamp = 12000
self.nch = 6
self.fs = 100
view_ranges = [1000, 3000, 6000, 12000]
self.nsamp_view = view_ranges[2]
self.data_cntrl = data.DataController(self.nch, self.nsamp, self.fs)
self.central_layout = QVBoxLayout()
self.central_widget = QWidget()
self.plot_widget = views.COPView(self.x, self.data_cntrl.meas_data.y_raw, self.data_cntrl, self.nsamp_view)
self.cop_plot_settings = []
# self.liveplot_view = views.LivePlotView(self.x, self.y)
self.central_layout.addWidget(self.plot_widget)
self.central_widget.setLayout(self.central_layout)
self.setCentralWidget(self.plot_widget)
self.saveAction.triggered.connect(self.save_data_csv)
self.openAction.triggered.connect(self.open_data_csv)
self.btn_start_measure.triggered.connect(self.start_measure)
self.btn_stop_measure.triggered.connect(self.stop_measure)
self.btn_show_liveplot.triggered.connect(lambda: self.set_central_widget(self.btn_show_liveplot))
self.btn_show_copplot.triggered.connect(lambda: self.set_central_widget(self.btn_show_copplot))
self.timer = QTimer()
port = 'COM8'
baudrate = 115200
self.run = False
self.ref_rate = 10
self.meas_cntrl = measurements.MesurementController(self.data_cntrl, port, baudrate=baudrate)
self.update_available_serial_ports()
for i, vrange in enumerate(view_ranges):
nsec = vrange // self.fs
if nsec >= 60:
vrange_format = f'{int(nsec/60)} min'
else:
vrange_format = f'{nsec} sec'
action = QAction(vrange_format)
action.setData(vrange)
action.setCheckable(True)
self.samp_range_view_menu.addAction(action)
self.samp_range_view_group.addAction(action)
if vrange == self.nsamp_view:
action.setChecked(True)
self.samp_range_view_group.triggered.connect(lambda action: self.set_view_range(int(action.data())))
refresh_rates = ['1', '5', '10']
for i, refrate in enumerate(refresh_rates):
action = QAction(refrate+' Hz')
action.setCheckable(True)
self.refresh_view_menu.addAction(action)
self.refresh_view_group.addAction(action)
if refrate == '10':
action.setChecked(True)
self.refresh_view_group.triggered.connect(lambda action: self.set_refresh_rate(int(action.text()[:-3])))
self.autoscale_view = True
self.autscale_action.setCheckable(self.autoscale_view)
self.autscale_action.setChecked(self.autoscale_view)
self.plot_widget.set_autoscale(self.autoscale_view)
self.autscale_action.triggered.connect(self.set_autoscale_view)
self.meas_time_timer = QTimer(self)
self.meas_time_timer.timeout.connect(self.update_meas_time)
self.meas_start_time = QTime.currentTime()
self.update_meas_time()
def set_central_widget(self, btn):
if btn is self.btn_show_liveplot:
new_widget = views.LivePlotView(*self.data_cntrl.get_meas(self.nsamp_view), self.data_cntrl, self.nsamp_view)
self.cop_plot_settings = self.plot_widget.get_settings()
elif btn is self.btn_show_copplot:
new_widget = views.COPView(*self.data_cntrl.get_meas(self.nsamp_view), self.data_cntrl, self.nsamp_view)
new_widget.update_canvas()
new_widget.set_settings(self.cop_plot_settings)
# self.central_layout.removeWidget(self.plot_widget)
# self.central_layout.insertWidget(0, self.plot_widget)
# self.central_layout.update()
if self.run:
self.timer.timeout.connect(new_widget.update_canvas)
else:
new_widget.update_canvas()
new_widget.show_slider(0, self.data_cntrl.cnt)
new_widget.show_slider(0, self.data_cntrl.cnt)
# print(self.plot_widget.get_view_range())
new_widget.slider.setRange(*self.plot_widget.get_view_range())
# new_widget.slider.repaint()
# self.plot_widget.close()
self.plot_widget = new_widget
self.plot_widget.set_autoscale(self.autoscale_view)
self.autscale_action.triggered.connect(self.set_autoscale_view)
self.setCentralWidget(self.plot_widget)
# self.plot_widget = new_widget
# if self.run:
# self.timer.timeout.connect(self.plot_widget.update_canvas)
# else:
# self.plot_widget.update_canvas()
# self.setCentralWidget(self.central_widget)
# self.timer.start(100)
def set_autoscale_view(self, val):
self.autoscale_view = val
self.plot_widget.set_autoscale(self.autoscale_view)
def set_view_range(self, nsamp):
self.nsamp_view = nsamp
self.plot_widget.nsamp_view = nsamp
def set_refresh_rate(self, refrate):
print(refrate)
self.ref_rate = refrate
if self.run:
self.timer.setInterval(1000 / self.ref_rate)
def update_available_serial_ports(self):
print('update_available_serial_ports')
available_ports = self.meas_cntrl.get_available_serial_ports()
for act in self.port_act_group.actions():
self.port_act_group.removeAction(act)
self.port_sel_menu.removeAction(act)
if len(available_ports) > 0:
for port in available_ports:
action = QAction(port)
action.setCheckable(True)
self.port_sel_menu.addAction(action)
self.port_act_group.addAction(action)
if port == self.meas_cntrl.port:
action.setChecked(True)
self.port_act_group.triggered.connect(lambda action: self.set_port(action.text()))
else:
self.statusbar_right_lbl.setText('no ports available')
def set_port(self, port):
self.meas_cntrl.set_port(port)
def start_measure(self):
self.data_cntrl.clear_data()
self.run = True
self.meas_cntrl.connect()
self.plot_widget.hide_slider()
self.timer.timeout.connect(self.plot_widget.update_canvas)
self.meas_cntrl.start_measure()
self.timer.start(int(1000 / self.ref_rate))
self.meas_time_timer.start(1000)
self.meas_start_time = QTime.currentTime()
self.setWindowTitle('Making measurements')
def stop_measure(self):
self.run = False
self.timer.timeout.disconnect(self.plot_widget.update_canvas)
self.timer.stop()
self.meas_time_timer.stop()
self.meas_cntrl.stop_measure()
self.data_cntrl.concatenate_data()
self.plot_widget.show_slider(0, self.data_cntrl.cnt)
self.plot_widget.update_canvas()
def save_data_csv(self):
fpath = QFileDialog.getSaveFileName(self, 'Save File', '', "CSV files (*.csv)")[0]
if fpath is not '':
df = self.data_cntrl.to_dataframe()
df.to_csv(fpath, float_format='%g')
def open_data_csv(self):
fpath = QFileDialog.getOpenFileName(self, 'Open File', '', "CSV files (*.csv)")[0]
if fpath is not '':
self.setWindowTitle(fpath)
df = pd.read_csv(fpath)
self.data_cntrl.from_dataframe(df)
self.plot_widget.update_canvas()
self.plot_widget.show_slider(0, self.data_cntrl.cnt)
def update_meas_time(self):
seconds = self.meas_start_time.secsTo(QTime.currentTime())
time = QTime().fromMSecsSinceStartOfDay(1000*seconds)
text = time.toString('mm:ss')
self.measure_time_label.setText(text)
# def closeEvent(self, event):
# self.run = False
# try:
# self.central_widget.close()
# except:
# pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())