Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Averaging window for ai in Python nidaqmx?

Hello, (my device is a combination of cDAQ-9171 and a USB-6343) I am reading voltages from ai channels using the python nidaqmx package. Currently I can get one value (or N values) from each channel every-time I call task.read. I would like to have a moving average where task.read returns one value per channel which is the average of the last recorded N values. If possible I would like the Ni board to do this because (1) if I do this in python via post processing I have to save much more data (2) If I do this in python real time I get latency issues. Documentation for nidaqmx suggests ai_averaging_count as a value I can set but I am not sure how to implement it. The following code is my attempt. 

 

 

device_read = {
            "MFC1": "Dev1/ai0",
            "MFC2": "Dev1/ai1",
            "MFC3": "Dev1/ai2",
            "MFC4": "Dev1/ai3",
            "SENSOR_DELTAV": "Dev1/ai4"
    }

def initializeSystem(device_read):
    MIN_MFC_VAL = 0.0
    MAX_MFC_VAL = 5.0
    MIN_SENSOR_VAL = 0.0
    MAX_SENSOR_VAL = 10.0
    AVERAGING_COUNT = 10
    RECORDS_PER_SECOND = 30
    RATE = AVERAGING_COUNT*RECORDS_PER_SECOND

    device_read_task = nidaqmx.Task()
    for key in device_read:
        if key == "SENSOR_DELTAV":
            device_read_task.ai_channels.add_ai_voltage_chan(device_read[key], name_to_assign_to_channel=key, min_val=MIN_SENSOR_VAL, max_val=MAX_SENSOR_VAL)
            #device_read_task.ai_channels[-1].ai_averaging_count = AVERAGING_COUNT
        else:
            device_read_task.ai_channels.add_ai_voltage_chan(device_read[key], name_to_assign_to_channel=key, min_val=MIN_MFC_VAL, max_val=MAX_MFC_VAL)
        device_read_task.timing.cfg_samp_clk_timing(RATE, source='', sample_mode=AcquisitionType.FINITE, samps_per_chan=AVERAGING_COUNT) 
        device_read_task.ai_averaging_count = AVERAGING_COUNT
    return  device_read_task




data = np.array(device_read_task.read()) ###<< I want this to return a single value per channel which is the average of AVERAGING_COUNT samples per channel

 

 

0 Kudos
Message 1 of 2
(1,113 Views)

Sorry my question is still the same, but I had a typo in my example code. It is supposed to read 

ai_averaging_win_size not ai_averaging_count. For some more information: If I attempt to assign an ai_averaging_win_size to a single channel in the task I get error:
 

nidaqmx.errors.DaqError: Specified property is not supported by the device or is not applicable to the task.

Property: DAQmx_AI_AveragingWinSize

Channel Name: SENSOR_DELTAV

 

But by assigning to all channels in the task, the code proceeds without error, however, does not work correctly. 

 

Im new to NI and would appreciate any help you can provide! Thanks

 

0 Kudos
Message 2 of 2
(1,061 Views)