From Thursday, May 23rd (05:00 PM CDT) through Friday, April 24th (1:30 AM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

Acquiring Data on Trigger with the PXI-5152 in Python

Solved!
Go to solution

I'm trying to use the nidaqmx python library to make a program that can acquire data on trigger from the NI PXI-5152. I can get the python program to read data. I can get the python program to tell me it detected the trigger. But, the data that it acquires after the trigger is not correct. I am feeding in a square wave and using the ai1 channel to detect a rising edge. When I plot the specified number of points acquired, the plot is random where the signal is acquired from. I am wondering if there is something I am missing about how to acquire data on trigger. I am using a 1073 chassis with the 5152 digitizer card. Below is my python program. I would appreciate any suggestions. 

 

import numpy as np
import nidaqmx
from nidaqmx.constants import Edge

def capture_data_on_trigger(num_samples, sampling_rate, channel, trigger_threshold):
    """Capture data on trigger.

    Parameters:
    -----------
    num_samples : int
        Number of samples to capture.
    sampling_rate : int
        Sampling rate in Hz.
    channel : str
        Channel name.
    trigger_threshold : float
        Voltage threshold for triggering in volts.

    Returns:
    --------
    numpy.ndarray
        Captured data.
    """
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan(channel, terminal_config=nidaqmx.constants.TerminalConfiguration.RSE)

        # Configure sample clock timing
        task.timing.cfg_samp_clk_timing(sampling_rate, sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=num_samples)

        # Configure digital edge trigger for rising edge
        task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_edge=Edge.RISING, trigger_source='')

        # Start the task and wait for the trigger
        print("Waiting for trigger...")
        task.start()

        # Read data after the trigger threshold has been exceeded
        data = task.read(number_of_samples_per_channel=num_samples, timeout=nidaqmx.constants.WAIT_INFINITELY)

        # Check if the trigger threshold is exceeded
        if any(sample > trigger_threshold for sample in data):
            print("Trigger detected!")

    return np.array(data)

if __name__ == "__main__":
    # Example usage
    num_samples = 5000
    sampling_rate = 10e5  # Card can do up to 2 GS/s
    channel = "Dev1/ai0"  # Replace with the appropriate channel name
    trigger_threshold = 0.9  # Trigger threshold voltage in volts

    # Capture data on trigger with threshold detection only
    captured_data = capture_data_on_trigger(num_samples, sampling_rate, channel, trigger_threshold)

    # Plot captured data (example)
    import matplotlib.pyplot as plt
    time_axis = np.arange(len(captured_data)) / sampling_rate
    plt.plot(time_axis, captured_data)
    plt.xlabel('Time (s)')
    plt.ylabel('Voltage (V)')
    plt.title('Captured Data')
    plt.grid(True)
    plt.show()

0 Kudos
Message 1 of 4
(177 Views)
Solution
Accepted by topic author burban8383

NI-5152 is a digitizer and uses NI-Scope driver.

Using NI-DAQmx, you are not controlling NI-5152 but another device.

Use Examples — NI Modular Instruments Python API 1.1.4 documentation (nimi-python.readthedocs.io)

-------------------------------------------------------
Control Lead | Intelline Inc
0 Kudos
Message 2 of 4
(168 Views)

Thanks for the fast response. 

I will search the niscope examples and see if I can get this running. It might solve all the problems.

0 Kudos
Message 3 of 4
(98 Views)

The comment is useful.

The NI-Scope library solves the problems. From there is was possible to implement a detect trigger and acquire function. There is a limitation with speed for python at around 1 kHz. So, if there are triggers faster, using Python seems to no be good enough (unless there is something I am missing).

0 Kudos
Message 4 of 4
(77 Views)