diff --git a/pyrelacs/dataio/daq_producer.py b/pyrelacs/dataio/daq_producer.py new file mode 100644 index 0000000..b24f7f5 --- /dev/null +++ b/pyrelacs/dataio/daq_producer.py @@ -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()