Anda di halaman 1dari 74

Lesson 4

Implementing a VI
TOPICS
A.
B.
C.
D.
E.
F.

Front Panel Design


LabVIEW Data Types
Documenting Code
While Loops
For Loops
Timing a VI

G. Iterative Data Transfer


H. Plotting Data
I. Case Structures

ni.com/training

A. Front Panel Design


Inputs and outputs lead to front panel design
Retrieve the inputs by the following methods:
Acquiring from a device
Reading directly from a file
Manipulating controls

Output data by the following methods:


Displaying with indicators
Logging to a file
Outputting to a device

ni.com/training

A. Front Panel Design Labels/Captions


Labels short descriptions
Captions long descriptions
Captions do not appear on block diagram

ni.com/training

A. Front Panel Design Color Tips


Start with a gray scheme
Select one or two shades of gray
Add highlight colors sparingly for important settingson
plots, abort buttons, and the slider thumbs

ni.com/training

A. Front Panel Design Spacing

ni.com/training

A. Front Panel Design Text & Fonts


Bad Example

ni.com/training

A. Front Panel Design Tab Controls


Use tab controls to
overlap front panel
controls and indicators
in a smaller area

ni.com/training

A. Front Panel Design Decorations


Use decorations to visually
group or separate objects on
a front panel with boxes, lines,
or arrows
These objects are for
decoration only

ni.com/training

B. LabVIEW Data Types Terminals


Terminals visually communicate information about the data
type represented

ni.com/training

B. LabVIEW Data Types Numerics


The numeric data type represents
numbers of various types
To change the representation
of a numeric, right-click the
control, indicator, or constant,
and select Representation
from the shortcut menu

ni.com/training

B. LabVIEW Data Types Boolean


Behavior of Boolean controls is specified by the mechanical
action
In LabVIEW, the Boolean
data type is represented
with the color green

ni.com/training

Mechanical Action of Booleans

Use the Mechanical Action of Booleans VI located in the NI


Example Finder to learn about the different switch and latch
actions.
DEMONSTRATION

B. Data Types String


A sequence of displayable or non-displayable
ASCII characters
On the front panel, strings appear as tables,
text entry boxes, and labels
Change the display type from the short-cut
menu: Normal, \ Codes, Password and Hex
Edit and manipulate strings with the String functions on the
block diagram
In LabVIEW, the string data type is represented with the
color pink
ni.com/training

B. Data Types Enum


An enum
represents a
pair of values,
a string and a
numeric, where the
enum can be one
of a defined list of
values

ni.com/training

B. Data Types Enum


Enum: enumerated
control, constant, or
indicator
Enums are useful because it is easier
to manipulate numbers than strings
on the block diagram

ni.com/training

B. Data Types Dynamic


Stores the information generated or acquired by an Express
VI
Non-Express VIs do not accept the dynamic data type
To use a built-in VI or function to analyze or process the
dynamic data type, you must convert the data type
Numeric, waveform, or Boolean data indicators or inputs
automatically convert the dynamic data type when wired

In LabVIEW, the dynamic data type is represented with the


color dark blue
ni.com/training

C. Documenting Code Front Panels

Tip Strips
Descriptions
VI Properties
Good Design

ni.com/training

C. Documenting Code Naming


Giving controls and indicators logical and descriptive names
adds usability to front panels

ni.com/training

C. Documenting Code Block Diagram


Use block diagram comments to:
Describe algorithms
Explain the data contents of wires

Use the Labeling tool or place a


free label from the Functions
palette

ni.com/training

Configuring Your LabVIEW Environment


Options Dialog Box
Controls/Functions Palettes page
Select Load palettes during launch to make Search Palettes
immediately usable after launch
Set Palette to Category (Icons and Text)

Block Diagram page


Uncheck Place front panel terminals as icons to place control
and indicator terminals in a compact format
Configure Block Diagram Cleanup to customize your block
diagram
ni.com/training

Configuring Your LabVIEW Environment


Functions Palette
Tack the Functions palette and select ViewChange Visible
Categories then click Select All

Controls Palette
Tack the Controls palette and select ViewChange Visible
Categories then click Select All

ni.com/training

Exercise 4-1
Determine Warnings VI

Create and document a VI.


GOAL

Exercise 4-1
Determine Warnings VI

What happens if the Max. Temp value is lower than the Min.
Temp value?
DISCUSSION

D. While Loops

Repeat (code);

Until Condition met;


End;

LabVIEW While Loop

Flowchart

Pseudo Code
ni.com/training

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

ni.com/training

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
ni.com/training

D. While Loops - Error Checking and Error


Handling
Use an error cluster in a While Loop to stop the While Loop if
an error occurs

ni.com/training

Exercise 4-2
Auto Match VI

Use a While Loop and an iteration terminal and pass data


through a tunnel.
GOAL

Exercise 4-2
Auto Match VI

How many times is the # of iterations indicator updated?


Why?
DISCUSSION

E. For Loops

N=100;
i=0;

Until i=N:

Repeat (code;i=i+1);

End;

LabVIEW For Loop

Flowchart

Pseudo Code
ni.com/training

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

ni.com/training

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

ni.com/training

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

ni.com/training

E. For Loop/While Loop Comparison


For Loop

While Loop

Executes a set number of times unless


a conditional terminal is added
Can execute zero times
Tunnels automatically output an array
of data

Stops executing only if the value at the


conditional terminal meets the
condition
Must execute at least once
Tunnels automatically output the last
value
ni.com/training

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

ni.com/training

E. 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 format
Coercion Dot
LabVIEW chooses the
representation that
uses more bits
However, the For Loop
count terminal always
coerces to a 32-bit signed
integer
ni.com/training

E. For Loops Numeric Conversion


