Skip to content

Statemachine

class Statemachine

A Statemachine defines the behaviour of a Controller as a finite state machine: its states, the transitions between them, and the initial entry point. A Controller has at most one statemachine.

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

Functions

Besides the functions of the individual states, the statemachine itself has hooks:

initFunction

Called at the start of a controller and before the cyclic processing of the statemachine.

alwaysFunction_before

Called cyclically before the states of the statemachine are executed.

alwaysFunction_after

Called cyclically after the processing of the states of the statemachine.

notExistBehaviour

Defines the behaviour of the statemachine while the controller's exist flag is false. Without any definition the native single-answer implementation is active. Use a singleAnswerOverwrite definition to make the single-answer behaviour depend on the requested command, or define a function to implement a different behaviour during single-answer mode.

Simulation

Simulation is a central element of modelling the statemachine. A realistic simulation allows testing the software without the hardware, or with only parts of it. As a rule, every state that exchanges data with hardware should have a behaviour for the simulation case.

There are two kinds:

Single-answer simulation

The simplest form. It is active as soon as a controller is present but switched to not-exist. The command sent is confirmed directly with the corresponding state:

Parent > Child, Command = START
Child > Parent, State   = START

This expects a state with the same name as the command, and that this state is the target of the command.

State simulation

Here a behaviour for the simulation case is modelled for each state. When a controller is put into simulation, the hardware is separated on the software side, which allows hardware signals to be set from the simulation function.

State representation on PLC

Like commands, states are represented by constant values on the PLC. States receive the next free constant value during generation. Two exceptions allow that assignment to be influenced:

  1. If a state is named exactly like a command - case sensitive - the state gets the same constant value.
  2. If a SuperState with busy and final state has the structure below, the SuperState receives the value of the corresponding command minus 2, and the busy state the value minus 1.

state-constants