# Statechart Module Tutorial Part 5: Using Orthogonal Regions and State History

LabVIEW 2009 Statechart Module Help

Edition Date: June 2009

Part Number: 372103C-01

»View Product Info

In Part 4 of this tutorial, you learned how to create and configure regions and substates. You also learned how to use some debugging tools included in the LabVIEW Statechart Module. In Part 5 of this tutorial, you add orthogonal regions. You also use state history.

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

## Defining More State Data

The first step is defining an additional state data type. This type of data is modified in the Concurrent orthogonal region. Complete the following steps to define this data type.

1. Ensure you have closed all debugging windows and statechart diagrams.
2. Display the Project Explorer window for the My Getting Started Statechart.lvsc statechart that you constructed in Part 4 of this tutorial. You also can navigate to the labview\examples\Statechart\Tutorial directory and open Getting Started 4.lvsc for the completed statechart from Part 4 of this tutorial.
3. Double-click the StateData.ctl item. LabVIEW displays the Control Editor window for this type definition.
4. Right-click in the Control Editor window to access the Controls palette.
5. Select Express»Text Indicators»String Indicators to add a string indicator to the type definition.
6. Label this indicator Color.
7. Drag this indicator inside the StateData cluster.
8. Press the <Ctrl-S> keys to save this type definition.
9. Close the Control Editor window to return to the Project Explorer window.

## Creating the Orthogonal Region

Orthogonal regions are multiple regions in the same state. Substates in orthogonal regions execute concurrently, which means these states both execute during a single iteration of the statechart. Complete the following steps to add orthogonal regions to this statechart.

1. Display the statechart diagram.
2. Click and drag to increase the size of the Play superstate.
3. Place a second region inside the Play superstate and to the right of the Tick Counter region.
4. Label this new region Concurrent.
5. Place two substates inside this region.
6. Label the substates Red and Green.
7. Place an Initial pseudostate in the Concurrent region.
8. Create the following transitions:
• From the Initial pseudostate to the Red substate.
• From the Red substate to the Green substate.
• From the Green substate to the Red substate.

The Play superstate now resembles the following figure:

## Configuring the Substates and Transitions

The next step is configuring the actions of these substates and transitions. Complete the following steps to configure these items.

1. Configure the initial transition to write an Empty String Constant to the Color indicator. This action resets this indicator every time the statechart enters the Concurrent region.
2. Configure the entry action of the Red substate to write Red to the Color indicator.
3. Locate the transition node on the transition from the Red substate to the Green substate. Configure this transition to write Green to the Color indicator.

Configure this transition to react to the Return trigger only. Remember that the caller VI sends this trigger during the Timeout event of the Event structure.
4. Configure the other transition to react to the Return trigger only.
 Note  Even though you configured both transitions to react to the same trigger, the statechart checks only transitions that exit the current substate. Therefore, when the statechart is in the Green substate, the statechart does not consider triggers that exit the Red substate, and vice versa.
5. Save the statechart.
6. Generate code for the statechart.
7. Close the statechart diagram.

## Running the Caller VI

Complete the following steps to see the effect of the orthogonal region.

1. Run the caller VI from the Debugging the Statechart section of Part 4 of this tutorial, watch the state data, and click the Play front panel button. Notice the Counter indicator increments while the Color changes from Red to Green.
2. Click the Highlight Execution button to see the statechart move between orthogonal substates concurrently. You also can see the Increment trigger pass to the internal queue of the statechart.
3. Click this button again to turn off execution highlighting. Then, stop the caller VI.
4. Close all debugging windows. You cannot edit a statechart while debugging windows are open.

## Taking State History into Account

While the statechart is in the Play superstate, clicking the Pause front panel button and then the Play front panel button resets the Counter variable to 0. The next step is configuring the statechart to remember the previous value of the Counter indicator. In this configuration, when you re-enter the Play superstate from the Pause state, the variable does not reset from 0. Instead, the statechart remembers the value of the Counter indicator and begins counting from that value.

Complete the following steps to define this behavior.

1. Display the statechart diagram.
2. Place a Deep History pseudostate, located on the Statechart Development palette, in the Tick Counter region.
 Note  In this situation, you also can use the Shallow History pseudostate because the Play superstate contains only one level of hierarchy.
3. Locate the transition from the Pause state to the Play superstate. The triangular port of this transition is in the Play superstate.
4. Move the cursor over this triangular port until the cursor changes to the Wiring tool.
 Note  If you do not see the Wiring tool, you might have disabled automatic tool selection. Either enable this feature or press the key until the Wiring tool appears.
5. Click to initiate a transition segment.
6. Move the cursor over the Deep History pseudostate.
7. Click to complete the transition. The Play superstate now resembles the following figure:

8. Save the statechart.
9. Generate code for the statechart.

In this configuration, the first time the statechart enters the Tick Counter region, the statechart executes the Tick substate because this substate is connected to the Initial pseudostate. However, if you move to the Pause state and then return to the Play superstate, the statechart enters the substate that was active when the statechart left the Play superstate. The Deep History pseudostate defines this behavior. Because this behavior bypasses the Initial pseudostate, the Counter indicator does not reset to 0.

 Note  Only the Pause state has a transition to the Deep History pseudostate. Therefore, if you leave the Play superstate and return to the Menu state, the statechart resets the Counter indicator as usual. Also, notice that the Deep History pseudostate applies only to the Tick Counter region.

## Watching the Counter

Complete the following steps to see this behavior.

1. Run the caller VI and watch the state data.
2. Click the Play front panel button. The statechart begins incrementing the Counter variable.
3. After the Counter indicator reaches 10, click the Pause front panel button. The statechart moves to the Pause state.
4. Click the Play front panel button again. The Counter variable resumes counting from the number 10.
5. After the Counter indicator reaches 20, click the Menu front panel button. The statechart moves to the Menu state.
6. Click the Play front panel button again. The Counter indicator resets because the Menu state does not have a transition to the Deep History pseudostate.
7. Stop the caller VI.
8. Close the caller VI and all debugging windows.

## Summary

• Creating orthogonal regions.
• Defining substates that execute concurrently.
• Extending an existing transition.
• Using state history.

## Where to Go from Here

In Part 6 of this tutorial, you create one transition that enters and exits multiple substates simultaneously.

Your Feedback!  Poor  |  Excellent    Yes No
 Document Quality?