You are on page 1of 63

LabVIEW Core 1

1
ni.com/training
Course Learning Map
Lesson 1 Lesson 4 Lesson 7
Developing Modular Using Sequential and State
Navigating LabVIEW
Applications Machine Algorithms

Lesson 2 Lesson 5 Lesson 8


Troubleshooting & Debugging Creating and Leveraging Data Solving Dataflow Challenges
VIs Structures with Variables

Lesson 3 Lesson 6
Managing File and Hardware
Implementing a VI
Resources

2 ni.com/training
Lesson 4
Developing Modular Applications

TOPICS
A. Understanding Modularity
B. Icon
C. Connector Pane
D. Using SubVIs

3
ni.com/training
A. Understanding Modularity

4
ni.com/training
Understanding Modularity

Modularity — The degree to which a program is


composed of discrete modules such that a change to
one module has minimal impact on other modules

LabVIEW uses subVIs to create modularity.

5
ni.com/training
Understanding Modularity – SubVIs

SubVI — A VI within another VI

• SubVIs correspond to subroutines in text-based


programming languages.
• The upper-right corner of the front panel and block
diagram displays the icon for the VI.
• This icon identifies the VI when you place the VI on a
block diagram.

6
ni.com/training
Understanding Modularity – SubVIs

Repeated code can become subVIs.

7
ni.com/training
Understanding Modularity – SubVIs

8
ni.com/training
Understanding Modularity – SubVIs
Function Code Calling Program Code
function average (in1, in2, main
out) {
{ average (point1, point2,
out = (in1 + in2)/2.0; pointavg)
} }

SubVI Block Diagram Calling VI Block Diagram

9
ni.com/training
B. Icon
Characteristics of a Good Icon
Using the Icon Editor

10
ni.com/training
Icon

• An icon is a graphical representation of a VI.


• If you use a VI as a subVI, the icon identifies the subVI on
the block diagram of the VI.

11
ni.com/training
Characteristics of a Good Icon
Good icons convey the functionality of the VI using:
− Relevant graphics
− Descriptive text, if necessary

12
ni.com/training
Creating Icons - Icon Editor
Open the Icon Editor using one of these methods:
− Right-click the icon in the upper-right corner of the front panel
or block diagram and select Edit Icon.
− Double-click the icon.

13
ni.com/training
Icon Editor
Use the editing tools to modify an icon manually.

14
ni.com/training
Icon Editor
Use the Glyphs tab to display glyphs you can include in the icon.

15
ni.com/training
Icon Editor
Use the Icon Text tab to specify the text to display in the icon.

16
ni.com/training
Icon Editor
Use the Templates tab to display icon templates you can use as a
background for the icon.

17
ni.com/training
C. Connector Pane
Patterns
Standards

18
ni.com/training
Connector Pane

• The connector pane is displayed


next to the icon in the upper
right corner of the front panel.
− Each rectangle on the connector
pane represents a terminal.
− Use the terminals to assign inputs
and outputs.
• Select a different pattern by
right-clicking the connector pane
and selecting Patterns from the
shortcut menu.

19
ni.com/training
Connector Pane – Standards

• Use this connector pane layout as a standard.

• Top terminals are usually reserved for references, such as a


file reference.
• Bottom terminals are
usually reserved for
error clusters.

20
ni.com/training
D. Using SubVIs
Using on Block Diagram
Terminal Settings
Handling Errors
Creating from a Section of Block Diagram

21
ni.com/training
Using SubVIs

Options to place a subVI on the block diagram:


• Drag the VI from the Project Explorer to the block diagram.
• Click Select a VI on the Functions palette and then
navigate to the VI.
• Drag the icon from an open VI to the block diagram of
another VI.

22
ni.com/training
Terminal Settings

• Bold
− Required terminal
• Plain
− Recommended
terminal
• Dimmed
− Optional terminal

23
ni.com/training
Handling Errors
Use a Case structure to handle errors passed into the subVI.

24
ni.com/training
Handling Errors
Avoid using LabVIEW error handler VIs inside subVIs.

25
ni.com/training
Convert a Section of a VI to SubVI
To convert a section of a VI into a subVI:
1. Use the Positioning tool to select the section of the block
diagram you want to reuse.
2. Select Edit»Create SubVI.

26
ni.com/training
Exercise 4-1
Temperature Warnings VI

Create the icon and connector pane for a VI so you can use
the VI as a subVI. Call the subVI from a test VI.
GOAL
Exercise 4-1
Temperature Warnings VI

• Do the terminal names in the calling VI need to match the


subVI terminal names?
• Do the data types in the calling VI need to match the subVI
terminal data types?

DISCUSSION
Summary—Quiz

1. On a subVI, which terminal setting causes a broken VI if


the terminal is not wired?
a) Required
b) Recommended
c) Optional

29
ni.com/training
Summary—Quiz Answer

1. On a subVI, which terminal setting causes a broken VI if


the terminal is not wired?
a) Required
b) Recommended
c) Optional

30
ni.com/training
Summary—Quiz

2. You must create a custom icon to use a VI as a subVI.


a) True
b) False

31
ni.com/training
Summary—Quiz Answer

2. You must create a custom icon to use a VI as a subVI.


a) True
b) False

You do not need to create a custom icon to use a


VI as a subVI, but it is highly recommended to
increase the readability of your code.

