【问题标题】:Continuous Acquistion with nidaqmx使用 ni daqmx 进行连续采集
【发布时间】:2019-05-29 17:47:39
【问题描述】:

我无法在 Python 3 上使用 nidaqxm 从 NI DAQ 获取连续数据。

我已经用类似的代码获取了有限的数据,虽然我不明白我需要改变什么来连续获取数据。

import nidaqmx
from nidaqmx import constants
from nidaqmx import stream_readers
from nidaqmx import stream_writers
import matplotlib.pyplot as plt

#user input Acquisition
Ch00_name = 'A00'
Sens_Ch00 = 100#sensibilidade em mV/g
Ch01_name = 'A01'
Sens_Ch01 = 100#sensibilidade em mV/g
fs_acq = 1651 #sample frequency
t_med = 2 #time to acquire data

with nidaqmx.Task() as task:
    task.ai_channels.add_ai_accel_chan(physical_channel="cDAQ9191-1B7B393Mod1/ai0", name_to_assign_to_channel=Ch00_name,
                                       sensitivity=Sens_Ch00, min_val=-5, max_val=5, current_excit_val=0.002)
    task.ai_channels.add_ai_accel_chan(physical_channel="cDAQ9191-1B7B393Mod1/ai1", name_to_assign_to_channel=Ch01_name,
                                       sensitivity=Sens_Ch01, min_val=-5, max_val=5, current_excit_val=0.002)

    task.timing.cfg_samp_clk_timing(rate=fs_acq, sample_mode= constants.AcquisitionType.CONTINUOUS, samps_per_chan=(t_med * fs_acq),)

    reader = stream_readers.AnalogMultiChannelReader(task.in_stream)
    writer = stream_writers.AnalogMultiChannelWriter(task.out_stream)

我必须对代码进行哪些更改才能获取连续数据?

【问题讨论】:

    标签: python python-3.x nidaqmx


    【解决方案1】:

    您需要注册一个回调函数。我假设你的盒子正在运行,并且有某种类型的状态 LED 闪烁,表明任务正在运行。

    import nidaqmx
    from nidaqmx import constants
    from nidaqmx import stream_readers
    from nidaqmx import stream_writers
    import matplotlib.pyplot as plt
    
    import numpy as np
    
    #user input Acquisition
    Ch00_name = 'A00'
    Sens_Ch00 = 100#sensibilidade em mV/g
    Ch01_name = 'A01'
    Sens_Ch01 = 100#sensibilidade em mV/g
    num_channels = 2
    fs_acq = 1651 #sample frequency
    t_med = 2 #time to acquire data
    
    
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_accel_chan(physical_channel="cDAQ9191-1B7B393Mod1/ai0", name_to_assign_to_channel=Ch00_name,
                                           sensitivity=Sens_Ch00, min_val=-5, max_val=5, current_excit_val=0.002)
        task.ai_channels.add_ai_accel_chan(physical_channel="cDAQ9191-1B7B393Mod1/ai1", name_to_assign_to_channel=Ch01_name,
                                           sensitivity=Sens_Ch01, min_val=-5, max_val=5, current_excit_val=0.002)
    
        task.timing.cfg_samp_clk_timing(rate=fs_acq, sample_mode=constants.AcquisitionType.CONTINUOUS, 
                                        samps_per_chan=(t_med * fs_acq),) # you may not need samps_per_chan
    
        # I set an input_buf_size
        samples_per_buffer = int(fs_acq // 30)  # 30 hz update
        # task.in_stream.input_buf_size = samples_per_buffer * 10  # plus some extra space
    
        reader = stream_readers.AnalogMultiChannelReader(task.in_stream)
        writer = stream_writers.AnalogMultiChannelWriter(task.out_stream)
    
        def reading_task_callback(task_idx, event_type, num_samples, callback_data=None):
            """After data has been read into the NI buffer this callback is called to read in the data from the buffer.
    
            This callback is for working with the task callback register_every_n_samples_acquired_into_buffer_event.
    
            Args:
                task_idx (int): Task handle index value
                event_type (nidaqmx.constants.EveryNSamplesEventType): ACQUIRED_INTO_BUFFER
                num_samples (int): Number of samples that was read into the buffer.
                callback_data (object)[None]: No idea. Documentation says: The callback_data parameter contains the value
                    you passed in the callback_data parameter of this function.
            """
            buffer = np.zeros((num_channels, num_samples), dtype=np.float32)
            reader.read_many_sample(buffer, num_samples, timeout=constants.WAIT_INFINITELY)
    
            # Convert the data from channel as a row order to channel as a column
            data = buffer.T.astype(np.float32)
    
            # Do something with the data
    
        task.register_every_n_samples_acquired_into_buffer_event(samples_per_buffer, reading_task_callback)
    

    这对我有用。希望对你有帮助

    【讨论】:

    • 感谢您的回答。我试图运行这段代码,但它没有工作......首先是“self.dtype”它无法识别,并且代码也不会连续运行。如何获取连续数据,例如打印我正在测量的数据?
    • @JoseGuilherme 抱歉,“self.dtype”是一些遗留代码。该点应该只是您想要的结果数据类型。我将其更改为np.float32。如果您希望它运行几秒钟,那么您需要保持程序运行。如果您在任务开始后输入time.sleep(10),您应该会看到 NI 设备闪烁 10 秒。如果您想对数据执行某些操作,则必须将数据打印或附加到您看到“# Do something with the data”的数组中。通常,您使用循环缓冲区,可以在代码的另一部分中通过计时器或线程访问该缓冲区。
    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多