Advanced Data Acquisition


In this section, we'll look at some more advanced scenarios in data acquisition, such as timing and triggering, continuous data acquisition, streaming data to disk, and counting events.

DAQmx Timing and DAQmx Trigger

As we mentioned at the beginning of this section, you can configure timing and triggering options before you start using your task. In this case, your NI-DAQmx applications will be logically organized something like Figure 11.54.

Figure 11.54. Block diagram showing the logical organization of a typical NI-DAQmx application


Both DAQmx Timing and DAQmx Trigger are polymorphic VIs. After placing them on the block diagram, use the Polymorphic VI Selector ring to choose a specific instance.

Figure 11.55. DAQmx Timing


Figure 11.56. DAQmx Trigger


DAQmx Timing (Measurement I/O>> DAQmx Data Acquisition palette) configures the number of samples to acquire or generate and creates a buffer when needed. The instances of this polymorphic VI correspond to the type of timing to use for the task. The DAQmx Timing properties include all timing options included in this VI and additional timing options.

DAQmx Trigger (Measurement I/O>>DAQmx Data Acquisition palette) configures triggering for the task. The instances of this polymorphic VI correspond to the trigger and trigger type to configure.

The DAQmx Trigger properties include all triggering options included in this VI and additional triggering options.

Now, let's take what you've learned about NI-DAQmx timing and triggering and build a VI using both of these features.

Activity 11-6: Triggered Data Acquisition Using Tasks

You will now learn how to use the DAQmx Timing and DAQmx Triggering VIs to acquire a buffered analog input signal when triggered by a digital line. Each time the trigger event occurs, you will read the acquired data and display it in a waveform graph. You will use the VI created in Activity 11-4 to generate the trigger signal needed for acquiring the data and the VI created in Activity 11-5 to generate the analog signal that will be acquired.

For this activity to run, you will need a physical NI-DAQmx device. The signal connection (wiring) table is at the end of this activity.


1.

Build the front panel shown in Figure 11.57.

Figure 11.57. Front panel of the VI you will create during this activity


DAQmx Physical Channel Control

The physical channels control on the front panel is a DAQmx Physical Channel (found on the Modern>>I/O>>DAQmx Name Controls palette).

2.

Build the block diagram shown in Figure 11.58. All DAQmx VIs used in this activity are found on the Measurement I/O>>DAQmx Data Acquisition palette, with the exception of DAQmx Control Task, which is found on the Measurement I/O>>DAQmx Data Acquisition>>DAQmx Advanced Task Options palette.

DAQmx Control Task

Figure 11.58. Block diagram of the VI you will create during this activity


3.

Configure DAQmx Create Virtual to create an analog input voltage task by setting the Polymorphic VI Selector to Analog Input>>Voltage.

4.

Configure the task for finite acquisition by first setting the Polymorphic VI Selector of DAQmx Timing to Sample Clock (Analog/Counter/Digital) and then setting the sample mode input to Finite Samples. You can create the sample mode ring constant by right-clicking on the sample mode input and selecting Create>>Constant from the pop-up menu.

5.

Configure the task for Digital Edge start triggering by setting the Polymorphic VI Selector of DAQmx Trigger to Start>>Digital Edge.

There are a lot of useful triggering optionsdigital edge triggering is just one possibility. You can also trigger off a digital pattern (the state of multiple lines in a port), an analog edge (when an analog signal crosses some threshold), or an analog window (only acquire data when an analog signal is within a specified range). This is a very powerful feature of NI-DAQmx!

6.

Configure DAQmx Read to return a 1D DBL Array from a single channel by setting its Polymorphic VI Selector to Analog>>Single Channel>>Multiple Samples>>1D DBL.

DAQmx Read

7.

Allow the acquisition to be aborted by placing DAQmx Control Task (found on the Measurement I/O>>DAQmx Data Acquisition>>DAQmx Advanced Task Options palette) in the lower loop, and setting its action input to abort. You can create the action ring constant by right-clicking on the action input and selecting Create>>Constant from the pop-up menu.

DAQmx Control Task

Sometimes errors are good! For example, in this activity, the upper loop will keep running until an error occurs (note that the error cluster is wired to the Conditional Terminal of the upper loop, which is set to Stop if True). We will intentionally generate an error that occurs in the upper loop by calling DAQmx Control Task (in the lower loop) with the abort action. When the DAQmx Read is aborted, it will generate error code -88710, which basically means "the read operation failed, because the task was aborted." Because error -88710 is expected (and intentional, as our way of exiting the loop), we do not want to display this error to the end user. So, the General Error handler is used to cancel that specific error (and only that error).

8.

Use the General Error Handler (found on the Programming>>Dialog and User Interface palette) to filter error code -88710, by setting type of dialog to no dialog, exception action to no action, and exception code to -88710.

General Error Handler

9.

Use Merge Errors (found on the Programming>>Dialog and User Interface palette) to combine the error clusters of the upper and lower loops. This will ensure that if an error occurs in either loop, we will catch it.

Merge Errors

10.

Save the VI as triggered Buffered Analog In.vi in your MYWORK folder.

11.

Before you run your VI, you will need to connect your signals. Use the signal connection list in Table 11.2 to help you.

Table 11.2. Signal Connections
DescriptionInputOutput

Connect analog output channel 0 to analog input channel 0. Connect reference channel to ground.

dev1/ai0 dev1/ai4

dev1/ao0 dev1/gnd

Connect the port 0 line 0 digital output to the programmable function input (PFI) trigger line 0.

dev1/pfi0

dev1/port0/line0


12.

Open Write to Digital Line.vi that you created in Activity 11-4. You will use this VI to generate the signal that triggers the analog read task.

13.

Open Write Continuous Analog Waveform.vi that you created in Activity 11-5. You will use this VI to generate the analog signal that will be acquired.

14.

Configure the acquisition setup parameters on the front panel (Source, Timing, and Trigger) and run your VI. Toggle the digital output Boolean in Write to Digital Line.vi to TRUE.

Congratulations. You've just created some very powerful data acquisition tools! You will be able to use these VIs, and variations of them, to achieve all sorts of functionalityyour imagination is the limit!

Multichannel Acquisition

Earlier, you did an activity where you acquired several channels at once. This is known as multi-channel acquisition.

This is a good time to point out the distinction between scan rate and sampling rate:

  • Sampling rate refers to how often a measurement should be taken for a particular data channel.

  • Scan rate refers to how often a data acquisition board samples ALL the channels, usually in quick successive fashion (hence the term "scan").

On DAQ boards, we usually talk about setting the scan rate. Most of the time scan rate = sampling rate. However, that is not always true, and there is an important limitation of multi-channel I/O that you need to be aware of.

If you set a high scan rate for multiple channels, and observe the data from each channel over actual time (not over array index values), you will notice a successive phase delay between each channel. Why is this? Most DAQ devices can only perform one A/D conversion at a time. That's why it's called scanning: The data at the input channels is digitized sequentially one channel at a time. A delay, called the interchannel delay (depicted in Figure 11.59), arises between each channel sample.

Figure 11.59. Interchannel delay


By default, the interchannel delay is as small as possible, but is highly dependent on the DAQ device. A few DAQ devices, such as the National Instruments S Series devices, do support simultaneous sampling of channels. Other devices support either interval scanning or round robin scanning (depicted in Figure 11.60). Devices that support interval scanning have a much smaller interchannel delay than those that use round robin scanning.

Figure 11.60. Interval scanning (left) and round robin scanning (right)


With DC and low-frequency signals, this phase delay in scans is generally not a problem. The interchannel delay may be so much smaller than the scan period that the DAQ device appears to virtually sample each channel simultaneously: for example, the interchannel delay may be in the microsecond range and the sampling rate may be 1 scan/sec. However, at higher frequencies, the delay can be very noticeable and may present measurement problems if you are depending on the signals being synchronized.

Continuous Data Acquisition

Continuous data acquisition, or real-time data acquisition, returns data from an acquisition in progress without interrupting the acquisition. This approach usually involves a circular buffer scheme, as shown in Figure 11.61. You specify the size of a large circular buffer. The DAQ device collects data and stores the data in this buffer. When the buffer is full, the DAQ device starts writing data at the beginning of the buffer (writing over the previously stored data, whether or not it has been read by LabVIEW). This process continues until the system acquires the specified number of samples, LabVIEW clears the operation, or an error occurs. Continuous data acquisition is useful for applications such as streaming data to disk and displaying data in real time.

Figure 11.61. Circular buffer


In the next activity, you will create a VI that acquires data continuously and plots the most recent data.

Activity 11-7: Continuous Acquisition

You will create a VI that performs a continuous acquisition by configuring NI-DAQmx to acquire data into a circular buffer. You will read data out of the circular buffer as it becomes available and display it on a waveform chart. You will also validate that you are reading data fast enough (and that the buffer has not overflowed, causing data loss) by checking whether the amount of data available is less than the size of the circular buffer.

1.

Build the front panel shown in Figure 11.62.

Figure 11.62. Front panel of the VI you will create during this activity


Make sure to use a Waveform Chart control (rather than a Waveform Graph) because we want the chart to display all the data that we have read, not just the data that we read each time we read data from the circular buffer.

DAQmx Physical Channel Control

The physical channels to read control on the front panel is a DAQmx Physical Channel (found on the Modern>>I/O>>DAQmx Name Controls palette).

2.

Build the block diagram shown in Figure 11.63. All DAQmx VIs used in this activity may be found on the Measurement I/O>>DAQmx Data Acquisition palette.



Figure 11.63. Block diagram of the VI you will create during this activity


3.

Configure DAQmx Create Virtual to create an analog input voltage task by setting the Polymorphic VI Selector to Analog Input>>Voltage.

4.

Configure the DAQmx Timing to set the task to continuous acquisition mode by setting the Polymorphic VI Selector to Sample Clock (Analog/Counter/Digital) and setting the sample mode input to Continuous Samples.

5.

Configure the DAQmx Read Property Node to read the Status>>Available Samples Per Channel attribute. This property tells you the number of samples that are in the circular buffer. You will pass this value into DAQmx Read, in order to read all of the available data in the buffer.

DAQmx Read Property Node

DAQmx Read Property Node (Measurement I/O>>DAQmx Data Acquisition palette) is a Property Node with the DAQmx Read class preselected (see Figure 11.64). Right-click the Property Node and choose Select Filter from the shortcut menu to make the Property Node show only the properties supported by a particular device installed in the system or supported by all the devices installed in the system.

Figure 11.64. DAQmx Read Property Node


6.

Configure DAQmx Read to read the data as a 2D array (channels are in rows and samples are in columns) by setting the Polymorphic VI Selector to Analog>>Multiple Channels>>Multiple Samples>>2D DBL.

7.

Save the VI as Continuous Acquisition.vi in your MYWORK folder.

Now configure the acquisition setup parameters on the front panel and run your VI. Note that your loop rate must be fast enough that the buffer does not overflow! Adjust the Buffer Read Loop Rate so that each loop cycle time is less than the time it takes to fill the buffer. You can calculate the time that it takes to fill the buffer using the following, simple equation:

Time to Fill the Buffer = Buffer Size / Sampling Rate 


The Continuous Acquisition VI that you just created is designed to run continuously, or "forever." In order to accomplish this without running out of memory, a fixed-length buffer is allocated, filled up with samples of data from beginning to end, and then the data at the beginning is overwritten as the buffer is filled up again. As an example, suppose this VI were run with a buffer size of 10 and the sampling rate was once per second. Here are snapshots of how the buffer is being filled up with data:

Before the VI is run, the buffer is empty:

After one second, one sample is filled:

After nine seconds, the buffer is almost full:

After twelve seconds, the buffer has filled up and the data at the beginning is being overwritten:

Why the Samples in Buffer (Scan Backlog) indicator? It's pretty useful to know if LabVIEW is keeping up with reading the data. If the buffer fills up faster than your VI can retrieve the data in it, you will start losing some of this data, because the buffer will be overwritten.

Are you ready for some TLAs (that's "Three-Letter Acronyms," as you learned in Chapter 10)? You'll definitely want to refer back to the "DAQ and Other Data Acquisition Acronyms" section of Chapter 10, if you want to understand these next two paragraphs!

The Continuous Acquisition VI you just made takes advantage of NI-DAQmx's hardware timing and memory handling capabilities. If, while your continuous acquisition task is running, an OS event ties up the CPU, NI-DAQmx will use the DAQ device's buffer and DMA capability (if any) to continue collecting data without any CPU involvement. DMA allows the DAQ hardware to write directly to the computer's memory, even if the processor is tied up. If the OS event interrupts the CPU longer than the on-board FIFO buffer and the DMA buffer can handle, only then will NI-DAQmx lose data samples.

This can best be understood by referring to the previous four snapshots of the buffer. First, assume the DAQ device has no on-board FIFO, but it has DMA capability. Ideally, the DAQ hardware will write data into the buffer continuously, and LabVIEW will be continuously reading a few samples behind the last sample written to the buffer. Suppose the CPU gets tied up after one second, and LabVIEW has read the first sample. When the CPU is tied up, the DAQ device can write to this buffer, but LabVIEW can't read from the buffer. If after twelve seconds, the CPU is still tied up, then LabVIEW has missed the data in the second slot from the left when it contained good data (light gray).

Streaming Data to a File

You have already written exercises that send data to a spreadsheet file. In those exercises, LabVIEW converted the data to a spreadsheet file format and stored it in an ASCII file after the acquisition completed. A different and sometimes more efficient approach is to write small pieces of the data to the hard disk while the acquisition is still in progress. This type of file I/O is called streaming. An advantage of streaming data to file is that it's fast, so you can execute continuous acquisition applications and yet have a stored copy of all the sampled data. Another reason to stream data to file is that if you are acquiring data continuously at a very fast rate for a long period of time, you will likely run out of memory.

Unless your data rate is very low (for example, less than 10 samples/sec), you normally will need to stream to binary, not ASCII, files. Binary files are much smaller in size and allow you to write data more compactly than is possible with text files.

With continuous applications, the speed at which LabVIEW can retrieve data from the acquisition buffer and then stream it to disk is crucial. You must be able to read and stream the data fast enough so that the DAQ device does not attempt to overwrite unread data in the circular buffer. To increase the efficiency of the data retrieval, you should avoid executing other functions, such as analysis functions, while the acquisition is in progress. Also, you can configure DAQmx Read to return raw binary data rather than voltage data (by selecting one of the raw data modes beneath More>>Raw>> from its Polymorphic VI Selector). This increases the efficiency of the retrieval as well as the streaming. When you configure DAQmx Read to produce only binary data, it can return the data faster to the buffer than if you used the analog waveform or voltage modes. One disadvantage to reading and streaming binary data is that users of other applications cannot easily read the file.

You can easily modify a continuous DAQ VI to incorporate the streaming feature. Although you may not have used binary files before, you can still build the VI in the following example. We will be discussing binary and other file types in Chapter 14.

Activity 11-8: Streaming Data to File

In this activity, you will modify Continuous Acquisition.vi, which you created in Activity 11-7, so that it streams data to file.

1.

Open Continuous Acquisition.vi, which you created in Activity 11-7, and save a copy as Continuous Acquisition to File.vi.

2.

Modify the VI so that it looks like the front panel and block diagram in Figures 11.65 and 11.66.

Figure 11.65. Front panel of the VI you will create during this activity


Figure 11.66. Block diagram of the VI you will create during this activity


In this activity, you'll need to use the Write to Binary File function (from the Programming>>File I/O palette), which you learned about in Chapter 9, "Exploring Strings and File I/O." You'll learn more about reading and writing binary files in Chapter 14.

3.

Use the Max & Min function to choose the max of points available or minimum points to read, as the number of data points to read by DAQmx Read. This will allow the software to run efficiently. (If the buffer already contains more samples than your threshold, it will be emptied in one large gulp. Otherwise, the program will wait until it can take the smallest nibble you specified. This results in the fewest cycles.)

Max & Min

4.

Test for a buffer overflow by comparing whether the buffer size is less than or equal to the points available in the buffer. If a buffer overflow occurs, exit the While Loop.

5.

Run this VI for a few seconds. You are now streaming data to a file!

To see the data written to disk, use the companion example VI Read Streamed Data File.vi, shown in Figures 11.67 and 11.68, found on the CD at EVERYONE\CH11. We've provided this VI for you because it involves a few concepts, such as type casting data, which you have not yet learned about. (You will learn about the Type Cast function in Chapter 14.)

Figure 11.67. Read Streamed Data File.vi front panel, used for reading the data files you create during this activity


Figure 11.68. Read Streamed Data File.vi block diagram, used for reading the data files you create during this activity


Run this VI, choosing the filename you used in the previous VI. Notice that the first VI, which streams data, doesn't attempt to graph the data. The point of streaming is to acquire data to the disk fastand look at it later.

Take a look at the VI's block diagram to see how it reads the binary data from the file (see Figure 11.68). Some of the stuff should make sense, but some of it might puzzle you. Don't waste time trying to figure out the puzzling stuff yet. We'll get to it later.

Counting Frequency and Events

As you know, digital signals can be either off or on, 0 or 1, true or false. Despite the fact that digital signals are bi-polar, they actually have a lot of interesting personality! Digital signals can have a wealth of timing and event characteristics. For example, how many times has a digital line changed states, at what rate is it changing state, and for what amount of time has it been in the on state versus the off state? You can actually measure these digital signal characteristics and generate signals having the characteristics of your choosing!

However, trying to measure and generate digital signal timing and events using only software is very limited and complicatedit just doesn't work very well because software loops run slowly and unsteadily when compared to most digital signals. Fortunately, there is a special type of signal conditioning hardware for acquiring and generating digital timing and event signals: It is called the counter (or counter/timer).

A counter is an ASIC (Application Specific Integrated Circuit) computer chip that is designed for measuring and generating digital timing and events. It is likely that your multifunction DAQ device will have one or two counters on it. There also exist specific Counter/Timer Devices that have several counters (for example, 4, 8, or 16 counters) and several digital I/O lines but no analog I/O.

Measuring Digital Events and Timing with Counters

So, what kinds of counter measurements might you want to make? Table 11.3 lists common counter measurements, some of which are also illustrated in Figure 11.69.

Table 11.3. Counter Measurement Types
MeasurementDescription

Edge Count

The number of pulses (rising or falling edges) that have occurred.

Period

The spacing between pulses.

Frequency

The rate at which pulses are occurring (1/period).

Pulse Width

The time that a pulse is active.

Semi-Period

The time that a pulse is either active or inactive (idle).

Two-Edge Separation

The time delay between the pulse edges of two signals.


Figure 11.69. Counter measurement types


Activity 11-9: Counting Digital Pulses

So, how do you count pulses in LabVIEW? In this exercise, you will see just how simple it is to count pulses with NI-DAQmx in LabVIEW.

For this activity, a simulated NI-DAQmx device will run, but it will not show pulse counts. You will need a physical NI-DAQmx device that has at least one counter channel, in order to count pulses.


1.

Open a new VI.

2.

Build the front panel and block diagram shown in Figures 11.70 and 11.71. All of the DAQmx VIs used in this example are found on the Measurement I/O>>DAQmx palette.

Figure 11.70. Front panel of the VI you will create during this activity


Figure 11.71. Block diagram of the VI you will create during this activity


3.

After placing DAQmx Create Virtual Channel onto the block diagram, select the Counter Input>>Count Edges instance from the Polymorphic VI Selector, as shown in Figure 11.72.



Figure 11.72. Selecting Counter Input>>Count Edges for DAQmx Create Virtual Channel


4.

Create the counter, edge, count direction, and initial count front panel controls by right-clicking on the corresponding input terminals of DAQmx Create Virtual Channel and selecting Create>>Control from the pop-up menu.

5.

After placing DAQmx Read onto the block diagram, select the Counter>> Single Sample>>U32 instance from the Polymorphic VI Selector, as shown in Figure 11.73.

Figure 11.73. Selecting Counter>>Single Sample>>U32 for DAQmx Read


6.

Save your VI as Count Input Pulses.vi.

7.

Now let's have some fun! Run your VI and start counting pulses. But, how do you generate pulses? Easysimply take a wire and connect it to the ground terminal (GND) of your DAQ device. Then, to generate a pulse, grab the other end of this wire and make it touch the PFI0 terminal (if you have selected CTR0otherwise, use PFI1 for CTR1, etc.). Grounding the counter input creates a pulse for as long as you keep it grounded.

Each time you touch the jumper wire to connect the terminals, you may see hundreds of counts. This is because the connection of the wire touching the terminal is not very good.

Digital Pulse Generation with Counters

Counters also allow you to generate output signals with the timing characteristics that you definethe same characteristics that a counter can measure, as just described. Table 11.4 contains the basic setup parameters for generating signals using your counter, some of which are illustrated in Figure 11.74.

Table 11.4. Pulse Setup Parameter Descriptions
Pulse SetupDescription

High Time

The time that the pulse is high, in seconds.

Low Time

The time that the pulse is low, in seconds.

Initial Delay

The time before pulse generation begins.

Idle State

The polarity of the generated signal:

  • HighThe terminal is at a high state at rest. Pulses move the terminal to a low state.

  • LowThe terminal is at a low state at rest. Pulses move the terminal to a high state.


Figure 11.74. Pulse setup parameter illustration


You can also specify how many pulses you wish to generate, or whether you want to generate pulses forever (or at least until you stop the NI-DAQmx generation task). This is called the Generation Mode, and the possible options are described in Table 11.5.

Table 11.5. Counter Signal Generation Modes
Generation ModeDescription

1 pulse

A single pulse.

N pulses

A train of N consecutive pulses. You specify the number of pulses to generate.

Continuous pulses

A train of continuous pulses.


Activity 11-10: Generating Digital Pulses

Experiment and try to create a VI that generates 1 pulse, another that generates N pulses, and another that generates continuous digital pulses. (Use the Digital Output>>Counter Output task type.)

Wire the counter output that you use for this activity into the counter input you used for the last activity, "Counting Digital Pulses," and count the pulses that you generate.




LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net