LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Acquire one sample per Trigger

Solved!
Go to solution

Hi All,

 

I wanted to know whether we can Trigger  DAQ to acquire one sample per each trigger input . Currently I'm using NI-DAQ 6251 board and I have configured PFI0 channel to trigger at the falling edge of the trigger input with the function " DAQmxCfgDigEdgeStartTrig " . Then I want to acquire a single sample to the Analog input channel AI0. for that I had called " DAQmxReadAnalogScalarF64 " . But when I ran the program It gives me an error code of " DAQmxErrorTrigWhenOnDemandSampTiming   ( -200262)" . I do not understand  how to solve this error and I couldn't find any documentation which has acquire a single channel ,single data point with trigger input.

 

So, is it possible to acquire a single data point per each trigger input ?

 

* I am using Matlab software to communicate with DAQ card. here is the code I was using.

 

h = 10;
DAQRate = 1e6 ; %S/s
IntgSample = double(0);
IntegArray = zeros(1,h);

AIvoltage = DAQmxCreateTask('');
DAQmxCreateAIVoltageChan(AIvoltage,'Dev1/ai0','AIin','DAQmx_Val_Cfg_Default',0,10,'DAQmx_Val_Volts','');


[StatusTrig, AIvoltage,~] = DAQmxCfgDigEdgeStartTrig(AIvoltage,'/Dev1/PFI0','DAQmx_Val_Falling');

 

pb_start;  % ignore this line. this is my trigger input


[StatusStart, AIvoltage] = DAQmxStartTask(AIvoltage);


for i = 1:h
[StatusReadAnalogScal,AIvoltage,IntgSample,~] = DAQmxReadAnalogScalarF64(AIvoltage,timeout,IntgSample);
IntegArray(i) = IntgSample ;

end


[StatusStop, AIvoltage] = DAQmxStopTask(AIvoltage);

DAQmxClearTask(AIvoltage);

 

 

thanks in advance.

 

0 Kudos
Message 1 of 10
(3,527 Views)
Solution
Accepted by Hansi@u

The hardware designers didn't see the need to waste circuitry on this particular scenario.  You are describing a kind of "on-demand" single sample acquisition mode.  What the trigger circuitry was probably designed for was a precise start for a Continuous acquisition.  However, you can "have your cake and eat it, too" -- set up your DAQ task for Finite Samples, specify 2 samples in Triggered mode, choose a suitably fast sample rate (higher than your expected trigger rate), and simply throw the second sample away.

 

So let's assume you are triggering at 10 Hz.  Set up Finite Samples, 1 kHz, 2 samples.  Each trigger, the DAQmx Read will take 2 ms, leaving you 98 ms until the next trigger comes in, plenty of time to discard the second sample.

 

Bob "When NI/LabVIEW Hands You a Lemon, Make Lemonade" Schor

0 Kudos
Message 2 of 10
(3,495 Views)

Hi Bob,

 

Thank you so much for the reply. This is a dumb question, But what do you mean by discarding the second sample. program it in a away that it delete the second value ?

 

In my case, I'm triggering at 500 Hz . so according to your argument I can use 1 MHz  sampling rate with 2 samples. so I will have  almost 199 ms window. But now I got confused with the discarding part.

Please let me know.

 

Thanks in advance.

0 Kudos
Message 3 of 10
(3,477 Views)

You'll get an array with 2 elements in it.  Use the first element using Index Array.  Just ignore the second element.

0 Kudos
Message 4 of 10
(3,469 Views)

Thanks a lot. Smiley Happy

0 Kudos
Message 5 of 10
(3,465 Views)
Solution
Accepted by topic author Bob_Schor

At a "trigger" rate of 500 Hz, you'd need a device that supports hardware retriggering capability.  Your 6251 doesn't.

 

So instead, you just need a small mental shift.  Simply cofingure your task to use the external signal as a Sample Clock instead of a trigger.  That's a more natural way to take 1 sample for every incoming pulse.  You don't need to configure any "triggering" at all.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
Message 6 of 10
(3,408 Views)

I took the liberty of marking Kevin Price's reply as "the Better Solution" to this question.  His response emphasizes an extremely important (but sometimes over-looked) LabVIEW Principle, almost as important as the Principle of Data Flow, namely "Know Your Hardware!", or, informally, "Read the Manual!" (abbreviated RTFM).

 

Bob Schor

0 Kudos
Message 7 of 10
(3,399 Views)