32
ni.com/training
Lesson 5
Creating and Leveraging Data Structures

TOPICS
A. Arrays
B. Common Array Functions
C. Polymorphism
D. Auto-Indexing
E. Clusters
F. Type Definitions
33
ni.com/training
A. Arrays
1D and 2D Arrays
Creating an Array Control and Constant
Initializing Arrays

34
ni.com/training
Arrays
An array:
• Is a collection of data elements
that are of same type.
• Has one or more dimensions.
• Contains up to (231)–1 elements
per dimension, memory
permitting.
• Accesses elements by its index.
Note: The first element is index 0.

35
ni.com/training
Arrays – 1D and 2D Examples

ID array
One row of 10-elements
0 1 2 3 4 5 6 7 8 9
1.2 3.2 8.2 8.0 4.8 5.1 6.0 1.0 2.5 1.7

Index
2D array
Five-row by seven-column table of 35 numbers
elements
0 1 2 3 4 5 6
0
1
2
3
4

36
ni.com/training
Why Use Arrays?

Use arrays when you work with a collection of similar data and
when you perform repetitive computations.

37
ni.com/training
Viewing Arrays on the Front Panel
The elements at First element
Second
index 0 are not at index 1 element at
shown because index 2
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.

38
ni.com/training
Creating an Array Control
For a new array:
1. Select an Array control from the
Controls palette on the front panel.
2. Place a data object, such as a
numeric control, into the array
shell.
3. Add a second dimension, if necessary, by
resizing the index.
From a block diagram terminal or wire:
1. Right-click the object and select Create»Control
or Create»Indicator.

39
ni.com/training
Creating an Array Constant
For a new array:
1. Select Array Constant from the Functions
palette on the block diagram.
2. Place a constant, such as a numeric,
into the array shell.
3. Add a second dimension, if necessary, by
resizing the index.
From a block diagram terminal or wire:
1. Right-click and select Create»Constant.

40
ni.com/training
Initializing Arrays
• 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.

41
ni.com/training
2D Arrays

• 2D arrays:
− Store elements in a grid.
− Require a column index and a row index to locate an element,
both of which are zero-based.
• Create a multidimensional array on the front panel by right-
clicking the index display and selecting Add Dimension
from the shortcut menu.
• Resize the index display until you have as many dimensions
as you want.

42
ni.com/training
B. Common Array Functions
Array Size
Initialize Array
Array Subset
Build Array
Index Array

43
ni.com/training
Common Array Functions

• Array Size
• Initialize Array
• Array Subset
• Build Array
• Index Array

44
ni.com/training
Array Size

45
ni.com/training
Initialize Array

46
ni.com/training
Array Subset

47
ni.com/training
Build Array

48
ni.com/training
Index Array

49
ni.com/training
C. Polymorphism

50
ni.com/training
Polymorphism
Polymorphism - The ability of VIs and functions to
automatically adapt to accept input data of different
data types

Functions are polymorphic to varying degrees:


• None, some, or all of their inputs can be polymorphic.
• Some accept numeric or Boolean values.
• Some accept numeric or strings.
• Some accept scalars, numeric arrays, or clusters of
numerics.

51
ni.com/training
Arithmetic Functions Are Polymorphic
Combination Result

52
ni.com/training
D. Auto-Indexing
Use in For Loops and While Loops
Waveform Graphs
Auto-Indexing with a Conditional Terminal
Creating 2D Arrays
Auto-Indexing Input to a Loop

53
ni.com/training
Auto-Indexing Auto-Indexing Enabled

Wire becomes thicker


• Allows For Loops and While
Loops to accumulate arrays at
their boundaries.
1D Array
• Is the default behavior for
For Loops. 0 1 2 3 4 5

• Is disabled by default for Auto-Indexing Disabled


While Loops.
Wire remains the same size
• Is enabled/disabled by right-
clicking on a tunnel.
• Produces arrays that are always
equal in size to the number of Only one value (last
iteration) is passed out of
iterations of the loop. the loop

54
ni.com/training
Waveform Graph
• Is a graphical display of data.
• Displays one or more plots of
evenly sampled measurements.
• Is used to plot pre-generated
arrays of data.
• Can display plots with any
number of data points.

55
ni.com/training
Charts vs. Graphs – Single-Plot

56
ni.com/training
Auto-Indexing with a Conditional Tunnel

Right-click on a tunnel and select


Tunnel Mode»Conditional.

57
ni.com/training
Creating 2D Arrays

1D Array 2D Array

0 1 2 3 4 5

• Inner loop creates column elements.


• Outer loop stacks column elements into rows.

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

59
ni.com/training
59
Auto-Indexing Input
Use an auto-indexing input array to perform calculations
on each element in an array.
• Wire an array to an auto-indexing tunnel on a For Loop.
• You do not need to wire the count (N) terminal.
− The For Loop executes
the number of times
equal to the
number of
elements in the array.
− The Run button is not broken.

60
ni.com/training
60
Exercise 5-1
Concept: Manipulating Arrays

HOMEWORK

Manipulate arrays using various LabVIEW functions.


GOAL
Exercise 5-1
Concept: Manipulating Arrays

• In the All Data Channel case, how can you verify that the
two approaches yield the same results?
– Polymorphic Add of array + scalar
– Auto-indexed array elements added to scalar

DISCUSSION
End of Week 4

63 DISCUSSION

You might also like