Skip to content

Example: DAQ write analog loopback.ยค

"""Example: DAQ write analog loopback."""

import time

from nominal_instro.instruments import NominalDAQ
from nominal_instro.instruments.daq.types import DAQVendor, Direction
from nominal_instro.lib.publishers.nominal_core import NominalCorePublisher

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

# Vendor-specific configuration. For example purposes.
# Each vendor uses different channel naming conventions and resource ID formats. See vendor documentation for details.
match VENDOR:
    case DAQVendor.LABJACK_T_SERIES:
        AO_CH0 = "DAC0"
        AO_CH1 = "DAC1"
        AI_CH0 = "AIN0"
        AI_CH1 = "AIN1"
        RESOURCE_ID = "440020473"  # LabJack serial number
    case DAQVendor.NI:
        AO_CH0 = "ao0"
        AO_CH1 = "ao1"
        AI_CH0 = "ai0"
        AI_CH1 = "ai1"
        RESOURCE_ID = "Dev1"  # NI device name, as defined in MAX (e.g., "Dev1", "Dev2")
    case DAQVendor.MCC:
        AO_CH0 = "0"
        AO_CH1 = "1"
        AI_CH0 = "0"
        AI_CH1 = "1"
        RESOURCE_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 = NominalDAQ.create(name="myDAQ", vendor=VENDOR, resource=RESOURCE_ID)
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 NominalDAQ acquires data on the inputs in the background.
            # Sit in this while loop until ctrl+c is pressed.
            # NominalDAQ 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()