0% found this document useful (0 votes)
176 views

LabVIEW Programming Structures

The document discusses several common LabVIEW programming structures: - For Loops repeat a subdiagram a set number of times based on a count input. Shift registers transfer values between loop iterations. - While Loops repeat a subdiagram until a conditional input becomes true/false. - Case Structures contain multiple subdiagrams where one executes depending on a selector input value. A default case handles out-of-range values. - Sequence Structures contain subdiagrams that execute sequentially in order. Frames execute from 0 to the last frame to complete the structure. - Formula Nodes perform mathematical operations based on numeric inputs.

Uploaded by

marquezgauna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

LabVIEW Programming Structures

The document discusses several common LabVIEW programming structures: - For Loops repeat a subdiagram a set number of times based on a count input. Shift registers transfer values between loop iterations. - While Loops repeat a subdiagram until a conditional input becomes true/false. - Case Structures contain multiple subdiagrams where one executes depending on a selector input value. A default case handles out-of-range values. - Sequence Structures contain subdiagrams that execute sequentially in order. Frames execute from 0 to the last frame to complete the structure. - Formula Nodes perform mathematical operations based on numeric inputs.

Uploaded by

marquezgauna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

LabVIEW Programming Structures

Introduction
In this session, you will write simple VIs to incorporate basic programming structures in LabVIEW. The structures featured include For Loops, While Loops, Case Structures, Sequence Structures, and Formula Nodes.

Objectives
Learn the differences between a For Loop and a While Loop. Learn how the Case Structure executes. Learn how the Sequence Structure executes. Learn how the Formula Node is used.

Theory
Structures are graphical representations of the loops and case statements of text-based programming languages. Use structures on the block diagram to repeat blocks of code and to execute code conditionally or in a specific order. Like other nodes, structures have terminals that connect them to other block diagram nodes, execute automatically when input data are available, and supply data to output wires when execution completes. Each structure has a distinctive, resizable border to enclose the section of the block diagram that executes according to the rules of the structure. The section of the block diagram inside the structure border is called a subdiagram. The terminals that feed data into and out of structures are called tunnels. A tunnel is a connection point on a structure border. Use the following structures located on the FunctionsStructures palette to control how a block diagram executes processes: For LoopExecutes a subdiagram a set number of times. While LoopExecutes a subdiagram until a condition is met. Case structureContains multiple subdiagrams, only one of which executes depending on the input value passed to the structure. Sequence structureContains one or more subdiagrams, which execute in sequential order. Formula NodePerforms mathematical operations based on numeric input.

For Loops

A For Loop executes a subdiagram a set number of times. The value in the count terminal (an input terminal) indicates how many times to repeat the subdiagram. Set the count explicitly by wiring a value from outside the loop to the left or top side of the count terminal, or set the count implicitly with auto-indexing. The iteration terminal (an output terminal) contains the number of completed iterations. The iteration count always starts at zero. During the first iteration, the iteration terminal returns 0. Both the count and iteration terminals are signed long integers. If you wire a floating-point number to the count terminal, LabVIEW rounds it and coerces it to within range. If you wire 0 or a negative number to the count terminal, the loop does not execute.

While Loops

The While Loop executes the subdiagram until the conditional terminal, an input terminal, receives a specific Boolean value. The default behavior and appearance of the conditional terminal is Continue If True. When a conditional terminal is Continue If True, the While Loop executes its subdiagram until the conditional terminal receives a FALSE value. You can change the behavior and appearance of the conditional terminal by right-clicking the terminal or the border of the While Loop and selecting Stop If True. You also can use the Operating tool to click the conditional terminal to change the condition. When a conditional terminal is Stop If True, the While Loop executes its subdiagram until the conditional terminal receives a TRUE value. Because the VI checks the conditional terminal at the end of each iteration, the While Loop always executes at least one time. The VI is broken if you do not wire the conditional terminal. The iteration terminal (an output terminal) contains the number of completed iterations. The iteration count always starts at zero. During the first iteration, the iteration terminal returns 0.

Shift Registers in Loops

Use shift registers with For Loops and While Loops to transfer values from one loop iteration to the next. A shift register appears as a pair of terminals 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.

A 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. You can create multiple shift registers on a structure, and you can have more than one left terminal to retain more than one previous value. 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. If you do not initialize the register, the loop uses the value written to the 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.

Case Structures

A Case structure has two or more subdiagrams, or cases. Only one subdiagram is visible at a time, and the structure executes only one case at a time. An input value determines which subdiagram executes. The Case structure is similar to case statements or if...then...else statements in text-based programming languages. The case selector label at the top of the Case structure contains the name of the selector value that corresponds to the case in the center and decrement and increment arrows on each side. Click the decrement and increment arrows to scroll through the available cases. You also can click the down arrow next to the case name and select a case from the pull-down menu. Wire an input value, or selector, to the selector terminal to determine which case executes. You must wire an integer, Boolean value, string, or enumerated type value to the selector terminal. You can position the selector terminal anywhere on the left border of the Case structure. If the type of selector terminal is Boolean, the structure has a TRUE case and a FALSE case. If the selector terminal is an integer, string, or enumerated type value, the structure can have arbitrarily many cases.

Specify a default case for the Case structure to handle out-of-range values. Otherwise, you must explicitly list every possible input value. For example, if the selector is an integer and you specify cases for 1, 2, and 3, you must specify a default case to execute if the input value is 4 or any other valid integer value. You can create multiple input and output tunnels for a Case structure. Inputs are available to all cases, but cases do not need to use each input. However, you must define each output tunnel for each case. When you create an output tunnel in one case, tunnels appear at the same position on the border in all the other cases. If at least one output tunnel is not wired, all output tunnels on the structure appear as white squares. You can define a different data source for the same output tunnel in each case, but the data types must be compatible for each case. You also can right-click the output tunnel and select Use Default If Unwired from the shortcut menu to use the default value for the tunnel data type for all unwired tunnels.

Sequence Structures

A Sequence structure contains one or more subdiagrams, or frames, which execute in sequential order. The frame label at the top of the Sequence structure is similar to the case selector label of the Case structure. The frame label contains the frame number in the center and decrement and increment arrows on each side. Click the decrement and increment arrows to scroll through the available frames. You also can click the down arrow next to the frame number and select a frame from the pull-down menu. Unlike the case selector label, you cannot enter values in the frame label. When you add, delete, or rearrange frames in a Sequence structure, LabVIEW automatically adjusts the numbers in the frame labels. A Sequence structure executes frame 0, then frame 1, then frame 2, until the last frame executes. The Sequence structure does not complete execution or return any data until the last frame finishes. The sequence selector identifier at the top of the Sequence structure contains the current frame number and range of frames in the center and decrement and increment arrow buttons on each

side. For example, in the sequence selector, 0 is the current frame number and [0..2] is the range of frames. Click the decrement and increment arrow buttons to scroll through the available frames. Use the Sequence structure to control the execution order when natural data dependency does not exist. A node that receives data from another node depends on the other node for data and always executes after the other node completes execution. Within each frame of a Sequence structure, as in the rest of the block diagram, data dependency determines the execution order of nodes. The tunnels of Sequence structures can have only one data source, unlike Case structures. The output can emit from any frame, but data leaves the Sequence structure only when all frames complete execution, not when the individual frames complete execution. As with Case structures, data at input tunnels are available to all frames. To pass data from one frame to any subsequent frame, use a sequence local terminal. An outward-pointing arrow appears in the sequence local terminal of the frame that contains the data source. The terminal in subsequent frames contains an inward-pointing arrow, indicating that the terminal is a data source for that frame. You cannot use the sequence local terminal in frames that precede the first frame where you wired the sequence local.

Formula Node

The Formula Node is a convenient text-based node you can use to perform mathematical operations on the block diagram. You do not have to access any external code or applications, and you do not have to wire low-level arithmetic functions to create equations. In addition to text-based equation expressions, the Formula Node can accept text-based versions of if statements, while loops, for loops, and do loops, which are familiar to C programmers. These programming elements are similar to what you find in C programming but are not identical. Formula Nodes are useful for equations that have many variables or are otherwise complicated and for using existing text-based code. You can copy and paste the existing text-based code into a Formula Node rather than recreating it graphically. Formula Nodes use type checking to make sure that array indexes are numeric data and that operands to the bit operations are integer data. Formula Nodes also check to make sure array indexes are in range. For arrays, an out-of-range value defaults to zero, and an out-of-range assignment defaults to nop to indicate no operation occurs.