Okey, That was my bad, not knowing my DAQ manual. But thanks for pointing that to me.

 

I tried what you suggested to do. And it seems working. But correct me if i understand this wrong.

Lets say I want my external clock to work at 500 Hz. then I set the rate to 500 Hz and timeout to be infinity. Then if I want to acquire 500 samples it will take 1 minute to acquire. sounds good. right ?

 

But the issue is, Even I don't connect my clock input to the DAQ program, runs and collect data. Ideally if I set my timeout to be infinity, program should be busy until it receive sufficient input signal to the PFIO input. But It does not.

here I'm attaching the code I was using.please let me know what I'm missing here.

 

clc;
clear;

h = 500;
h1 = 1;
timeout = -1;
data = zeros(1,h);
DAQRate = 500;
AIvoltage = DAQmxCreateTask('');
DAQmxCreateAIVoltageChan(AIvoltage,'Dev1/ai0','AnalogIn','DAQmx_Val_Cfg_Default',-10,10,'DAQmx_Val_Volts','');
[StatusClk, AIvoltage,~] = DAQmxCfgSampClkTiming(AIvoltage,'/Dev1/PFI0',DAQRate,'DAQmx_Val_Falling','DAQmx_Val_FiniteSamps',h);
[StatusStart, AIvoltage] = DAQmxStartTask(AIvoltage);
tic
[StatusRead,AIvoltage,data, h1 ,~] = DAQmxReadAnalogF64(AIvoltage,h,timeout,'DAQmx_Val_GroupByChannel',data, h, h1,'');
toc
[StatusStop, AIvoltage] = DAQmxStopTask(AIvoltage);


DAQmxClearTask(AIvoltage);

 

PS: the example ( "Acq-ExtClk.c" ) I'm attaching here is from the NIDAQmx-Example folder and there at line 24 it says, "Note: The Rate should be AT LEAST twice as fast as the maximum frequency component of the signal being acquired." But if I consider that for defining my clock rate, isn't that will be different than the actual trigger/clock i need to use for acquire a sample. ?

 

Thanks in advance.

 

0 Kudos
Message 8 of 10
(3,345 Views)

1. 500 samples at 500 Hz should give you 1 second of data, not 1 minute.

 

2. An infinite timeout is usually a bad plan because, well, forever is an awfully long time...   

    Seriously, an infinite timeout can make your code hang in a way that can't be interrupted cleanly.  It's much better to loop over code with a finite timeout, recognize and handle timeout errors, and provide some means of escape from this loop.

 

3. I do all my programming in LabVIEW so I can't 100% vouch for the code you posted.  It generally looks like it should do what you've described that you want -- sample on falling edges of an external signal at PFI0.  But it's possible there's some mistake in parameter order in your function calls or something subtle like that.  I just don't know the text API to be able to evaluate.

 

4. What is the source of the external clock signal you wire to PFI0?   Is your code running and acquiring samples even when that signal is physically disconnected from PFI0?

 

5. The example comments about sampling "AT LEAST twice as fast..." are just a reminder about the Nyquist Theorem and good digitizing practices.  The *appropriate* sample rate depends on the signal being sampled.   I'm not sure what else to say as I don't understand exactly what you're asking.

   If you wanted to sample a 10 Hz pure sine wave, this comment would say that your sample rate must be AT LEAST 20 Hz.  More common rules of thumb suggest sampling at 10x (100 Hz) or 20x (200 Hz) the highest frequency content (10 Hz) in the signal.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
0 Kudos
Message 9 of 10
(3,330 Views)

Hi Keven,

 

I just found out my mistake. Thanks for helping me out Keven. It was a silly error in my function. Where I defined it to take only the internal clock not any other source input for DAQmxCfgSampClkTiming function.  So problem solved.

1. It was a typo. I meant 1 second. not 1 minute.

2. I do not use infinite timeout at all. It was only for testing purposes. by doing the math accounting # of samples, and sampling rate I can use a appropriate timeout.

 

3. yp. That's where I made the mistake.

 

4. I was using SR250 Gated integrator busy output signal to the DAQ card which has 500 Hz repetition. Because I was averaging my signal using SR250 integrator and acquire data points using DAQ card for each trigger. 

 

5. Got it. I knew it was about Nyquist theorem But some how I messed up the whole idea.

 

Thank you again for helping me out and bearing my silly mistakes.

 

 

 

0 Kudos
Message 10 of 10
(3,322 Views)