gruppo di misure meccaniche e termiche unibs - dimi previously… we created a simulated temperature...
TRANSCRIPT
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
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
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
D. While Loops
LabVIEW While Loop Flowchart Pseudo Code
Repeat (code);
Until Condition met;
End;
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
D. While Loops
• Iteration terminal: returns number of times loop has executed; zero indexed• Conditional terminal: defines when the loop stops
Iteration Terminal Conditional Terminal
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice
Add 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!!
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
D. While Loops – Tunnels
• Tunnels transfer data into and out of structures• The tunnel adopts the color of the data type wired to the
tunnel• Data pass out of a loop after the loop terminates• When a tunnel
passes data into a loop, the loop executes only after data arrive at the tunnel
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice
Please 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?
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
F. Timing a VI
Why do you need timing in a VI?• Control the frequency at which a loop executes• Provide the processor with time to complete other tasks,
such as processing the user interface
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
F. Timing a VI – Wait Functions
• A wait function inside a loop allows the VI to sleep for a set amount of time• Allows the processor to address other tasks during the wait
time• Uses the operating system millisecond clock
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
F. Timing a VI – Elapsed Time Express VI
• Determines how much time elapses after some point in your VI• Keep track of time while the VI continues to execute• Does not provide the processor with
time to complete other tasks
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice
Open 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
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. For Loops
LabVIEW For Loop Flowchart Pseudo Code
N=100;
i=0;
Until i=N:
Repeat (code;i=i+1);
End;
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. 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 menu• The value in the count terminal (an input terminal)
indicates how many times to repeat the subdiagram
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. 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
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. For Loops – Numeric Conversion
• The number of iterations a For Loop executes must be specified in nonnegative integers
• If 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
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
• Normally, when you wire different representation types to the inputs of a function, the function returns an output in the larger or wider format • LabVIEW chooses the
representation that uses more bits• However, the For Loop
count terminal alwayscoerces to a 32-bit signedinteger
E. For Loops – Numeric Conversion
Coercion Dot
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. For Loops – Numeric Conversion
• Avoid coercion for better performance− Choose matching data type− Programmatically convert to the matching data type
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice
Reproduce the following block diagram
What will be the result displayed in A? in B? in C? When?What will be the content of D?
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. For Loops – Conditional Terminal
For Loops configured for a conditional exit have:• Red glyph next to the count terminal • Conditional terminal in the lower right corner
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
E. For Loop/While Loop Comparison
For Loop
• Executes a set number of times unless a conditional terminal is added
• Can execute zero times• Tunnels automatically output an array
of data
While Loop
• Stops executing only if the value at the conditional terminal meets the condition
• Must execute at least once• Tunnels automatically output the last
value
28.10.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays
• An array consists of elements and dimensions− Elements: data that make up the array− Dimension: the length, height, or depth of an array− An array can have one or more dimensions
and as many as (231)–1 elements per dimension, memory permitting
• Consider using arrays when you work with a collection of similar data and when you perform repetitive computations
2028.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays
• The first element shown in the array (3.00) is at index 1 and the second element (1.00) is at index 2
• The element at index 0 is not shown in this image, because element 1 is selected in the index display
• The element selected in the index display always refers to the element shown in the upper left corner of the element display
2128.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Creating
1. Place an array shell on the front panel2. Drag a data object or element into the array shell
2228.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. 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-based• To create a multidimensional array on the front panel, right-
click the index display and select Add Dimension from the shortcut menu• You also can resize the index display until you have as
many dimensions as you want
23
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Initializing
• You can initialize an array, or leave it uninitialized• For initialized arrays, you define the number of elements in
each dimension, and the contents of each element• Uninitialized arrays have dimension but no elements
24
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Creating Constants
• To create an array constant: − Select an array constant on the
Functions palette − Place the array shell on the block diagram− Place a constant in the array shell• You can use an array constant to store
constant data or as a basis for comparison with another array• Array constants also are useful for passing data into a subVI
25
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. 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 tunnel• The tunnel changes from a solid square to the image shown
above to indicate auto-indexing
2628.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Auto-indexing Input
For Loop executes a number of times equal to the number of elements in the array
2728.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. 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.
2828.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice
Create a new VI having the following block diagram
Which values will be displayed by A,B and C?
28.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Auto-indexing Output
• When you auto-index an array output tunnel, the output array receives a new element from every iteration of the loop• Auto-indexed output arrays are always equal in size to the
number of iterations
3028.09.2012
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
A. Arrays – Creating 2D Arrays
You can use two For Loops, one inside the other, to create a 2D array
31
Gruppo di Misure Meccaniche e TermicheUNIBS - DIMI
Practice @HOME
Open 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
28.09.2012