From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM 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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Triggering Analog Input with Python

Hello, I have an NI USB-6361 and I am using the ni-DAQmx Python API to read data. I have two analog inputs into channel ai0 and ai1. My problem is I cannot use ai0 as the trigger to start reading data from ai1 channel. Here is my code:

 

import nidaqmx
import numpy as np
import matplotlib.pyplot as plt

num_samples = 1000;
s_freq = 1e3;
tend = num_samples/s_freq;
t = np.linspace(0,tend,num_samples)

#read from DAQ
def readdaq():
task_trig = nidaqmx.Task() # for the trigger
task_sig = nidaqmx.Task() # for the signal I am interested in acquiring

task_trig.ai_channels.add_ai_voltage_chan("Dev1/ai0",max_val=10, min_val=0)
task_trig.triggers.reference_trigger.cfg_anlg_edge_ref_trig("Dev1/ai0", pretrigger_samples = 10, trigger_slope=nidaqmx.constants.Slope.FALLING, trigger_level = 5)

task_sig.ai_channels.add_ai_voltage_chan("Dev1/ai1",max_val=10, min_val=0)
task_sig.timing.cfg_samp_clk_timing(s_freq, sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=num_samples)

task_sig.start()

value = task_sig.read(number_of_samples_per_channel=num_samples)
task_sig.stop()
task_sig.close()
task_trig.stop()
task_trig.close()
return value

 I want my code to start acquiring the specified number of data points after the ai0 channel trigger is executed. The problem is that I cannot specify one channel as the trigger and another channel to read from. Also, which ever channel I specify as the trigger the program wants to read data from the same channel.

 

Thank you for your help.

0 Kudos
Message 1 of 5
(1,901 Views)

I don't really know the python API for DAQmx, but here's a few general pointers:

 

1. You don't seem to ever start your trigger task.  If you had tried, there would have been an error though I'm not sure if your code would have noticed it.

    You have a multiplexed device, so you can only run 1 AI task at a time because that task will take ownership of the A/D converter resource.

 

2. So you need to put both ai0 and ai1 in the same task.  When you go to read your data, you'll probably get a 2D array of floating point sample values.  In LabVIEW, rows are for individual channels, columns are for individual samples.  Dunno if it'll be the same in Python.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 2 of 5
(1,868 Views)

Regarding point 1, I did not realize I could only run one task at a time. However, I would like to be able to start a new task with the ai1 because the voltage range is much smaller [0,1] Volts compared to the trigger which is from [2,8] Volts. And I would like to keep as high a resolution on ai1 by setting the min and max voltage as small as possible. Ultimately, that is why I started a new task over a different voltage range.

 

Do you know if I can have separate min and max values for different channels in the same task?

 

Thanks

0 Kudos
Message 3 of 5
(1,850 Views)

Yes, you can have different min and max voltage ranges for each channel in a task.  There are certain circumstances where the change in min/max voltage while multiplexing across channels can cause "ghosting" effects (where the higher voltage of a prior channel can influence the next channel in the list), but you're pretty unlikely to run into those at your 1000 Hz rate.  It tends to be more of an issue as you start approaching the board's rated limits for sample rate.

 

Something you can do to check for this influence is to put 2 instances of the same lower voltage channel next to each other in the list.  (Yes, you can include the same channel more than once in a task!)  Then you can see whether they differ in a way that indicates such influence, such as the earlier one always giving higher values.

 

But again, at 1000 Hz, you'll likely not have an issue.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 4 of 5
(1,844 Views)

Apologies - last question on this thread. What do you mean by two instances? It is the following?

 

task1 = nidaqmx.Task()
task2 = nidaqmx.Task()

Then proceed to define the channel locations with min/max value.

 

0 Kudos
Message 5 of 5
(1,818 Views)