Formula Nodes also perform automatic type conversion. The Formula Node located on the FunctionsStructures and Functions MathematicsFormula palettes is a resizable box similar to the For Loop, While Loop, Case structure, and Sequence structure. However, instead of containing a subdiagram, the Formula Node contains one or more C-like statements delimited by semicolons, as in the following example. As with C, add comments by enclosing them inside a slash/asterisk pair (/*comment*/). When you work with variables, remember the following points: There is no limit to the number of variables or equations in a Formula Node. No two inputs and no two outputs can have the same name, but an output can have the same name as an input. Declare an input variable by right-clicking the Formula Node border and selecting Add Input from the shortcut menu. You cannot declare input variables inside the Formula Node. Declare an output variable by right-clicking the Formula Node border and selecting Add Output from the shortcut menu. The output variable name must match either an input variable name or the name of a variable you declare inside the Formula Node. You can change whether a variable is an input or an output by right-clicking it and selecting Change to Input or Change to Output from the shortcut menu. You can declare and use a variable inside the Formula Node without relating it to an input or output wire. You must wire all input terminals. Variables can be floating-point numeric scalars, whose precision depends on the configuration of your computer. You also can use integers and arrays of numerics for variables. Variables cannot have units.

Session Procedure
Example 1: Second Equation Roots Write a VI that calculate the roots of a second order equation. AX^2 + BX + C = 0 Assume that the roots are real. Run the VI with A=1, B=5 and C=6

Example 2: Square Root VI Complete the following steps to build a VI that checks whether a number is positive. If it is, the VI calculates the square root of the number. Otherwise, the VI returns an error message. 1. Open a new VI and build the following front panel.

2. Build the following block diagram.

a. Place a Case structure located on the FunctionsStructures palette. b. Click the decrement or increment arrow button to select the FALSE case. c. Place the Greater or Equal to 0? function located on the FunctionsComparison palette. This function returns TRUE if Number is greater than or equal to 0. d. Right-click the numeric constant and select Format & Precision from the shortcut menu. Set Digits of Precision to 1, select Floating Point Notation, and click the OK button. This ensures there is no data conversion between the constant and the numeric indicator outside the Case structure. e. Place the One Button Dialog function located on the FunctionsTime & Dialog palette. This function displays a dialog box that will contain the message Error...Negative Number. f. Right-click the message terminal of the One Button Dialog function, select CreateConstant from the shortcut menu, type Error...Negative Number, and press the <Enter> key. g. Select the TRUE case and place the Square Root function located on the FunctionsNumeric palette, as shown in the following block diagram. This function returns the square root of Number.

3. Save the VI as Square Root.vi. 4. Display the front panel and run the VI. If Number is positive, the VI executes the TRUE case and returns the square root of Number. If Number is negative, the VI executes the FALSE case, returns 99999.0, and displays a dialog box with the message Error...Negative Number. 5. Close the VI. Note: Here you can use String indicator in the front panel and string constant in the diagram panel in the place of the false case to shows the message rather than the one button dialog.

Example 3: Summation of the series terms Write a VI that that sums the first 10 terms of the series (1+i^2). Where i is an integer varies from 0 to infinity.

10

Example 4: Index of an array Write a VI that determine the index of an array when the values of its elements less than 5

11

Assignment 2:
1. What are the differences between a For Loop and a While Loop? 2. How does a Case Structure execute? 3. Write a VI that solve any second order equation, you should use the Formula Node and the case statement. The VI has three inputs A,B, and C as in assignment 1, and two outputs, the real part and the imaginary part.

Assignment 3:
1. How is data transferred between iterations of a loop? 2. Write a VI that that sums the first 10 terms of the series (1+i)^3. Where i is an integer varies from 0 to infinity.

12

You might also like