﻿

# Statechart Module Tutorial Part 5: Using Orthogonal Regions

LabVIEW 2010 Statechart Module Help

Edition Date: June 2013

Part Number: 372103F-01

»View Product Info

In Part 4 of this tutorial, you learned how to create and configure regions and substates. In Part 5 of this tutorial, you add orthogonal regions.

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

## 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 the statechart.

1. Display the Statechart Editor 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\Synchronous directory and open Getting Started 4.lvsc for the completed statechart from Part 4 of this tutorial. You can display the Statechart Editor window by double-clicking the Diagram.vi item in the Project Explorer window for the statechart.
2. Click and drag to increase the size of the Watch Running superstate.
3. Place a second region inside the Watch Running superstate below the Display region.
4. Label this new region Timer.
5. Place two states inside this region.
6. Label the substates Idle and Running.
7. Place an Initial pseudostate in the Timer region.
8. Create the following transitions:
• From the Initial pseudostate to the Idle substate.
• From the Idle substate to the Running substate. Configure this transition to react to a Start trigger only.
• From the Running substate to the Idle substate. Configure this transition to react to a Stop/Reset trigger only.

The statechart diagram now resembles the following image:

## Defining a Transition Guard

The next step is specifying that the statechart transitions from the Idle to the Running state only when the statechart also is executing the Timer substate. To specify this behavior, you use a transition guard. A transition guard is LabVIEW block diagram code that determines whether the statechart takes the associated transition. The guard code must return either TRUE or FALSE. If the guard returns TRUE, the statechart takes the transition. If the guard returns FALSE, the statechart does not take the transition.

Complete the following steps to define this guard.

1. Double-click the transition node from the Idle state to the Running state to display the Configure Transition dialog box.
2. Click the Guard tab.
3. Click the Inputs cluster control on the left-side of the block diagram and select StatechartState.
4. Use the IsIn function on the Statechart Communication palette to return TRUE if the Timer state is active.
1. Place the IsIn function on the block diagram.
2. Wire the StatechartState cluster control to the Statechart States input of the IsIn function.
3. Wire the IsIn? output to the Execute? cluster element.
4. Right-click the IsIn function and select Select State from the shortcut menu. LabVIEW displays the Select State dialog box. You use this dialog box to specify the state vector, which is the path of states and regions that contain the state.
5. Select Watch Running»Display»Timer. The Current Selection text box displays Watch Running\Display\Timer.
6. Click the OK button. The block diagram resembles the following image:

6. Repeat steps 1-4 to create a transition guard from the Running state to the Idle state.
7. Save the statechart.

With this configuration, the statechart checks the guard code every time the caller VI sends the Start or Stop/Reset trigger. The guard code returns FALSE if the Timer substate is not active.

## Modifying State Data

The next step is to modify state data while the Running and Idle states run. You do this by creating static reactions to modify the state data controls. Complete the following steps to modify the Timer and Clock state data controls.

1. Define a static reaction in the Running state called Increment.
1. On the Action tab, click the Outputs cluster element on the left side of the diagram and select StateData»Timer.
2. Place the Add function on the block diagram.
3. Wire the StateData.Timer cluster element to the x input of the Add function.
4. Right-click the y input of the Add function and select Create»Constant from the shortcut menu.
5. Enter 50 for the value of the constant.
6. Click the Outputs cluster element on the right side of the diagram and select StateData»Timer.
7. Wire the x+y output of the Add function to the StateData.Timer cluster element on the right side of the diagram. The action code for the Increment static reaction resembles the following image:

2. Configure the Increment static reaction to react to all triggers.
1. Navigate to the Triggers/Groups tab.
2. Click the Select All Triggers button.
3. Define a static reaction in the Idle state called Reset. You configure the Reset reaction to reset the Timer state data control to 0.
1. On the Action tab, click the Outputs cluster control on the left side of the diagram and select StateData»Timer.
2. Click the Outputs cluster control on the right side of the diagram and select StateData»Timer.
3. Right-click the StateData.Timer cluster element on the right side of the block diagram and select Create»Constant from the shortcut menu. Leave the default value 0 for the constant.
4. On the Triggers/Groups tab, specify the Reset static reaction only reacts to the Stop/Reset trigger.
5. Click the OK button to close the Configure State dialog box.
6. Save the statechart.
7. Close the Statechart Editor window.

## Enabling Debugging for the Statechart

If you want to use the debugging tools for a statechart, you first must enable debugging for the statechart.

Complete the following steps to enable debugging for the statechart.

1. Right-click the My Getting Started Statechart.lvsc item in the Project Explorer window and select Properties from the shortcut menu. LabVIEW launches the Properties dialog box.
2. Select the Statechart Code Generation item from the Category list. LabVIEW displays options relating to statechart code generation.
3. Select Enabled from the Debugging pull-down menu to specify that debugging is enabled for the statechart. The default value is Enabled.
 Note  Debugging is only available on Desktop, Real-Time, and FPGA targets.
4. Click the OK button to save changes and return to the Project Explorer window.
5. Right-click the My Getting Started Statechart.lvsc item and select Generate Code from the shortcut menu to generate code for the statechart.

## Running the Caller VI Again

1. Run the caller VI and watch the state data.
2. Click the Insert Battery button. The Time Display indicator displays the current time.
3. Click the Mode front panel button. The Time Display indicator displays 00.00.00.0, which means the statechart is in the Timer substate.
4. Click the Start front panel button to start the timer.
5. Click the Mode front panel button to switch the statechart to the Clock substate.
6. Click the Mode front panel button again to switch back to the Timer substate. Notice the Time Display indicator continued to increment while the statechart was in the Clock substate.
7. Click the Quit front panel button to stop the caller VI.
8. Save the caller VI.

## Summary

In this tutorial, you learned the following tasks:

• Creating orthogonal regions.
• Defining regions that execute concurrently.
• Defining the guard of a transition.
• Generating code for the statechart from the Project Explorer window.

## Where to Go from Here

• Documentation related to the LabVIEW Statechart Module.
• The Getting Started with LabVIEW manual contains an in-depth introduction to LabVIEW, including several tutorials that showcase LabVIEW features.
• The LabVIEW Fundamentals book provides information about LabVIEW programming concepts, techniques, features, VIs, and functions you can use to create many types of applications.
• Refer to the National Instruments Web site for additional developer resources, training, technical support, and so on.
 (Windows) To view topics related to the Statechart Module, click the Locate button, shown at left, in the toolbar at the top of this window. The LabVIEW Help highlights this topic in the Contents tab so you can navigate the related topics.