Вы находитесь на странице: 1из 28

State Machines

• You use the state machine design pattern to


implement algorithms that can be described by a state
diagram or flow chart.

• A state machine, which is more precisely defined as a


finite state machine, consists of a set of states and a
transition function that maps to the next state.

• The two most common finite state machines are the


Mealy machine and the Moore machine.
State Machines
• A Mealy machine performs an action for each transition.

• A Moore machine performs a specific action for each state in


the state transition diagram.

• The state machine design pattern template in LabVIEW


implements any algorithm described by a Moore machine.

• Each state can lead to one or multiple states or end the process
flow.
State Machines
• A state machine relies on user input or in-state
calculation to determine which state to go to
next.

• Many applications require an initialization


state and a default state, where many different
actions can be performed.
State Machines
Translating the state transition diagram into a
LabVIEW block diagram requires the
following components:

• While Loop
• Case Structure
• Shift Register
• State Functionality Code
• Transition Code
State Machines
• The flow of the state transition diagram is
implemented by the While Loop.

• It continually executes the various states.

• The individual states are represented by cases in the


Case structure

• It contains a case for each state and the code to


execute for each case.
State Machines
• A Shift register on the While Loop keeps track
of the current state and communicates the
current state to the Case structure input.

• The State functionality code is used to


implement the function of the state.

• The transition code determine the next state in


the sequence.
Enum Data Type
• An enum data type is a combination of data types. It
represents a pair of values, such as a string and a
numeric.
• The enum can be one of a list of values.
• For example, if you created an enum type called
Month, the possible value pairs for a Month variable
are January-0, through December-11.
• Enums are useful because manipulating numbers on
the block diagram is easier than strings.
Enum Data Type
Controlling the State Machine
• Use enumerated type controls as case selectors to control the
initialization and transition of state machines.

• With enumerated type controls, if a user attempts to add or


delete a state from the enumerated type control, the remaining
wires connected to the copies of this enumerated type control
break.

• One solution to this problem is to type define the enumerated


control.

• Creating a type defined enumerated type control causes all the


enumerated type control copies to automatically update if you
add or remove a state.
Controlling the State Machine
Transition Code
• Use enumerated type controls to control the
initialization and transition of state machines.
• There are several common design patterns used to
develop the transition code.
• You choose the transition code design pattern based
on the number of states you need to transition
between:
• Transitioning to another state (Default Transition)
• Transition between two possible states
• Transition among two or more possible states
Transition Code
Default Transition
A design pattern that uses a default transition implemented
for a temperature data acquisition system is displayed. For
the default transition, no code is needed to determine the
next state, as there is only one possible choice.
Transition Between Two States
• There are several patterns commonly used to accomplish
transition between two states.

• The Select function transition implemented for a temperature


data acquisition system is displayed.

• This method works well if you know that the individual state
always transitions between two states.

• However, this method limits the scalability of the application.


If you need to modify the state to transition among more than
two states, this solution would not work and would require a
major modification of the transition code
Transition Between Two States
Transition Among Two or More States
• You can create a more scalable architecture by
using one of the following methods for
transition among multiple states.

• Case Structure
• Transition Array
• State Diagram Tool Kit
Using the Case Structure
• Each case in a Case structure corresponds to an item
in the enumerated type control, making it easy to read
and understand the code.

• A Case structure is also scalable and you can add


more transitions to a particular state by adding more
cases to the Case structure.

• However, due to the nature of the Case structure, you


cannot see the complete functionality of the transition
code at a glance.
Using Transition Array
• If you need more of the code to be visible, you
can create a transition array for all the
transitions that can take place in the transition
code for a state.

• You can also index the Boolean array for the


next state.
Using Transition Array
Using State Diagram Toolkit
• The State Diagram Toolkit uses a large Case structure
for every state and a smaller While Loop that iterates
through the state transitions until the proper state
transition is met.

• It also adds the State Diagram Editor function to


LabVIEW to visually draw the logic that defines an
application, and generate the LabVIEW code that acts
as the foundation of your application.
Using State Diagram Toolkit
State Machine Design Pattern
• It usually has a start up and a shut down phase.
• The main application phase consists of a Case
structure embedded in the loop.
• This architecture allows you to run different code
each time the loop executes depending upon some
condition.
• Each case defines a state of the machine, hence the
name, state machine.
• Use this design pattern for VIs that are easily divided
into several simpler tasks, such as VIs that act as a
user interface.
State Machine Design Pattern
• Each state of the state machine is a separate
case in the Case structure.
• Place VIs and other code that the state should
execute within the appropriate case.
• A shift register stores the state that should
execute upon the next iteration of the loop.
• The block diagram of a state machine VI is
displayed. The possible states are Startup, Idle,
Event 1, Event 2, and Shutdown.
State Machine Design Pattern
• For the displayed VI, the possible states are Startup, Idle, Event 1, Event 2, and
Shutdown. An enumerated constant stores the states. Each state has its own case in
the Case structure. The outcome of one case determines the case that next executes.
The shift register stores a value that determines which case runs next.
State Machine Design Pattern
• The state machine design pattern can make the block diagram much smaller, and
therefore easier to read and debug.
• Another advantage of the state machine architecture is that each case determines the
next state, unlike Sequence structures that cannot skip a frame.
• A disadvantage of the state machine design pattern is that it makes it possible to
skip states. If two states in the structure are called at the same time, this model
handles only one state, and the other state does not execute.

Вам также может понравиться