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.

Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Edge counting & pause trigger

Solved!
Go to solution

Dear all,

 

Hello, can I ask a question about pause trigger & edge counting?

I'm using USB-6212, python 3.7.6, windows 10, NIDAQmx 19.6 and PyDAQmx1.4.3

(Please let me know if you have any idea not only in python, but also in C, etc. Probably I can translate it to python.)

 

I have one TTL signal which I use for triggering.

I take AI data when the TTL signal is low (using pause trigger and callback function)

 

Meanwhile, I also want to count falling edge of TTL signal, cause I want to know how many times the TTL signal went to low.

 

For example, I want to take 1000 data while the TTL signal is low. I take 1000 events (TTL low), do not take event (TTL high), take 1000 events again (TTL low), and repeat the sequence. And I want to distinguish every 1000 events(I mean I want to distinguish the first 1000 events, the second 1000 events, the third 1000 events and so on). I call the callback function when there is 100 events stored in the buffer, so I cannot count the # of triggers in the callback function.

 

Is that possible to do this job?

 

I wanted to use counter, but I have no idea how can I use it with pause trigger + callback function.

I tried to put

nidaq.DAQmxCreateCICountEdgesChan(AItaskHandle,b"/Dev%d/ctr0" %num,"",DAQmx_Val_Falling,0,DAQmx_Val_CountUp)
but don't know where can I read it. (and not completely sure if ctr0 matches PFI0)

Maybe I should not use callback function and trigger but do this job by reading DI one by one and find the falling edge and signal low?(Take one DI(PFI0), try to find falling edge and the state(high/low) every time, take one AI data, save one AI data somewhere(file or array, etc) if the DI is low using while(taskStarted) loop seems not elegant.)

 

 

Here is a piece of my code.

import PyDAQmx as pydaqmx
import PyDAQmx.DAQmxCallBack as pydaqmxcb
nidaq = ctypes.windll.nicaiu
 
...
 
nidaq.DAQmxSetDigLvlPauseTrigSrc(AItaskHandle, b"/Dev%d/PFI0" %num)
nidaq.DAQmxSetPauseTrigType(AItaskHandle, DAQmx_Val_DigLvl)
nidaq.DAQmxSetDigLvlPauseTrigWhen(AItaskHandle, DAQmx_Val_High)
nidaq.DAQmxRegisterEveryNSamplesEvent(AItaskHandle,1,1000,0,EveryNCallback,id_AIdata)
 
...
 
def EveryNCallback_py(taskHandleeventTypenSamplescallbackData_ptr) :

DAQmx_Val_GroupByScanNumber = 1

callbackData = pydaqmxcb.get_callbackdata_from_id(callbackData_ptr)
read = uInt32()
nidaq.DAQmxReadAnalogF64(taskHandle,nSamples,float64(-1),DAQmx_Val_GroupByScanNumber,AIdata.ctypes.data,Length,ctypes.byref(read),None)
return 0

EveryNCallback = pydaqmx.DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py)
 
...
 
 
Thank you in advance!
 
0 Kudos
Message 1 of 4
(2,959 Views)
Solution
Accepted by topic author Serenade

I'm a LabVIEW guy and can't really help with the text API syntax.  But I can describe an approach or two.

 

First, I'd strongly recommend you make use of your device's hardware features for triggering and sampling.  Don't try to poll a DIO line and then make decisions in software about taking single AI samples on-demand.  You can do *far* better than that with your device.

 

There are many approaches you could take, I'm not sure what to recommend without more info about your system's timing and your data segmentation needs.

 

A pause-triggered AI task will acquire and buffer samples whenever the pause trigger is in the "enable" state.   The buffer of data will look just like it would if you were acquiring continuously without any pauses.  So you need some additional method to help you segment the data properly.

 

A counter task can help you do that, but again, there are different ways to approach it.

 

1. Configure a counter to count the AI sample clock signal while using a rising edge of the external signal as a sample clock.   This data can be used to know how many AI samples were taken during each "enabled" interval.   

 

2. Use the falling edge of the external signal as a start trigger for a retriggerable finite pulse train with 1000 pulses.  Configure AI to use this pulse train as its sample clock, while still using the external signal as a pause trigger.

 

Note that no method can guarantee that you get the full 1000 samples during an enabled interval.  That depends on the sample rate (which you can control) and the interval duration (which you can't know until after it ends).

   Some methods can guarantee that you never get *more* than 1000, others would let you collect them but inform you of how many you got so you could ignore the extras if you choose.

 

Can you describe the nature of your system, the timing of those external "pause/enable" pulses, your sample rate needs, the importance of having *exactly* 1000 samples per interval, and any other pertinent details?

 

 

-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 4
(2,915 Views)

Hello, thank you for the detailed reply! 🙂

 

I solved the problem by adding counter.

I created CI count edges channel as

nidaq.DAQmxCreateCICountEdgesChan(CItaskHandle,b"/Dev%d/ctr0" %num,"",DAQmx_Val_Rising,0,DAQmx_Val_CountUp)
and read the counter result in the callback function EveryNCallback_py like
nidaq.DAQmxReadCounterScalarU32(CItaskHandle,float64(-1),countdata.ctypes.data,None)

and it works well 🙂

I'm a 1 month newbie, so was bit confused which channel is ctr0, if I can use one channel for both counting and triggering, if digital input also stored in a buffer, etc... I missed some basic info. bit ashamed of it 😛

 

Fortunately, I don't need to take exactly 1000 events / trigger, and I can throw away sub tens of events at the beginning & the end of this ~1000 events. So probably I can read the data when small # of events are stored in a buffer, like 100 or so.

Now I can take the data with 400kS/s sampling rate, but it'll be ~1MS/s in the near future. Timing of the external "pause/enable" pulses is ~kHz. I'm not sure if I need FPGA module or not.

 

Thank you again!

The answer helped me a lot to have an idea.

 

0 Kudos
Message 3 of 4
(2,896 Views)

One little caution to be aware of:

 

The AI task will keep pushing data into your task buffer in the background, whenever the external signal is in the enable state.  And when make a DAQmx Read call to retrieve data, it's going to give you the *oldest* data you haven't read previously.

 

So here's the implication:  if during one enable cycle you ask to read 1000 samples but 1500 samples are actually taken, then the next time you request 1000, you'll get the 500 "old" ones from the previous enable interval and 500 from the present enable interval.

 

Even though you add the counter task to count which interval you're in *right now*, the data you retrieve from the AI task may not *all* correspond to the same interval.  Fortunately there are ways to deal with this, they just wouldn't be particularly obvious if you're moderately new to NI's data acq boards.

 

Given your external signal rate of ~1 kHz and sample rate of up to 1 MHz, I don't think you can count on your callback code keeping up.  I think you'll need to use a method like the one I outlined as method 1 in msg #2..  That allows you to retrieve bigger chunks of data at a more leisurely pace while giving you the info you need to break it up into the proper segments.

 

 

-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 4
(2,887 Views)