Skip to main content

Simple State Machine

A simple state machine consists of states, transitions which control the state transition, and actions or methods. This allows you to design and implement the behavior of finite machines.

Creating an application with a statechart

  1. Create a new project with the Standard Project template. For PLC_PRG in, select the UML Statechart implementation language.

    The project is created.

  2. In the device tree, select the application and click Add Object → POU in the context menu.

  3. Create a function block named FB_Simple_Machine in the UML Statechart (SC) implementation language.

    _uml_img_sc_simple_device_tree.png
  4. Instantiate the function block in the program PLC_PRG and implement the call of the function block instance.

    PROGRAM PLC_PRG
    VAR
        fb_Simple_Machine_A : FB_Simple_Machine;
    END_VAR
    fb_Simple_Machine_A();
  5. Save the project as SimpleMachine.project.

Designing flow logic as a statechart

Example of requirements on the machine:

  • The machine is in the state Idle after the initialization phase.

  • When events occur, the machine switches to the state DoIt.

  • When all actions and methods have been processed, the completion event is triggered and the machine returns to the idle state.

  • The machine can be switched from the idle state to the state Exit and then to the end state.

In this case, the design of the machine is as follows.

_uml_img_sc_simple_machine_design.png

The design does not yet contain any guard conditions that control the flow logic. In addition, no actions or methods are called yet, so that the machine is still inoperative.

In the device tree, double-click the function block FB_Simple_Machine to open the editor and create the diagram illustrated above.

Declaring variables for guard conditions

PROGRAM FB_Simple_Machine
VAR
    bInitDone : BOOL := FALSE;
    bShutDown : BOOL := FALSE;
    bDoSomething : BOOL := FALSE;
    bDoFinished : BOOL := FALSE;
END_VAR

User input is mapped to Boolean variables which are usually set externally by a user via a user interface. When you assign the variable as a guard condition in the statechart, the flow logic becomes switchable. The variables act as control variables.

_uml_img_sc_simple_machine_control.png

Adding methods and actions

When you have implemented the flow logic as a statechart, you add functionality to the states and transitions. In doing so, you extend the states with ENTRY, DO, and EXIT actions or methods. You can also add an action or method to a transition, which is then called one time at the state transition.

You can add actions to a state by selecting one of the _uml_icon_sc_action_entry.png, _uml_icon_sc_action_do.png, or _uml_icon_sc_action_exit.png symbols. You can click the _uml_icon_sc_transition_action.png symbol to add actions to a transition.

_uml_img_sc_simple_machine_function.png

If you assign an action to transitions, then you can design a similar logic that differs only slightly in time behavior.

_uml_img_sc_simple_machine_function_as_well.png

In the Devices view, you can trace the assignment of actions and methods. The actions and methods were created in the ST implementation language.

_uml_img_sc_simple_machine_devices.png