LabVIEW Statechart Module

Statechart Module Tutorial Part 4: Adding Regions and Substates

  • Updated2023-02-21
  • 11 minute(s) read

In Parts 2 and 3 of this tutorial, you created a statechart consisting of three states, two pseudostates, and transitions between these objects. You also defined types output and state data types, configured the statechart to modify this data, and configured a caller VI.

In Part 4 of this tutorial, you add hierarchical substates to the statechart. You also send a trigger to the internal trigger queue, define a transition guard, and use some more in-depth debugging techniques.

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

Adding a Region and Two Substates

A region defines the area in which you can place states. You can place regions within states, which means you can use regions to define hierarchical substates. Complete the following steps to add a region and two substates to this statechart.

  1. Display the Statechart Editor window for the My Getting Started Statechart.lvsc statechart that you constructed in Part 3 of this tutorial. You also can navigate to the labview\examples\Statechart\Tutorial\Asynchronous directory and open Getting Started 3.lvsc for the completed statechart from Part 3 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. Move the cursor over the Play state until resizing handles appear.
  3. Drag the resizing handles down and to the right to increase the size of this state.
  4. Display the Statechart Development palette.
  5. Click the Region icon.
  6. Move the cursor within the Play state. Click and drag to place this region on the statechart diagram.
  7. Notice the region is labeled Region. Enter Tick Counter as the new label.
  8. Place two states in this region.
  9. Label the first state Tick and the second state Tock.
  10. Save the statechart.

The statechart diagram now resembles the following figure:

Note  The Play state now contains two substates. Therefore, the Play state now is a superstate.

Creating Another Trigger

The next step is creating a trigger. You will configure a transition to react to this trigger. Unlike the triggers you created in Part 2 of this tutorial, you will not send this trigger from the caller VI to the statechart. Instead, you will cause the statechart to send this trigger to the internal trigger queue of the statechart. You can send triggers to this queue only as part of a statechart action or a subVI that an action calls.

Complete the following steps to create this trigger.

  1. Display the Project Explorer window for this statechart.
  2. Double-click the Edit Triggers and Groups item. LabVIEW launches the Edit Triggers and Groups dialog box, which you use to create triggers and groups of triggers.
  3. Click the Create Trigger button to add a trigger to the list.
  4. Enter Increment as the name of this trigger.
  5. Click the OK button to save changes.

Configuring the Tick Substate

The next step is configuring the Tick substate to change the Current Activity indicator you defined in Part 3 of this tutorial. You also configure the Tick substate to send the Increment trigger to the internal queue of the statechart. Later in this part of the tutorial, you will configure a transition to react to this trigger.

Complete the following steps to define this behavior.

  1. Right-click the Tick substate and select Configure State to launch the Configure State dialog box.
  2. Define the entry action of this substate to display Counting in the Current Activity indicator. Refer to the Defining a State Entry Action section of Part 3 of this tutorial for more information about configuring the Current Activity indicator.
  3. Move the cursor over the Outputs.Current Activity element to display resizing handles.
  4. Click and drag to display another terminal of this element.
  5. Click this additional terminal and select InternalQueue»All Elements.
  6. Repeat step 5 for the Outputs terminal cluster on the left side of the block diagram.
  7. Place a Send Internal Trigger function, located on the Statechart Communication palette, on the block diagram.
  8. Wire the InternalQueue terminal cluster on the left side of the block diagram to the Internal Queue In input of the Send Internal Trigger function.
  9. Wire the Internal Queue Out output of the Send Internal Trigger function to the InternalQueue terminal on the right side of the block diagram.
  10. Right-click the Trigger input of this function and select Create»Constant from the shortcut menu.
  11. Select Increment as the value of this constant. The entry action resembles the following figure:

  12. Click the OK button to save changes and return to the statechart diagram.

The entry action of the Tick substate now updates the Current Activity indicator and sends the Increment trigger to the internal queue of the statechart.

Creating and Configuring Transitions in the Tick Counter Region

