Compare commits
17 Commits
calibratio
...
e43bb16bd4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e43bb16bd4 | ||
|
|
e555573f09 | ||
|
|
4d53a0f51d | ||
|
|
d127750e7b | ||
|
|
b18f870a6b | ||
|
|
0b067df69c | ||
|
|
e3ed2fcc75 | ||
|
|
ff84d63fe1 | ||
| 19c6b90d5c | |||
| a7b62c5b3a | |||
|
|
54f0d61fc9 | ||
|
|
815838eab7 | ||
|
|
314e609472 | ||
|
|
2719d49eb0 | ||
|
|
a0c524326d | ||
|
|
c2285f3750 | ||
|
|
9327d1bac9 |
81
poetry.lock
generated
81
poetry.lock
generated
@@ -1,4 +1,4 @@
|
|||||||
# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand.
|
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "asttokens"
|
name = "asttokens"
|
||||||
@@ -153,6 +153,20 @@ files = [
|
|||||||
{file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"},
|
{file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "exceptiongroup"
|
||||||
|
version = "1.2.2"
|
||||||
|
description = "Backport of PEP 654 (exception groups)"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
files = [
|
||||||
|
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
|
||||||
|
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
test = ["pytest (>=6)"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "executing"
|
name = "executing"
|
||||||
version = "2.1.0"
|
version = "2.1.0"
|
||||||
@@ -270,6 +284,17 @@ files = [
|
|||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
numpy = ">=1.19.3"
|
numpy = ">=1.19.3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iniconfig"
|
||||||
|
version = "2.0.0"
|
||||||
|
description = "brain-dead simple config-ini parsing"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
files = [
|
||||||
|
{file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
|
||||||
|
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ipython"
|
name = "ipython"
|
||||||
version = "8.27.0"
|
version = "8.27.0"
|
||||||
@@ -284,6 +309,7 @@ files = [
|
|||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
colorama = {version = "*", markers = "sys_platform == \"win32\""}
|
colorama = {version = "*", markers = "sys_platform == \"win32\""}
|
||||||
decorator = "*"
|
decorator = "*"
|
||||||
|
exceptiongroup = {version = "*", markers = "python_version < \"3.11\""}
|
||||||
jedi = ">=0.16"
|
jedi = ">=0.16"
|
||||||
matplotlib-inline = "*"
|
matplotlib-inline = "*"
|
||||||
pexpect = {version = ">4.3", markers = "sys_platform != \"win32\" and sys_platform != \"emscripten\""}
|
pexpect = {version = ">4.3", markers = "sys_platform != \"win32\" and sys_platform != \"emscripten\""}
|
||||||
@@ -291,6 +317,7 @@ prompt-toolkit = ">=3.0.41,<3.1.0"
|
|||||||
pygments = ">=2.4.0"
|
pygments = ">=2.4.0"
|
||||||
stack-data = "*"
|
stack-data = "*"
|
||||||
traitlets = ">=5.13.0"
|
traitlets = ">=5.13.0"
|
||||||
|
typing-extensions = {version = ">=4.6", markers = "python_version < \"3.12\""}
|
||||||
|
|
||||||
[package.extras]
|
[package.extras]
|
||||||
all = ["ipython[black,doc,kernel,matplotlib,nbconvert,nbformat,notebook,parallel,qtconsole]", "ipython[test,test-extra]"]
|
all = ["ipython[black,doc,kernel,matplotlib,nbconvert,nbformat,notebook,parallel,qtconsole]", "ipython[test,test-extra]"]
|
||||||
@@ -758,6 +785,21 @@ tests = ["check-manifest", "coverage", "defusedxml", "markdown2", "olefile", "pa
|
|||||||
typing = ["typing-extensions"]
|
typing = ["typing-extensions"]
|
||||||
xmp = ["defusedxml"]
|
xmp = ["defusedxml"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pluggy"
|
||||||
|
version = "1.5.0"
|
||||||
|
description = "plugin and hook calling mechanisms for python"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.8"
|
||||||
|
files = [
|
||||||
|
{file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"},
|
||||||
|
{file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
dev = ["pre-commit", "tox"]
|
||||||
|
testing = ["pytest", "pytest-benchmark"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "prompt-toolkit"
|
name = "prompt-toolkit"
|
||||||
version = "3.0.48"
|
version = "3.0.48"
|
||||||
@@ -903,6 +945,28 @@ files = [
|
|||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
numpy = ">=1.22.0"
|
numpy = ">=1.22.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pytest"
|
||||||
|
version = "8.3.3"
|
||||||
|
description = "pytest: simple powerful testing with Python"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.8"
|
||||||
|
files = [
|
||||||
|
{file = "pytest-8.3.3-py3-none-any.whl", hash = "sha256:a6853c7375b2663155079443d2e45de913a911a11d669df02a50814944db57b2"},
|
||||||
|
{file = "pytest-8.3.3.tar.gz", hash = "sha256:70b98107bd648308a7952b06e6ca9a50bc660be218d53c257cc1fc94fda10181"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
colorama = {version = "*", markers = "sys_platform == \"win32\""}
|
||||||
|
exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
|
||||||
|
iniconfig = "*"
|
||||||
|
packaging = "*"
|
||||||
|
pluggy = ">=1.5,<2"
|
||||||
|
tomli = {version = ">=1", markers = "python_version < \"3.11\""}
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "python-dateutil"
|
name = "python-dateutil"
|
||||||
version = "2.9.0.post0"
|
version = "2.9.0.post0"
|
||||||
@@ -1026,6 +1090,17 @@ pure-eval = "*"
|
|||||||
[package.extras]
|
[package.extras]
|
||||||
tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"]
|
tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tomli"
|
||||||
|
version = "2.0.1"
|
||||||
|
description = "A lil' TOML parser"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
files = [
|
||||||
|
{file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"},
|
||||||
|
{file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tomlkit"
|
name = "tomlkit"
|
||||||
version = "0.13.2"
|
version = "0.13.2"
|
||||||
@@ -1104,5 +1179,5 @@ files = [
|
|||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.12"
|
python-versions = "^3.10"
|
||||||
content-hash = "b1076b7f750e8f7e66542918ec746e74544fbfdb376875158f083fb573d18107"
|
content-hash = "c661a83b9d93748d39d3e4577f20e3b534a35f270231b6f48a9f13498a3658bd"
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ tomlkit = "^0.13.2"
|
|||||||
scipy = "^1.14.1"
|
scipy = "^1.14.1"
|
||||||
nixio = "^1.5.3"
|
nixio = "^1.5.3"
|
||||||
pyqtgraph = "^0.13.7"
|
pyqtgraph = "^0.13.7"
|
||||||
|
pytest = "^8.3.3"
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
pyrelacs = "pyrelacs.app:main"
|
pyrelacs = "pyrelacs.app:main"
|
||||||
|
|||||||
81
pyrelacs/dataio/buffer.py
Normal file
81
pyrelacs/dataio/buffer.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import time
|
||||||
|
import faulthandler
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from pyqtgraph import transformToArray
|
||||||
|
import uldaq
|
||||||
|
import numpy as np
|
||||||
|
from IPython import embed
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
|
||||||
|
log = config_logging()
|
||||||
|
faulthandler.enable()
|
||||||
|
|
||||||
|
|
||||||
|
class DataBuffer:
|
||||||
|
def __init__(self, channels, samples):
|
||||||
|
self.channels = channels
|
||||||
|
self.samples = samples
|
||||||
|
|
||||||
|
def read_analog_continously(
|
||||||
|
self,
|
||||||
|
device: uldaq.DaqDevice,
|
||||||
|
samplerate: float = 40_000.0,
|
||||||
|
):
|
||||||
|
data_array = []
|
||||||
|
|
||||||
|
max_len_buffer = self.channels * self.samples
|
||||||
|
self.buffer = deque(maxlen=max_len_buffer)
|
||||||
|
|
||||||
|
samples_per_channel = 40_000
|
||||||
|
self.device = device
|
||||||
|
self.ai_device = self.device.get_ai_device()
|
||||||
|
|
||||||
|
data_analog_input = uldaq.create_float_buffer(
|
||||||
|
self.channels, samples_per_channel
|
||||||
|
)
|
||||||
|
|
||||||
|
er = self.ai_device.a_in_scan(
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
uldaq.AiInputMode.SINGLE_ENDED,
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
samples_per_channel,
|
||||||
|
samplerate,
|
||||||
|
uldaq.ScanOption.CONTINUOUS,
|
||||||
|
uldaq.AInScanFlag.DEFAULT,
|
||||||
|
data=data_analog_input,
|
||||||
|
)
|
||||||
|
|
||||||
|
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:
|
||||||
|
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||||
|
|
||||||
|
# The index into the data buffer immediately following the last sample transferred.
|
||||||
|
curren_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
|
||||||
|
|
||||||
|
self.ai_device.scan_stop()
|
||||||
|
|
||||||
|
|
||||||
|
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 = DataBuffer(channels=2, samples=100_000)
|
||||||
|
buf.read_analog_continously(daq_device)
|
||||||
159
pyrelacs/dataio/circbuffer.py
Normal file
159
pyrelacs/dataio/circbuffer.py
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
from typing import Tuple
|
||||||
|
import numpy as np
|
||||||
|
from IPython import embed
|
||||||
|
|
||||||
|
|
||||||
|
class CircBuffer:
|
||||||
|
def __init__(self, size: int, channels: int = 1, samplerate: float = 40_000):
|
||||||
|
self.__size = size
|
||||||
|
self.__channels = channels
|
||||||
|
self.__samplereate = samplerate
|
||||||
|
self.__buffer = np.zeros(
|
||||||
|
(channels, size), dtype=np.double
|
||||||
|
) # or dtype of your choice
|
||||||
|
self.__index = [0 for i in range(channels)]
|
||||||
|
self.__is_full = [False for i in range(channels)]
|
||||||
|
self.__totalcount = [0 for i in range(channels)]
|
||||||
|
self.__overflows = [0 for i in range(channels)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def size(self):
|
||||||
|
return self.__size
|
||||||
|
|
||||||
|
@property
|
||||||
|
def samplerate(self):
|
||||||
|
return self.__samplereate
|
||||||
|
|
||||||
|
@property
|
||||||
|
def channel_count(self):
|
||||||
|
return self.__channels
|
||||||
|
|
||||||
|
def totocount(self, channel: int = 0):
|
||||||
|
return self.__totalcount[channel]
|
||||||
|
|
||||||
|
def is_full(self, channel: int = 0):
|
||||||
|
return self.__is_full[channel]
|
||||||
|
|
||||||
|
def write_index(self, channel: int = 0):
|
||||||
|
return self.__index[channel]
|
||||||
|
|
||||||
|
def append(self, item, channel: int = 0):
|
||||||
|
self.__buffer[channel, self.write_index(channel)] = item
|
||||||
|
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
||||||
|
self.__totalcount[channel] += 1
|
||||||
|
if self.__index[channel] == 0:
|
||||||
|
self.__is_full[channel] = True
|
||||||
|
self.__overflows[channel] += 1
|
||||||
|
|
||||||
|
def get_all(self, channel: int = 0):
|
||||||
|
"""
|
||||||
|
Return all valid values from the specified channel
|
||||||
|
"""
|
||||||
|
if self.__is_full[channel]:
|
||||||
|
return np.concatenate(
|
||||||
|
(
|
||||||
|
self.__buffer[channel, self.__index[channel] :],
|
||||||
|
self.__buffer[channel, : self.__index[channel]],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return self.__buffer[channel, : self.__index[channel]]
|
||||||
|
|
||||||
|
def has_value(self, index, channel):
|
||||||
|
if index <= 0 and self.is_full(channel):
|
||||||
|
return True
|
||||||
|
elif index < 0 and not self.is_full(channel):
|
||||||
|
return False
|
||||||
|
|
||||||
|
if index >= self.size:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# test if the ring buffer is at the start but
|
||||||
|
# and the index is greater than the write index
|
||||||
|
if index > self.write_index(channel) and self.is_full(channel):
|
||||||
|
return True
|
||||||
|
elif index >= self.write_index(channel) and not self.is_full(channel):
|
||||||
|
raise IndexError("Index has no value, not written")
|
||||||
|
|
||||||
|
if index == self.write_index(channel) and self.__totalcount[channel] == 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def valid_range(self, channel: int = 0) -> Tuple[int, int]:
|
||||||
|
"""
|
||||||
|
Return the start index and the extend that are valid within the buffer
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
channel : int
|
||||||
|
channel of the buffer
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Tuple[int, int]
|
||||||
|
start, extend of the valid range
|
||||||
|
"""
|
||||||
|
start = 0
|
||||||
|
extend = 0
|
||||||
|
if self.__totalcount[channel] == 0:
|
||||||
|
return start, extend
|
||||||
|
|
||||||
|
if not self.is_full(channel):
|
||||||
|
extend = self.__totalcount[channel]
|
||||||
|
else:
|
||||||
|
extend = self.size
|
||||||
|
return start, extend
|
||||||
|
|
||||||
|
def get(self, index: int = -1, channel: int = 0):
|
||||||
|
# easy case first, we can spare the effort of further checking
|
||||||
|
if index >= 0 and index <= self.write_index(channel):
|
||||||
|
if self.has_value(index, channel):
|
||||||
|
return self.__buffer[channel, index]
|
||||||
|
else:
|
||||||
|
raise IndexError(
|
||||||
|
f"Invalid index {index} on ring buffer for channel{channel}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if index < 0:
|
||||||
|
index = self.write_index() - 1
|
||||||
|
if self.has_value(index, channel):
|
||||||
|
return self.__buffer[channel, index]
|
||||||
|
else:
|
||||||
|
raise IndexError(
|
||||||
|
f"Invalid index {index} on ring buffer for channel{channel}"
|
||||||
|
)
|
||||||
|
|
||||||
|
def read(self, start, count=1, channel=0):
|
||||||
|
"""Reads a numpy array from buffer"""
|
||||||
|
if start < 0 or count < 0:
|
||||||
|
raise IndexError(
|
||||||
|
f"Invalid start ({start}) or count ({count}) for channel{channel}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if count == 1:
|
||||||
|
return np.array(self.get(start, channel))
|
||||||
|
|
||||||
|
vs, vc = self.valid_range(channel)
|
||||||
|
if start > self.__totalcount[channel]:
|
||||||
|
raise IndexError(
|
||||||
|
f"Invalid start index {start} is invalid with totalcount {self.__totalcount[channel]} for channel{channel}"
|
||||||
|
)
|
||||||
|
if start > self.size:
|
||||||
|
raise IndexError(
|
||||||
|
f"Invalid start index {start} for buffer with size {self.size}"
|
||||||
|
)
|
||||||
|
if count > self.size:
|
||||||
|
count = self.size
|
||||||
|
if count > vc:
|
||||||
|
count = vc
|
||||||
|
|
||||||
|
if (start + count) < self.size:
|
||||||
|
return self.__buffer[channel, start : start + count]
|
||||||
|
else:
|
||||||
|
return np.concatenate(
|
||||||
|
(
|
||||||
|
self.__buffer[channel, start:],
|
||||||
|
self.__buffer[channel, : count - self.size + start],
|
||||||
|
)
|
||||||
|
)
|
||||||
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()
|
||||||
@@ -11,7 +11,7 @@ from pyrelacs.util.logging import config_logging
|
|||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
class MccDac:
|
class MccDaq:
|
||||||
"""
|
"""
|
||||||
Represents the Digital/Analog Converter from Meassuring Computing.
|
Represents the Digital/Analog Converter from Meassuring Computing.
|
||||||
provides methods for writing and reading the Analog / Digital input and output.
|
provides methods for writing and reading the Analog / Digital input and output.
|
||||||
@@ -43,6 +43,7 @@ class MccDac:
|
|||||||
except uldaq.ul_exception.ULException:
|
except uldaq.ul_exception.ULException:
|
||||||
self.disconnect_dac()
|
self.disconnect_dac()
|
||||||
self.connect_dac()
|
self.connect_dac()
|
||||||
|
|
||||||
self.ai_device = self.daq_device.get_ai_device()
|
self.ai_device = self.daq_device.get_ai_device()
|
||||||
self.ao_device = self.daq_device.get_ao_device()
|
self.ao_device = self.daq_device.get_ao_device()
|
||||||
self.dio_device = self.daq_device.get_dio_device()
|
self.dio_device = self.daq_device.get_dio_device()
|
||||||
@@ -6,10 +6,8 @@ import uldaq
|
|||||||
from IPython import embed
|
from IPython import embed
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from scipy.signal import welch
|
|
||||||
from scipy.signal import find_peaks
|
|
||||||
|
|
||||||
from pyrelacs.devices.mccdac import MccDac
|
from pyrelacs.devices.mccdaq import MccDaq
|
||||||
from pyrelacs.util.logging import config_logging
|
from pyrelacs.util.logging import config_logging
|
||||||
|
|
||||||
log = config_logging()
|
log = config_logging()
|
||||||
@@ -17,7 +15,7 @@ log = config_logging()
|
|||||||
faulthandler.enable()
|
faulthandler.enable()
|
||||||
|
|
||||||
|
|
||||||
class Calibration(MccDac):
|
class Calibration(MccDaq):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.SAMPLERATE = 40_000.0
|
self.SAMPLERATE = 40_000.0
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
from pathlib import Path as path
|
||||||
|
|
||||||
from PyQt6.QtGui import QAction, QIcon, QKeySequence
|
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 (
|
from PyQt6.QtWidgets import (
|
||||||
QGridLayout,
|
QGridLayout,
|
||||||
QPushButton,
|
QPushButton,
|
||||||
@@ -11,19 +13,21 @@ from PyQt6.QtWidgets import (
|
|||||||
QStatusBar,
|
QStatusBar,
|
||||||
)
|
)
|
||||||
import uldaq
|
import uldaq
|
||||||
import numpy as np
|
|
||||||
import nixio as nix
|
import nixio as nix
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
from pathlib import Path as path
|
|
||||||
from scipy.signal import welch, find_peaks
|
|
||||||
|
|
||||||
|
from pyrelacs.dataio.daq_producer import DaqProducer
|
||||||
from pyrelacs.worker import Worker
|
from pyrelacs.worker import Worker
|
||||||
from pyrelacs.repros.repros import Repro
|
from pyrelacs.repros.repros import Repro
|
||||||
from pyrelacs.util.logging import config_logging
|
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||||
|
|
||||||
from pyrelacs.ui.about import AboutDialog
|
from pyrelacs.ui.about import AboutDialog
|
||||||
from pyrelacs.ui.plots.calibration import CalibrationPlot
|
from pyrelacs.ui.plots.calibration import CalibrationPlot
|
||||||
|
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
|
||||||
log = config_logging()
|
log = config_logging()
|
||||||
_root = path(__file__).parent.parent
|
_root = path(__file__).parent.parent
|
||||||
|
|
||||||
@@ -43,8 +47,8 @@ class PyRelacs(QMainWindow):
|
|||||||
filename = path.joinpath(path.cwd(), "data.nix")
|
filename = path.joinpath(path.cwd(), "data.nix")
|
||||||
if filename.exists():
|
if filename.exists():
|
||||||
self.nix_file = nix.File.open(str(filename), nix.FileMode.ReadOnly)
|
self.nix_file = nix.File.open(str(filename), nix.FileMode.ReadOnly)
|
||||||
else:
|
filename = path.joinpath(path.cwd(), "calibration.nix")
|
||||||
self.nix_file = nix.File.open(str(filename), nix.FileMode.Overwrite)
|
self.nix_calibration = nix.File.open(str(filename), nix.FileMode.Overwrite)
|
||||||
|
|
||||||
self.calibration_plot = CalibrationPlot(self.figure, self.nix_file)
|
self.calibration_plot = CalibrationPlot(self.figure, self.nix_file)
|
||||||
|
|
||||||
@@ -68,6 +72,11 @@ class PyRelacs(QMainWindow):
|
|||||||
widget.setLayout(layout)
|
widget.setLayout(layout)
|
||||||
self.setCentralWidget(widget)
|
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):
|
def create_actions(self):
|
||||||
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
|
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
|
||||||
self._rlx_exitaction.setStatusTip("Close relacs")
|
self._rlx_exitaction.setStatusTip("Close relacs")
|
||||||
@@ -99,6 +108,10 @@ class PyRelacs(QMainWindow):
|
|||||||
self._daq_calibaction.setStatusTip("Calibrate the attenuator device")
|
self._daq_calibaction.setStatusTip("Calibrate the attenuator device")
|
||||||
# self._daq_calibaction.setShortcut(QKeySequence("Alt+d"))
|
# self._daq_calibaction.setShortcut(QKeySequence("Alt+d"))
|
||||||
self._daq_calibaction.triggered.connect(self.calibration_plot.plot)
|
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()
|
self.create_menu()
|
||||||
|
|
||||||
def create_menu(self):
|
def create_menu(self):
|
||||||
@@ -117,6 +130,7 @@ class PyRelacs(QMainWindow):
|
|||||||
device_menu.addAction(self._daq_disconnectaction)
|
device_menu.addAction(self._daq_disconnectaction)
|
||||||
device_menu.addSeparator()
|
device_menu.addSeparator()
|
||||||
device_menu.addAction(self._daq_calibaction)
|
device_menu.addAction(self._daq_calibaction)
|
||||||
|
device_menu.addAction(self._run_action)
|
||||||
|
|
||||||
if help_menu is not None:
|
if help_menu is not None:
|
||||||
help_menu.addSeparator()
|
help_menu.addSeparator()
|
||||||
@@ -137,6 +151,7 @@ class PyRelacs(QMainWindow):
|
|||||||
daq_toolbar.addAction(self._daq_connectaction)
|
daq_toolbar.addAction(self._daq_connectaction)
|
||||||
daq_toolbar.addAction(self._daq_disconnectaction)
|
daq_toolbar.addAction(self._daq_disconnectaction)
|
||||||
daq_toolbar.addAction(self._daq_calibaction)
|
daq_toolbar.addAction(self._daq_calibaction)
|
||||||
|
daq_toolbar.addAction(self._run_action)
|
||||||
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar)
|
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar)
|
||||||
|
|
||||||
repro_toolbar = QToolBar("Repros")
|
repro_toolbar = QToolBar("Repros")
|
||||||
@@ -148,7 +163,7 @@ class PyRelacs(QMainWindow):
|
|||||||
lambda checked, n=rep, f=fn: self.run_repro(n, f)
|
lambda checked, n=rep, f=fn: self.run_repro(n, f)
|
||||||
)
|
)
|
||||||
repro_toolbar.addAction(repro_action)
|
repro_toolbar.addAction(repro_action)
|
||||||
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, repro_toolbar)
|
self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar)
|
||||||
|
|
||||||
def create_buttons(self):
|
def create_buttons(self):
|
||||||
self.daq_connect_button = QPushButton("Connect Daq")
|
self.daq_connect_button = QPushButton("Connect Daq")
|
||||||
@@ -163,11 +178,50 @@ class PyRelacs(QMainWindow):
|
|||||||
self.plot_calibration_button.setCheckable(True)
|
self.plot_calibration_button.setCheckable(True)
|
||||||
self.plot_calibration_button.clicked.connect(self.calibration_plot.plot)
|
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):
|
def connect_dac(self):
|
||||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||||
try:
|
try:
|
||||||
self.daq_device = uldaq.DaqDevice(devices[0])
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||||
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||||
|
self.daq_device.connect()
|
||||||
|
log.debug("connected")
|
||||||
except IndexError:
|
except IndexError:
|
||||||
log.error("DAQ is not connected")
|
log.error("DAQ is not connected")
|
||||||
log.error("Please connect a DAQ device to the system")
|
log.error("Please connect a DAQ device to the system")
|
||||||
@@ -179,9 +233,6 @@ class PyRelacs(QMainWindow):
|
|||||||
except uldaq.ul_exception.ULException as e:
|
except uldaq.ul_exception.ULException as e:
|
||||||
log.error(f"Could not Connect to DAQ: {e}")
|
log.error(f"Could not Connect to DAQ: {e}")
|
||||||
self.daq_connect_button.setDisabled(True)
|
self.daq_connect_button.setDisabled(True)
|
||||||
else:
|
|
||||||
log.debug("Already handeld the error")
|
|
||||||
pass
|
|
||||||
|
|
||||||
def disconnect_dac(self):
|
def disconnect_dac(self):
|
||||||
try:
|
try:
|
||||||
@@ -196,7 +247,7 @@ class PyRelacs(QMainWindow):
|
|||||||
|
|
||||||
def run_repro(self, n, fn):
|
def run_repro(self, n, fn):
|
||||||
self.text.appendPlainText(f"started Repro {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.result.connect(self.print_output)
|
||||||
worker.signals.finished.connect(self.thread_complete)
|
worker.signals.finished.connect(self.thread_complete)
|
||||||
worker.signals.progress.connect(self.progress_fn)
|
worker.signals.progress.connect(self.progress_fn)
|
||||||
|
|||||||
98
test/test_buffer.py
Normal file
98
test/test_buffer.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import pytest
|
||||||
|
import numpy as np
|
||||||
|
from IPython import embed
|
||||||
|
|
||||||
|
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||||
|
|
||||||
|
|
||||||
|
def test_init():
|
||||||
|
buff = CircBuffer(1000, 2)
|
||||||
|
|
||||||
|
assert buff.size == 1000
|
||||||
|
assert buff.channel_count == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_hasvalue():
|
||||||
|
buff = CircBuffer(1000, 2)
|
||||||
|
|
||||||
|
assert buff.has_value(0, 0) == False
|
||||||
|
assert buff.has_value(-1, 0) == False
|
||||||
|
|
||||||
|
buff.append(10, 0)
|
||||||
|
|
||||||
|
assert buff.write_index(0) == 1
|
||||||
|
assert buff.write_index(1) == 0
|
||||||
|
assert buff.has_value(0, 0) == True
|
||||||
|
assert buff.has_value(0, 1) == False
|
||||||
|
|
||||||
|
buff.append(10, 1)
|
||||||
|
assert buff.write_index(1) == 1
|
||||||
|
assert buff.has_value(0, 1) == True
|
||||||
|
|
||||||
|
for i in range(1100):
|
||||||
|
buff.append(i, 0)
|
||||||
|
buff.append(i, 1)
|
||||||
|
assert buff.write_index(0) == buff.write_index(1)
|
||||||
|
assert buff.has_value(0, 0) == True
|
||||||
|
assert buff.has_value(0, 1) == True
|
||||||
|
assert buff.has_value(buff.write_index(0), 0) == True
|
||||||
|
assert buff.has_value(buff.write_index(1), 1) == True
|
||||||
|
|
||||||
|
|
||||||
|
def test_validrange():
|
||||||
|
buff = CircBuffer(1000, 2)
|
||||||
|
|
||||||
|
# without any values the range is (0, 0)
|
||||||
|
assert buff.valid_range() == (0, 0)
|
||||||
|
|
||||||
|
buff.append(0, 0)
|
||||||
|
assert buff.valid_range() == (0, 1)
|
||||||
|
|
||||||
|
for i in range(100):
|
||||||
|
buff.append(i, 0)
|
||||||
|
|
||||||
|
assert buff.valid_range() == (0, 101)
|
||||||
|
|
||||||
|
for i in range(1000):
|
||||||
|
buff.append(i, 0)
|
||||||
|
|
||||||
|
assert buff.valid_range() == (0, 1000)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get():
|
||||||
|
buff = CircBuffer(1000, 2)
|
||||||
|
|
||||||
|
# with no items written to the buffer
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
item = buff.get(index=-1)
|
||||||
|
|
||||||
|
buff.append(10, 0)
|
||||||
|
item = buff.get(index=-1)
|
||||||
|
assert item == 10
|
||||||
|
|
||||||
|
# Check if index is not written jet
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
item = buff.get(index=10)
|
||||||
|
|
||||||
|
for i in range(1000):
|
||||||
|
buff.append(i, 0)
|
||||||
|
item = buff.get(index=-1)
|
||||||
|
# the first item should be 999.0 because of we append a value in the earlier test
|
||||||
|
assert item == 999.0
|
||||||
|
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
item = buff.get(10001)
|
||||||
|
|
||||||
|
|
||||||
|
def test_read():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_write():
|
||||||
|
buff = CircBuffer(1000, 2)
|
||||||
|
|
||||||
|
samplecount = 1000
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_get()
|
||||||
Reference in New Issue
Block a user