Avoid coercion for better performance
Choose matching data type
Programmatically convert to the matching data type

ni.com/training

Exercise 4-3
Concept: While Loops versus For Loops

Understand when to use a While Loop and when to use a For


Loop.
GOAL

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

ni.com/training

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

ni.com/training

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

ni.com/training

Wait Chart VI

Compare and contrast using a Wait function and the Elapsed


Time Express VI for software timing.
DEMONSTRATION

G. Iterative Data Transfer


When programming with loops, you often need to know the
values of data from previous iterations of the loop
Shift registers transfer values from one loop iteration to the
next

ni.com/training

G. Iterative Data Transfer Shift Registers


Right-click the border and select Add Shift Register from
the shortcut menu
Right shift register stores data on completion of an iteration
Left shift register provides stored data at beginning of the
next iteration

ni.com/training

G. Iterative Data Transfer Initializing


Run once

VI finishes

Block Diagram

Run again

1st run

2nd run

Initialized
Shift
Register

Output = 5

Output = 5

Not
Initialized
Shift
Register

Output = 4

Output = 8

ni.com/training

G. Iterative Data Transfer


Stacked Shift Registers
Stacked shift registers remember values from multiple
previous iterations and carry those values to the next
iterations
Right-click the left shift register and select Add Element
from the shortcut menu

ni.com/training

Exercise 4-4
Average Temperature VI

Use a For Loop and shift registers to average data.


GOAL

Exercise 4-4
Average Temperature VI

You calculated the average of the last 3 temperature


readings. How would you modify the VI to calculate the
average of the last 5 temperature readings?
DISCUSSION

H. Plotting Data Waveform Chart


Special type of numeric indicator that displays one or more
plots of data, typically acquired at a constant rate
Displays single or multiple plots

ni.com/training

H. Plotting Data Chart Update Modes


Right-click the chart and select AdvancedUpdate Mode
from the shortcut menu
Strip chart is the default update mode
Scope chart and Sweep chart modes display plots
significantly faster than the strip chart mode

ni.com/training

H. Plotting Data

ni.com/training

H. Plotting Data Waveform Graphs


Use the Context Help window to determine how to wire multiplot data to Waveform Graphs and XY Graphs

ni.com/training

Exercise 4-5
Temperature Multiplot VI

Plot multiple data sets on a single waveform chart and


customize the chart view.
GOAL

Exercise 4-5
Temperature Multiplot VI

For this exercise, is it better to use a chart or a graph?


DISCUSSION

I. Case Structures

Have two or more subdiagrams or cases


Execute and displays only one case at a time
An input value determines which subdiagram to execute
Similar to case statements or if...then...else
statements in text-based programming languages

ni.com/training

I. Case Structures
Case Selector Label: contains the name of the current case
and decrement and increment buttons on each side

Selector Terminal: Wire an input value, or selector, to


determine which case executes
ni.com/training

I. Case Structures Default Case


You can specify a default case
for the Case structure
If you specified cases for 1, 2,
and 3, but you get an input of
4, the Case structure executes
the default case

Right-click the Case structure


border to add, duplicate,
remove, or rearrange cases
and to select a default case

ni.com/training

I. Case Structures Input & Output Tunnels


You can create multiple input and output tunnels
Inputs are available to all cases if needed
You must define each output tunnel for each case

ni.com/training

I. Case Structures Use Default if Unwired


Default values are:

Data Type

Default Value

Numeric

Boolean

FALSE

String

Empty

Avoid using the Use Default If Unwired option on Case


structure tunnels
Adds a level of complexity to your code
Complicates debugging your code
ni.com/training

I. Case Structures Boolean


Boolean input creates two cases: True and False

ni.com/training

I. Case Structures Integer


Add a case for each integer as necessary
Integers without a defined case use the default case

ni.com/training

I. Case Structures String


Add a case for each string as necessary
Strings without a defined case use the default case

ni.com/training

I. Case Structures Enum


Gives users a list of items from which to select
The case selector displays a case for each item in the
enumerated type control

ni.com/training

I. Case Structures - Error Checking and Error


Handling
Use Case Structures inside VIs to execute the code if there
is no error and skip the code if there is an error

ni.com/training

Exercise 4-6
Determine Warnings VI

Modify a VI to use a Case structure to make a software


decision.
GOAL

Exercise 4-6
Determine Warnings VI

What happens if all the values are 10? How could you fix
this?
DISCUSSION

SummaryQuiz
1. Which identifies the control or indicator on the block
diagram?
a) Caption
b) Location
c) Label
d) Value

ni.com/training

SummaryQuiz Answer
1. Which identifies the control or indicator on the block
diagram?
a) Caption
b) Location
c) Label
d) Value

ni.com/training

SummaryQuiz
2. Which structure must run at least one time?
a) While Loop
b) For Loop

ni.com/training

SummaryQuiz Answer
2. Which structure must run at least one time?
a) While Loop
b) For Loop

ni.com/training

SummaryQuiz
3. Which is only available on the block diagram?

a)
b)
c)
d)

Control
Constant
Indicator
Connector Pane

ni.com/training

SummaryQuiz Answer
3. Which is only available on the block diagram?

a)
b)
c)
d)

Control
Constant
Indicator
Connector Pane

ni.com/training

SummaryQuiz
4. Which mechanical action causes a Boolean in the False state to
change to True when you click it and stay True until you release
it and LabVIEW has read the value?

a)
b)
c)
d)

Switch Until Released


Switch When Released
Latch Until Released
Latch When Released

ni.com/training

SummaryQuiz Answer
4. Which mechanical action causes a Boolean in the False state to
change to True when you click it and stay True until you release
it and LabVIEW has read the value?

a)
b)
c)
d)

Switch Until Released


Switch When Released
Latch Until Released
Latch When Released

ni.com/training