Você está na página 1de 22

Lesson 8

Common Design Techniques and Patterns

TOPICS
A. Using Sequential Programming
B. State Programming
C. State Machines

ni.com/training
A. Using Sequential Programming
• Many of the VIs you write accomplish sequential tasks
• There is nothing in this block diagram to force the
execution order of these tasks - any one of these tasks
could happen first

ni.com/training
A. Using Sequential Programming

Use error clusters to force order of execution

ni.com/training
A. Using Sequential Programming
To force execution order, use a Sequence structure
• A structure with frames, where each frame executes in order
• The second frame cannot begin execution until everything in the first
frame completes execution

ni.com/training
A. Using Sequential Programming

• Avoid overusing Sequence structures


• You cannot stop the execution part way through a sequence

ni.com/training
A. Using Sequential Programming

The best way to write this VI is to enclose the dialog boxes in


Case structures, wiring the error cluster to the case selectors

ni.com/training
B. Using State Programming

Although Sequence structures or sequentially wired subVIs


accomplish the purpose, it is not always the best choice:
• 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 the 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?

ni.com/training
B. Using State Programming –
State Transition Diagram
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

ni.com/training
8
B. Using State Programming –
State Transition Diagram
Furnace
Example:

ni.com/training
9
C. State Machines

• The state machine design pattern implements a state


diagram or flow chart
• When to use state machines?
− Commonly used to create user interfaces, where different user
actions send the user interface into different states
− Commonly used for process tests, where a state represents
each segment of the process

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

Shift Register Case Structure ni.com/training


C. State Machines – Default Transition

ni.com/training
C. State Machines –
Transition Between Two States

ni.com/training
C. State Machines –
Case Structure Transition

ni.com/training
C. State Machines –
Transition Array Transition

ni.com/training
Course Project

Demonstrate an implementation of a state machine.

<Exercises>\LabVIEW Core 1\Demonstrations\Course Project

DEMONSTRATION
Exercise 8-1
State Machine VI

Create a simple state machine using a type-defined


enumerated control and practice the skills learned throughout
this course.
GOAL
Exercise 8-1
State Machine VI

• If you want to add a Process 3 to the program, how would


you modify the VI?

DISCUSSION
Summary—Quiz
1. When using a Sequence structure, you can stop the execution
part way through a sequence.
a) True
b) False

ni.com/training
19
Summary—Quiz Answer
1. When using a Sequence structure, you can stop the execution
part way through a sequence.
a) True
b) False

You cannot stop the execution part way through a


sequence.

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

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

ni.com/training
22

Você também pode gostar