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

Statechart Module Tutorial Part 3: Defining and Modifying Statechart Data Types

LabVIEW Statechart Module 1.0 Help
August 2007

NI Part Number:
372103A-01

»View Product Info

In Part 2 of this tutorial, you created the beginnings of a statechart and learned how to find errors on the statechart diagram. In Part 3 of this tutorial, you define types of data upon which the statechart acts. You also configure the statechart to modify some of this data.

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

Defining the Type of Output Data

If you want the statechart to modify data, you first must define the type of data. This process involves creating the controls and/or indicators that represent the statechart data.

Complete the following steps to define the statechart output data. Later in this part of the tutorial, you configure the statechart to modify this data.

  1. Display the Project Explorer window for My Getting Started Statechart.lvsc.
  2. Double-click the Outputs.ctl item. LabVIEW displays the Control Editor window for this type definition.
  3. Notice the numeric control Numeric inside the Outputs cluster. Right-click this control and select Replace»Text Indicators»String Indicator. This action replaces the numeric control with a string indicator.
  4. Double-click the String label and enter Current Activity as the new label.
  5. Press the <Ctrl-S> keys to save this typedef.
  6. Close the Control Editor window to return to the Project Explorer window.

Defining the Type of State Data

State data is different from output data. State data is accessible from only the statechart, whereas output data also is accessible from a VI that calls the statechart. Use state data to define data types that other VIs do not need to access.

Complete the following steps to define the statechart state data. You modify this data in Part 4 of this tutorial.

  1. In the Project Explorer window, double-click the StateData.ctl item.
  2. Right-click the Data control and select Change to Indicator. This action replaces the numeric control with a numeric indicator.
  3. Double-click the Data label and enter Counter as the new label.
  4. Press the <Ctrl-S> keys to save this typedef.
  5. Close the Control Editor window to return to the Project Explorer window.

Configuring a State Entry Action

The next step is configuring each state to modify the Current Activity indicator that you defined earlier. You accomplish this task by defining an entry action for a state. An entry action is LabVIEW block diagram code that the statechart executes immediately after entering a state.

Complete the following steps to configure the Menu state to update this indicator.

  1. Double-click the Diagram.vi item to display the Statechart Editor window.
  2. Right-click the border of the Menu state and select Configure State. This action launches the Configure State dialog box.

    The left side of this dialog box contains the Reactions list. The right side of this dialog box displays several tabs that correspond to the item you select from the Reactions list. By default, the Entry Action item in the Reactions list is selected. This selection specifies that you want to configure the entry action of the state.

    The tabs on the right correspond to the selected item in the Reactions list. By default, the Action tab is selected. LabVIEW displays the block diagram you use to configure the entry action.
  3. Notice the Outputs cluster element on the right side of this block diagram. Click this cluster element and select Outputs»Current Activity. This element now displays Outputs.Current Activity, which indicates you can write to the value of the typedef you defined.
  4. Move the cursor over the input of this cluster element until the cursor changes to the Wiring tool.
  5. Right-click the input and select Create»Constant. LabVIEW places a string constant on the block diagram.
    Note  You cannot access the front panel window for this block diagram. Therefore, National Instruments recommends using block diagram constants instead of front panel controls.
  6. Enter Menu as the value of the constant. The block diagram now resembles the following figure:



    This entry action code means that, immediately after entering the Menu state, the statechart changes the Current Activity indicator to read Menu. Later in this tutorial, you will display the value of this indicator in the VI that calls the statechart.
  7. Click the OK button to save changes and return to the statechart diagram.
  8. The next step is configuring the Play and Pause states to update the Current Activity indicator appropriately. Configure the Entry Action for each state to update this indicator to Playing or Paused, respectively.
  9. Save the statechart.

Configuring the Action of a Transition

The transition from the Menu state to the Terminal pseudostate also causes a change in the activity of the statechart. Complete the following steps to configure this transition.

  1. Double-click the transition node in between the Menu state and Terminal pseudostate. This action launches the Configure Transition dialog box. Notice the tabs in this dialog box are identical to those in the Configure State dialog box. The only difference is that the Configure Transition dialog box does not have a list of items on the left side of the dialog box. These tabs apply to the whole transition.
  2. Click the Action tab.
  3. Write the block diagram code that changes the value of the Current Activity indicator to Stopped.
  4. Click the OK button to save changes and return to the statechart diagram.

Notice the transition node you just configured now displays two blue rectangles. The second blue rectangle indicates the transition has a specified action.

Duplicating the Transition Node

The next step is creating transitions from the Play and Pause states to the Terminal pseudostate. Instead of configuring additional transition nodes, you can duplicate the transition node you configured already. Complete the following steps to duplicate this transition node.

  1. Press the <Ctrl> key and click and drag the transition node you just configured.
  2. Move this node under the Play state.
  3. Click the bottom border of the Play state to initiate a transition segment.
  4. Move the cursor over the incoming tunnel of the transition node until LabVIEW displays a tip strip that reads Segment In.
  5. Click again to complete the transition segment. The segment appears broken because you have not created the outgoing transition segment yet.
  6. Create a transition segment from the other side of this node to the Terminal pseudostate. You might have to hold down the mouse button to initiate this segment.
  7. (Optional) Right-click the transition node and select either Rotate or Reverse to ensure the node faces the proper direction.
  8. Repeat steps 1–7 to create an additional transition node from the Pause state to the Terminal pseudostate. All three states now have a transition to the Terminal pseudostate.
  9. Save the statechart.

Generating LabVIEW Code for the Statechart

The next step is generating LabVIEW code for the statechart. This code is contained in a single Run Statechart function that you place on the block diagram of the caller VI. Complete the following steps to generate LabVIEW code for this statechart.

  1. Display the Statechart Editor window.
  2. Click the Generate Code button , which is located on the toolbar of this window.
  3. Close the Statechart Editor window.

Configuring the Caller VI

The next step is configuring a caller VI to execute and send triggers to the statechart. This VI contains two functions, Run Statechart and Send External Trigger, that you must link to the statechart. Complete the following steps to link these functions to the statechart.

  1. In the Project Explorer window, select File»Open.
  2. Browse to the labview\examples\Statechart\Tutorial\ directory and double-click Getting Started Caller VI (Unlinked).vi.
  3. Display the block diagram of this VI. All the wires are broken because neither the Run Statechart nor the Send External Trigger function is linked to a statechart.
  4. Locate the Run Statechart function. This function executes the statechart when you run the caller VI.
  5. Right-click this function and select Link to Statechart. LabVIEW prompts for a .lvsc file to link to.
  6. Browse to My Getting Started Statechart.lvsc and click the OK button. Notice the wires to and from this function now are intact.
  7. Repeat steps 4–6 for the Send External Trigger function.
  8. Select File»Save As and save the caller VI as Getting Started Caller VI Linked.vi.
  9. Press the <Ctrl-R> keys to run the caller VI. Click the front panel buttons and notice how the Current Activity indicators changes according to the state.
  10. Stop the caller VI.
  11. Save and close the caller VI.

Summary

In this tutorial, you learned about the following tasks:

  • Defining types of data that the statechart can modify.
  • Configuring the entry action of a state.
  • Configuring the action of a transition.
  • Generating LabVIEW code for the statechart.
  • Linking the Run Statechart and Send External Trigger functions to a statechart.

Where to Go from Here

In Part 4 of this tutorial, you create and configure regions and substates.


Resources


 

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