Saturday, September 25, 2010

Accessing Previous Loop Data

When programming with loops, you often need to access data from previous iterations of the loop. For example, you may have a VI that reads the temperature and displays it on a graph. If you want to display a running average of the temperature as well, you need to use data generated in previous iterations. Two ways of accessing this data include the shift register and theFeedback Node.

Shift Registers

Use shift registers on For Loops and While Loops to transfer values from one loop iteration to the next. Shift registers are similar to static variables in text-based programming languages.
shiftregs.png A shift register appears as a pair of terminals, shown in Media 1, directly opposite each other on the vertical sides of the loop border. The right terminal contains an up arrow and stores data on the completion of an iteration. LabVIEW transfers the data connected to the right side of the register to the next iteration. Create a shift register by right-clicking the left or right border of a loop and selecting Add Shift Register from the shortcut menu.
shift register transfers any data type and automatically changes to the data type of the first object wired to the shift register. The data you wire to the terminals of each shift register must be the same type.
To initialize a shift register, wire any value from outside the loop to the left terminal. If you do not initialize the shift register, the loop uses the value written to the shift register when the loop last executed or the default value for the data type if the loop has never executed.
Use a loop with an uninitialized shift register to run a VI repeatedly so that each time the VI runs, the initial output of the shift register is the last value from the previous execution. Use an uninitialized shift register to preserve state information between subsequent executions of a VI. After the loop executes, the last value stored in the shift register remains at the right terminal. If you wire the right terminal outside the loop, the wire transfers the last value stored in the shift register.
You can add more than one shift register to a loop. If you have multiple operations within a loop, use multiple shift registers to store the data values from those different processes in the structure. The block diagram in Figure 1 uses two initialized shift registers.
Figure 1
Figure 1 (shiftregex.png)

Stacked Shift Registers

To create a stacked shift register, right-click the left terminal and select Add Element from the shortcut menu. Stacked shift registers let you access data from previous loop iterations. Stacked shift registers remember values from previous iterations and carry those values to the next iterations.
Stacked shift registers, as shown in Figure 2, can only occur on the left side of the loop because the right terminal only transfers the data generated from the current iteration to the next iteration.
Figure 2
Figure 2 (stackshiftregex.png)
If you add two more elements to the left terminal, values from the last three iterations carry over to the next iteration, with the most recent iteration value stored in the top shift register. The second terminal stores the data passed to it from the previous iteration, and the bottom terminal stores data from two iterations ago.

Feedback Nodes

feedback.png The Feedback Node, shown in Media 4, appears automatically in a For Loop or While Loop if you wire the output of a subVI, function, or group of subVIs and functions to the input of that same VI, function, or group. Like a shift register, the Feedback Node stores data when the loop completes an iteration, sends that value to the next iteration of the loop, and transfers any data type. Use the Feedback Node to avoid unnecessarily long wires in loops. The Feedback Node arrow indicates in which direction the data flows along the wire.
You also can select the Feedback Node on the Structures palette and place it inside a For Loop or While Loop. If you place the Feedback Node on the wire before you branch the wire that connects the data to the tunnel, the Feedback Node passes each value to the tunnel. If you place the Feedback Node on the wire after you branch the wire that connects data to the tunnel, the Feedback Node passes each value back to the input of the VI or function and then passes the last value to the tunnel. The Accessing Previous Data VIexercise contains an example of this behavior.

Content actions


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers