Modify the Simple State Machine LabVIEW Template

Updated Oct 26, 2023

Environment

Software

  • LabVIEW

The Simple State Machine template facilitates defining the execution sequence for sections of code. This particular implementation often is referred to as a Moore machine, which determines the next state based on decisions made in the current state. The design of this template makes it easy to insert new sections of code, remove sections of code, or change the order in which sections execute – all without making major modifications to the structure of the application.

LabVIEW provides a template of this architecture for you to build your state machine. Read through State Machine overview documentation to learn about state machines in LabVIEW, common terminology, and what steps to complete before creating a state machine. Once you familiarize yourself with the architecture, this tutorial walks through how to update the State Machine template to fit your needs.

This tutorial presents information on an advanced LabVIEW architecture and requires you to have knowledge of basic programming practices.

Run The State Machine Template

  1. Launch LabVIEW and select Create Project. From the Create Project dialog, launch the Simple State Machine template.
  2. In the Project Explorer window, open and run Main.vi.
  3. Click the front panel controls to display different pop-up dialog boxes.
    • (Optional) Observe behavior on the block diagram by turning on Highlight Execution. Watch the flow of data when controls on the front panel are pressed.
 

    Modifying the Data Types that States can Access

    1. In the Project Explorer, locate and open Data.ctl under the Type Definitions folder. LabVIEW will open the Control Editor window.
    2. Customize the control to your needs.
    3. Select File»Apply Changes.
     

    Adding Initialization Code

    1. Open Main.vi from the Project Explorer.
    2. Locate the Initialize subdiagram of the Case structure.
    3. Add code that initializes your application.
      • For example, you may want to open a file on disk for logging, initialize the data in Data.ctl to specific values, etc.
    4. Decide what state the application should transition to.
      • By default, the Initialize state transitions to the Wait for Event state. Depending on the needs of your application, you can modify this code to transition to a different state.

     

    Adding a Control that Initiates a State Transition

    1. In Main.vi, add a control to the front panel.
    2. Locate the Wait for Event subdiagram of the Case structure.
    3. Add an event case to the Event structure.
    4. Configure the event to trigger when the value of this new control changes.
    5. Click OK. LabVIEW creates a subdiagram in the Event structure.
    6. Drag the block diagram terminal for the new control inside this subdiagram.
    7. Decide what state you want to transition to as a result of the user interacting with this control and wire an enum with this state's value to the Next State output tunnel.
      • If the state you want to transition to does not exist, add it.
     

    Adding a State

    1. Update States.ctl, the typedef that contains the valid states:
      1. In Main.vi, locate an enum of State.ctl and open the typedef. LabVIEW will launch the Control Editor window.
      2. Right-click the enum and select Add new item after (or before) and enter the name of the new state.
        1. Alternatively, use the Edit items... dialog to configure the states listed in the enum
      3. Click outside the control to add the name to the list of states.
      4. Select File»Apply Changes.
    2. Add the state to the state machine:
      1. Add a subdiagram to the Case structure in Main.vi.
      2. Add code that the state will execute. As you do this, pay attention to the following guidelines:
        • Use the Unbundle by Name and Bundle by Name functions to access and modify state data.
        • Merge the error terminals of your functions with the included error wires.
        • Wire a FALSE constant to the Boolean output tunnel. Only the Stop state can stop the loop.
        • Each state must specify a transition to another state. Wire the value of the next state to the Next State output tunnel. You can wire this terminal directly, or you can implement conditional logic. 
        • Ensure the application contains a transition to the new state.
          • Updating the State enum and creating the state does not ensure that the state will be executed. Consider the logic or event needed to trigger this event.
          • If this trigger is based on user input, see the above section on Adding a Control to Initiate that Initiates a State Transition.
     


    Adding Shutdown Code

    Add shutdown code to the Stop subdiagram of the Case structure. Because this subdiagram is the only one that can stop the application, any code you add to this subdiagram is guaranteed to execute before the application stops and not at any other time. This design prevents accidental and partial shutdowns.
    Shutdown code commonly accomplishes the following tasks:
    • Frees memory by closing any open references.
    • Flushes any buffers in use.
    • Writes safe values to hardware input channels
     

    Adding Error Handling

    By default, this template stops if any function returns an error on its error out terminal. You might want to ignore specific errors or implement some more intelligent error handling. Complete the following steps to add error handling:
    1. Add an Error state.
      • See Adding a State section above
    2. In the Error state, create the code that handles errors in the ways you want.
    3. In all other states, transition to the Error state whenever an error occurs. 
      • This can be accomplished by adding conditional logic to the next state output terminal

     

    Removing the User Interface

    If your application does not need a user interface, complete the following steps to remove it:
    1. Delete all front panel controls and indicators.
    2. Delete the Wait for Event subdiagram from the Case structure in Main.vi.
      • Deleting the Waoit for Event case removes the Default subdiagram, which is the subdiagram that executes when the Case selector is told to execute to an unknown subdiagram. For error-handling purposes, make another subdiagram the default.
    3. Fix any broken wires.
    4. (Optional) You also can remove Wait for Event from State.ctl. This step ensures that your state machine can never attempt to transition to the state you just removed. However, before you do this, make a note of the enums in your application that call this state. After you save the updated State.ctl, these enums will change. Ensure the new, changed state is what you want.