简体   繁体   English

NI DAQmx - 触发输入通道使用外部模拟触发输入(python)读取 N 个样本

[英]NI DAQmx - Trigger input channel to read N samples using external analog trigger input (python)

I am having trouble using Python and the nidaqmx library to properly trigger an analog input channel to read N values.我在使用 Python 和 nidaqmx 库正确触发模拟输入通道以读取 N 个值时遇到问题。 I have two analog input channels, ai0 and ai1.我有两个模拟输入通道,ai0 和 ai1。 Channel ai0 is the trigger channel that reads 9V until the circuit is connected and the voltage goes to 3V.通道 ai0 是触发通道,读取 9V,直到电路连接并且电压变为 3V。 At that moment, I want to read N samples from channel ai1.那一刻,我想从通道 ai1 读取 N 个样本。 I want to repeat this process for 1 minute.我想重复这个过程 1 分钟。 I am using an NI USB-6361 daq.我正在使用 NI USB-6361 daq。

My code is below and the problem is my code does not wait to execute the read task until the trigger is satisfied.我的代码在下面,问题是我的代码不会等到触发器满足后才执行读取任务。 The trigger is configured correctly because if I remove ai1 task, the program will read N samples from channel ai0 once the voltage drops.触发器配置正确,因为如果我删除 ai1 任务,一旦电压下降,程序将从通道 ai0 读取 N 个样本。 However, I want to read the signal from ai1 but use ai0 as the trigger.但是,我想从 ai1 读取信号,但使用 ai0 作为触发器。 Hope this makes sense and thanks for your help.希望这是有道理的,并感谢您的帮助。

import nidaqmx

num_samples = 1000;
s_freq = 1e3;
tend = num_samples/s_freq;

#read from DAQ
def readdaq():
    task = nidaqmx.Task()
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0",max_val=10, min_val=0)
    task.triggers.reference_trigger.cfg_anlg_edge_ref_trig("Dev1/ai0", pretrigger_samples = 10, trigger_slope=nidaqmx.constants.Slope.FALLING, trigger_level = 5)
    task.stop()
    task.close()
    
    task = nidaqmx.Task()
    task.ai_channels.add_ai_voltage_chan("Dev1/ai1",max_val=10, min_val=0)
    task.timing.cfg_samp_clk_timing(s_freq, sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=num_samples)
    task.start()
    value = task.read(number_of_samples_per_channel=num_samples)
    task.stop()
    task.close()
return value

I guess issue is that you do not set trigger to ai1 channel, but to ai0 channel.我想问题是您没有将触发器设置为ai1通道,而是设置为ai0通道。 Try this:尝试这个:

def readdaq():
    task = nidaqmx.Task()
    task.ai_channels.add_ai_voltage_chan("Dev1/ai1",max_val=10, min_val=0)
    task.triggers.reference_trigger.cfg_anlg_edge_ref_trig("Dev1/ai0", pretrigger_samples = 10, trigger_slope=nidaqmx.constants.Slope.FALLING, trigger_level = 5)
    task.timing.cfg_samp_clk_timing(s_freq, sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=num_samples)
    task.start()
    value = task.read(number_of_samples_per_channel=num_samples)
    task.stop()
    task.close()
return value

So task is created just for channel ai1 , and it has set trigger based on ai0 channel.所以任务是为通道ai1创建的,它设置了基于ai0通道的触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM