[ui] adding plot that reads after pressing run the input of the daqproducer
This commit is contained in:
		
							parent
							
								
									e555573f09
								
							
						
					
					
						commit
						e43bb16bd4
					
				| @ -1,5 +1,7 @@ | ||||
| from pathlib import Path as path | ||||
| 
 | ||||
| from PyQt6.QtGui import QAction, QIcon, QKeySequence | ||||
| from PyQt6.QtCore import Qt, QSize, QThreadPool | ||||
| from PyQt6.QtCore import Qt, QSize, QThreadPool, QTimer | ||||
| from PyQt6.QtWidgets import ( | ||||
|     QGridLayout, | ||||
|     QPushButton, | ||||
| @ -13,12 +15,13 @@ from PyQt6.QtWidgets import ( | ||||
| import uldaq | ||||
| import nixio as nix | ||||
| import pyqtgraph as pg | ||||
| import numpy as np | ||||
| 
 | ||||
| from pathlib import Path as path | ||||
| 
 | ||||
| from pyrelacs.dataio.daq_producer import DaqProducer | ||||
| from pyrelacs.worker import Worker | ||||
| from pyrelacs.repros.repros import Repro | ||||
| from pyrelacs.dataio.buffer import DataBuffer | ||||
| from pyrelacs.dataio.circbuffer import CircBuffer | ||||
| 
 | ||||
| from pyrelacs.ui.about import AboutDialog | ||||
| from pyrelacs.ui.plots.calibration import CalibrationPlot | ||||
| @ -44,15 +47,14 @@ class PyRelacs(QMainWindow): | ||||
|         filename = path.joinpath(path.cwd(), "data.nix") | ||||
|         if filename.exists(): | ||||
|             self.nix_file = nix.File.open(str(filename), nix.FileMode.ReadOnly) | ||||
|         else: | ||||
|             self.nix_file = nix.File.open(str(filename), nix.FileMode.Overwrite) | ||||
|         filename = path.joinpath(path.cwd(), "calibration.nix") | ||||
|         self.nix_calibration = nix.File.open(str(filename), nix.FileMode.Overwrite) | ||||
| 
 | ||||
|         self.calibration_plot = CalibrationPlot(self.figure, self.nix_file) | ||||
| 
 | ||||
|         self.threadpool = QThreadPool() | ||||
|         self.repros = Repro() | ||||
| 
 | ||||
|         # self.buffers = DataBuffer(channels=1, samples=100_000) | ||||
|         self.text = QPlainTextEdit() | ||||
|         self.text.setReadOnly(True) | ||||
| 
 | ||||
| @ -70,6 +72,11 @@ class PyRelacs(QMainWindow): | ||||
|         widget.setLayout(layout) | ||||
|         self.setCentralWidget(widget) | ||||
| 
 | ||||
|         self.buffer = CircBuffer(size=500) | ||||
|         self.connect_dac() | ||||
|         self.daq_producer = DaqProducer(self.buffer, self.daq_device) | ||||
|         self.plot_daq() | ||||
| 
 | ||||
|     def create_actions(self): | ||||
|         self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self) | ||||
|         self._rlx_exitaction.setStatusTip("Close relacs") | ||||
| @ -101,6 +108,10 @@ class PyRelacs(QMainWindow): | ||||
|         self._daq_calibaction.setStatusTip("Calibrate the attenuator device") | ||||
|         # self._daq_calibaction.setShortcut(QKeySequence("Alt+d")) | ||||
|         self._daq_calibaction.triggered.connect(self.calibration_plot.plot) | ||||
| 
 | ||||
|         self._run_action = QAction(QIcon(":/icons/calibration.png"), "Run", self) | ||||
|         self._run_action.triggered.connect(self.run_daq) | ||||
| 
 | ||||
|         self.create_menu() | ||||
| 
 | ||||
|     def create_menu(self): | ||||
| @ -119,6 +130,7 @@ class PyRelacs(QMainWindow): | ||||
|                 device_menu.addAction(self._daq_disconnectaction) | ||||
|                 device_menu.addSeparator() | ||||
|                 device_menu.addAction(self._daq_calibaction) | ||||
|                 device_menu.addAction(self._run_action) | ||||
| 
 | ||||
|             if help_menu is not None: | ||||
|                 help_menu.addSeparator() | ||||
| @ -139,6 +151,7 @@ class PyRelacs(QMainWindow): | ||||
|         daq_toolbar.addAction(self._daq_connectaction) | ||||
|         daq_toolbar.addAction(self._daq_disconnectaction) | ||||
|         daq_toolbar.addAction(self._daq_calibaction) | ||||
|         daq_toolbar.addAction(self._run_action) | ||||
|         self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar) | ||||
| 
 | ||||
