Compare commits
3 Commits
d127750e7b
...
e43bb16bd4
Author | SHA1 | Date | |
---|---|---|---|
e43bb16bd4 | |||
e555573f09 | |||
4d53a0f51d |
@ -20,6 +20,10 @@ class CircBuffer:
|
||||
def size(self):
|
||||
return self.__size
|
||||
|
||||
@property
|
||||
def samplerate(self):
|
||||
return self.__samplereate
|
||||
|
||||
@property
|
||||
def channel_count(self):
|
||||
return self.__channels
|
||||
|
89
pyrelacs/dataio/daq_producer.py
Normal file
89
pyrelacs/dataio/daq_producer.py
Normal file
@ -0,0 +1,89 @@
|
||||
import time
|
||||
import faulthandler
|
||||
|
||||
import uldaq
|
||||
import numpy as np
|
||||
from IPython import embed
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
faulthandler.enable()
|
||||
|
||||
|
||||
class DaqProducer:
|
||||
def __init__(self, buffer: CircBuffer, device: uldaq.DaqDevice):
|
||||
self.buffer = buffer
|
||||
self.device = device
|
||||
self.ai_device = self.device.get_ai_device()
|
||||
|
||||
def read_analog_continously(
|
||||
self,
|
||||
samplerate: float = 20,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
log.debug("starting acquisition")
|
||||
channel = self.buffer.channel_count
|
||||
size = self.buffer.size
|
||||
data_in = uldaq.create_float_buffer(channel, size)
|
||||
|
||||
# BUG: Check for multiple channels
|
||||
er = self.ai_device.a_in_scan(
|
||||
1,
|
||||
1,
|
||||
uldaq.AiInputMode.SINGLE_ENDED,
|
||||
uldaq.Range.BIP10VOLTS,
|
||||
self.buffer.size,
|
||||
samplerate,
|
||||
uldaq.ScanOption.CONTINUOUS,
|
||||
uldaq.AInScanFlag.DEFAULT,
|
||||
data=data_in,
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
daq_status = uldaq.ScanStatus.IDLE
|
||||
while daq_status == uldaq.ScanStatus.IDLE:
|
||||
daq_status = self.ai_device.get_scan_status()[0]
|
||||
prev_count = 0
|
||||
prev_index = 0
|
||||
while daq_status != uldaq.ScanStatus.IDLE:
|
||||
while time.time() - start_time < 10:
|
||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||
# The index into the data buffer immediately following the last sample transferred.
|
||||
current_index = transfer_status.current_index
|
||||
# total samples since start of the scan
|
||||
total_samples = transfer_status.current_total_count
|
||||
# The number of samples per channel transferred since the scan started
|
||||
channel_samples = transfer_status.current_scan_count
|
||||
|
||||
if current_index > prev_index:
|
||||
self.buffer.append(data_in[current_index])
|
||||
prev_index = current_index
|
||||
|
||||
self.ai_device.scan_stop()
|
||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||
log.debug(daq_status)
|
||||
log.debug(transfer_status.current_index)
|
||||
log.debug(transfer_status.current_total_count)
|
||||
log.debug(transfer_status.current_scan_count)
|
||||
log.info("stopping")
|
||||
break
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||
try:
|
||||
daq_device = uldaq.DaqDevice(devices[0])
|
||||
except uldaq.ul_exception.ULException as e:
|
||||
log.error("Did not found daq devices, please connect one")
|
||||
raise e
|
||||
daq_device.connect()
|
||||
|
||||
buf = CircBuffer(size=300)
|
||||
producer = DaqProducer(buf, daq_device)
|
||||
producer.read_analog_continously()
|
@ -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