Hardware Timed Loops Using AI Single Scan.vi
Overview
AI Single Scan.vi, one of the Advanced Analog Input VIs, is often used in single point Real-Time applications, such as PID control. Assuming that the functionality is enabled in the NI-RT.ini file for the RT Engine, AI Single Scan behaves differently in LabVIEW Real-Time than in LabVIEW for Windows. There is also a similar VI in the intermediate DAQ Analog Input palette. This version of the VI just calls AI Single Scan and has the same inputs, except data in.
Using the Wait and Wait Until Next ms Multiple functions, a LabVIEW Real-Time programmer can achieve loop rates with sleep modes of 1000 Hz at best. A user can achieve higher loop rates but this would be done without sleeping at all. However, a new functionality of AI Single Scan for LabVIEW Real-Time allows the analog input scan clock to control when a thread sleeps and wakes up. This also gives a sleeping resolution finer than 1 millisecond.
After a DAQ board is configured for non-buffered input at a certain scan rate and is ready to begin sampling, the hardware scan clock controls the timing of analog to digital (A/D) conversions. Each time the scan clock pulses, the board performs an A/D conversion for every channel in the scan list and generates an interrupt that is handled by the AI Single Scan.vi in LabVIEW Real-Time. The DAQ board stores the data from the scan in its hardware FIFO buffer. Meanwhile, the thread from which the AI Single Scan.vi was called wakes up when the interrupt is detected, and the AI Single Scan.vi acquires the data from the hardware FIFO buffer. The rest of the code in the loop is executed, and upon the next iteration, the AI Single Scan.vi node goes to sleep again to repeat the cycle.
This document will discuss some of the features of AI Single Scan.vi in relation to LabVIEW Real-Time.
Table of Contents
Using Sleep Mode to Time Loops
In LabVIEW for Windows, when doing non-buffered analog input (buffer size = 0), AI Single Scan will return immediately if there is data in the FIFO of the DAQ board. If there is no data in the FIFO, then AI Single Scan will wait until data arrives in the FIFO before returning.In LabVIEW Real-Time, starting with NI-DAQ 6.7.0, AI Single Scan has a sleep mode which is enabled by default in the NI-RT.ini file. When sleep mode is enabled, and you are performing non-buffered analog input, when AI Single Scan is called, it will sleep until the next active scan clock edge occurs. Only then will AI Single Scan return, even if the FIFO had data when AI Single Scan was called. While AI Single Scan is waiting for the next active scan clock edge it causes the entire thread it is in to sleep. A thread is comprised of all the VIs (and other processes) in the same execution system at the same priority, so the VI AI Single Scan is in, and all other VIs set to the same priority and execution system as the VI that AI Single Scan is in, will sleep until the next active scan clock edge. Since Data Acquisition is usually done at time critical priority, the sleep mode of AI Single Scan is necessary to allow lower priority processes to run.
Without this sleep mode, you would have to use Wait.vi or Wait Until Next ms Multiple.vi to make your VIs sleep to allow lower priority VIs to run, but these Wait VIs only have resolution of 1 millisecond. With this sleep mode, AI Single Scan provides the right amount of sleep time automatically.
Preallocating arrays for Control Loops
Since memory management harms determinism, AI Single Scan has an input called data in. AI Single Scan is a polymorphic VI, meaning it can be configured for different data types. The Single-Point Waveform version is the most common version of this VI used in Real-Time applications and is shown below. This document will discuss the features of this VI specifically, but the principles apply to the other versions as well.

When data in is not wired, a call is made to the LabVIEW Memory Manager to allocate an array for scaled data. In order to prevent memory management, an array of the same size as scaled data out is expected to be passed in. This array would be created at the beginning of the program, and passed into the loop where AI Single Scan is located.
See Also:
Preallocating Arrays for Deterministic Loops
Using the AI Single Scan Opcode
AI Single Scan has an input called opcode which specifies the type of data retrieval the VI performs. There are 5 opcodes:
0: Do not change the opcode setting (default input).
1: Read oldest data (default setting).
2: Read newest data.
3: Set the value of the data remaining indicator only (no data is read).
4: Empty the FIFO only (for timed, non-buffered acquisition only). No data is returned.
The two of most interest for LabVIEW Real-Time applications are 1. Read oldest data, and 2. Read newest data. When performing non-buffered input, read oldest data will cause AI Single Scan to return the oldest scan from the FIFO after the next scan clock active edge arrive, and leave the other scans in the FIFO. Read newest data will return the scan that gets acquired at the next scan clock active edge, and clear the FIFO of old data.
For single point real-time applications, Read newest data should be used at least on the first iteration so that the FIFO is cleared of any scans that have arrived between the time that AI Start was called, and the first call of AI Single Scan. After the first iteration Read oldest data can be used in conjunction with data remaining to monitor if the VI is keeping real-time, as will be discussed next.
Determining Real-Time Status Using the Data Remaining Parameter
The data remaining output can be used to monitor whether or not an application is keeping up with the data coming into the FIFO. When using the Read oldest data opcode, if the FIFO had been initially cleared on the first iteration by using read newest data, then each new scan will also be the oldest, and after each call of AI Single Scan, the FIFO should be empty, and the data remaining output should be zero, indicating that data is not remaining in the FIFO. If for some reason, the program does not keep real-time, AI Single Scan is called with read newest data and there are one or more scans in the FIFO, then the data remaining output will be 1, indicating that your program did not keep real-time.
Verifying the Sleep Mode Configuration
You can verify that this sleeping functionality is enabled by looking for the following entry in your NI-RT.ini file associated with the target you are executing code on:
[NI-DAQ]
UseSleepingTimedNonBuffered=TRUE
This entry is present by default for all networked LabVIEW Real-Time Series PXI Controller targets. Networked LabVIEW Real-Time Series PXI Controller's NI-RT.ini files can be found in C:\NI-RT.ini of the target's hard drive. The AI Single Scan sleep mode is not turned on by default for the 7030 family LabVIEW Real-Time Series DAQ devices in LabVIEW RT 5.1.2, but is turned on by default in LabVIEW RT 6i. The 7030 product family's NI-RT.ini file can be found in your ~\LabVIEW RT\Resource folder.
Related Links:
Optimized Hardware-Timed PID Loop
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
