4
1/4 www.ni.com Simple State Machine Template Documentation 1. 2. 3. 4. 5. 6. 7. 8. 1. 2. Overview The Simple State Machine template facilitates defining the execution sequence for sections of code. This particular implementation often is referred to as a Moore machine, which determines the next state based on decisions made in the current state. The design of this template makes it easy to insert new sections of code, remove sections of code, or change the order in which sections execute – all without making major modifications to the structure of the application. Table of Contents Example Developer Walkthrough System Requirements Use Cases Overview Running This Template Modifying this Template Important Information Example Refer to the , available from the dialog box, for an example of adapting this template to a measurement application. Single Shot Measurement sample project Create Project Developer Walkthrough See a of the Simple State Machine template. developer walkthrough System Requirements LabVIEW Base, Full, or Professional Development System Use Cases This template is suitable for applications that can be described as a combination of: States Decision-making logic that determines when to move to a particular state For example, the following applications can benefit from the state machine pattern: Single-page or tabbed dialog boxes. Each tab of the dialog box corresponds to a state. A user initiates state transitions by clicking a particular tab. For each tab, any actions the user can perform are included in the state.  An automate d teller mach ine (ATM). The s tates in this ap plication might include waiting for u ser input, che cking the re quested amo unt against th e account ba lance, disp ensing the money, printin g the receipt, and so on.  An applica tion that takes o ne measure ment, logs it to disk, and then waits for ano ther user actio n. The states in this applica tion might in clude wai ting for user in put, performi ng the measu rement, logging the data, displaying the data, and so on. Overview Notes: Each state in the diagram above corresponds to a subdiagram of the Case structure in Main.vi. Each state: Performs some action Tells the state machine what the next state is by passing an instruction to a shift register on the While Loop.  After initializ ation, the state machine transitions to the Wait for Event sta te. This state con tains an Eve nt structure that w aits for front pa nel change s. When a use r clicks a butto n, LabVIEW recognizes the event and switches to the appropriate subdiagram of the Event structure. This subdiagram initiates a transition to the appropriate state.  : Document Type Tutorial : Yes NI Supported : Aug 17, 2012 Publish Date

Simple State Machine Template Documentation

Embed Size (px)

Citation preview

Page 1: Simple State Machine Template Documentation

7/30/2019 Simple State Machine Template Documentation

http://slidepdf.com/reader/full/simple-state-machine-template-documentation 1/41/4 www.ni.c

Simple State Machine Template Documentation

1.

2.

3.

4.

5.

6.

7.

8.

1.

2.

Overview

The Simple State Machine template facilitates defining the execution sequence for sections of code. This particular implementation often is referred to as a Moore machine, which determines the

next state based on decisions made in the current state. The design of this template makes it easy to insert new sections of code, remove sections of code, or change the order in which sections

execute – all without making major modifications to the structure of the application.

Table of Contents

Example

Developer Walkthrough

System Requirements

Use Cases

Overview

Running This Template

Modifying this Template

Important Information

Example

Refer to the , available from the dialog box, for an example of adapting this template to a measurement application.Single Shot Measurement sample project Create Project

Developer Walkthrough

See a of the Simple State Machine template.developer walkthrough

System Requirements

LabVIEW Base, Full, or Professional Development System

Use Cases

This template is suitable for applications that can be described as a combination of:

States

Decision-making logic that determines when to move to a particular state

For example, the following applications can benefit from the state machine pattern:

Single-page or tabbed dialog boxes. Each tab of the dialog box corresponds to a state. A user initiates state transitions by clicking a particular tab. For each tab, any actions the user can perfor

are included in the state.

 An automated teller machine (ATM). The states in this application might include waiting for user input, checking the requested amount against the account balance, dispensing the money, prin

the receipt, and so on.

 An application that takes one measurement, logs it to disk, and then waits for another user action. The states in this application might include waiting for user input, performing the measuremenlogging the data, displaying the data, and so on.

Overview

Notes:

Each state in the diagram above corresponds to a subdiagram of the Case structure in Main.vi. Each state:Performs some action