|         repro_toolbar = QToolBar("Repros") | ||||
| @ -150,7 +163,7 @@ class PyRelacs(QMainWindow): | ||||
|                 lambda checked, n=rep, f=fn: self.run_repro(n, f) | ||||
|             ) | ||||
|             repro_toolbar.addAction(repro_action) | ||||
|         self.addToolBar(Qt.ToolBarArea.TopToolBarArea, repro_toolbar) | ||||
|         self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar) | ||||
| 
 | ||||
|     def create_buttons(self): | ||||
|         self.daq_connect_button = QPushButton("Connect Daq") | ||||
| @ -165,11 +178,50 @@ class PyRelacs(QMainWindow): | ||||
|         self.plot_calibration_button.setCheckable(True) | ||||
|         self.plot_calibration_button.clicked.connect(self.calibration_plot.plot) | ||||
| 
 | ||||
|         self.daq_run_button = QPushButton("Run") | ||||
|         self.daq_run_button.setCheckable(True) | ||||
|         self.daq_run_button.clicked.connect(self.run_daq) | ||||
| 
 | ||||
|     def plot_daq(self): | ||||
|         self.figure.setBackground("w") | ||||
|         self.daq_plot = self.figure.addPlot(row=0, col=0) | ||||
|         self.time = list(range(10)) | ||||
|         pen = pg.mkPen("red") | ||||
|         self.data = list(range(10)) | ||||
|         self.line = self.daq_plot.plot(self.time, self.data, pen=pen) | ||||
| 
 | ||||
|         self.timer = QTimer() | ||||
|         self.timer.setInterval(50) | ||||
|         self.timer.timeout.connect(self.update_plot) | ||||
|         self.timer.start() | ||||
|         # self.update_plot() | ||||
| 
 | ||||
|     def update_plot(self): | ||||
|         self.time = self.time[1:] | ||||
|         self.time.append(self.time[-1] + 1) | ||||
|         self.data = self.data[1:] | ||||
|         try: | ||||
|             item = self.buffer.get(self.buffer.write_index() - 1) | ||||
|         except IndexError: | ||||
|             item = 0 | ||||
| 
 | ||||
|         self.data.append(item) | ||||
|         self.line.setData(self.time, self.data) | ||||
| 
 | ||||
|     def run_daq(self): | ||||
|         read_daq = Worker(self.daq_producer.read_analog_continously) | ||||
|         read_daq.signals.result.connect(self.print_output) | ||||
|         read_daq.signals.finished.connect(self.thread_complete) | ||||
|         read_daq.signals.progress.connect(self.progress_fn) | ||||
|         self.threadpool.start(read_daq) | ||||
| 
 | ||||
|     def connect_dac(self): | ||||
|         devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB) | ||||
|         try: | ||||
|             self.daq_device = uldaq.DaqDevice(devices[0]) | ||||
|             log.debug(f"Found daq devices {len(devices)}, connecting to the first one") | ||||
|             self.daq_device.connect() | ||||
|             log.debug("connected") | ||||
|         except IndexError: | ||||
|             log.error("DAQ is not connected") | ||||
|             log.error("Please connect a DAQ device to the system") | ||||
| @ -195,7 +247,7 @@ class PyRelacs(QMainWindow): | ||||
| 
 | ||||
|     def run_repro(self, n, fn): | ||||
|         self.text.appendPlainText(f"started Repro {n}, {fn}") | ||||
|         worker = Worker(self.repros.run_repro, self.nix_file, n, fn) | ||||
|         worker = Worker(self.repros.run_repro, self.nix_calibration, n, fn) | ||||
|         worker.signals.result.connect(self.print_output) | ||||
|         worker.signals.finished.connect(self.thread_complete) | ||||
|         worker.signals.progress.connect(self.progress_fn) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user