Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to sync AI and AO in real time

Hello,

I have the following set-up. (I'm using visual studio 5 w/ C#. My card is NI E series 6259). I'm outputing the contents of a file through one AO channel on my card (using an async write function), and at the same time acquiring input from 16 AI channels. The data being acquired from the AI channels should change according to the output data, and I would like to understand this change by comparing the two. (the AI data and the AO data). For that reason, I must be sure that  the acquired data is properly sync'ed with the AO. I am worried about the two being out of sync, possibly due to one or the other (AO or AI) "dropping frames" as it were, e.g. the input skipping over a buffer of data, somewhere along the line. I want everything to be as close to real-time as possible, and I want to lose as little data as possible. Most importantly, I need the measure of time according to the AI, and the measure of time according to the AO, to be the same (sync'ing).

I hope that is all clear. Does anyone have any ideas how I might best go about doing this? Right now I just have two separate tasks, for AO and AI. Each runs an async operation, then calls itself again. They are essentially running independently. Initially, one's start (Task.Start()) is triggered by the other, but that doesn't really matter. I don't care how much before the AO task the AI task starts, as long as they are sync'ed once they are both running.

What would sharing sample clocks accomplish? I also had the idea that I could have some kind of counter (a digital spike), sync'ed to the AO data, which I could somehow read to the data file along side the AI data, basically keeping time. Maybe I could use the internal counters and clocks on the card for this. I think I can just hook up a (counter, or DO?) output channel to an input channel, and get something to this effect. Might that be useful? And is there a way to do it without having to run a wire from output to input like that? A way internal to the card?

Any help and other ideas would be very greatly appreciated. If you think you might have some help but are unsure about the details I have laid out, please ask me to clarify! I really do need some help.

And one more question—what is the best, most responsive way to implement (16) constantly refreshing graphs of the incoming data, in C#?

Thanks so much!
0 Kudos
Message 1 of 6
(3,845 Views)
Hi scorpsjl,
 
It sounds like you already had the right idea with sharing the sample clocks.  Both the analog output and analog input will timed with the rising edges of the same clock, which will ensure synchronization.  And by using a master/slave type setup, we can make sure that both the AI and AO will start on the same initial clock pulse.  Setting this up to all be done in hardware allows us to achieve very precise timing.
 
I found a good example for you to look at.  Looks like it has been temporarily removed from the website for review (which is why you may not have found it if you have searched the example database).  It will show you how to set up the code to share the sample clock without any external wires:
 
I hope this helps!
 
Thanks,
 
Justin M.
National Instruments

Message Edited by Justin M. on 08-07-2006 10:12 PM

0 Kudos
Message 2 of 6
(3,824 Views)
Thanks Justin.

I've gotten my program to work with shared sample clock, and I'll later be able to debug it and hopefully it offers sufficient performance. Is there always an error thrown when the card skips over / misses samples? Because this might ensure that I'm not missing anything. I know there sometimes is.

However, my problem is that the two tasks that I'm interested in, AI and AO, need to operate at different frequencies. Specifically, I want my AO channel operating at about 44 khz (44,000 samples / second), and want my 16 AI channels sampling at around 10,000 samples a second (10 khz). How can I manage this? Is there some way to set up an internal clock running at a least common multiple of the two frequencies, and then have each task sample after a certain  number of ticks of this clock? Alternatively, since my AI frequency doesn't have to be exactly 10 khz (e.g. it could be 11 khz),  I could set the AO frequency to 44khz and then  use its sample clock for the AI. However, I would still need to make the AI operate at only every 4 beats of sample clock. Is there a way to do this?

Also, I'm curious as to the exact mechanisms at work. THe sample clock i know is a hardware mechanism. Presumably at each tick of the clock, one input and one output sample is acquired or output, via an ADC or DAC respectively. Then what happens? How are buffers involved, how is the operating system involved, and might this slow things down? In particular, since I'm assuming not everything can happen "at once" at each beat, in what order do these things happen?

Thanks a lot! If you can offer any advice, especially in response to the second paragraph where I actually have a programming question, it would be appreciated. I also have two more grab-bag questions, the first being very important to my application:

-What is the best way to create a graph with constant refresh of the incoming data (In C#). I don't have Mesurement studio.

what is the difference between async read/write and just read/write when it comes to AO and AI tasks? Presumably async allows you to call a function at the end of each cycle, but how is this implemented?

thanks again.
-scorpsjl
0 Kudos
Message 3 of 6
(3,809 Views)
scorpsjl,
 
Looks like Laura already posted an answer for you here.  It's a good one!
 
-Justin
0 Kudos
Message 4 of 6
(3,792 Views)
Hello,

Thanks to Laura for her response! So, if I want one task to go at speed X and the other one to sync to it but go at speed X/4 (operate once every four clock ticks) is there a simple way to do it?  Or do I need to use counters? And if I do, might you tell me what the simplest counter set-up would be? Thanks!

Finally, I really need to solve this graphing-problem. I need to graph the data as it comes in. I have written a graphing module in C# but it doesn't go fast enough and eventually I get a buffer error. How can I get around this!?

Thanks again,
scorpsjl
0 Kudos
Message 5 of 6
(3,752 Views)
This thread continues here.
0 Kudos
Message 6 of 6
(3,729 Views)