-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
508 lines (455 loc) · 20.1 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# --- From DAQ Control --- #
from reader import *
from writer import *
from parameters import *
from plotter import *
from calibration import *
class MainWindow(QMainWindow):
"""
Main application window
Handles creating the UI elements and acts as the controller logic for connecting user inputs to
the desired action
"""
def __init__(self):
super().__init__()
# initialize parameters database
self.magnetic_param_tree = MagneticControlsParamTree()
self.channel_param_tree = ChannelParameters()
self.setting_param_tree = ConfigParamTree()
self.init_ui()
self.init_daq_io()
saved_offsets = [
self.setting_param_tree.get_param_value(
"Reader Config", "Calibration Offsets", ch
)
for ch in CHANNEL_NAMES_IN
]
if DEBUG_MODE:
self.calibration_dialog = CalibrationWindow(
parent=self,
writer=self.writer,
reader=self.writer,
write_channels=self.setting_param_tree.get_write_channels(),
read_channels=self.setting_param_tree.get_read_channels(),
saved_offsets=saved_offsets,
)
self.writer.incoming_data.connect(
self.calibration_dialog.apply_calibration)
# DAQ connected mode
else:
self.calibration_dialog = CalibrationWindow(
parent=self,
writer=self.writer,
reader=self.read_thread,
write_channels=self.setting_param_tree.get_write_channels(),
read_channels=self.setting_param_tree.get_read_channels(),
saved_offsets=saved_offsets,
)
self.read_thread.incoming_data.connect(
self.calibration_dialog.apply_calibration
)
self.calibration_dialog.corrected_data.connect(
self.plotter.update_plot)
self.calibration_dialog.corrected_data.connect(self.legend.on_new_data)
self.calibration_dialog.offsets_received.connect(
self.setting_param_tree.save_offsets
)
self.calibration_dialog.show()
# Connect the output signal from changes in the param tree to change
self.start_signal_btn.clicked.connect(self.start_signal_btn_click)
self.save_settings_btn.clicked.connect(self.commit_settings_btn_click)
self.tabs.currentChanged.connect(self.on_tab_change)
self.magnetic_param_tree.paramChange.connect(
self.magnetic_param_change)
self.channel_param_tree.paramChange.connect(self.channels_param_change)
self.setting_param_tree.paramChange.connect(self.settings_param_change)
def init_ui(self):
"""
Create the UI elements and arranges them on the application
"""
self.resize(700, 700) # non maximized size
# self.setWindowState(QtCore.Qt.WindowMinimized)
# self.setMinimumSize(QSize(500, 400))
self.setWindowTitle("DAQ Interface")
self.mainbox = QWidget(self)
self.setCentralWidget(self.mainbox)
layout = QVBoxLayout()
self.mainbox.setLayout(layout)
title = QLabel("DAQ Controller")
title.setAlignment(Qt.AlignHCenter)
self.legend = Legend(self.setting_param_tree.get_read_channels())
# TODO: update legend frequency on startup with param tree values
# use signals instead of passing in legend might be better
self.plotter = SignalPlot(self.legend)
# self.plotter.setMaximumSize(800, 360)
self.start_signal_btn = QPushButton("Press to start signal out")
# self.channel_param_tree.setMinimumSize(100, 200)
self.settings_tab = QWidget(self)
self.settings_tab.layout = QVBoxLayout(self)
self.save_settings_btn = QPushButton("Commit Settings")
self.settings_tab.layout.addWidget(self.setting_param_tree)
self.settings_tab.layout.addWidget(self.save_settings_btn)
self.settings_tab.setLayout(self.settings_tab.layout)
self.tabs = QTabWidget(self)
self.tabs.addTab(self.magnetic_param_tree, "Magnetic Controls")
self.tabs.addTab(self.channel_param_tree, "Channels Controls")
self.tabs.addTab(self.settings_tab, "DAQ Settings")
self.current_tab = 0
self.tabs.setCurrentIndex(self.current_tab)
# place widgets in their respective locations
layout.addWidget(title) # row, col, rowspan, colspan
layout.addWidget(self.plotter)
layout.addWidget(self.legend)
layout.addWidget(self.start_signal_btn)
# layout.addWidget(self.setting_param_tree, 3, 1, 1, 1)
layout.addWidget(self.tabs)
def init_daq_io(self):
"""
Initialize Input and Output DAQs
Note: The self.writer object (SignalGenerator) will be instantiated once but passed to the
magnetic controls so that they can use the same signal generator. This means that the magnetic
controls and individual controls all reference the same SignalGenerator object
"""
voltages = [] # RMS Voltage
frequencies = [] # Frequency in Hz
shifts = [] # Phase shift in degrees
output_states = [] # Output on or off
# get param values from the UI widgets
for ch in CHANNEL_NAMES_OUT:
branch = "Output " + ch
voltages.append(
self.channel_param_tree.get_param_value(branch, "Voltage RMS")
)
frequencies.append(
self.channel_param_tree.get_param_value(branch, "Frequency")
)
shifts.append(
self.channel_param_tree.get_param_value(branch, "Phase Shift")
)
output_states.append(
self.channel_param_tree.get_param_value(
branch, "Toggle Output")
)
# Update graph legend
self.legend.update_rms_params(
sample_rate=self.setting_param_tree.get_param_value(
"Reader Config", "Sample Rate"
)
)
# When NI DAQ hardware is attached
if not DEBUG_MODE:
# initiate read threads for analog input
self.read_thread = SignalReader(
sample_rate=self.setting_param_tree.get_param_value(
"Reader Config", "Sample Rate"
),
sample_size=self.setting_param_tree.get_param_value(
"Reader Config", "Sample Size"
),
channels=self.setting_param_tree.get_read_channels(),
dev_name=self.setting_param_tree.get_param_value(
"Reader Config", "Device Name"
),
)
self.read_thread.start()
# initiate writer for analog output
# not handled on separate thread b/c not blocking
self.writer = SignalWriterDAQ(
voltages=voltages,
frequencies=frequencies,
shifts=shifts,
output_states=output_states,
sample_rate=self.setting_param_tree.get_param_value(
"Writer Config", "Sample Rate"
),
sample_size=self.setting_param_tree.get_param_value(
"Writer Config", "Sample Size"
),
channels=self.setting_param_tree.get_write_channels(),
dev_name=self.setting_param_tree.get_param_value(
"Writer Config", "Device Name"
),
)
self.writer.create_task()
# Debugging on computer without NI instrument
else:
# Use software signal generator and read from that
# Plot is displaying the samples and rate at which the real
# signal generator would write to the output DAQ
self.writer = SignalGeneratorBase(
voltages=voltages,
frequencies=frequencies,
shifts=shifts,
output_states=output_states,
sample_rate=self.setting_param_tree.get_param_value(
"Writer Config", "Sample Rate"
),
sample_size=self.setting_param_tree.get_param_value(
"Writer Config", "Sample Size"
),
)
# pass initialized writer so the magnetic manipulators can use it when it needs to
self.mag_alignment = MagneticAlignment(
writer=self.writer,
state=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Toggle Output"
),
phi=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Elevation"),
theta=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Azimuth"),
amplitude=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Amplitude"
),
frequency=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Frequency"
),
kx=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Coefficients", "kx"
),
ky=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Coefficients", "ky"
),
kz=self.magnetic_param_tree.get_param_value(
"3D Alignment", "Coefficients", "kz"
),
)
self.mag_rotation = MagneticRotation(
writer=self.writer,
state=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Toggle Output"
),
phi=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Elevation"),
theta=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Azimuth"),
amplitude=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Amplitude"
),
frequency=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Frequency"
),
kx=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Coefficients", "kx"
),
ky=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Coefficients", "ky"
),
kz=self.magnetic_param_tree.get_param_value(
"3D Rotation", "Coefficients", "kz"
),
)
@pyqtSlot()
def start_signal_btn_click(self):
"""
Start or pause the signal generation process
"""
if self.writer.is_running:
print("Stopped DAQ signal")
self.writer.pause()
self.start_signal_btn.setText("Press to resume signal")
else:
print("Started DAQ signal")
if self.current_tab == 0:
self.writer.realign_channel_phases()
if self.mag_alignment.output_state:
self.mag_alignment.update_params()
elif self.mag_rotation.output_state:
self.mag_rotation.update_params()
self.writer.resume()
if self.current_tab == 1:
self.writer.resume()
self.start_signal_btn.setText("Press to pause signal")
@pyqtSlot()
def commit_settings_btn_click(self):
"""
All the values in the settings tab will be committed to the NI hardware at once
"""
print("Commit settings btn pressed")
writer_sample_rate = self.setting_param_tree.get_param_value(
"Writer Config", "Sample Rate"
)
writer_sample_size = self.setting_param_tree.get_param_value(
"Writer Config", "Sample Size"
)
reader_sample_rate = self.setting_param_tree.get_param_value(
"Reader Config", "Sample Rate"
)
reader_sample_size = self.setting_param_tree.get_param_value(
"Reader Config", "Sample Size"
)
# update read channel names
self.legend.update_channels(
self.setting_param_tree.get_read_channels())
self.legend.update_rms_params(sample_rate=writer_sample_rate)
if not DEBUG_MODE:
# change the task parameters
self.read_thread.input_channels = (
self.setting_param_tree.get_read_channels()
)
self.read_thread.sample_rate = reader_sample_rate
self.read_thread.sample_size = reader_sample_size
self.writer.sample_rate = writer_sample_rate
self.writer.sample_size = writer_sample_size
# restart writer to update refresh times
self.read_thread.restart()
self.writer.pause()
self.writer.resume()
else:
self.writer.sample_rate = writer_sample_rate
self.writer.sample_size = writer_sample_size
# update refresh times in debug writer
self.writer.pause()
self.writer.resume()
print("Restarted signal reader")
@pyqtSlot(int)
def on_tab_change(self, t):
"""
Handles logic on tab switches
When switch to tab 1 (Magnetic Controls Tab):
- Realign the phase shift that may have been changed from the previous controls
- Set the writer output to the values stored in the MagneticControls object with update_params()
When switch to tab 2 (Individual Controls Tab):
- Realign the phase shift that may have been changed from the previous controls
- Set individual channel outputs to that stored in the channel params
When switch to tab 3 (Settings Tab):
- Do nothing for right now
:param t: the tab index
"""
print("changed to tab: ", t)
self.current_tab = t
if t == 0:
self.writer.realign_channel_phases()
if self.mag_alignment.output_state:
self.mag_alignment.update_params()
if self.mag_rotation.output_state:
self.mag_rotation.update_params()
elif t == 1:
self.writer.realign_channel_phases()
for i, ch in enumerate(CHANNEL_NAMES_OUT):
parent = self.channel_param_tree.params.child("Output " + ch)
self.writer.output_states[i] = parent.child(
"Toggle Output").value()
self.writer.voltages[i] = parent.child("Voltage RMS").value()
self.writer.frequencies[i] = parent.child("Frequency").value()
self.writer.shifts[i] = parent.child("Phase Shift").value()
elif t == 2:
pass
# TODO: 3D alignment will be changed to 3D Rotation when
# 3D rotation param is changed, and vice versa
def magnetic_param_change(self, parameter, changes):
"""
Handles the actions whenever some param in the Magnetic Controls Tab has changed or is updated
:param parameter: the GroupParameter object that holds the Channel Params
:param changes: list that contains [ParameterObject, 'value', data]
"""
for param, change, data in changes:
path = self.magnetic_param_tree.params.childPath(param)
print(path)
if path[0] == "3D Alignment":
if path[1] == "Toggle Output":
if data:
self.mag_alignment.update_params()
self.magnetic_param_tree.set_param_value(
False, "3D Rotation", "Toggle Output"
)
self.mag_alignment.output_state = data
elif path[1] == "Amplitude":
self.mag_alignment.amplitude = data
self.mag_alignment.update_params()
elif path[1] == "Frequency":
self.mag_alignment.frequency = data
elif path[1] == "Elevation":
self.mag_alignment.phi = data
self.mag_alignment.update_params()
elif path[1] == "Azimuth":
self.mag_alignment.theta = data
self.mag_alignment.update_params()
elif path[1] == "Coefficients":
print(path[2])
if path[2] == "k" + CHANNEL_NAMES_OUT[0].lower():
self.mag_alignment.kx = data
if path[2] == "k" + CHANNEL_NAMES_OUT[1].lower():
self.mag_alignment.ky = data
if path[2] == "k" + CHANNEL_NAMES_OUT[2].lower():
self.mag_alignment.kz = data
self.mag_alignment.update_params()
elif path[0] == "3D Rotation":
if path[1] == "Toggle Output":
if data:
self.mag_rotation.update_params()
self.magnetic_param_tree.set_param_value(
False, "3D Alignment", "Toggle Output"
)
self.mag_rotation.output_state = data
elif path[1] == "Amplitude":
self.mag_rotation.amplitude = data
self.mag_rotation.update_params()
elif path[1] == "Frequency":
self.mag_rotation.frequency = data
elif path[1] == "Elevation":
self.mag_rotation.phi = data
self.mag_rotation.update_params()
elif path[1] == "Azimuth":
self.mag_rotation.theta = data
self.mag_rotation.update_params()
elif path[1] == "Coefficients":
print(path[2])
if path[2] == "k" + CHANNEL_NAMES_OUT[0].lower():
self.mag_rotation.kx = data
if path[2] == "k" + CHANNEL_NAMES_OUT[1].lower():
self.mag_rotation.ky = data
if path[2] == "k" + CHANNEL_NAMES_OUT[2].lower():
self.mag_rotation.kz = data
self.mag_rotation.update_params()
def channels_param_change(self, parameter, changes):
"""
Handles the actions whenever some param in the Individual Controls Tab has changed or is updated
:param parameter: the GroupParameter object that holds the Channel Params
:param changes: list that contains [ParameterObject, 'value', data]
"""
for param, change, data in changes:
path = self.channel_param_tree.params.childPath(param)
ch = CHANNEL_NAMES_OUT.index(path[0].split()[1])
print(ch)
if path[1] == "Toggle Output":
self.writer.output_states[ch] = data
if path[1] == "Voltage RMS":
self.writer.voltages[ch] = data
if path[1] == "Frequency":
self.writer.frequencies[ch] = data
self.legend.legend_items[ch].frequency = data
if path[1] == "Phase Shift":
self.writer.shifts[ch] = data
def settings_param_change(self, parameter, changes):
"""
Handles the actions whenever some param in the Settings Tab has changed or is updated
Currently there is no need because the settings should only be applied when the "Commit Settings" button is pressed
:param parameter: the GroupParameter object that holds the Channel Params
:param changes: list that contains [ParameterObject, 'value', data]
"""
pass
def closeEvent(self, event):
"""
When the application is closed
"""
print("Closing...")
if not DEBUG_MODE:
self.read_thread.is_running = False
self.read_thread.wait()
self.writer.is_running = False
self.writer.end()
else:
self.writer.end()
self.channel_param_tree.save_settings()
self.setting_param_tree.save_settings()
self.magnetic_param_tree.save_settings()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())