04-03-2024 05:55 PM
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_
"""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_
# Configure sample clock timing
task.timing.cfg_samp_clk_
# Configure digital edge trigger for rising edge
task.triggers.start_trigger.
# 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_
# 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_
# 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()
Solved! Go to Solution.
04-03-2024 06:31 PM
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)
04-04-2024 11:57 AM
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.
04-11-2024 10:59 AM
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).