Skip to content

Statemachine

class Statemachine

Each controller has a state machine. It is generated automatically when the controller is created. The state machine describes the sequence or logic of a controller. The state machine is based on the specification of a UML state machine.

The statemachine has a graphical representation (diagram). This supports the developer when creating states and transitions.

The UML state diagram shows the different states a [controller] can be in and the transitions between these states. A state represents a stage in the behavior pattern of a controller. It is possible to have start and end states. An initial state, also called creation state, is the state in which a controller is at startup, while an end state is a state from which no transitions lead out. A transition is a transition from one state to another and is triggered by an event that is either internal or external to the controller.

Open the statemachine resource

Double-click on the statemachine property to get to the statemachine resource:

tg3t0rdh

vmdy9y8h

Open the state machine diagram

There are several ways to get to the statemachine diagram:

  • Click on the aggregation symbol:

beru45lz

  • From the context menu of the controller:

znveozcs

  • Double-click the Controller Instance in Controller Tree

0zxc95gs

Start Definition

First, a starting point must be defined. The starting point determines the state in which the control is started when the control is switched on.

9llgsyl5

State types

There are different types of states:

State

class State extends StateBase

SuperState

class SuperState extends StateBase, AStatemachineStart, AJunctionProperty

SuperState_withBusyFinal

This state extends the SuperState with an initialization function. When this state is created, a start state, a transition state and a target state are created.

class SuperState_WithBusyFinal extends SuperState


Best practice

If possible, the SuperState_WithBusyFinal should be used instead of a simple state. This automatically creates an often required transition state.


Create a state

  • Selection of the state type from the palette:

o1r61b78

  • Creating a transition from start to first state (in this case the SuperState 'INIT_Group')

ufx3u4lu

Sequence behaviour of the state machine

The following section describes the behavior when changing states.

Situation 1: start → state

qowbtdj8

sequenceDiagram
 loop every PLC task cycle
    task->>STATE1_entry: ev_entry
  activate task
   activate STATE1_entry
    STATE1_entry->>STATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task


  task->>STATE1_state: ev_cyclic
  activate task
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no tranistion active
   deactivate STATE1_state
  deactivate task
 end

Situation 2: start →  state → transition (self) → state

5vbduudw

sequenceDiagram
 loop every PLC task cycle
     task->>STATE1_entry: ev_entry
  activate task
   activate STATE1_entry
    STATE1_entry->>STATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: transition_1==true
   deactivate STATE1_state
  deactivate task


  task->>STATE1_exit: ev_exit [transition_1==true]
  activate task
   activate STATE1_exit  
    STATE1_exit->>TRANSITION_1_function: ev_transition [transition_1==true]
   deactivate STATE1_exit 
   activate TRANSITION_1_function
    TRANSITION_1_function->>STATE1_entry: ev_entry [transition_1==true]
   deactivate TRANSITION_1_function
   activate STATE1_entry
    STATE1_entry->>STATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no tranistion active
   deactivate STATE1_state
  deactivate task

  task->>STATE1_state: ev_cyclic
  activate task
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no tranistion active
   deactivate STATE1_state
  deactivate task
 end

Situation 3: start →  state → transition → state

p5qp7sq1

sequenceDiagram
 loop every PLC task cycle
     task->>SSTATE1_entry: ev_entry
  activate task
   activate SSTATE1_entry
    SSTATE1_entry->>STATE1_entry: ev_entry
   deactivate SSTATE1_entry
   activate STATE1_entry
    STATE1_entry->>SSTATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task

  task->>SSTATE1_state: ev_cyclic
  activate task
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task
 end

Situation 4: start →  super state → state

ti95yi6n

sequenceDiagram
 loop every PLC task cycle
     task->>SSTATE1_entry: ev_entry
  activate task
   activate SSTATE1_entry
    SSTATE1_entry->>STATE1_entry: ev_entry
   deactivate SSTATE1_entry
   activate STATE1_entry
    STATE1_entry->>SSTATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task

  task->>SSTATE1_state: ev_cyclic
  activate task
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task
 end

To be considered:

The cyclic part of the superstates is only executed for the first time after the completed 'entry' cycle.

Situation 4: start →  super state → state → super state → state

buz1bc4p

No transition active

sequenceDiagram
 loop every PLC task cycle
     task->>SSTATE1_entry: ev_entry
  activate task
   activate SSTATE1_entry
    SSTATE1_entry->>STATE1_entry: ev_entry
   deactivate SSTATE1_entry
   activate STATE1_entry
    STATE1_entry->>SSTATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task

  task->>SSTATE1_state: ev_cyclic
  activate task
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: no transition active
   deactivate STATE1_state
  deactivate task
 end

