Compare commits
3 Commits
3766ea0ea2
...
dc96359f1d
Author | SHA1 | Date | |
---|---|---|---|
dc96359f1d | |||
bb88053aaa | |||
1a642c6783 |
120
pyrelacs/app.py
120
pyrelacs/app.py
@ -3,7 +3,7 @@ import sys
|
|||||||
import pathlib
|
import pathlib
|
||||||
import ctypes
|
import ctypes
|
||||||
|
|
||||||
from PyQt6.QtCore import QSize, QThreadPool, Qt
|
from PyQt6.QtCore import QProcess, QSize, QThreadPool, Qt
|
||||||
from PyQt6.QtWidgets import (
|
from PyQt6.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
QGridLayout,
|
QGridLayout,
|
||||||
@ -11,6 +11,7 @@ from PyQt6.QtWidgets import (
|
|||||||
QToolBar,
|
QToolBar,
|
||||||
QWidget,
|
QWidget,
|
||||||
QMainWindow,
|
QMainWindow,
|
||||||
|
QPlainTextEdit,
|
||||||
)
|
)
|
||||||
import tomli
|
import tomli
|
||||||
import uldaq
|
import uldaq
|
||||||
@ -27,7 +28,11 @@ class PyRelacs(QMainWindow):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.setWindowTitle("PyRelacs")
|
self.setWindowTitle("PyRelacs")
|
||||||
|
self.setMinimumSize(1000, 1000)
|
||||||
|
|
||||||
self.threadpool = QThreadPool()
|
self.threadpool = QThreadPool()
|
||||||
|
# for starting a Qprocess
|
||||||
|
self.p = None
|
||||||
|
|
||||||
self.daq_connect_button = QPushButton("Connect Daq")
|
self.daq_connect_button = QPushButton("Connect Daq")
|
||||||
self.daq_connect_button.setCheckable(True)
|
self.daq_connect_button.setCheckable(True)
|
||||||
@ -37,17 +42,17 @@ class PyRelacs(QMainWindow):
|
|||||||
self.daq_disconnect_button.setCheckable(True)
|
self.daq_disconnect_button.setCheckable(True)
|
||||||
self.daq_disconnect_button.clicked.connect(self.disconnect_dac)
|
self.daq_disconnect_button.clicked.connect(self.disconnect_dac)
|
||||||
|
|
||||||
self.repro_button = QPushButton("Load Repros")
|
self.text = QPlainTextEdit()
|
||||||
self.repro_button.setCheckable(True)
|
self.text.setReadOnly(True)
|
||||||
self.repro_button.clicked.connect(self.repro)
|
|
||||||
|
|
||||||
layout = QGridLayout()
|
layout = QGridLayout()
|
||||||
layout.addWidget(self.daq_connect_button, 0, 0)
|
layout.addWidget(self.daq_connect_button, 0, 0)
|
||||||
layout.addWidget(self.daq_disconnect_button, 0, 1)
|
layout.addWidget(self.daq_disconnect_button, 0, 1)
|
||||||
layout.addWidget(self.repro_button, 1, 0)
|
layout.addWidget(self.text, 2, 0, 1, 2)
|
||||||
|
|
||||||
self.toolbar = QToolBar("Repros")
|
self.toolbar = QToolBar("Repros")
|
||||||
self.addToolBar(self.toolbar)
|
self.addToolBar(self.toolbar)
|
||||||
|
self.repro()
|
||||||
|
|
||||||
self.setFixedSize(QSize(400, 300))
|
self.setFixedSize(QSize(400, 300))
|
||||||
widget = QWidget()
|
widget = QWidget()
|
||||||
@ -78,70 +83,59 @@ class PyRelacs(QMainWindow):
|
|||||||
log.debug("DAQ was not connected")
|
log.debug("DAQ was not connected")
|
||||||
|
|
||||||
def repro(self):
|
def repro(self):
|
||||||
config_path = pathlib.Path(__file__).parent.resolve() / "repro.toml"
|
repos_path = pathlib.Path(__file__).parent / "repros"
|
||||||
if not config_path.is_file:
|
repos_names = list(repos_path.glob("*.py"))
|
||||||
log.error("repro.toml not found")
|
# exclude the repos.py file
|
||||||
QApplication.quit()
|
repos_names = [
|
||||||
repors = []
|
f.with_suffix("").name for f in repos_names if not f.name == "repos.py"
|
||||||
|
]
|
||||||
|
for rep in repos_names:
|
||||||
|
individual_repro_button = QAction(rep, self)
|
||||||
|
individual_repro_button.setStatusTip("Button")
|
||||||
|
individual_repro_button.triggered.connect(
|
||||||
|
lambda checked, n=rep: self.run_repro(n)
|
||||||
|
)
|
||||||
|
self.toolbar.addAction(individual_repro_button)
|
||||||
|
|
||||||
with open(config_path, mode="rb") as fp:
|
def message(self, s):
|
||||||
self.config = tomli.load(fp)
|
self.text.appendPlainText(s)
|
||||||
for r in self.config:
|
|
||||||
repors.append(r)
|
|
||||||
|
|
||||||
individual_repro_button = QAction(f"{repors[0]}", self)
|
def run_repro(self, name_of_repo):
|
||||||
individual_repro_button.setStatusTip("Button")
|
if self.p is None:
|
||||||
individual_repro_button.triggered.connect(self.run_repro)
|
self.message(f"Executing process {name_of_repo}")
|
||||||
self.toolbar.addAction(individual_repro_button)
|
self.p = QProcess()
|
||||||
|
self.p.setWorkingDirectory(str(pathlib.Path(__file__).parent / "repros/"))
|
||||||
|
# log.debug(pathlib.Path(__file__).parent / "repos")
|
||||||
|
self.p.readyReadStandardOutput.connect(self.handle_stdout)
|
||||||
|
self.p.readyReadStandardError.connect(self.handle_stderr)
|
||||||
|
self.p.stateChanged.connect(self.handle_state)
|
||||||
|
self.p.finished.connect(self.process_finished)
|
||||||
|
self.p.start("python3", [f"{name_of_repo}" + ".py"])
|
||||||
|
|
||||||
def run_repro(self):
|
def handle_stderr(self):
|
||||||
daq_thread = Worker(self.read_daq)
|
if self.p is not None:
|
||||||
self.threadpool.start(daq_thread)
|
data = self.p.readAllStandardError()
|
||||||
|
stderr = bytes(data).decode("utf8")
|
||||||
|
self.message(stderr)
|
||||||
|
|
||||||
def on_scan_finished(self):
|
def handle_stdout(self):
|
||||||
log.debug("Scan finished")
|
if self.p is not None:
|
||||||
|
data = self.p.readAllStandardOutput()
|
||||||
|
stdout = bytes(data).decode("utf8")
|
||||||
|
self.message(stdout)
|
||||||
|
|
||||||
def read_daq(self, progress_callback):
|
def handle_state(self, state):
|
||||||
log.debug("running repro")
|
states = {
|
||||||
time = np.arange(0, 10, 1 / 30_000.0)
|
QProcess.ProcessState.NotRunning: "Not running",
|
||||||
data = 2 * np.sin(2 * np.pi * 10 * time)
|
QProcess.ProcessState.Starting: "Starting",
|
||||||
|
QProcess.ProcessState.Running: "Running",
|
||||||
|
}
|
||||||
|
state_name = states[state]
|
||||||
|
self.message(f"State changed: {state_name}")
|
||||||
|
|
||||||
buffer = ctypes.c_double * len(time)
|
def process_finished(self):
|
||||||
buffer_ai = uldaq.create_float_buffer(1, len(time))
|
self.text.appendPlainText("Process finished")
|
||||||
data_c = buffer(*data)
|
self.p = None
|
||||||
log.debug(f"Created C_double data {data_c}")
|
|
||||||
|
|
||||||
ao_device = self.daq_device.get_ao_device()
|
|
||||||
ai_device = self.daq_device.get_ai_device()
|
|
||||||
|
|
||||||
er = ai_device.a_in_scan(
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
uldaq.AiInputMode.SINGLE_ENDED,
|
|
||||||
uldaq.Range.BIP10VOLTS,
|
|
||||||
int(len(data)),
|
|
||||||
30_000.0,
|
|
||||||
uldaq.ScanOption.EXTTRIGGER,
|
|
||||||
uldaq.AInScanFlag.DEFAULT,
|
|
||||||
data=buffer_ai,
|
|
||||||
)
|
|
||||||
# ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
|
||||||
|
|
||||||
err = ao_device.a_out_scan(
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
uldaq.Range.BIP10VOLTS,
|
|
||||||
int(len(data)),
|
|
||||||
30_000.0,
|
|
||||||
uldaq.ScanOption.DEFAULTIO,
|
|
||||||
uldaq.AOutScanFlag.DEFAULT,
|
|
||||||
data_c,
|
|
||||||
)
|
|
||||||
# INFO: Need to wait for the acquistion
|
|
||||||
self.daq_disconnect_button.setDisabled(True)
|
|
||||||
self.daq_connect_button.setEnabled(True)
|
|
||||||
embed()
|
|
||||||
exit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
import uldaq
|
|
||||||
from IPython import embed
|
|
||||||
import typer
|
|
||||||
|
|
||||||
class Output_daq():
|
|
||||||
def __init__(self) -> None:
|
|
||||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
|
||||||
daq_device = uldaq.DaqDevice(devices[0])
|
|
||||||
daq_device.connect()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
daq_input = Input_daq()
|
|
||||||
|
|
86
pyrelacs/repros/inandout.py
Normal file
86
pyrelacs/repros/inandout.py
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
import ctypes
|
||||||
|
|
||||||
|
import uldaq
|
||||||
|
from IPython import embed
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
|
||||||
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class ReadWrite:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||||
|
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||||
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||||
|
self.daq_device.connect()
|
||||||
|
log.debug("Connected")
|
||||||
|
# self.daq_device.enable_event(
|
||||||
|
# uldaq.DaqEventType.ON_DATA_AVAILABLE,
|
||||||
|
# 1,
|
||||||
|
# self.read_write,
|
||||||
|
# (uldaq.DaqEventType.ON_DATA_AVAILABLE, 1, 1),
|
||||||
|
# )
|
||||||
|
|
||||||
|
def read_write(self) -> None:
|
||||||
|
# event_type = callback_args.event_type
|
||||||
|
# event_data = callback_args.event_data
|
||||||
|
# user_data = callback_args.user_data
|
||||||
|
|
||||||
|
FS = 30_000.0
|
||||||
|
DURATION = 10
|
||||||
|
FREQUENCY = 100
|
||||||
|
|
||||||
|
time = np.arange(0, DURATION, 1 / FS)
|
||||||
|
data = 2 * np.sin(2 * np.pi * FREQUENCY * time)
|
||||||
|
|
||||||
|
buffer = ctypes.c_double * len(time)
|
||||||
|
data_c = buffer(*data)
|
||||||
|
buf = uldaq.create_float_buffer(1, len(time))
|
||||||
|
|
||||||
|
# Get the Ananlog In device and Analog Info
|
||||||
|
ai_device = self.daq_device.get_ai_device()
|
||||||
|
ai_info = ai_device.get_info()
|
||||||
|
|
||||||
|
# Get the Analog Out device
|
||||||
|
ao_device = self.daq_device.get_ao_device()
|
||||||
|
ao_info = ao_device.get_info()
|
||||||
|
|
||||||
|
er_ao = ao_device.a_out_scan(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
int(len(data)),
|
||||||
|
30_000.0,
|
||||||
|
uldaq.ScanOption.DEFAULTIO,
|
||||||
|
uldaq.AOutScanFlag.DEFAULT,
|
||||||
|
data_c,
|
||||||
|
)
|
||||||
|
|
||||||
|
er_ai = ai_device.a_in_scan(
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
uldaq.AiInputMode.SINGLE_ENDED,
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
len(time),
|
||||||
|
FS,
|
||||||
|
uldaq.ScanOption.DEFAULTIO,
|
||||||
|
uldaq.AInScanFlag.DEFAULT,
|
||||||
|
data=buf,
|
||||||
|
)
|
||||||
|
ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
||||||
|
log.debug("Scanning")
|
||||||
|
|
||||||
|
self.daq_device.disconnect()
|
||||||
|
self.daq_device.release()
|
||||||
|
plt.plot(buf)
|
||||||
|
plt.plot(data_c)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
daq_input = ReadWrite()
|
||||||
|
daq_input.read_write()
|
@ -42,10 +42,11 @@ class ReadData:
|
|||||||
)
|
)
|
||||||
ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
||||||
log.debug("Scanning")
|
log.debug("Scanning")
|
||||||
embed()
|
|
||||||
|
|
||||||
self.daq_device.disconnect()
|
self.daq_device.disconnect()
|
||||||
self.daq_device.release()
|
self.daq_device.release()
|
||||||
|
plt.plot(buf)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
52
pyrelacs/repros/output.py
Normal file
52
pyrelacs/repros/output.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import ctypes
|
||||||
|
|
||||||
|
import uldaq
|
||||||
|
from IPython import embed
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class Output_daq:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||||
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||||
|
self.daq_device.connect()
|
||||||
|
|
||||||
|
def write_daq(self):
|
||||||
|
log.debug("running repro")
|
||||||
|
time = np.arange(0, 10, 1 / 30_000.0)
|
||||||
|
data = 2 * np.sin(2 * np.pi * 10 * time)
|
||||||
|
|
||||||
|
buffer = ctypes.c_double * len(time)
|
||||||
|
data_c = buffer(*data)
|
||||||
|
|
||||||
|
log.debug(f"Created C_double data {data_c}")
|
||||||
|
ao_device = self.daq_device.get_ao_device()
|
||||||
|
ao_info = ao_device.get_info()
|
||||||
|
|
||||||
|
log.debug(
|
||||||
|
f"Analog info,\n Channels available {ao_info.get_num_chans()}, \n Max Samplerate: {ao_info.get_max_scan_rate()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
err = ao_device.a_out_scan(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
int(len(data)),
|
||||||
|
30_000.0,
|
||||||
|
uldaq.ScanOption.DEFAULTIO,
|
||||||
|
uldaq.AOutScanFlag.DEFAULT,
|
||||||
|
data_c,
|
||||||
|
)
|
||||||
|
ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 11)
|
||||||
|
|
||||||
|
self.daq_device.disconnect()
|
||||||
|
self.daq_device.release()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
daq_input = Output_daq()
|
||||||
|
daq_input.write_daq()
|
@ -18,3 +18,9 @@ class Repos:
|
|||||||
|
|
||||||
def reload_repo(self) -> None:
|
def reload_repo(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def read_daq(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write_daq(self) -> None:
|
||||||
|
pass
|
Loading…
Reference in New Issue
Block a user