Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Feb 12, 2008

Overview of Code Generated by the LabVIEW Statechart Module

3 ratings | 4.00 out of 5
Print

Overview

This document gives an overview of the code generated by the LabVIEW Statechart Module. The code evolves from the basic state machine to include all states and transitions.

 Introduction 

This document gives an overview of the code generated by the LabVIEW Statechart Module.  This document is a simplified diagram rather than an exact implementation.

The first step is describing a common LabVIEW architecture for a simple state machine.  This architecture uses a single While Loop and an enumerated type constant wired to a shift register, as the following figure shows:


[+] Enlarge Image 

The shift register stores the current active state, and the Case structure determines which case(s) will run for the specified state.  

The statechart code evolves from this same basic architecture.  However, the statechart code includes the following additional structures to handle each component.

 Caching Data by Using Shift Registers 

Statecharts provide for the concepts of inputs, outputs, and state data.  LabVIEW caches this data by using shift registers, as the following figure shows:


[+] Enlarge Image

 Triggers Determine Which State Runs 

The next step introduces a trigger that determines which case to run under different active states.  The following figure shows this code:


[+] Enlarge Image

 Transitions Contain Guard and/or Action Code 

The statechart diagram uses triggers to evaluate transitions from a state.  Each transition can contain guard and/or action code.  When evaluating a state, LabVIEW executes the guard code first to determine if a transition is valid.  When LabVIEW finds a valid transition, LabVIEW executes the action code at the end of the state evaluation.  Adding a single transition to the state machine yields the following code:


[+] Enlarge Image

When there are multiple transitions triggered by the same trigger, the guard evaluation code is chained by a Case structure according to the order of transition priorities.

 Static Reactions 

In the statechart diagram, a state also can have static reactions that run when the state is active. A static reaction can have a guard and trigger.  Static reactions execute only when there is no valid transition, the trigger condition matches, and the static reaction guard code evaluates to TRUE. The following figure shows this situation:


[+] Enlarge Image

If multiple static reactions exist, then the code will be chained inside of the Case structure.

 Entry and Exit Actions 

The only missing components are the state entry and exit actions.  Adding these components completes the simplified code.  The resulting code, as shown in the following figure, is a state machine with transitions and actions.


[+] Enlarge Image

The entry action is evaluated only during a state change.  The code executes the entry action of the next state, thus satisfying the requirement that the statechart always ends an iteration at a known state.

 Hierarchy 

The resulting code is a sample implementation of a set of states in a single region.  When a statechart uses a hierarchy, or states within states, the Statechart Module generates a code pattern for each region and calls the code from the owning state.  The following figure shows this situation:


[+] Enlarge Image

This paper covers most of the generated code.  The only additions are listed below and are necessary to support hierarchy:

1.  Instead of storing an enum constant for the current active state, the statechart actually stores an array of active states.

2.  The code that determines the next active state(s) works on an array of states instead of a single enum.

For more information about the LabVIEW Statechart Module, refer to the National Instruments Web site at ni.com/statechart.

Related Links

Learn About the LabVIEW Statechart Module
NI LabVIEW Statechart Module
Developing Applications with the LabVIEW Statechart Module

3 ratings | 4.00 out of 5
Print

Reader Comments | Submit a comment »

 

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 may 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 USE (http://ni.com/legal/termsofuse/unitedstates/us/).