The next step is creating and configuring two transitions in the Tick Counter region. Complete the following steps to configure these transitions:

  1. Place an Initial pseudostate in this region.
  2. Create a transition from this pseudostate to the Tick substate. LabVIEW creates a transition node for this transition.
  3. Double-click the new transition node to launch the Configure Transition dialog box.
    Note  Notice that the Triggers/Groups and Guard tabs are disabled. You cannot define triggers or guards for initial transitions.
  4. Ensure you are on the Action page of this dialog box.
  5. Write LabVIEW code that sets the Counter indicator to 0. You defined this indicator in Part 3 of this tutorial.

    The following figure shows this code:

  6. Click the OK button to save changes and return to the statechart diagram.
  7. Create a transition from the Tick substate to the Tock substate.
  8. Configure this transition to react to the Increment trigger only.
  9. Click the Action tab.
  10. Use the Increment function to write LabVIEW code that increments the Counter indicator by 1. The following figure shows this code.

  11. Click the OK button to save changes and return to the statechart diagram.
  12. Create a transition from the Tock substate to the Tick substate.
  13. Create a new trigger named Return. Configure this transition to react to the Return trigger only. Later in this part of the tutorial you will configure the caller VI to send this trigger.
  14. Save the statechart.

The Play superstate now resembles the following figure:

Defining a Transition Guard

The next step is specifying that the statechart leaves the Play superstate automatically after the Counter indicator reaches a certain value. To specify this behavior, you can 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. The Play superstate already has one transition to the Menu state. Create a second transition from the Play superstate to the Menu state.
  2. Double-click the new transition node. LabVIEW launches the Configure Transition dialog box.
  3. Configure this transition to react to the Return trigger only.
  4. Click the Guard tab.
  5. Use the Greater? function to write LabVIEW code that returns TRUE if the Counter indicator is greater than 25. The following figure shows this code:

  6. Click the OK button to save changes and return to the statechart diagram.
  7. Save the statechart.
  8. Close the Statechart Editor window.

With this configuration, the statechart checks the guard code every time the caller VI sends the Return trigger. The guard code returns FALSE if the value of the Counter indicator is 25 or less. When the Counter indicator reaches 26, the guard code returns TRUE, and the statechart takes the transition to the Menu state.

Configuring the Caller VI to Send the Return Trigger

Complete the following steps to configure the caller VI from the Configuring the Caller VI section of Part 3 of this tutorial to send the Return trigger to the statechart.

  1. Open the caller VI and display the block diagram.
  2. Select the Timeout event case of the Event structure. Notice the blue output tunnel on the right side of this structure. A wire connects this tunnel to the Trigger input of the Send External Trigger function.
  3. Right-click this output tunnel and select Create»Constant from the shortcut menu. LabVIEW creates an enumerated type constant that contains the triggers you can send to this statechart.
  4. Select Return as the value of this constant.
  5. Save the caller VI.

The caller VI now is configured to send the Return trigger when a Timeout event occurs. The following figure shows this configuration in the caller VI.

The 500 constant specifies that a timeout occurs after 500 milliseconds elapse with no event occurring. Therefore, the statechart takes the transition from the Tick substate to the Tock substate every 500 milliseconds. Now the statechart increments the Counter indicator until you send the Pause, Stop, or Menu trigger. These triggers cause the statechart to exit the Play superstate. The statechart also exits the Play superstate when the Counter indicator reaches 26.

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 and select Properties. 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. 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.

Watching Statechart Data

The next step is running the caller VI and watching how the statechart increments the Counter indicator.

  1. Right-click the Run Statechart function and select Debug Statechart. LabVIEW displays the Statechart Debugging window for the statechart.
  2. Click the Open Statechart Data Display button , located on the toolbar of this window. LabVIEW displays the Statechart Data Display window. This window shows the values of the output data, state data, and internal trigger queue.
  3. Run the caller VI. The Current Activity indicator reads Menu.
  4. Look at the Statechart Data Display window. Notice the Current Activity indicator reads Menu, which is the same value as the front panel indicator of the caller VI.
  5. Click the Play front panel button. The Current Activity indicator displays Counting, which means the statechart is in the Tick substate. Watch the Statechart Data Display window as the value of the Counter indicator increases.

    When the value of the Counter indicator reaches 26, the statechart returns to the Menu state. This behavior is what you configured by using a transition guard.

