Company Events Academic Community Support Solutions Products & Services Contact NI MyNI

Statechart Module Tutorial Part 2: Building a Statechart

LabVIEW 2010 Statechart Module Help

Edition Date: June 2013

Part Number: 372103F-01

»View Product Info
Download Help (Windows Only)

In Part 1 of this tutorial, you examined a statechart that represented an average watch. You recreate this statechart in Parts 2-5 of this tutorial.

  You can complete these exercises in approximately 35 minutes.
Note  You can refer to the labview\examples\Statechart\Tutorial\Synchronous\Getting Started 2.lvsc for a completed version of the statechart you create in this part of the tutorial.

Creating and Configuring the Statechart

Complete the following steps to create and configure the statechart.

  1. Launch LabVIEW and display the Getting Started window.
  2. Select File»New to display the New dialog box.
  3. Select Other Files»Statechart from the Create New tree.
  4. Click the OK button. LabVIEW prompts you to save the statechart.
  5. Enter My Getting Started Statechart.lvsc and save the statechart in a convenient location on disk.

    LabVIEW displays a Project Explorer window that contains the necessary support files under the My Getting Started Statechart.lvsc project item. This project item represents the .lvsc file in which LabVIEW stores statechart information.
  6. Right-click the My Getting Started Statechart.lvsc item and select Properties from the shortcut menu. LabVIEW launches the Properties dialog box.
  7. Select the Statechart Code Generation item from the Category list. LabVIEW displays options relating to statechart code generation.
  8. Verify that the Usage pull-down menu displays Synchronous.
  9. Click the OK button to save changes and return to the Project Explorer window.

Defining States

The next step is defining the states that comprise the statechart. A state is a unique condition in which the statechart can exist. For example, a basic light bulb can be either ON or OFF. Therefore, a statechart that represents this light bulb would contain only those two states.

The statechart you construct represents a basic watch. This statechart consists of two fundamental states: Watch Dead and Watch Running. Complete the following steps to create these states.

  1. In the Project Explorer window, double-click the Diagram.vi item to open the Statechart Editor window.

    This window displays the statechart diagram. You build a statechart by placing statechart objects on the statechart diagram.
    Note  If you do not see the Functions palette, select View»Functions Palette to display this palette.
  2. On the Functions palette, select Statechart»Statechart Development to display the Statechart Development palette.
  3. Click the State object.
  4. Move the cursor over the statechart diagram. Click to place the top left corner of the state, drag the cursor diagonally to establish the size of the state, and click again to place the state on the statechart diagram.
  5. Notice the state is labeled State. Double-click this label to enable text editing and enter Watch Dead as the new label.
  6. Move the cursor over the gray outline of the Watch Dead state until the cursor changes to the Positioning tool.
  7. Press the <Ctrl> key and click and drag the cursor below the Watch Dead state to create a copy of the state.
  8. Label this new state Watch Running.
  9. Return to the Statechart Development palette and place a Terminal pseudostate to the right of the Watch Dead state.
    Note  A pseudostate is a statechart object that represents a state. The Terminal pseudostate represents a state that terminates the execution of the statechart.

The statechart diagram now resembles the following image:

Creating a Transition

The next step is defining how the statechart moves between states. To define this behavior, you use transitions to connect one state to another visually. Complete the following steps to create transitions between two states.

  1. Move the cursor just inside the bottom border of the Watch Dead state until the cursor changes to the Wiring tool.
  2. Click the state border. As you move the cursor down the statechart diagram, LabVIEW draws a dotted line between the state and the Wiring tool.
  3. Move the cursor over the top border of the Watch Running state. A gray box appears under the cursor.
  4. Click the border to finish the transition. LabVIEW creates a transition between the two states.

The statechart diagram now resembles the following image:

The object that appears in the middle of the transition is a transition node. You use this node to configure the transition.

Creating a Trigger and Configuring a Transition

The next step is specifying when the statechart takes the transition. The statechart takes a transition only after receiving a specific event, called a trigger.

You create a list of triggers for use with each statechart. In this tutorial, you configure a transition by associating a trigger with the transition. Complete the following steps to create a trigger and configure the transition.

  1. Double-click the transition node between the Watch Dead and Watch Running states to launch the Configure Transition dialog box. You use this dialog box to configure transitions.

    The first tab in this dialog box is the Triggers/Groups tab. Notice that the NULL trigger is selected by default in the Triggers and Groups list. Every statechart has this trigger by default.
  2. Click the Edit Triggers and Groups button to launch the Edit Triggers and Groups dialog box. You use this dialog box to create and modify triggers.
  3. Click the Create Trigger button to create a new trigger, which appears in the Triggers and Groups list.
  4. Enter Insert Battery as the new trigger name.
  5. Click the OK button to save changes and return to the Configure Transition dialog box. Notice the new trigger appears in the Triggers list.
  6. In the Triggers/Groups list, place a checkmark in the Insert Battery checkbox to specify that this trigger causes the statechart to take the transition.
  7. Remove the checkmark from the NULL checkbox.
  8. Click the OK button to save changes and return to the statechart diagram.
  9. Save the statechart by selecting File»Save in the Statechart Editor window. You also can press the <Ctrl-S> keys.

Notice the blue rectangle on the transition node. This rectangle indicates that you have specified a non-NULL trigger for the transition. If you move the cursor over the transition node, the Context Help window displays the properties of the transition, including the trigger. If you do not see the Context Help window, press the <Ctrl-H> keys to display this window.

Creating and Configuring Additional Transitions

When you create a transition, the state border you click first is the state that initiates the transition. Because transitions are unidirectional, you must create a second transition to return to the initiating state.

Note  Notice that the transition itself forms an arrow indicating the direction of the transition.

Create and configure the following transitions and triggers. You may want to create all of the triggers at one time and then configure the transitions. Remember to deselect the NULL trigger for each transition.

  • From the Watch Running state to the Watch Dead state. Configure this transition to react to a Dead Battery trigger.
  • From the Watch Running state to the Terminal pseudostate. Configure this transition to react to a Quit trigger.
  • From the Watch Dead state to the Terminal pseudostate. Configure this transition to react to a Quit trigger.
Tip  Right-click a transition and select Clean Up Wire to automatically reroute a transition on the diagram.

Save the statechart by pressing the <Ctrl-S> keys.

The statechart diagram now resembles the following image:

Finding and Fixing Statechart Errors

The toolbar of the Statechart Editor window contains a button that you click to generate code for the statechart. Currently, this button appears broken . This icon means that this statechart contains errors. You must fix these errors before you can generate code for this statechart.

Complete the following steps to find and fix errors in this statechart.

  1. Click the List Errors button to display the Error list window, which displays several errors.

    These errors appear because the top-level statechart diagram does not have an Initial pseudostate. This pseudostate defines the first state that the statechart enters. The top-level statechart diagram must have one and only one Initial pseudostate.
  2. Click the Close button to return to the statechart diagram.
  3. Place an Initial pseudostate, located on the Statechart Development palette, to the left of the Watch Dead state.
  4. Wire the Initial pseudostate to the Watch Dead state to create a transition. Notice the Generate Code button replaces the List Errors button. Transitions that exit Initial pseudostates cannot have triggers or guards. Therefore, you do not have to configure this transition.

    The statechart diagram now resembles the following image:

  5. Save the statechart.
  6. Close the Statechart Editor window.

Summary

In this tutorial, you learned about the following tasks:

  • Creating a statechart.
  • Creating states.
  • Creating transitions between states.
  • Creating triggers.
  • Configuring a transition to react to a trigger.
  • Finding errors on the statechart diagram.
  • Specifying the first state that a statechart executes.
  • Specifying a state that terminates the statechart.

Where to Go from Here

In Part 3 of this tutorial, you define statechart data.


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit