Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Synchronized non regenerate multifunction AO AI

Solved!
Go to solution

I am trying to build a VI that reads and writes a signal simultaneosly to use as a device test. I want the signal to be non-regenerative and I want to read at 80kS/s with at least 4096 samples. 

 

I have built my VI starting out with the standard producer-consumer template and the multifunction AO AI example that can be found under "Hardware Input Output/DAQmx/Synchronized/Multifunction" but with some slight changes to make it non regenerative. This however is not working because there seems to be an offset between the two signals after the first iterations. At first the signals overlap but then I get a delay. For example setting the samplerate and samplesize to 1000 yields overlapping signals for a short while but then a delay of about .25 seconds appears. There are also various buffer errors all stating that the buffer has been closed or overwritten before samples could be read. If there is anyone who would know how to fix this I would be extremely grateful.

 

I would also appreciate any advice on how to be able to update the sample rate and size in real time.

 

Best regards,

Felix Turner

0 Kudos
Message 1 of 2
(2,996 Views)
Solution
Accepted by topic author felixturner

Hi Felix,

 

Welcome to the NI forums!  The synchronization part of your code looks pretty good assuming the AI and AO tasks are running on of the same device--you're triggering one task off of the other so they should start at the same time (if they are the same device, the timebase will be the same for each clock so you should not get any drift over time).  An alternative way to synchroinze the two tasks (since they are going at the same rate) would be to simply share the sample clock between the tasks.

 

Some issues that I do see with the code:

 

I think the buffer problems problems are coming from the fact that the AI and AO tasks are in the same thread.  DAQmx Write is a blocking call, so in your code it is preventing DAQmx Read from being called until enough space has become available in the output buffer to write the desired number of samples (1000 in your case).  By that time, it sounds like the analog input buffer is being completely filled.  You could try increasing the buffer size, but you might also consider running the two tasks in parallel loops.

 

Also, reading (-1) samples is going to return whatever data is in the input buffer.  This would likely be a different number from the amount of samples you are writing per loop iteration, so the data may appear out of sync when it is read back in. 

 

 

 

As far as updating the number of samples to read and write per loop, this is changeable at run-time so all you have to do is put the control inside the loop (you can put a local variable outside the loop to give the initial parameters).

 

The rate of the ai/ao sample clocks is not changeable at runtime, but what you can do is use a counter to generate a sample clock for the two tasks (just use the internal output as the sample clock for each task).  This example shows how you can change the rate of a counter output on the fly.

 

 

Putting all of this together, here's what I would try:

 

I would put the two DAQmx Tasks in parallel loops. Rather than sharing a start trigger between the two tasks, use one of the counters to generate a variable frequency sample clock to use.  The AI and AO tasks must start before the counter.

 

You should also probably use the Number of Samples to Read input of DAQmx Read so that each loop iteration is going to match up with the data written in the previous AO loop iteration.  If you're not careful when implementing the changeable Number of Samples to Read parameter, you could end up with a Race Condition where only one loop updates the samples to read and the other doesn't update until the following iteration.  Typically Functional Global Variables are used in situations like this, but another idea might be to adjust the AO Buffer Size such that we can guarantee the AO loop is always going to execute before the AI loop.  You could put the control inside the AO loop and the local variable inside the AI loop.

 

 

Don't hesitate to post back if you have any questions!

 

Best Regards,

John

 

John Passiak
Message 2 of 2
(2,986 Views)