LabVIEW Basics II

Course Learning Map
Lesson 1
Lesson 4
Lesson 7
Navigating LabVIEW
Developing Modular
Applications
Using Sequential and State
Machine Algorithms
Lesson 2
Lesson 5
Lesson 8
Troubleshooting & Debugging
VIs
Creating and Leveraging Data
Structures
Solving Dataflow Challenges
with Variables
Lesson 3
Lesson 6
Implementing a VI
Managing File and Hardware
Resources
ni.com/training
Lesson 7
Using Sequential and
State Machine Algorithms
TOPICS
A. Using Sequential Programming
B. Using State Programming
C. State Machines
Lesson 7, Slide 1
A. Using Sequential Programming
Flow-Through Parameters
Sequence Structures
Error Case Structures
Lesson 7, Slide 2
Using Sequential Programming
• Many of the VIs you write accomplish sequential tasks.
• By default, LabVIEW does not force sequential programming.
Example: Nothing forces the execution order of these tasks.
Any one of these tasks could happen first.
Lesson 7, Slide 3
Flow-Through Parameters Force Execution Order
Use error clusters and refnums to force order of execution.
Lesson 7, Slide 4
Sequence Structures Force Execution Order
• Sequence structures are a structure with frames, where each frame
executes in order.
• The second frame cannot begin execution until everything in the first
frame completes execution.
Lesson 7, Slide 5
Avoid Overuse of Sequence Structures
You cannot stop the execution in the middle of a sequence.
Lesson 7, Slide 6
Better to Use Error Case Structures
The best way to write this VI is to enclose the dialog boxes in
Case structures, wiring the error cluster to the case selectors.
Lesson 7, Slide 7
B. Using State Programming
State Programming
State Transition Diagrams
Lesson 7, Slide 8
State Programming
State programming helps you solve the following issues that
sequential programming or flow-through parameters do not:
• What if you need to change the order of the sequence?
• What if you need to repeat one item in the sequence more
often than other items?
• What if some items in the sequence execute only when
certain conditions are met?
• What if you need to stop the program immediately, rather
than waiting until the end of the sequence?
Lesson 7, Slide 9
State Transition Diagram
A state transition diagram is a type of flowchart that indicates
the states of a program and transitions between states.
State – Part of a program that satisfies a condition,
performs an action or waits for an event
Transition – Condition, action, or event that causes
the program to move to the next state
Lesson 7, Slide 10
State Transition Diagram
Furnace
Example:
Lesson 7, Slide 11
C. State Machines
Common Uses
Infrastructure
Transitions
Lesson 7, Slide 12
State Machines
• The state machine design pattern implements a state
diagram or flow chart.
• Common uses of state machines:
− To create user interfaces, where different user actions send the
user interface into different states.
− For process tests, where a state represents each segment of
the process.
Lesson 7, Slide 13
State Machines – Infrastructure
• A state machine consists of a set of states and a transition
function that maps to the next state.
• Each state can lead to one or multiple states or end the
process flow.
While Loop
Type-Defined
Enum
Case Structure
Shift Register
Lesson 7, Slide 14
State Machines – Default Transition
Lesson 7, Slide 15
State Machines – Transition Between Two
States
Lesson 7, Slide 16
State Machines – Case Structure Transition
Lesson 7, Slide 17
State Machines – Transition Array Transition
Lesson 7, Slide 18
Course Project Transition Diagram
Lesson 7, Slide 19
Course Project
Demonstrate an implementation of a state machine.
<Solution>\LabVIEW Core 1\Exercise7-1
DEMONSTRATION
Exercise 7-1
Weather Station Project
Create a Weather Station application using a state machine
design pattern. Practice skills learned throughout this course.
GOAL
Exercise 7-1
Weather Station Project
How would you change the diagram to add an Initialize and
Shutdown state?
DISCUSSION
Summary—Quiz
1. When using a Sequence structure, you can stop the execution in
the middle of a sequence.
a) True
b) False
Lesson 7, Slide 23
Summary—Quiz Answer
1. When using a Sequence structure, you can stop the execution in
the middle of a sequence.
a) True
b) False
You cannot stop the execution in the middle of a
sequence.
Lesson 7, Slide 24
Summary—Quiz
2. Which of the following are benefits of using a state machine
instead of a sequential structure?
a) You can change the order of the sequence.
b) You can repeat individual items in the sequence.
c) You can set conditions to determine when an item in the
sequence should execute.
d) You can stop the program at any point in the sequence.
Lesson 7, Slide 25
Summary—Quiz Answers
2. Which of the following are benefits of using a state machine
instead of a sequential structure?
a) You can change the order of the sequence.
b) You can repeat individual items in the sequence.
c) You can set conditions to determine when an item in the
sequence should execute.
d) You can stop the program at any point in the sequence.
Lesson 7, Slide 26
Lesson 8
Solving Dataflow Challenges with Variables
TOPICS
A.
B.
C.
D.
E.
Communicating Between Parallel Loops
Writing to Controls & Reading from Indicators
Variables
Local Variables
Race Conditions
Lesson 8, Slide 1
A. Communicating Between Parallel
Loops
Lesson 8, Slide 2
Communicating Between Parallel Loops
Dual chart example: Execute multiple tasks at the same time.
Lesson 8, Slide 3
How do the loops stop in this example?
Passing data among parallel loops is a challenge.
Lesson 8, Slide 4
How do the loops stop in this example?
You cannot pass data between parallel loops with a wire.
Lesson 8, Slide 5
B. Writing to Controls & Reading from
Indicators
Lesson 8, Slide 6
Writing to Controls & Reading from Indicators
How would you handle the following dataflow challenges?
− Initialize front panel controls with values from a configuration file?
− Copy a “Ship To” address to a “Bill To” address?
− Initialize indicators that will be written to later in your code?
− Write to an indicator in two cases of a Case structure without
writing to it in all cases?
Sometimes you need to write to a control or read from an
indicator.
Lesson 8, Slide 7
C. Variables
Lesson 8, Slide 8
Variables
Variables – Block diagram elements that allow
you to access or store data in another location
Variables can be of the following types:
• Local—Stores data in front panel controls and indicators.
• Global —Stores data in special repositories that can be
accessed from multiple VIs.
• Functional Global—Stores data in While Loop shift
registers.
• Shared—Transfers data between various distributed
targets connected together over a network.
Lesson 8, Slide 9
D. Local Variables
When To Use Local Variables
Local Variables and Boolean Mechanical Actions
How To Create Local Variables
Lesson 8, Slide 10
Local Variables
Use local variables to pass data within a single VI.
Lesson 8, Slide 11
Local Variables
Use local variables to modify front panel control values.
Lesson 8, Slide 12
Use Switch Mechanical Action
• Boolean controls with
associated local
variables must use
switch mechanical
action.
• Boolean latch action
is incompatible with
local variables.
Lesson 8, Slide 13
Creating Local Variables
Create and use local variables.
<Exercises>\LabVIEW Core 1\Demonstrations\Local
Variables
DEMONSTRATION
Exercise 8-1
Weather Station UI with Local Variable VI
Use a local variable to write to and read from a control.
GOAL
Exercise 8-1
Weather Station UI with Local Variable VI
What functionality does the local variable provide for this
application?
DISCUSSION
E. Race Conditions
Definition
How To Avoid Race Conditions
Controlling Shared Resources
Lesson 8, Slide 17
Race Conditions
Race Condition – A situation where the timing of
events or the scheduling of tasks may unintentionally
affect an output or data value
Race conditions are a common problem for programs
that execute multiple tasks in parallel and share data
between the tasks.
Lesson 8, Slide 18
What is the final value of the Value variable?
Four possible outcomes:
•
•
•
•
Value = (Value * 5) +2
Value = (Value + 2) * 5
Value = Value * 5
Value = Value +2
Lesson 8, Slide 19
Race Conditions
• Race conditions are very difficult to identify and debug.
• Often, code with a race condition can return the same result
thousands of times in testing, but still be capable of
returning a different result.
• Avoid race conditions by:
− Controlling shared resources.
• Use one writer, multiple readers.
− Properly sequencing instructions.
− Reducing use of variables.
Lesson 8, Slide 20
Controlling Shared Resources
Bad
Good!
Lesson 8, Slide 21
Summary—Quiz
1. You should use variables in your VI whenever possible.
a) True
b) False
Lesson 8, Slide 22
Summary—Quiz Answer
1. You should use variables in your VI whenever possible.
a) True
b) False
You should use variables only when necessary. Use
wires to transfer data whenever possible.
Lesson 8, Slide 23
Summary—Quiz
2. When controlling resources, which combination of writers and
readers reduces chance of race conditions?
a) One writer, one reader
b) One writer, multiple readers
c) Multiple writers, one reader
d) Multiple writers, multiple readers
Lesson 8, Slide 24
Summary—Quiz Answer
2. When controlling resources, which combination of writers and
readers reduces chance of race conditions?
a) One writer, one reader
b) One writer, multiple readers
c) Multiple writers, one reader
d) Multiple writers, multiple readers
Lesson 8, Slide 25
End of LabVIEW Core 1
Lesson 8, Slide
New User
Experienced User
Advanced User
LabVIEW Core 1
LabVIEW Core 3
Managing Software
Engineering in LabVIEW
LabVIEW Core 2
LabVIEW Connectivity
Advanced Architectures
in LabVIEW
Object-Oriented Design
and Programming in LabVIEW
Certifications
Certified LV Associate
Developer Exam
LabVIEW Performance
Certified LabVIEW
Developer Exam
Certified LabVIEW
Architect Exam
Other Courses
LabVIEW Real-Time 1
LabVIEW Real-Time 2
LabVIEW Instrument Control
LabVIEW Modular Instruments
LabVIEW FPGA
DAQ & Signal Conditioning
Lesson 1, Slide 53
LabVIEW Core 2
Lesson 1, Slide 54
What You Need To Get Started
• LabVIEW Core 2 Course Manual
• LabVIEW Core 2 Exercise Manual
• LabVIEW Core 2 Course Kit
Computer running:
• LabVIEW 2012 or later
• Windows XP or later
Lesson 1, Slide 55
File Locations
The course installer places the course files in the following
location:
Root Directory
Exercises
<or>
Solutions
LabVIEW
Core 2
Lesson 1, Slide 56
Course Goals
This course prepares you to:
• Use event programming effectively.
• Apply common design patterns that use queues and events.
• Programmatically control user interface objects.
• Evaluate file I/O formats and use them in applications.
• Modify existing code for improved usability.
• Prepare, build, and deploy stand-alone applications.
Lesson 1, Slide 57
Configuring Your LabVIEW Environment
Change the LabVIEW environment in the Options dialog box.
• Front Panel page
− Set Control Style for New VIs to Silver style.
• Block Diagram page
− Uncheck
• Place front panel terminals as icons
• Enable automatic error handling in new VIs
• Enable automatic error handling dialogs
− Configure Block Diagram Cleanup to customize your block diagram.
Lesson 1, Slide 58
Configuring Your LabVIEW Environment
• Functions Palette
− Tack the Functions palette and select Customize»Change
Visible Palettes then click Select All.
• Controls Palette
− Tack the Controls palette and select Customize»Change
Visible Palettes then click Select All.
Lesson 1, Slide 59
Course Learning Map
Lesson 1
Lesson 4
Moving Beyond Dataflow
File I/O Techniques
Lesson 2
Lesson 5
Implementing Design Patterns
Improving an Existing VI
Lesson 3
Lesson 6
Controlling the User Interface
Creating & Distributing Applications
Lesson 1, Slide 60
Lesson 1
Moving Beyond Dataflow
TOPICS
A. Asynchronous Communication
B. Queues
C. Event-Driven Programming
Lesson 1, Slide 61
A. Asynchronous Communication
Lesson 1, Slide 62
Asynchronous Communication
LabVIEW is a dataflow language.
• Functions depend on other functions for data.
• Dependent functions do not execute until their
dependencies have completed execution.
• Wires transfer data between functions.
However, sometimes you need break dataflow and program
using asynchronous communication.
Lesson 1, Slide 63
Asynchronous Communication
Asynchronous Communication — Transfer
information without using wires
• Asynchronous communication is between the following:
− Parallel loops
− UI and Block Diagram
− VIs
− Application instances (LabVIEW projects, executables, etc.)
• Information includes the following:
− Data
− Notification that something happened
Lesson 1, Slide 64
B. Queues
Queues
Queue Operations
Producer/Consumer (Data) Design Pattern
Lesson 1, Slide 65
Queues
• Are used for communicating data between parallel loops.
• Store multiple pieces of data (i.e. buffer data).
• Work in a FIFO (first in, first out) manner by default.
• Can hold data of any type.
Lesson 1, Slide 66
Drawbacks of Variables
Drawbacks of using variables to communicate between loops:
• It’s possible to read duplicate
data.
• It’s possible to miss data.
• You can create read-modifywrite race conditions.
Lesson 1, Slide 67
Queue Operations
Use the Queue Operations functions to
create and use a queue for communicating data between:
• Different sections of a VI.
• Different VIs.
Lesson 1, Slide 68
Producer/Consumer Design Pattern (Data)
Lesson 1, Slide 69
Exercise 1-1
Concept: Comparing Queues with Local Variables
Run and examine a producer/consumer design pattern VI that
transfers data that a producer loop generates to consumer
loops using local variables and queues.
GOAL
Exercise 1-1
Concept: Comparing Queues with Local Variables
1. What is the data type associated with the queue?
2. What function determines the number of elements in the
queue?
3. What is the purpose of the While Loop before the Release
Queue?
DISCUSSION
End of Week 6
73
ni.com/training