Compare commits
3 Commits
e43bb16bd4
...
cd936b1ed1
Author | SHA1 | Date | |
---|---|---|---|
cd936b1ed1 | |||
85fd70f8ca | |||
8910305262 |
@ -4,7 +4,7 @@ from IPython import embed
|
|||||||
|
|
||||||
|
|
||||||
class CircBuffer:
|
class CircBuffer:
|
||||||
def __init__(self, size: int, channels: int = 1, samplerate: float = 40_000):
|
def __init__(self, size: int, channels: int = 1, samplerate: int = 40_000):
|
||||||
self.__size = size
|
self.__size = size
|
||||||
self.__channels = channels
|
self.__channels = channels
|
||||||
self.__samplereate = samplerate
|
self.__samplereate = samplerate
|
||||||
@ -14,6 +14,7 @@ class CircBuffer:
|
|||||||
self.__index = [0 for i in range(channels)]
|
self.__index = [0 for i in range(channels)]
|
||||||
self.__is_full = [False for i in range(channels)]
|
self.__is_full = [False for i in range(channels)]
|
||||||
self.__totalcount = [0 for i in range(channels)]
|
self.__totalcount = [0 for i in range(channels)]
|
||||||
|
self.__time = [0.0 for i in range(channels)]
|
||||||
self.__overflows = [0 for i in range(channels)]
|
self.__overflows = [0 for i in range(channels)]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -41,6 +42,7 @@ class CircBuffer:
|
|||||||
self.__buffer[channel, self.write_index(channel)] = item
|
self.__buffer[channel, self.write_index(channel)] = item
|
||||||
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
||||||
self.__totalcount[channel] += 1
|
self.__totalcount[channel] += 1
|
||||||
|
self.__time[channel] += 1 / self.__samplereate
|
||||||
if self.__index[channel] == 0:
|
if self.__index[channel] == 0:
|
||||||
self.__is_full[channel] = True
|
self.__is_full[channel] = True
|
||||||
self.__overflows[channel] += 1
|
self.__overflows[channel] += 1
|
||||||
|
@ -14,42 +14,59 @@ faulthandler.enable()
|
|||||||
|
|
||||||
|
|
||||||
class DaqProducer:
|
class DaqProducer:
|
||||||
def __init__(self, buffer: CircBuffer, device: uldaq.DaqDevice):
|
def __init__(
|
||||||
|
self, buffer: CircBuffer, device: uldaq.DaqDevice, channels: list[int]
|
||||||
|
):
|
||||||
self.buffer = buffer
|
self.buffer = buffer
|
||||||
self.device = device
|
self.device = device
|
||||||
self.ai_device = self.device.get_ai_device()
|
self.ai_device = self.device.get_ai_device()
|
||||||
|
self.channels = channels
|
||||||
|
|
||||||
def read_analog_continously(
|
def read_analog_continously(
|
||||||
self,
|
self,
|
||||||
samplerate: float = 20,
|
|
||||||
*args,
|
*args,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
log.debug("starting acquisition")
|
log.debug("starting acquisition")
|
||||||
channel = self.buffer.channel_count
|
if self.channels[0] == self.channels[1]:
|
||||||
size = self.buffer.size
|
channel_range = np.arange(1)
|
||||||
data_in = uldaq.create_float_buffer(channel, size)
|
else:
|
||||||
|
channel_range = np.arange(self.channels[0], self.channels[1] + 1)
|
||||||
|
|
||||||
|
assert channel_range.size == self.buffer.channel_count, ValueError(
|
||||||
|
f"Missmatch in channel count,\n daq_channel: "
|
||||||
|
f"{channel_range.size}\n buffer_channel: {self.buffer.channel_count}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# let the buffer for the daq device hold 5 seconds of data
|
||||||
|
daq_buffer_size = self.buffer.samplerate * 5
|
||||||
|
|
||||||
|
data_in = uldaq.create_float_buffer(channel_range.size, daq_buffer_size)
|
||||||
|
log.debug(f"Buffersize for daq {len(data_in)}")
|
||||||
|
log.debug(f"Buffersize {self.buffer.size}")
|
||||||
|
|
||||||
# BUG: Check for multiple channels
|
|
||||||
er = self.ai_device.a_in_scan(
|
er = self.ai_device.a_in_scan(
|
||||||
1,
|
self.channels[0],
|
||||||
1,
|
self.channels[1],
|
||||||
uldaq.AiInputMode.SINGLE_ENDED,
|
uldaq.AiInputMode.SINGLE_ENDED,
|
||||||
uldaq.Range.BIP10VOLTS,
|
uldaq.Range.BIP10VOLTS,
|
||||||
self.buffer.size,
|
daq_buffer_size,
|
||||||
samplerate,
|
self.buffer.samplerate,
|
||||||
uldaq.ScanOption.CONTINUOUS,
|
uldaq.ScanOption.CONTINUOUS,
|
||||||
uldaq.AInScanFlag.DEFAULT,
|
uldaq.AInScanFlag.DEFAULT,
|
||||||
data=data_in,
|
data=data_in,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
chunk_size = int(daq_buffer_size / 10)
|
||||||
|
wrote_chunk = False
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
daq_status = uldaq.ScanStatus.IDLE
|
daq_status = uldaq.ScanStatus.IDLE
|
||||||
while daq_status == uldaq.ScanStatus.IDLE:
|
while daq_status == uldaq.ScanStatus.IDLE:
|
||||||
daq_status = self.ai_device.get_scan_status()[0]
|
daq_status = self.ai_device.get_scan_status()[0]
|
||||||
|
while daq_status != uldaq.ScanStatus.IDLE:
|
||||||
prev_count = 0
|
prev_count = 0
|
||||||
prev_index = 0
|
prev_index = 0
|
||||||
while daq_status != uldaq.ScanStatus.IDLE:
|
|
||||||
while time.time() - start_time < 10:
|
while time.time() - start_time < 10:
|
||||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||||
# The index into the data buffer immediately following the last sample transferred.
|
# The index into the data buffer immediately following the last sample transferred.
|
||||||
@ -59,19 +76,83 @@ class DaqProducer:
|
|||||||
# The number of samples per channel transferred since the scan started
|
# The number of samples per channel transferred since the scan started
|
||||||
channel_samples = transfer_status.current_scan_count
|
channel_samples = transfer_status.current_scan_count
|
||||||
|
|
||||||
if current_index > prev_index:
|
new_data_count = total_samples - prev_count
|
||||||
|
# check if counts if new data is bigger than the buffer
|
||||||
|
# if that happends stop the acquisition
|
||||||
|
if new_data_count > len(data_in):
|
||||||
|
self.ai_device.scan_stop()
|
||||||
|
log.error("A Buffer overrun occurred")
|
||||||
|
break
|
||||||
|
|
||||||
|
if new_data_count > chunk_size:
|
||||||
|
wrote_chunk = True
|
||||||
|
# index wraps around the buffer
|
||||||
|
if prev_index + chunk_size > len(data_in) - 1:
|
||||||
|
log.debug("Chunk wraps around buffersize")
|
||||||
|
first_chunk = len(data_in) - prev_index
|
||||||
|
[
|
||||||
|
self.buffer.append(data_in[prev_index + i])
|
||||||
|
for i in range(first_chunk)
|
||||||
|
]
|
||||||
|
second_chunk = chunk_size - first_chunk
|
||||||
|
[
|
||||||
|
self.buffer.append(data_in[i])
|
||||||
|
for i in range(second_chunk)
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
log.debug("Writing chunk to buffer")
|
||||||
|
[
|
||||||
|
self.buffer.append(data_in[prev_index + i])
|
||||||
|
for i in range(chunk_size)
|
||||||
|
]
|
||||||
|
|
||||||
self.buffer.append(data_in[current_index])
|
self.buffer.append(data_in[current_index])
|
||||||
prev_index = current_index
|
|
||||||
|
if total_samples - prev_count > len(data_in):
|
||||||
|
self.ai_device.scan_stop()
|
||||||
|
log.error("A Buffer overrun occurred")
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
wrote_chunk = False
|
||||||
|
if wrote_chunk:
|
||||||
|
prev_count += chunk_size
|
||||||
|
prev_index += chunk_size
|
||||||
|
prev_index %= daq_buffer_size
|
||||||
|
|
||||||
self.ai_device.scan_stop()
|
self.ai_device.scan_stop()
|
||||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||||
|
current_index = transfer_status.current_index
|
||||||
log.debug(daq_status)
|
log.debug(daq_status)
|
||||||
|
|
||||||
log.debug(transfer_status.current_index)
|
log.debug(transfer_status.current_index)
|
||||||
log.debug(transfer_status.current_total_count)
|
log.debug(transfer_status.current_total_count)
|
||||||
log.debug(transfer_status.current_scan_count)
|
log.debug(transfer_status.current_scan_count)
|
||||||
|
log.debug(self.buffer.totocount())
|
||||||
|
log.debug("Appending last chunk")
|
||||||
|
|
||||||
|
if prev_index + chunk_size > len(data_in) - 1:
|
||||||
|
log.debug("Chunk wraps around buffersize")
|
||||||
|
first_chunk = len(data_in) - prev_index
|
||||||
|
[
|
||||||
|
self.buffer.append(data_in[prev_index + i])
|
||||||
|
for i in range(first_chunk)
|
||||||
|
]
|
||||||
|
second_chunk = chunk_size - first_chunk
|
||||||
|
[self.buffer.append(data_in[i]) for i in range(second_chunk)]
|
||||||
|
else:
|
||||||
|
log.debug("Writing chunk to buffer")
|
||||||
|
[
|
||||||
|
self.buffer.append(data_in[prev_index + i])
|
||||||
|
for i in range(chunk_size)
|
||||||
|
]
|
||||||
|
|
||||||
|
self.buffer.append(data_in[current_index])
|
||||||
log.info("stopping")
|
log.info("stopping")
|
||||||
|
|
||||||
break
|
break
|
||||||
break
|
break
|
||||||
|
return "Done. "
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -84,6 +165,6 @@ if __name__ == "__main__":
|
|||||||
raise e
|
raise e
|
||||||
daq_device.connect()
|
daq_device.connect()
|
||||||
|
|
||||||
buf = CircBuffer(size=300)
|
buf = CircBuffer(size=1_000_000, samplerate=100)
|
||||||
producer = DaqProducer(buf, daq_device)
|
producer = DaqProducer(buf, daq_device, [1, 1])
|
||||||
producer.read_analog_continously()
|
producer.read_analog_continously()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from pathlib import Path as path
|
from pathlib import Path as path
|
||||||
|
from re import I
|
||||||
|
|
||||||
from PyQt6.QtGui import QAction, QIcon, QKeySequence
|
from PyQt6.QtGui import QAction, QIcon, QKeySequence
|
||||||
from PyQt6.QtCore import Qt, QSize, QThreadPool, QTimer
|
from PyQt6.QtCore import Qt, QSize, QThreadPool, QTimer
|
||||||
@ -72,9 +73,9 @@ class PyRelacs(QMainWindow):
|
|||||||
widget.setLayout(layout)
|
widget.setLayout(layout)
|
||||||
self.setCentralWidget(widget)
|
self.setCentralWidget(widget)
|
||||||
|
|
||||||
self.buffer = CircBuffer(size=500)
|
self.buffer = CircBuffer(size=1_000_000, samplerate=20)
|
||||||
self.connect_dac()
|
self.connect_dac()
|
||||||
self.daq_producer = DaqProducer(self.buffer, self.daq_device)
|
self.daq_producer = DaqProducer(self.buffer, self.daq_device, [1, 1])
|
||||||
self.plot_daq()
|
self.plot_daq()
|
||||||
|
|
||||||
def create_actions(self):
|
def create_actions(self):
|
||||||
@ -190,6 +191,7 @@ class PyRelacs(QMainWindow):
|
|||||||
self.data = list(range(10))
|
self.data = list(range(10))
|
||||||
self.line = self.daq_plot.plot(self.time, self.data, pen=pen)
|
self.line = self.daq_plot.plot(self.time, self.data, pen=pen)
|
||||||
|
|
||||||
|
self.item_index = 0
|
||||||
self.timer = QTimer()
|
self.timer = QTimer()
|
||||||
self.timer.setInterval(50)
|
self.timer.setInterval(50)
|
||||||
self.timer.timeout.connect(self.update_plot)
|
self.timer.timeout.connect(self.update_plot)
|
||||||
@ -200,10 +202,16 @@ class PyRelacs(QMainWindow):
|
|||||||
self.time = self.time[1:]
|
self.time = self.time[1:]
|
||||||
self.time.append(self.time[-1] + 1)
|
self.time.append(self.time[-1] + 1)
|
||||||
self.data = self.data[1:]
|
self.data = self.data[1:]
|
||||||
|
log.debug(self.buffer.totocount())
|
||||||
|
if self.buffer.totocount() > 20:
|
||||||
try:
|
try:
|
||||||
item = self.buffer.get(self.buffer.write_index() - 1)
|
log.debug(self.item_index)
|
||||||
|
item = self.buffer.get(self.item_index)
|
||||||
|
self.item_index += 1
|
||||||
except IndexError:
|
except IndexError:
|
||||||
item = 0
|
item = 0
|
||||||
|
else:
|
||||||
|
item = 0
|
||||||
|
|
||||||
self.data.append(item)
|
self.data.append(item)
|
||||||
self.line.setData(self.time, self.data)
|
self.line.setData(self.time, self.data)
|
||||||
@ -236,10 +244,8 @@ class PyRelacs(QMainWindow):
|
|||||||
|
|
||||||
def disconnect_dac(self):
|
def disconnect_dac(self):
|
||||||
try:
|
try:
|
||||||
log.debug(f"{self.daq_device}")
|
|
||||||
self.daq_device.disconnect()
|
self.daq_device.disconnect()
|
||||||
self.daq_device.release()
|
self.daq_device.release()
|
||||||
log.debug(f"{self.daq_device}")
|
|
||||||
self.daq_disconnect_button.setDisabled(True)
|
self.daq_disconnect_button.setDisabled(True)
|
||||||
self.daq_connect_button.setEnabled(True)
|
self.daq_connect_button.setEnabled(True)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -260,6 +266,7 @@ class PyRelacs(QMainWindow):
|
|||||||
def on_exit(self):
|
def on_exit(self):
|
||||||
log.info("exit button!")
|
log.info("exit button!")
|
||||||
self.add_to_textfield("exiting")
|
self.add_to_textfield("exiting")
|
||||||
|
self.disconnect_dac()
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def on_about(self, e):
|
def on_about(self, e):
|
||||||
|
Loading…
Reference in New Issue
Block a user