Skip to main content
daq_analog_loopback.py
"""Example: DAQ write analog loopback."""

import time

from instro.daq import InstroDAQ
from instro.daq.types import DAQVendor, Direction
from instro.lib.publishers.nominal_core import NominalCorePublisher

# Configuration: Choose your vendor.
VENDOR = DAQVendor.LABJACK_T_SERIES

# Vendor-specific configuration. Each vendor driver lives in its own package and
# owns its transport at construction time.
match VENDOR:
    case DAQVendor.LABJACK_T_SERIES:
        from instro.daq.drivers.labjack import LabJackTSeriesDriver

        AO_CH0 = "DAC0"
        AO_CH1 = "DAC1"
        AI_CH0 = "AIN0"
        AI_CH1 = "AIN1"
        driver = LabJackTSeriesDriver(device_id="440020473")  # LabJack serial number
    case DAQVendor.NI:
        from instro.daq.drivers.ni import NIDAQDriver

        AO_CH0 = "Dev1/ao0"
        AO_CH1 = "Dev1/ao1"
        AI_CH0 = "Dev1/ai0"
        AI_CH1 = "Dev1/ai1"
        driver = NIDAQDriver(device_id="Dev1")  # NI device name, as defined in MAX
    case DAQVendor.MCC:
        from instro.daq.drivers.mcc import MCCDriver

        AO_CH0 = "0"
        AO_CH1 = "1"
        AI_CH0 = "0"
        AI_CH1 = "1"
        driver = MCCDriver(
            device_id="344371:0"
        )  # MCC DAQ device ID, optionally suffixed with ":<board_number>" (default 0)

# Nominal Core dataset to send data to as the instrument is operated.
DATASET_RID = "<dataset_rid>"  # Replace with your dataset RID.

### Main code
daq = InstroDAQ(name="myDAQ", driver=driver)
daq.add_publisher(NominalCorePublisher(dataset_rid=DATASET_RID))

daq.open()

try:
    daq.configure_analog_channel(
        direction=Direction.INPUT, physical_channel=AI_CH0, alias="ai_0", range_min=0, range_max=5
    )
    daq.configure_analog_channel(
        direction=Direction.INPUT, physical_channel=AI_CH1, alias="ai_1", range_min=0, range_max=5
    )
    daq.configure_analog_channel(
        direction=Direction.OUTPUT, physical_channel=AO_CH0, alias="ao_0", range_min=0, range_max=5
    )
    daq.configure_analog_channel(
        direction=Direction.OUTPUT, physical_channel=AO_CH1, alias="ao_1", range_min=0, range_max=5
    )

    daq.configure_ai_sample_rate(sample_rate=100)

    # Start the acquisition.
    # This launches a background daemon that fetches samples from the DAQ device buffer.
    daq.start()

    for i in range(6):
        try:
            # Main progam loop. Set outputs while InstroDAQ acquires data on the inputs in the background.
            # Sit in this while loop until ctrl+c is pressed.
            # InstroDAQ will acquire data from the daq device in the background and publish to the configured Publisher.
            daq.write_analog_value("ao_0", i)
            daq.write_analog_value("ao_1", 5 - i)
            time.sleep(1)
        except KeyboardInterrupt:
            print("Exiting main loop")
            break

    daq.stop()

finally:
    print("Closing DAQ")
    daq.close()