Transition_1 active

sequenceDiagram
 loop every PLC task cycle
     task->>SSTATE1_entry: ev_entry
  activate task
   activate SSTATE1_entry
    SSTATE1_entry->>STATE1_entry: ev_entry
   deactivate SSTATE1_entry
   activate STATE1_entry
    STATE1_entry->>SSTATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>task: transition_1 active
   deactivate SSTATE1_state
  deactivate task

  task->>STATE1_exit: ev_exit [transition_1==true]
  activate task
   activate STATE1_exit
    STATE1_exit->>SSTATE1_exit: ev_exit [transition_1==true]
   deactivate STATE1_exit
   activate SSTATE1_exit
    SSTATE1_exit->>TRANSITION_1_function: ev_transition [transition_1==true]
   deactivate SSTATE1_exit
   activate TRANSITION_1_function
    TRANSITION_1_function->>SSTATE2_entry: ev_transition [transition_1==true]
   deactivate TRANSITION_1_function
   activate SSTATE2_entry
    SSTATE2_entry->>STATE2_entry: ev_transition [transition_1==true]
   deactivate SSTATE2_entry
   activate STATE2_entry
    STATE2_entry->>SSTATE2_state: ev_cyclic
   deactivate STATE2_entry
   activate SSTATE2_state
    SSTATE2_state->>STATE2_state: ev_cyclic
   deactivate SSTATE2_state
   activate STATE2_state
    STATE2_state->>STATE2_state: check transition
    STATE2_state->>task: no transition active
   deactivate STATE2_state
  deactivate task

  task->>SSTATE2_state: ev_cyclic
  activate task
   activate SSTATE2_state
    SSTATE2_state->>SSTATE2_state: check transition
    SSTATE2_state->>STATE2_state: ev_cyclic
   deactivate SSTATE2_state
   activate STATE2_state
    STATE2_state->>STATE2_state: check transition
    STATE2_state->>task: no transition active
   deactivate STATE2_state
  deactivate task
 end

Transition_2 active

sequenceDiagram
 loop every PLC task cycle
     task->>SSTATE1_entry: ev_entry
  activate task
   activate SSTATE1_entry
    SSTATE1_entry->>STATE1_entry: ev_entry
   deactivate SSTATE1_entry
   activate STATE1_entry
    STATE1_entry->>SSTATE1_state: ev_cyclic
   deactivate STATE1_entry
   activate SSTATE1_state
    SSTATE1_state->>SSTATE1_state: check transition
    SSTATE1_state->>STATE1_state: ev_cyclic
   deactivate SSTATE1_state
   activate STATE1_state
    STATE1_state->>STATE1_state: check transition
    STATE1_state->>task: transition_2 active
   deactivate STATE1_state
  deactivate task

  task->>STATE1_exit: ev_exit [transition_2==true]
  activate task
   activate STATE1_exit
    STATE1_exit->>SSTATE1_exit: ev_exit [transition_2==true]
   deactivate STATE1_exit
   activate SSTATE1_exit
    SSTATE1_exit->>TRANSITION_2_function: ev_transition [transition_2==true]
   deactivate SSTATE1_exit
   activate TRANSITION_2_function
    TRANSITION_2_function->>SSTATE2_entry: ev_transition [transition_2==true]
   deactivate TRANSITION_2_function
   activate SSTATE2_entry
    SSTATE2_entry->>STATE2_entry: ev_transition [transition_2==true]
   deactivate SSTATE2_entry
   activate STATE2_entry
    STATE2_entry->>SSTATE2_state: ev_cyclic
   deactivate STATE2_entry
   activate SSTATE2_state
    SSTATE2_state->>STATE2_state: ev_cyclic
   deactivate SSTATE2_state
   activate STATE2_state
    STATE2_state->>STATE2_state: check transition
    STATE2_state->>task: no transition active
   deactivate STATE2_state
  deactivate task

  task->>SSTATE2_state: ev_cyclic
  activate task
   activate SSTATE2_state
    SSTATE2_state->>SSTATE2_state: check transition
    SSTATE2_state->>STATE2_state: ev_cyclic
   deactivate SSTATE2_state
   activate STATE2_state
    STATE2_state->>STATE2_state: check transition
    STATE2_state->>task: no transition active
   deactivate STATE2_state
  deactivate task
 end