Tells the state machine what the next state is by passing an instruction to a shift register on the While Loop.

 After initialization, the state machine transitions to the Wait for Event state. This state contains an Event structure that waits for front panel changes. When a user clicks a button, LabVIEWrecognizes the event and switches to the appropriate subdiagram of the Event structure. This subdiagram initiates a transition to the appropriate state.

 

:Document Type Tutorial

: YesNI Supported

: Aug 17, 2012Publish Date

Page 2: Simple State Machine Template Documentation

7/30/2019 Simple State Machine Template Documentation

http://slidepdf.com/reader/full/simple-state-machine-template-documentation 2/42/4 www.ni.c

1.

2.

1.

2.

3.

1.

2.

3.

4.

5.

6.

7.

1.

Each state has access to a cluster of data. The data types in this cluster are defined in .Data.ctl

The valid states are listed in , which is a typedef. Using a typedef for state transitions restricts the transitions you can use, reducing the chances that the state machine gets into anState.ctlunrecognized state.

Only the state can stop the application. This design prevents accidental and partial shutdowns by guaranteeing that:Stop

Shutdown code runs only when the user wants to stop the application.

Shutdown code always runs to completion.

Only one state executes at a time, and the single While Loop means all tasks execute at a single rate. If you need multi-rate or parallel tasks, consider the Queued Message Handler or Actor Framework templates, available from the dialog box.Create Project

The Wait for Event state is the only one that recognizes user input. The state machine must be in this state for any user input to be accepted.

Running This Template

In the window, open and run Main.vi.Project Explorer 

Click the front panel controls to display different pop-up dialog boxes.

Modifying this Template

Determining Your Needs

Before you customize this template, ask yourself the following questions:

What states does the application consist of? The answer to this question determines the states you add.

For each state, what should the next state be? The answer to this question determines the value of the enum that each state sends to the shift register on the While Loop.Next State

 A single state can conditionally transfer to multiple states. An example is the Wait for Event state in the template, which transitions to a state based on user input.

What kind of data will each state need access to? The answer to this question determines what data types you add to Data.ctl.

What are some errors that could occur, and how should the application respond to these errors? The answers to these questions determine the amount of error handling you need.

Adding Initialization Code

Complete the following steps to add code that initializes your application:

Locate the subdiagram of the Case structure.Initialize

 Add code that initializes your application. For example, you may want to open a file on disk for logging, initialize the data in to specific values, and so on.Data.ctl

Decide what state the application should transition to. By default, the Initialize state transitions to the Wait for Event state:

Depending on the needs of your application, you can modify this code to transition to a different state.

Adding a Control that Initiates a State Transition

 Add a control to the front panel.

Locate the Wait for Event subdiagram of the Case structure.

 Add an event case to the Event structure.

Configure the event to trigger when the value of this new control changes. For example:

Click . LabVIEW creates a subdiagram in the Event structure.OK

Drag the block diagram terminal for the new control inside this subdiagram.

Decide what state you want to transition to as a result of the user interacting with this control and wire an enum with this state's value to the output tunnel:Next State

If the state you want to transition to does not exist, add it.

 

Adding a State

Complete the following steps to add a state:

Update States.ctl, the typedef that contains the valid states:

Page 3: Simple State Machine Template Documentation

7/30/2019 Simple State Machine Template Documentation

http://slidepdf.com/reader/full/simple-state-machine-template-documentation 3/43/4 www.ni.c

 

1.

2.

3.

4.

5.

2.

1.

2.

1.

2.

3.

Locate an enum of States.ctl and open the typedef:

LabVIEW displays the window.Control Editor 

 Add a space for the new state to the enum.States

Enter the name of the new state. For example:

Click outside the control to add the name to the list of states.

Select .File»Apply Changes

 Add the state to the state machine itself:

 Add a subdiagram to the Case structure in Main.vi.

 Add code that the state will execute. As you do this, pay attention to the following guidelines:

Use the Unbundle by Name and Bundle by Name functions to access and modify state data:

