Compare commits
6 Commits
9cd6aadb3b
...
b912159b76
Author | SHA1 | Date | |
---|---|---|---|
b912159b76 | |||
1a2185d5e4 | |||
deb60fa84c | |||
1579c947c9 | |||
d6e2f8c5ba | |||
8e73b2ae1f |
26
pyrelacs/repros/attcs3310.py
Normal file
26
pyrelacs/repros/attcs3310.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
import uldaq
|
||||||
|
from IPython import embed
|
||||||
|
from pyrelacs.repros.repos import MccDac
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class Attenuator(MccDac):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
SAMPLERATE = 40_000.0
|
||||||
|
DURATION = 5
|
||||||
|
AMPLITUDE = 1
|
||||||
|
SINFREQ = 1
|
||||||
|
|
||||||
|
att = Attenuator()
|
||||||
|
# att.set_attenuation_level(db_channel1=5, db_channel2=5)
|
||||||
|
|
||||||
|
att.check_attenuator()
|
@ -1,14 +1,18 @@
|
|||||||
import ctypes
|
import ctypes
|
||||||
|
import faulthandler
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import uldaq
|
import uldaq
|
||||||
from IPython import embed
|
from IPython import embed
|
||||||
from pyrelacs.repros.repos import MccDac
|
|
||||||
from pyrelacs.util.logging import config_logging
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from pyrelacs.repros.mccdac import MccDac
|
||||||
|
from pyrelacs.util.logging import config_logging
|
||||||
|
|
||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
faulthandler.enable()
|
||||||
|
|
||||||
|
|
||||||
class Calibration(MccDac):
|
class Calibration(MccDac):
|
||||||
@ -17,11 +21,11 @@ class Calibration(MccDac):
|
|||||||
|
|
||||||
def run_calibration(self):
|
def run_calibration(self):
|
||||||
# Stimulus
|
# Stimulus
|
||||||
time = np.arange(0, DURATION, 1 / SAMPLERATE)
|
t = np.arange(0, DURATION, 1 / SAMPLERATE)
|
||||||
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * time)
|
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * t)
|
||||||
# sending stimulus
|
# sending stimulus
|
||||||
|
|
||||||
stim, ao_device = self.write_analog_dac(
|
self.write_analog(
|
||||||
data, [0, 0], SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
data, [0, 0], SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,23 +33,66 @@ class Calibration(MccDac):
|
|||||||
# [0, 1], DURATION, SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
# [0, 1], DURATION, SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
||||||
# )
|
# )
|
||||||
|
|
||||||
# trigger the 0 channel to start the aqcuisition
|
self.diggital_trigger()
|
||||||
self.write_bit(channel=0, bit=1)
|
|
||||||
try:
|
try:
|
||||||
ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
self.ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||||
except uldaq.ul_exception.ULException:
|
except uldaq.ul_exception.ULException:
|
||||||
log.debug("Operation timed out")
|
log.debug("Operation timed out")
|
||||||
|
# reset the diggital trigger
|
||||||
self.write_bit(channel=0, bit=0)
|
self.write_bit(channel=0, bit=0)
|
||||||
self.disconnect_dac()
|
time.sleep(1)
|
||||||
self.connect_dac()
|
self.set_analog_to_zero()
|
||||||
|
|
||||||
|
def check_amplitude(self):
|
||||||
|
# write to ananlog 1
|
||||||
|
t = np.arange(0, DURATION, 1 / SAMPLERATE)
|
||||||
|
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * t)
|
||||||
|
|
||||||
|
# sending stimulus
|
||||||
|
log.debug(f"{data}, {data.shape}")
|
||||||
|
# self.set_attenuation_level(db_channel1=0.0, db_channel2=0.0)
|
||||||
|
# self.set_analog_to_zero()
|
||||||
|
# time.sleep(1)
|
||||||
|
log.debug(self.ao_device)
|
||||||
|
embed()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
self.write_analog(
|
||||||
|
data,
|
||||||
|
[1, 1],
|
||||||
|
SAMPLERATE,
|
||||||
|
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||||
|
)
|
||||||
|
# data_channel_one = self.read_analog(
|
||||||
|
# [0, 0], DURATION, SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
||||||
|
# )
|
||||||
|
log.debug(self.ao_device)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
self.diggital_trigger()
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
|
time.sleep(1)
|
||||||
self.set_analog_to_zero()
|
self.set_analog_to_zero()
|
||||||
|
except uldaq.ul_exception.ULException:
|
||||||
|
log.debug("Operation timed out")
|
||||||
|
# reset the diggital trigger
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
|
time.sleep(1)
|
||||||
|
self.set_analog_to_zero()
|
||||||
|
self.disconnect_dac()
|
||||||
|
embed()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
SAMPLERATE = 40_000.0
|
SAMPLERATE = 40_000.0
|
||||||
DURATION = 10
|
DURATION = 5
|
||||||
AMPLITUDE = 1
|
AMPLITUDE = 1
|
||||||
SINFREQ = 100
|
SINFREQ = 10
|
||||||
|
|
||||||
daq_input = Calibration()
|
daq_input = Calibration()
|
||||||
# daq_input.run_calibration()
|
# daq_input.run_calibration()
|
||||||
daq_input.check_attenuator()
|
daq_input.check_attenuator()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from ctypes import Array, c_double
|
from ctypes import Array, c_double
|
||||||
import time
|
import time
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from IPython import embed
|
from IPython import embed
|
||||||
import numpy.typing as npt
|
import numpy.typing as npt
|
||||||
import uldaq
|
import uldaq
|
||||||
@ -25,6 +26,10 @@ class MccDac:
|
|||||||
self.dio_device = self.daq_device.get_dio_device()
|
self.dio_device = self.daq_device.get_dio_device()
|
||||||
log.debug("Connected")
|
log.debug("Connected")
|
||||||
|
|
||||||
|
# log.debug("Activate Attenuator")
|
||||||
|
# self.set_attenuation_level(db_channel1=0.0, db_channel2=0.0)
|
||||||
|
# self.set_analog_to_zero()
|
||||||
|
|
||||||
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)
|
||||||
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")
|
||||||
@ -38,7 +43,7 @@ class MccDac:
|
|||||||
self.dio_device = self.daq_device.get_dio_device()
|
self.dio_device = self.daq_device.get_dio_device()
|
||||||
log.debug("Connected")
|
log.debug("Connected")
|
||||||
|
|
||||||
def read_analog_daq(
|
def read_analog(
|
||||||
self,
|
self,
|
||||||
channels: list[int],
|
channels: list[int],
|
||||||
duration: int,
|
duration: int,
|
||||||
@ -72,7 +77,7 @@ class MccDac:
|
|||||||
|
|
||||||
return data_analog_input
|
return data_analog_input
|
||||||
|
|
||||||
def write_analog_dac(
|
def write_analog(
|
||||||
self,
|
self,
|
||||||
data: Union[list, npt.NDArray],
|
data: Union[list, npt.NDArray],
|
||||||
channels: list[int],
|
channels: list[int],
|
||||||
@ -81,66 +86,32 @@ class MccDac:
|
|||||||
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
||||||
AOutScanFlag: uldaq.AOutScanFlag = uldaq.AOutScanFlag.DEFAULT,
|
AOutScanFlag: uldaq.AOutScanFlag = uldaq.AOutScanFlag.DEFAULT,
|
||||||
):
|
):
|
||||||
"""
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
data : Union[list, npt.NDArray]
|
|
||||||
|
|
||||||
channels : list[int]
|
|
||||||
|
|
||||||
duration : int
|
|
||||||
|
|
||||||
samplerate : float
|
|
||||||
|
|
||||||
AiInputMode : uldaq.AiInputMode
|
|
||||||
|
|
||||||
Range : uldaq.Range
|
|
||||||
|
|
||||||
ScanOption : uldaq.ScanOption
|
|
||||||
|
|
||||||
AInScanFlag : uldaq.AOutScanFlag
|
|
||||||
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Array[c_double]
|
|
||||||
ao_device
|
|
||||||
"""
|
|
||||||
|
|
||||||
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
||||||
|
|
||||||
if channels[0] != channels[1]:
|
log.debug(f"{len(data)}, {type(data)}")
|
||||||
buffer_len_channels = 2
|
|
||||||
else:
|
|
||||||
buffer_len_channels = 1
|
|
||||||
|
|
||||||
buffer = c_double * (len(data) * buffer_len_channels)
|
buffer = c_double * len(data)
|
||||||
assert len(data) != len(data) * buffer_len_channels, log.error(
|
|
||||||
"Data must be duplicated for both channels data[len(data_channel) + len(data_channel1)]..."
|
|
||||||
)
|
|
||||||
data_analog_output = buffer(*data)
|
data_analog_output = buffer(*data)
|
||||||
|
|
||||||
log.debug(f"Created C_double data {data_analog_output}")
|
log.debug(f"Created C_double data {data_analog_output}")
|
||||||
try:
|
|
||||||
err = self.ao_device.a_out_scan(
|
|
||||||
channels[0],
|
|
||||||
channels[1],
|
|
||||||
Range,
|
|
||||||
int(len(data)),
|
|
||||||
samplerate,
|
|
||||||
ScanOption,
|
|
||||||
AOutScanFlag,
|
|
||||||
data_analog_output,
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"{e}")
|
|
||||||
self.set_analog_to_zero()
|
|
||||||
self.disconnect_dac()
|
|
||||||
|
|
||||||
# ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 11)
|
log.info(self.ao_device)
|
||||||
|
|
||||||
return data_analog_output, self.ao_device
|
err = self.ao_device.a_out_scan(
|
||||||
|
channels[0],
|
||||||
|
channels[1],
|
||||||
|
Range,
|
||||||
|
int(len(data)),
|
||||||
|
samplerate,
|
||||||
|
ScanOption,
|
||||||
|
AOutScanFlag,
|
||||||
|
data_analog_output,
|
||||||
|
)
|
||||||
|
# self.diggital_trigger()
|
||||||
|
# self.ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 11)
|
||||||
|
# except Exception as e:
|
||||||
|
# print(f"{e}")
|
||||||
|
# self.set_analog_to_zero()
|
||||||
|
# self.disconnect_dac()
|
||||||
|
|
||||||
def set_analog_to_zero(self, channels: list[int] = [0, 1]):
|
def set_analog_to_zero(self, channels: list[int] = [0, 1]):
|
||||||
try:
|
try:
|
||||||
@ -159,15 +130,18 @@ class MccDac:
|
|||||||
log.error("disconnection dac")
|
log.error("disconnection dac")
|
||||||
self.disconnect_dac()
|
self.disconnect_dac()
|
||||||
|
|
||||||
def diggital_trigger(self) -> None:
|
def diggital_trigger(self, channel: int = 0) -> None:
|
||||||
if not self.read_bit(channel=0):
|
bit_channel = self.read_bit(channel)
|
||||||
self.write_bit(channel=0, bit=1)
|
log.debug(bit_channel)
|
||||||
|
if not bit_channel:
|
||||||
|
self.write_bit(channel, 1)
|
||||||
else:
|
else:
|
||||||
self.write_bit(channel=0, bit=0)
|
self.write_bit(channel, 0)
|
||||||
time.time_ns()
|
time.sleep(1)
|
||||||
self.write_bit(channel=0, bit=1)
|
self.write_bit(channel, 1)
|
||||||
|
|
||||||
def write_bit(self, channel: int = 0, bit: int = 1) -> None:
|
def write_bit(self, channel: int = 0, bit: int = 1) -> None:
|
||||||
|
log.debug(self.dio_device)
|
||||||
self.dio_device.d_config_bit(
|
self.dio_device.d_config_bit(
|
||||||
uldaq.DigitalPortType.AUXPORT, channel, uldaq.DigitalDirection.OUTPUT
|
uldaq.DigitalPortType.AUXPORT, channel, uldaq.DigitalDirection.OUTPUT
|
||||||
)
|
)
|
||||||
@ -176,6 +150,7 @@ class MccDac:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def read_bit(self, channel: int = 0):
|
def read_bit(self, channel: int = 0):
|
||||||
|
log.debug(self.dio_device)
|
||||||
bit = self.dio_device.d_bit_in(uldaq.DigitalPortType.AUXPORT, channel)
|
bit = self.dio_device.d_bit_in(uldaq.DigitalPortType.AUXPORT, channel)
|
||||||
return bit
|
return bit
|
||||||
|
|
||||||
@ -209,14 +184,6 @@ class MccDac:
|
|||||||
)
|
)
|
||||||
return data_digital_input
|
return data_digital_input
|
||||||
|
|
||||||
def write_byte(self, channel: int, byte: list[int]):
|
|
||||||
self.dio_device.d_config_bit(
|
|
||||||
uldaq.DigitalPortType.AUXPORT, channel, uldaq.DigitalDirection.OUTPUT
|
|
||||||
)
|
|
||||||
for bit in byte:
|
|
||||||
self.dio_device.d_bit_out(uldaq.DigitalPortType.AUXPORT, channel, bit)
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def disconnect_dac(self):
|
def disconnect_dac(self):
|
||||||
self.daq_device.disconnect()
|
self.daq_device.disconnect()
|
||||||
self.daq_device.release()
|
self.daq_device.release()
|
@ -13,22 +13,13 @@ log = config_logging()
|
|||||||
class Output_daq(MccDac):
|
class Output_daq(MccDac):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
# devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
|
||||||
# self.daq_device = uldaq.DaqDevice(devices[0])
|
|
||||||
# self.daq_device.connect()
|
|
||||||
|
|
||||||
def write_daq(self):
|
def write_daq(self):
|
||||||
log.debug("running repro")
|
log.debug("running repro")
|
||||||
time = np.arange(0, 10, 1 / 30_000.0)
|
time = np.arange(0, 10, 1 / 30_000.0)
|
||||||
data = 2 * np.sin(2 * np.pi * 1 * time)
|
data = 1 * np.sin(2 * np.pi * 1 * time)
|
||||||
self.send_analog_dac(
|
self.write_analog(data, [0, 0], 30_000, ScanOption=uldaq.ScanOption.EXTTRIGGER)
|
||||||
data, [0, 0], 30_000, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
self.diggital_trigger()
|
||||||
)
|
|
||||||
|
|
||||||
def trigger(self):
|
|
||||||
self.digital_trigger(1)
|
|
||||||
self.daq_device.disconnect()
|
|
||||||
self.daq_device.release()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user