Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

Synchronize AI and AO on cRIO

Hi,
 
I have a 9101 cRIO chassis with a 9012 controller, 9239 AI and 9263 AO.  Is there any reasonable way to synchronize the AI and AO such that acq and playback could occur on the same clock?  At a first pass, I'd just like to run a pass-through...data in/data out...for audio.
 
Chris
0 Kudos
Message 1 of 11
(5,467 Views)

Just put the two IO nodes in the same loop with no time delays.  I believe the only time delay you will have is the conversion times of the devices.  I'm not sure if these devices support single cycle loops.

SteveA
CLD

-------------------------------------
FPGA/RT/PDA/TP/DSC
-------------------------------------
0 Kudos
Message 2 of 11
(5,466 Views)
Hi Steve,
 
I started with the NI 9239 Getting Started example and added 9263 nodes for AO.  I thinned the code down to what I think are the bare necessities for IO.  Could you comment on the 9239's Digital Line Output Start/Stop nodes?  I included screen shots of the FPGA and Host in hopes that you could provide feedback on the code and any improvements that might be made.  Next I'll work with the DMA to see if there is any possibility of performing simple acoustic beamforming on the host.
 
Thanks!
Chris
Download All
0 Kudos
Message 3 of 11
(5,431 Views)

The start and stop nodes are needed on this module because it has it's own sample clock.  The way you have them setup in this example is fine.  I would suggest you look at DMA however to transfer your data to the host.

On your host vi, you want to put the Run node outside the loop.  Inside the loop read your data.

SteveA
CLD

-------------------------------------
FPGA/RT/PDA/TP/DSC
-------------------------------------
0 Kudos
Message 4 of 11
(5,367 Views)
SteveA,
 
I thought I was happy with the solution, but there is still a problem with synchronicity.  I created a DMA version (see FPGA_IO_DMA.jpg) and fed a maximal-length sequence to the output of the 9263 while simultaneously recording it on the 9239 (externally the I/O is connected through a coax, and you can see HOST_IO_DMA.jpg for configuration).  I expected a one-to-one correspondence between the MLS and the recorded data with, at worst, a static time delay.  This time (phase) difference should be easily examined by acquiring from the DMA exactly 2^N-1 samples plotted against the original 2^N-1 samples of the MLS.
 
However, on startup, the original MLS (sent to the output) and the recorded MLS are sliding past each other.  I suspect the DMA isn't cleared.  Then, after several seconds, the sampled data settles for the most part and I can see a quasi-static time difference.  But I continue to witness every few seconds, a shift of one sample between the input and output (sliding, not jitter) and, even less often, a multi-sample jump in the time difference.  That is to say, the IO is not behaving synchronously.  It seems the CPU has no problem keeping up with the request and there are no FPGA errors telling me that the DMA has wrapped around, so what is wrong?
 
Thanks again for your help!
Chris
0 Kudos
Message 5 of 11
(5,269 Views)

I don't know why the attachments didn't go through...Here's another try.

Chris

Download All
0 Kudos
Message 6 of 11
(5,268 Views)
Chris,  I'm not sure I know what is going on here DMA has not been my strong point.  Let me think about this.  Anyone else have any Ideas? feel free to jump in.
SteveA
CLD

-------------------------------------
FPGA/RT/PDA/TP/DSC
-------------------------------------
0 Kudos
Message 7 of 11
(5,254 Views)
Okay, I think I have it.  The DMA is not inherently buffered.  That is, I am trying to take N samples from it when it has scarcely placed N samples in.  Anytime I tax the PC's resources (say, changing controls or just excessive calculations), the non-deterministic nature of the PC kicks in...the DMA gains additional samples (N+x) during the time in which the PC collects N.  The addional x samples in the DMA now act as a buffer.  Taxing the resources further can cause the buffer to grow even more...and every time the buffer grows, I see it as a time delay in my data.  To fix it, I'll need to purposely build in a DMA buffer (starting the aquisition a little early) to compensate for variation in the PC operations.  Definitely, I'll want to minimize any controls to ensure the best deterministic results.
 
I'll let you know how it comes out.   Thanks for all your help steveA!
 
Chris
0 Kudos
Message 8 of 11
(5,250 Views)
I don't think the way DMA buffering works is your problem so much as the sequencing of the reads and writes in your program.  In your FPGA program, the reads and writes occur in parallel.  The way the code is written, the output I/O won't update until a point has been read from the AO DMA FIFO, and the sampled input won't be written until space is available inside the AI DMA FIFO.  However, on the host VI, the reads and writes are serialized and the read precedes the write inside the loop.  This means the first time the loop runs, the host VI will try to read some number of points from the FPGA VI before the write will occur.  From the FPGA side, the first time the loop runs, the AI data will be sampled and written to the AI FIFO, but the read from the AO FIFO will eventually timeout since you haven't written anything from your host VI.  The host VI didn't write anything because it was still waiting for N points from the FPGA VI.  In essence, you've effectively created a temporary deadlock.  Eventually, the write timeout on the AO DMA FIFO (FPGA code) or the read timeout on the AI DMA FIFO (host code) will expire and things will start rolling again.  I haven't worked everything out in my head, but I would expect the amount of buffered data available to the input and output slides by one sample each iteration of the loop until eventually it hits zero for one of them.  At this point, you will lose another point of data and the cycle will repeat.  To fix this, you either need reason through your sequencing to ensure this can't happen (it might be as simple as putting the write before the read in your host VI), or you should run the reads and writes in parallel in both VIs.  I would also recommend you create some logic around the timeout indicators on the FPGA and host VIs as debugging aids (or just for peace of mind) if you don't ever expect that a timeout should occur.  Either that, or you will need to write your program so that it can tolerate timeouts without loss of data if that's important for your application.

Finally, with this sort of stimulus / response or wrap back application, you should be aware of the input delay with the 9239.  Because this module internally performs digital filtering as part of the conversion process, there is an inherent delay from when a change in the external signal is seen in the digitized data.  This is referred to as the input delay in the specifications of the module and varies as a function of the sample rate.  This means the acquired signal from the 9239 will lag the signal generated by the 9263 by more than a single sample.  However, the lag should be constant throughout the acquisition and not "slide by a sample".
0 Kudos
Message 9 of 11
(5,227 Views)
Reddog,
 
You were correct that buffering the data in the FIFO was not the cure...although it did help somewhat.  As for the FPGA code, I guess I expected the "write" to output zeros (or something) if the AO FIFO was empty and still acquire data into the AI FIFO.  Timeouts could explain what I am seeing.  I'll make the corrections you recommend and also see if I can get the timeout information mapped up.  My application cannot tolerate timeouts. 
 
The input delay doesn't surprise me and can be tolerated.
 
Thanks for your feedback!
Chris
0 Kudos
Message 10 of 11
(5,216 Views)