Wire the error terminals of your functions to the error shift register.

Wire a FALSE constant to the Boolean output tunnel. Only the Stop state can stop the loop.

Each state must specify a transition to another state. Wire the value of the next state to the Next State output tunnel. You can wire this terminal directly, or you can implementconditional logic. The following figure shows an example of such logic:

In the code shown above, if is greater than 0, is the next state. Otherwise, is the next state. For more advanced conditional logic, use a CaseData User State 1 User State 2structure.

Ensure the application contains a transition to the new state.

 

Modifying the Data Types that States can Access

Complete the following steps to modify the data types that states have access to:

Locate an enum of and open the typedef:Data.ctl

LabVIEW displays the window.Control Editor 

Customize the control to your needs.

Select .File»Apply Changes

Adding Shutdown Code

 Add shutdown code to the Stop subdiagram of the Case structure. Because the this subdiagram is the only one that can stop the application, any code you add to this subdiagram is guaranteed

execute before the application stops and not at any other time. This design prevents accidental and partial shutdowns.

Shutdown code commonly accomplishes the following tasks:

Page 4: Simple State Machine Template Documentation

7/30/2019 Simple State Machine Template Documentation

http://slidepdf.com/reader/full/simple-state-machine-template-documentation 4/44/4 www.ni.c

 

1.

2.

3.

1.

2.

3.

4.

Frees memory by closing any open references.

Flushes any buffers in use.

Writes safe values to hardware input channels

 

Adding Error Handling

By default, this template stops if any function returns an error on its terminal. You might want to ignore specific errors or implement some more intelligent error handling. Complete theerror out

following steps to add error handling:

 Add an Error state.

In the Error state, create the code that handles errors in the ways you want.

In all other states, transition to the Error state whenever an error occurs. For example:

Removing the User Interface

If your application does not need a user interface, complete the following steps to remove it:

Delete all front panel controls and indicators.

Delete the Wait for Event subdiagram from the Case structure in Main.vi.

The previous step removes the subdiagram, which is the subdiagram that executes when the Case selector is told to execute to an unknown subdiagram. For error-handlingDefault

purposes, make another subdiagram the default.

Fix any broken wires.

You also can remove from . This step ensures that your state machine can never attempt to transition to the state you just removed. However, before you do this, make aWait for Event State.ctl

note of the enums in your application that call this state. After you save the updated , these enums will change. Ensure the new, changed state is what you want.State.ctl

Important Information

Copyright

© 2012 National Instruments. All rights reserved.

Under the copyright laws, this publication may not be reproduced or transmitted in any form, electronic or mechanical, including photocopying, recording, storing in an information retrieval system

or translating, in whole or in part, without the prior written consent of National Instruments Corporation.

National Instruments respects the intellectual property of others, and we ask our users to do the same. NI software is protected by copyright and other intellectual property laws. Where NI softwar

may be used to reproduce software or other materials belonging to others, you may use NI software only to reproduce materials that you may reproduce in accordance with the terms of any

applicable license or other legal restriction.

End-User License Agreements and Third-Party Legal Notices

You can find end-user license agreements (EULAs) and third-party legal notices in the following locations:

Notices are located in the <National Instruments>\_Legal Information and <National Instruments> directories.

EULAs are located in the <National Instruments>\Shared\MDF\Legal\license directory.

Review <National Instruments>\_Legal Information.txt for information on including legal information in installers built with NI products.

Trademarks

LabVIEW, National Instruments, NI, ni.com, the National Instruments corporate logo, and the Eagle logo are trademarks of National Instruments Corporation. Refer to the Trademark Information

for other National Instruments trademarks.ni.com/trademarks

Other product and company names mentioned herein are trademarks or trade names of their respective companies.

Patents

For patents covering the National Instruments products/technology, refer to the appropriate location: in your software, the patents.txt file on your media, or the National InstrumentHelp»Patents

Patent Notice at .ni.com/patents

 

Legal

This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial ma

not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS

TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF US

).http://ni.com/legal/termsofuse/unitedstates/us/