Compare commits
6 Commits
refactorin
...
9cd6aadb3b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cd6aadb3b | ||
|
|
9f7d28ccf8 | ||
|
|
3865bb8216 | ||
|
|
2317fd73c8 | ||
|
|
fbb4d3b81d | ||
|
|
7d02cb994f |
@@ -1,8 +1,9 @@
|
|||||||
import ctypes
|
import ctypes
|
||||||
|
import time
|
||||||
|
|
||||||
import uldaq
|
import uldaq
|
||||||
from IPython import embed
|
from IPython import embed
|
||||||
from pyrelacs.repros.repos import Repos
|
from pyrelacs.repros.repos import MccDac
|
||||||
from pyrelacs.util.logging import config_logging
|
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
|
||||||
@@ -10,7 +11,7 @@ import matplotlib.pyplot as plt
|
|||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
class Calibration(Repos):
|
class Calibration(MccDac):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
@@ -19,24 +20,32 @@ class Calibration(Repos):
|
|||||||
time = np.arange(0, DURATION, 1 / SAMPLERATE)
|
time = np.arange(0, DURATION, 1 / SAMPLERATE)
|
||||||
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * time)
|
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * time)
|
||||||
# sending stimulus
|
# sending stimulus
|
||||||
stim, ao_device = self.send_analog_dac(
|
|
||||||
|
stim, ao_device = self.write_analog_dac(
|
||||||
data, [0, 0], SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
data, [0, 0], SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
||||||
)
|
)
|
||||||
read_data = self.read_analog_daq(
|
|
||||||
[0, 1], DURATION, SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
# read_data = self.read_analog_daq(
|
||||||
)
|
# [0, 1], DURATION, SAMPLERATE, ScanOption=uldaq.ScanOption.EXTTRIGGER
|
||||||
self.digital_trigger()
|
# )
|
||||||
ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 11)
|
|
||||||
self.digital_trigger(data=0)
|
# trigger the 0 channel to start the aqcuisition
|
||||||
|
self.write_bit(channel=0, bit=1)
|
||||||
|
try:
|
||||||
|
ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||||
|
except uldaq.ul_exception.ULException:
|
||||||
|
log.debug("Operation timed out")
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
self.disconnect_dac()
|
self.disconnect_dac()
|
||||||
embed()
|
self.connect_dac()
|
||||||
exit()
|
self.set_analog_to_zero()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
SAMPLERATE = 40_000.0
|
SAMPLERATE = 40_000.0
|
||||||
DURATION = 5
|
DURATION = 10
|
||||||
AMPLITUDE = 3
|
AMPLITUDE = 1
|
||||||
SINFREQ = 1
|
SINFREQ = 100
|
||||||
daq_input = Calibration()
|
daq_input = Calibration()
|
||||||
daq_input.run_calibration()
|
# daq_input.run_calibration()
|
||||||
|
daq_input.check_attenuator()
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ import uldaq
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
from pyrelacs.util.logging import config_logging
|
from pyrelacs.util.logging import config_logging
|
||||||
from .repos import Repos
|
from .repos import MccDac
|
||||||
|
|
||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
class ReadData(Repos):
|
class ReadData(MccDac):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import ctypes
|
|||||||
|
|
||||||
import uldaq
|
import uldaq
|
||||||
from IPython import embed
|
from IPython import embed
|
||||||
from pyrelacs.repros.repos import Repos
|
from pyrelacs.repros.repos import MccDac
|
||||||
from pyrelacs.util.logging import config_logging
|
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
|
||||||
@@ -10,7 +10,7 @@ import matplotlib.pyplot as plt
|
|||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
class Output_daq(Repos):
|
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)
|
# devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from ctypes import Array, c_double
|
from ctypes import Array, c_double
|
||||||
|
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
|
||||||
@@ -10,7 +11,7 @@ from pyrelacs.util.logging import config_logging
|
|||||||
log = config_logging()
|
log = config_logging()
|
||||||
|
|
||||||
|
|
||||||
class Repos:
|
class MccDac:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
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")
|
||||||
@@ -19,6 +20,22 @@ class Repos:
|
|||||||
exit(1)
|
exit(1)
|
||||||
self.daq_device = uldaq.DaqDevice(devices[0])
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||||
self.daq_device.connect()
|
self.daq_device.connect()
|
||||||
|
self.ai_device = self.daq_device.get_ai_device()
|
||||||
|
self.ao_device = self.daq_device.get_ao_device()
|
||||||
|
self.dio_device = self.daq_device.get_dio_device()
|
||||||
|
log.debug("Connected")
|
||||||
|
|
||||||
|
def connect_dac(self):
|
||||||
|
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||||
|
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||||
|
if len(devices) == 0:
|
||||||
|
log.error("Did not found daq devices, please connect one")
|
||||||
|
exit(1)
|
||||||
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||||
|
self.daq_device.connect()
|
||||||
|
self.ai_device = self.daq_device.get_ai_device()
|
||||||
|
self.ao_device = self.daq_device.get_ao_device()
|
||||||
|
self.dio_device = self.daq_device.get_dio_device()
|
||||||
log.debug("Connected")
|
log.debug("Connected")
|
||||||
|
|
||||||
def read_analog_daq(
|
def read_analog_daq(
|
||||||
@@ -31,17 +48,17 @@ class Repos:
|
|||||||
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
||||||
AInScanFlag: uldaq.AInScanFlag = uldaq.AInScanFlag.DEFAULT,
|
AInScanFlag: uldaq.AInScanFlag = uldaq.AInScanFlag.DEFAULT,
|
||||||
) -> Array[c_double]:
|
) -> Array[c_double]:
|
||||||
if channels[0] == channels[1]:
|
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
||||||
channel_len = 1
|
|
||||||
|
if channels[0] != channels[1]:
|
||||||
|
buffer_len_channels = 2
|
||||||
else:
|
else:
|
||||||
channel_len = len(channels)
|
buffer_len_channels = 1
|
||||||
assert len(channels) == 2, log.error("Please provide a list with two ints")
|
|
||||||
|
|
||||||
ai_device = self.daq_device.get_ai_device()
|
|
||||||
buffer_len = np.shape(np.arange(0, duration, 1 / samplerate))[0]
|
buffer_len = np.shape(np.arange(0, duration, 1 / samplerate))[0]
|
||||||
data_analog_input = uldaq.create_float_buffer(channel_len, buffer_len)
|
data_analog_input = uldaq.create_float_buffer(buffer_len_channels, buffer_len)
|
||||||
|
|
||||||
er = ai_device.a_in_scan(
|
er = self.ai_device.a_in_scan(
|
||||||
channels[0],
|
channels[0],
|
||||||
channels[1],
|
channels[1],
|
||||||
AiInputMode,
|
AiInputMode,
|
||||||
@@ -52,11 +69,10 @@ class Repos:
|
|||||||
AInScanFlag,
|
AInScanFlag,
|
||||||
data=data_analog_input,
|
data=data_analog_input,
|
||||||
)
|
)
|
||||||
# ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
|
||||||
|
|
||||||
return data_analog_input
|
return data_analog_input
|
||||||
|
|
||||||
def send_analog_dac(
|
def write_analog_dac(
|
||||||
self,
|
self,
|
||||||
data: Union[list, npt.NDArray],
|
data: Union[list, npt.NDArray],
|
||||||
channels: list[int],
|
channels: list[int],
|
||||||
@@ -90,17 +106,24 @@ class Repos:
|
|||||||
-------
|
-------
|
||||||
Array[c_double]
|
Array[c_double]
|
||||||
ao_device
|
ao_device
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
buffer = c_double * len(data)
|
|
||||||
|
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
||||||
|
|
||||||
|
if channels[0] != channels[1]:
|
||||||
|
buffer_len_channels = 2
|
||||||
|
else:
|
||||||
|
buffer_len_channels = 1
|
||||||
|
|
||||||
|
buffer = c_double * (len(data) * buffer_len_channels)
|
||||||
|
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}")
|
||||||
ao_device = self.daq_device.get_ao_device()
|
try:
|
||||||
ao_info = ao_device.get_info()
|
err = self.ao_device.a_out_scan(
|
||||||
|
|
||||||
err = ao_device.a_out_scan(
|
|
||||||
channels[0],
|
channels[0],
|
||||||
channels[1],
|
channels[1],
|
||||||
Range,
|
Range,
|
||||||
@@ -110,33 +133,198 @@ class Repos:
|
|||||||
AOutScanFlag,
|
AOutScanFlag,
|
||||||
data_analog_output,
|
data_analog_output,
|
||||||
)
|
)
|
||||||
log.info(f"The actual scan rate was {err}")
|
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)
|
# ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 11)
|
||||||
|
|
||||||
return data_analog_output, ao_device
|
return data_analog_output, self.ao_device
|
||||||
|
|
||||||
def digital_trigger(self, portn: int = 0, data: int = 1) -> None:
|
def set_analog_to_zero(self, channels: list[int] = [0, 1]):
|
||||||
log.info(f"{self.daq_device}")
|
try:
|
||||||
dio_device = self.daq_device.get_dio_device()
|
err = self.ao_device.a_out_list(
|
||||||
|
channels[0],
|
||||||
|
channels[1],
|
||||||
|
[
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
uldaq.Range.BIP10VOLTS,
|
||||||
|
],
|
||||||
|
uldaq.AOutListFlag.DEFAULT,
|
||||||
|
[0, 0],
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
log.error("f{e}")
|
||||||
|
log.error("disconnection dac")
|
||||||
|
self.disconnect_dac()
|
||||||
|
|
||||||
dio_device.d_config_bit(
|
def diggital_trigger(self) -> None:
|
||||||
uldaq.DigitalPortType.AUXPORT, portn, uldaq.DigitalDirection.OUTPUT
|
if not self.read_bit(channel=0):
|
||||||
|
self.write_bit(channel=0, bit=1)
|
||||||
|
else:
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
|
time.time_ns()
|
||||||
|
self.write_bit(channel=0, bit=1)
|
||||||
|
|
||||||
|
def write_bit(self, channel: int = 0, bit: int = 1) -> None:
|
||||||
|
self.dio_device.d_config_bit(
|
||||||
|
uldaq.DigitalPortType.AUXPORT, channel, uldaq.DigitalDirection.OUTPUT
|
||||||
|
)
|
||||||
|
self.dio_device.d_bit_out(
|
||||||
|
uldaq.DigitalPortType.AUXPORT, bit_number=channel, data=bit
|
||||||
)
|
)
|
||||||
|
|
||||||
dio_device.d_bit_out(uldaq.DigitalPortType.AUXPORT, bit_number=portn, data=data)
|
def read_bit(self, channel: int = 0):
|
||||||
|
bit = self.dio_device.d_bit_in(uldaq.DigitalPortType.AUXPORT, channel)
|
||||||
|
return bit
|
||||||
|
|
||||||
|
def read_digitalio(
|
||||||
|
self,
|
||||||
|
channels: list[int],
|
||||||
|
duration,
|
||||||
|
samplerate,
|
||||||
|
ScanOptions: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
||||||
|
DInScanFlag: uldaq.DInScanFlag = uldaq.DInScanFlag.DEFAULT,
|
||||||
|
):
|
||||||
|
if channels[0] == channels[1]:
|
||||||
|
channel_len = 1
|
||||||
|
else:
|
||||||
|
channel_len = len(channels)
|
||||||
|
|
||||||
|
buffer_len = np.shape(np.arange(0, duration, 1 / samplerate))[0]
|
||||||
|
data_digital_input = uldaq.create_int_buffer(channel_len, buffer_len)
|
||||||
|
|
||||||
|
self.dio_device.d_config_port(
|
||||||
|
uldaq.DigitalPortType.AUXPORT, uldaq.DigitalDirection.INPUT
|
||||||
|
)
|
||||||
|
scan_rate = self.dio_device.d_in_scan(
|
||||||
|
uldaq.DigitalPortType.AUXPORT0,
|
||||||
|
uldaq.DigitalPortType.AUXPORT0,
|
||||||
|
len(data_digital_input),
|
||||||
|
samplerate,
|
||||||
|
ScanOptions,
|
||||||
|
DInScanFlag,
|
||||||
|
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()
|
||||||
|
|
||||||
def clean_up():
|
def check_attenuator(self):
|
||||||
pass
|
"""
|
||||||
|
ident : attdev-1
|
||||||
|
strobepin : 6
|
||||||
|
datainpin : 5
|
||||||
|
dataoutpin: -1
|
||||||
|
cspin : 4
|
||||||
|
mutepin : 7
|
||||||
|
zcenpin : -1
|
||||||
|
"""
|
||||||
|
|
||||||
def run_repo(self) -> None:
|
SAMPLERATE = 40_000.0
|
||||||
pass
|
DURATION = 5
|
||||||
|
AMPLITUDE = 1
|
||||||
|
SINFREQ = 1
|
||||||
|
t = np.arange(0, DURATION, 1 / SAMPLERATE)
|
||||||
|
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * t)
|
||||||
|
data_channels = np.concatenate((data, data))
|
||||||
|
|
||||||
def stop_repo(self) -> None:
|
db_values = [0, 0, -2, -5, -10, -20, -50]
|
||||||
pass
|
db_values = [0, -10, -20]
|
||||||
|
for i, db_value in enumerate(db_values):
|
||||||
|
log.info(f"Attenuating the Channels, with {db_value}")
|
||||||
|
if i == 1:
|
||||||
|
log.info("Muting the Channels")
|
||||||
|
self.set_attenuation_level(
|
||||||
|
db_value, db_value, mute_channel1=True, mute_channel2=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.set_attenuation_level(db_value, db_value)
|
||||||
|
|
||||||
def reload_repo(self) -> None:
|
stim, ao_device = self.write_analog_dac(
|
||||||
pass
|
data_channels,
|
||||||
|
[0, 1],
|
||||||
|
SAMPLERATE,
|
||||||
|
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||||
|
Range=uldaq.Range.BIP10VOLTS,
|
||||||
|
)
|
||||||
|
self.diggital_trigger()
|
||||||
|
|
||||||
|
try:
|
||||||
|
ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||||
|
except uldaq.ul_exception.ULException:
|
||||||
|
log.debug("Operation timed out")
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
|
self.disconnect_dac()
|
||||||
|
self.connect_dac()
|
||||||
|
self.set_analog_to_zero()
|
||||||
|
finally:
|
||||||
|
self.write_bit(channel=0, bit=0)
|
||||||
|
self.disconnect_dac()
|
||||||
|
self.connect_dac()
|
||||||
|
self.set_analog_to_zero()
|
||||||
|
|
||||||
|
log.info("Sleeping for 1 second, before next attenuation")
|
||||||
|
time.sleep(1)
|
||||||
|
self.deactivate_attenuator()
|
||||||
|
|
||||||
|
def set_attenuation_level(
|
||||||
|
self,
|
||||||
|
db_channel1: float = 5.0,
|
||||||
|
db_channel2: float = 5.0,
|
||||||
|
mute_channel1: bool = False,
|
||||||
|
mute_channel2: bool = False,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
ident : attdev-1
|
||||||
|
strobepin : 6
|
||||||
|
datainpin : 5
|
||||||
|
dataoutpin: -1
|
||||||
|
cspin : 4
|
||||||
|
mutepin : 7
|
||||||
|
zcenpin : -1
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.activate_attenuator()
|
||||||
|
hardware_possible_db = np.arange(-95.5, 32.0, 0.5)
|
||||||
|
byte_number = np.arange(1, 256)
|
||||||
|
byte_number_db1 = byte_number[hardware_possible_db == db_channel1][0]
|
||||||
|
binary_db1 = np.binary_repr(byte_number_db1, width=8)
|
||||||
|
byte_number_db2 = byte_number[hardware_possible_db == db_channel2][0]
|
||||||
|
binary_db2 = np.binary_repr(byte_number_db2, width=8)
|
||||||
|
if mute_channel1:
|
||||||
|
log.info("Muting channel one")
|
||||||
|
binary_db1 = "00000000"
|
||||||
|
if mute_channel2:
|
||||||
|
log.info("Muting channel one")
|
||||||
|
binary_db2 = "00000000"
|
||||||
|
|
||||||
|
channels_db = binary_db1 + binary_db2
|
||||||
|
self.write_bit(channel=4, bit=0)
|
||||||
|
for b in channels_db:
|
||||||
|
self.write_bit(channel=5, bit=int(b))
|
||||||
|
time.time_ns()
|
||||||
|
self.write_bit(channel=6, bit=1)
|
||||||
|
time.time_ns()
|
||||||
|
self.write_bit(channel=6, bit=0)
|
||||||
|
time.time_ns()
|
||||||
|
self.write_bit(channel=4, bit=1)
|
||||||
|
|
||||||
|
def activate_attenuator(self):
|
||||||
|
for ch, b in zip([4, 5, 6, 7], [1, 0, 0, 1]):
|
||||||
|
self.write_bit(channel=ch, bit=b)
|
||||||
|
|
||||||
|
def deactivate_attenuator(self):
|
||||||
|
# mute should be enabled for starting calibration
|
||||||
|
self.write_bit(channel=7, bit=0)
|
||||||
|
|||||||
Reference in New Issue
Block a user