Presentation on theme: "Previously… We created a simulated temperature reader which alerts if too hot or too cold… Download the solved practice to keep in sync: Thermostat.vi."— Presentation transcript:
1Previously…We created a simulated temperature reader which alerts if too hot or too cold… Download the solved practice to keep in sync: Thermostat.vi
2D. While Loops LabVIEW While Loop Flowchart Pseudo Code 28.09.2012 Repeat (code);Until Condition met;End;Demonstrate creation of while loop:To create a While Loop complete the following:Select the While Loop from the paletteUse the cursor to drag a selection rectangle around the section of the block diagram you want to repeatWhen you release the mouse button, a While Loop boundary encloses the section you selectedAdd block diagram objects to the While Loop by dragging and dropping them inside the While LoopLabVIEW While Loop Flowchart Pseudo Code
3D. While LoopsIteration terminal: returns number of times loop has executed; zero indexedConditional terminal: defines when the loop stopsIteration TerminalConditional Terminal
4PracticeAdd a STOP button to the previous practice, then include everything in a while loop in order to stop the program when the user hit STOP. Add a CHART and display the current iteration number on it. Please note: from now on the RUN CONTINOUSLY button won’t be used, use the RUN button instead. AVOID HITTING THE ABORT BUTTON AT ALL COSTS!!
5D. While Loops – TunnelsTunnels transfer data into and out of structuresThe tunnel adopts the color of the data type wired to the tunnelData pass out of a loop after the loop terminatesWhen a tunnel passes data into a loop, the loop executes only after data arrive at the tunnel
6PracticePlease open again the previous vi. Using an input tunnel display the time the vi has been active (in seconds) and stop it when such time is MORE THAN 20s or if the user hits the STOP button. Think about this: what are the differences between a simple wire and a tunnel? Can a wire HOLD data? Can a tunnel?
7F. Timing a VI Why do you need timing in a VI? Control the frequency at which a loop executesProvide the processor with time to complete other tasks, such as processing the user interface
8F. Timing a VI – Wait Functions A wait function inside a loop allows the VI to sleep for a set amount of timeAllows the processor to address other tasks during the wait timeUses the operating system millisecond clock
9F. Timing a VI – Elapsed Time Express VI Determines how much time elapses after some point in your VIKeep track of time while the VI continues to executeDoes not provide the processor with time to complete other tasks
10PracticeOpen task manager and run the previous vi. Add to the previous practice the proper wait function in order to force the loop time to 200ms Open task manager and run the vi again. Add to the previous practice the proper function in order to display a string version of time elapsed
11E. For Loops LabVIEW For Loop Flowchart Pseudo Code 28.09.2012 N=100; Until i=N:Repeat (code;i=i+1);End;LabVIEW For Loop Flowchart Pseudo Code
12E. For Loops Create a For Loop the same way you create a While Loop If you need to replace an existing While Loop with a For Loop, right-click the border of the While Loop, and select Replace with For Loop from the shortcut menuThe value in the count terminal (an input terminal) indicates how many times to repeat the subdiagram
13E. For Loops – Conditional Terminal You can add a conditional terminal to configure a For Loop to stop when a Boolean condition or an error occurs
14E. For Loops – Numeric Conversion The number of iterations a For Loop executes must be specified in nonnegative integersIf you wire a double-precision, floating-point numeric value to the count terminal, LabVIEW converts the larger numeric value to a 32-bit signed integer
15E. For Loops – Numeric Conversion Normally, when you wire different representation types to the inputs of a function, the function returns an output in the larger or wider formatLabVIEW chooses the representation that uses more bitsHowever, the For Loop count terminal always coerces to a 32-bit signed integerCoercion DotIf you use a signed integer with an unsigned integer, it will coerce to the unsigned integer. If you use an unsigned integer with a floating point, it will coerce to the floating point. If you use a floating point integer with a complex integer, it will coerce to the complex integer. If you use two numbers of the same type with different bit widths, LabVIEW will coerce to the larger of the two bit widths.However, the For Loop count terminal may work in the opposite manner. If you wire a double-precision, floating-point numeric value to the 32-bit count terminal, LabVIEW coerces the larger numeric value to a 32-bit signed integer. Although the conversion is contrary to normal conversion standards, it is necessary, because a For Loop can only execute an integer number of times. The For Loop count terminal will always coerce to a 32-bit signed integer data type.
16E. For Loops – Numeric Conversion Avoid coercion for better performanceChoose matching data typeProgrammatically convert to the matching data type
17PracticeReproduce the following block diagram What will be the result displayed in A? in B? in C? When? What will be the content of D?
18E. For Loops – Conditional Terminal For Loops configured for a conditional exit have:Red glyph next to the count terminalConditional terminal in the lower right corner
19E. For Loop/While Loop Comparison Executes a set number of times unless a conditional terminal is addedCan execute zero timesTunnels automatically output an array of dataWhile LoopStops executing only if the value at the conditional terminal meets the conditionMust execute at least onceTunnels automatically output the last value
20A. Arrays An array consists of elements and dimensions Elements: data that make up the arrayDimension: the length, height, or depth of an arrayAn array can have one or more dimensions and as many as (231)–1 elements per dimension, memory permittingConsider using arrays when you work with a collection of similar data and when you perform repetitive computations
21A. ArraysThe first element shown in the array (3.00) is at index 1 and the second element (1.00) is at index 2The element at index 0 is not shown in this image, because element 1 is selected in the index displayThe element selected in the index display always refers to the element shown in the upper left corner of the element display
22A. Arrays – Creating Place an array shell on the front panel Drag a data object or element into the array shell
23A. Arrays – 2D Array Stores elements in a grid Requires a column index and a row index to locate an element, both of which are zero-basedTo create a multidimensional array on the front panel, right-click the index display and select Add Dimension from the shortcut menuYou also can resize the index display until you have as many dimensions as you want
24A. Arrays – Initializing You can initialize an array, or leave it uninitializedFor initialized arrays, you define the number of elements in each dimension, and the contents of each elementUninitialized arrays have dimension but no elements
25A. Arrays – Creating Constants To create an array constant:Select an array constant on the Functions palettePlace the array shell on the block diagramPlace a constant in the array shellYou can use an array constant to store constant data or as a basis for comparison with another arrayArray constants also are useful for passing data into a subVI
26A. Arrays – Auto-indexing If you wire an array to or from a For Loop or While Loop, you can link each iteration of the loop to an element in that array by enabling auto-indexing on tunnelThe tunnel changes from a solid square to the image shown above to indicate auto-indexing
27A. Arrays – Auto-indexing Input For Loop executes a number of times equal to the number of elements in the array
28A. Arrays – Auto-indexing Input If the iteration count terminal is wired and arrays of different sizes are wired to auto-indexed tunnels, the actual number of iterations becomes the smallest of the choices.LabVIEW does not exceed the array size. This helps to protect against programming error. LabVIEW mathematical functions work the same way—if you wire a 10 element array to the x input of the Add function, and a 5 element array to the y input of the Add function, the output is a 5 element array.Although the for loop runs 5 times, the iterations are zero based, therefore the value of the Iterations indicators is 4.
29PracticeCreate a new VI having the following block diagram Which values will be displayed by A,B and C?
30A. Arrays – Auto-indexing Output When you auto-index an array output tunnel, the output array receives a new element from every iteration of the loopAuto-indexed output arrays are always equal in size to the number of iterations
31A. Arrays – Creating 2D Arrays You can use two For Loops, one inside the other, to create a 2D array
32Practice @HOMEOpen the thermostat practice VI, Instead of generating a single temperature value per cycle, create a set of 100 samples, put them in an array. Display all the 100 samples on both the CHART and the GRAPH, while controlling the boolean indicators only on the AVERAGE of the array Please try to keep you block diagram as tidy as possible