Viewing the Execution of the Transition Guard

The next step is viewing the execution of the transition guard you defined. Complete the following steps to view the execution of this guard.

  1. While the caller VI is running, display the Statechart Debugging window and locate the guarded transition node from the Play superstate to the Menu state.
  2. Right-click this transition node and select Guard. LabVIEW displays the block diagram of the VI that corresponds to the guard you defined. Notice the code appears as you created it earlier in this tutorial.
  3. Move the cursor over the green wire that exits the Greater? function.
  4. Click this wire to place a probe on this wire.
  5. Click the Play front panel button again. Notice the Counter indicator resets to 0 and begins incrementing again. You configured this behavior by using the transition between the Initial pseudostate and the Tick substate.
  6. Watch the probe as the Counter indicator increments. The probe shows that the guard code sends FALSE to the Execute? indicator. This FALSE value means the statechart does not take the transition to the Menu state.

    After the Counter indicator reaches 26, the Greater? function sends TRUE to the Execute? indicator, which means the guard returns TRUE. The statechart then takes the transition to the Menu state.
  7. Click the Stop front panel button to stop the statechart and the caller VI.
  8. Close all windows except for the Project Explorer window.

Debugging the Statechart

If you get unexpected data from a caller VI that references a statechart, you can debug the statechart to identify and correct problems with the statechart data flow.

Complete the following steps to use LabVIEW debugging tools to debug the statechart.

  1. Double-click the Getting Started Caller VI Linked.vi to open your caller VI.
  2. Display the block diagram of this VI.
  3. Right-click the Run Statechart function and select Debug Statechart from the context menu to display the statechart diagram in debugging mode.
  4. Click the Highlight Execution button located on the statechart diagram toolbar. Highlighting Statechart Execution highlights the appropriate transitions and states as the statechart receives triggers from the caller VI and moves from one state to another.
  5. Run the Getting Started Caller VI Linked.vi and click the Play button on the front panel.
  6. Switch back to the statechart diagram. Observe the statechart passing between states, pseudostates and transitions.
  7. Click the Stop front panel button to stop the statechart and the caller VI when you are done observing the statechart diagram.
  8. Switch to the statechart diagram in debugging mode.
  9. Right-click the Tick substate and select Set Breakpoint from the context menu. A red border appears around the substate. This breakpoint pauses execution when the statechart reaches the substate.
  10. Repeat steps 5-6. When the statechart progress reaches the Tick substate, the statechart pauses execution.
  11. Right-click the Tick substate and select Entry Action from the context menu to display the Tick:Entry Action Block Diagram.
    Note  You also can set breakpoints in the guard and action code for states and transitions.
  12. Click the Highlight Execution button.
  13. Switch to the statechart diagram in debugging mode.
  14. Click the Step Into button located on the statechart diagram toolbar to enter the Tick substate and pause execution.
  15. Click the Step Into button again to execute the entry action of the Tick substate.
  16. Switch to the Tick:Entry Action Block Diagram to observe the execution of the entry action.
  17. Click the Stop front panel button to stop the statechart and the caller VI when you are done observing the entry action and statechart diagram.
  18. Close all windows except for the Project Explorer window.

Summary

In this tutorial, you learned about the following tasks:

  • Creating regions.
  • Creating substates.
  • Creating triggers by using the Project Explorer window.
  • Sending triggers to the internal queue of a statechart.
  • Defining the guard of a transition.
  • Displaying statechart data during execution.
  • Viewing the execution of a guard.
  • Debugging a statechart.

Where to Go from Here

In Part 5 of this tutorial, you create and configure orthogonal regions. You also learn how to use state history.

Log in to get a better experience