12-30-2012 09:52 AM
Dave,
Here's a link to a snippet I posted before that shows how to configure a task to "throw away" old data and only return the N most recent samples on each call to DAQmx Read. The counter period task is a little bit of a special case though because if the encoder stops, no further periods will be buffered, and every loop iteration will keep returning the same increasingly-old period data as the encoder slowed to a halt. There are other DAQmx Read properties available to query such as "Total Samples Acquired" which you can use to detect that kind of case though. It's not hard to work around once you know of the need to do so.
The "Samples per Channel" you wire for DAQmx Timing is used to configure a buffer size that the task uses to store data until your application code does a DAQmx Read to retrieve it. In the case of Finite Sampling, it will set the exact buffer size. In the case of Continuous Sampling, it will set a minimum - the DAQmx driver has some default behavior that may create an even bigger buffer than requested to help you not lose samples at higher sample rates.
The "Samples to Read" you wire for DAQmx Read tells how many values to read from that buffer each time you do a read. The buffering itself continues in the background even between loop iterations when you are not calling DAQmx Read.
Sorry, out of time now, gotta go. Hope this much helps some.
-Kevin P
12-31-2012 09:55 AM
Hi Dave,
I wanted to mention that it looks like you are still reading 1000 samples during each loop iteration, and averaging 15 samples from the previous iteration each time. I suggest that you modify your code to more closely resemble my previous post and change the samples per channel to 15 so that it only measures 15 data points.
In terms of forcing execution flow, you would be correct to say that the way you have it will force you to get both the analog sample and the counter average before executing the For Loop. The Case Structure will also wait on input from the For Loop to execute as well. I am not sure if you need these structures for what you want to do in your end result, but it is a valid method of controlling execution.
In the Counter task, you are creating a 1000 element array of Period measurements in your hardware, then sending that array to your software. I believe at that point you are taking the first 15 measurements from the previous iteration (from the Shift Register) and computing the mean from those measurements. You then write the new data to an array and append the 15 measurements to the end of that array so that you have an array with 1015 elements. You then pass that data to the next loop iteration to begin the process again.
In the AI Voltage task, you are only reading in one sample each loop iteration, but there is a buffer on the hardware that stores the data while it is waiting to be read. The DAQmx Timing VI uses samples per channel to determine the buffer size when the sample mode is set to Continuous Samples. You can read about this by bringing up Context Help (Ctrl-H) and hovering your mouse over the function, then selecting “Detailed Help” in the Context Help dialog box.
Double is a data type, and “If you want just a single double…” means “If you want a single element of data type double…”
I hope this helps clear things up!
Regards,
Jason D
Applications Engineer
National Instruments
01-27-2013 10:09 PM
Hi Jason,
Thanks a lot for the input. I was reviewing some stuff in the HELP section and got myself a little confused again about this buffer. According to the Help in Labview 2012, (pasted below) the buffer is a default value of 1000 or 10,000 depending on the sample rate. So, if I set the "samples per channel" at some value less than the default buffer value, what happens to the unfilled elements of the buffer? I am having a hard time getting my code to do what I want it to do, and I suspect that my weak understanding of the buffer is the cause of it. In other words, when my dynamic conditions change, the VI is still outputting a result that would be expected for the initial conditions, which are no longer present, making me think it is reading old data stored in a buffer, but, I can't figure out how to fix this. I want this code to read a few periods (e.g. 20-30), average them, and then throw them all away, and start over again on the next iteration of the WHILE loop. I have adjusted the samples per channel down to values in the 20-35 range from 1000 according to your suggestion.
Also, for the AI voltage buffer that you are describing above, how can I make it read only a SINGLE VOLTAGE VALUE, and not go digging into any buffer for subsequent iterations of the DAQmx READ. I need to have the value only at the instant it is measured, and then have that data eliminated, erased, written over, anything, but not queued up for subsequent manipulation. Is there a way to get the AI Voltage buffer to have a single value only? What parameter can be set to "1" to assure that no buffer values are available to destroy the sequence I am trying to achieve with the VI?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
How Is Buffer Size Determined?
<script type="text/javascript">// // </script> Input Tasks
If your acquisition is finite (sample mode on the Timing function/VI set to Finite Samples), NI-DAQmx allocates a buffer equal in size to the value of the samples per channel attribute/property. For example, if you specify samples per channel of 1,000 samples and your application uses two channels, the buffer size would be 2,000 samples. Thus, the buffer is exactly big enough to hold all the samples you want to acquire.
If the acquisition is continuous (sample mode on the Timing function/VI set to Continuous Samples), NI-DAQmx allocates a buffer equal in size to the value of the samples per channel attribute/property, unless that value is less than the value listed in the following table. If the value of the samples per channel attribute/property is less than the value in the table, NI-DAQmx uses the value in the table.
Sample Rate |
Buffer Size |
No rate specified |
10 kS |
0–100 S/s |
1 kS |
101–10,000 S/s |
10 kS |
10,001–1,000,000 S/s |
100 kS |
>1,000,000 S/s |
1 MS |
01-29-2013 11:22 AM
Hi Dav2010,
The buffer size is set to equal to the samples per channel attribute by default. However, if you were to set the samples per channel property to 15, then the buffer size would be allocated to 1,000 etc (depending on the sample rate). You can overwrite this default buffer size using the Configure Input Buffer Size.vi. Try selecting 15 samples (or 20-30) per channel on the DAQmx Timing VI, then configure the buffer to only allow for 15 samples. Then read 15 samples inside the loop by selecting 15 as the Samples per Channel on the DAQmx Read VI. You may need to adjust the wait in the loop to adjust for timeout errors.
To read a single voltage value for each loop iteration you can set the samples per channel to 1 sample and configure the input buffer to 1 sample.
However, if you are concerned with synchronizing the two tasks, then the architecture below may be more beneficial. This is taken from this example on how to create a buffered period measurement:
https://decibel.ni.com/content/docs/DOC-26286
As well as the synchronization example posted by Jason.:
https://decibel.ni.com/content/docs/DOC-8231
Please let me know if you have any questions about this.
Regards,
01-29-2013 01:03 PM
Hi Kaitlin,
Thanks so much for all the advice. I'll need to study it and spend some time digesting it to configure it into my system. I have 2 questions, please.
1.) If I set the "samples per channel" to 15, then, the buffer remains 1000, unless you override the default with Configure Input Buffer Size.vi.. Is that correct?
Where do I find "Configure Input Buffer Size.vi."?
2.) Regarding the voltage reader, if I set the samples per channel to "1" to get a single voltage per loop, what should the "RATE" on the DAQmx TIMING vi be set to? Does it matter? Currently we have the TIMING vi set to "Sample Clock"
Thanks,
Dave
01-30-2013 10:57 PM
Hi Dave,
1.) That is correct. You can find the the Configure Input Buffer Size.vi on the functions palette under Measurement I/O >> DAQmx >> Advanced. See picture below.
2.) The rate of the sample clock will determine how often a new single sample is written to the buffer. This is up to you to set, but ideally you may choose the same frequency as the input to your counter task so that you will have one voltage sample for each period measurement.
Regards,
01-31-2013 05:35 AM
Hi Kaitlin,
In the sequence, DAQmx CREATE CHANNEL->TIMING->START->READ->CLEAR, where is the "Configure Input Buffer.vi" wired to limit the buffer size? Where does it go? Before TIMING, after TIMING? At the READ?
Thanks,
Dave
02-02-2013 08:27 AM
Dave,
I'm writing again to reiterate my most recent previous post. Be sure to follow the link too. It shows a method for retrieving the most recent samples from a data acq task's buffer while ignoring older ones. It sounded to me like that's what you really want, on-demand recent measurements from tasks that run constantly.
-Kevin P
02-04-2013 12:37 PM
Hi Dave,
The Configure Input Buffer.vi would go just before the Start Task.vi.
Regards,