UnitStateMachine
- Namespace
- ZApplication
- Implements
FUNCTION_BLOCK UnitStateMachine IMPLEMENTS ZApplication.IUnitStateMachine, ZCore.IUnmanagedObject, ZCore.IObject, ZCore.IError
This is function block which implements the standard finite-state-machine of the Zeugwerk Template We have designed this standardized statemachine with predefined states which are defined in this UnitStateMachineState enumeration. There are also userdefined states possible by calling SetUserdefinedStateSequence and actions by calling SetUserdefinedActionSequence.
For more information on how to use the Zeugwerk Template Statemachine please read the doc here
Constructor
FB_init
METHOD FB_init (
[input] bInitRetains : BOOL,
[input] bInCopyCode : BOOL) : BOOL
Inputs
Returns
- BOOL
Properties
Busy
PROPERTY Busy : BOOL
Similarily to the object implementation of Busy, this property
returns TRUE, if the UnitStateMachine is currently working. StateMachines are busy if they are in a valid active state and have a
sequence associated with it.
Property Value
- BOOL
Done
PROPERTY Done : BOOL
If the UnitStateMachine is in idle state, often mentioned as "not on error and not busy" then this property returns TRUE.
This object returns Busy TRUE if the UnitStateMachine is currently working.
StateMachines are busy if they are in a valid active state including an initialized Sequence.
Property Value
- BOOL
Error
PROPERTY Error : BOOL
This property returns TRUE if a fault occured within a sequence or the initialization phase
of an object, respectively. If this property returns TRUE, one of the ErrorMessage methods
gives insight about the origin of the fault.
It is also possible to distinguish between initialization errors and sequence errors by checking the exact internal state of the object using the State method.
Property Value
- BOOL
StateAsString
PROPERTY StateAsString : ZCore.ZString
Property Value
Methods
ErrorId
METHOD ErrorId () : UDINT
Returns the error code of the first error source for this object. The method recursively goes down the error stack until the initial source of error of this object can be found. For performance reasons, the error stack is not cleared when the error state is reset. So this method should always be used in conjunction with Error.
Returns
- UDINT
ErrorMessage
METHOD ErrorMessage () : ZCore.ZString
Returns the error description of the first error source for this object. The method recursively goes down the error stack until the initial source of error of this object can be found. For performance reasons, the error stack is not cleared when the error state is reset. So this method should always be used in conjunction with Error.
Returns
ErrorSource
METHOD ErrorSource () : ZCore.IError
This method returns the direct error source of this object. This method can then be used to retrieve the actual error source by using the method of the returned IError interface.
Returns
IsTransitionAllowed
METHOD IsTransitionAllowed (
[input] state : INT) : BOOL
The state machine controls all transitions and checks if a specific transition is allowed. When changing from one state to another, this method checks if this is allowed and returns TRUE. For example a direct change from state init to state automatic is not possible, in this case this method returns FALSE.
Inputs
Returns
- BOOL
SetAutomaticSequence
METHOD SetAutomaticSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every state, it is possible to set a specific sequence. A Zeugwerk Template unit does this in its FB_init initialization, for every default active state This method sets the Sequence for its UnitStateMachineState.Automatic state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetBootSequence
METHOD SetBootSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every state, it is possible to set a specific sequence. A Zeugwerk Template unit does this in its FB_init initialization, for every default active state This method sets the Sequence for its UnitStateMachineState.Automatic state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetFaultReactionSequence
METHOD SetFaultReactionSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every state, it is possible to set a specific sequence. A Zeugwerk Template unit does this in its FB_init initialization, for every default active state This method sets the Sequence for its UnitStateMachineState.Automatic state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetFaultSequence
METHOD SetFaultSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every state, it is possible to set a specific sequence. Usually A Zeugwerk Template unit does not have any sequence for default inactive states (e.g. Fault, Idle, Init), but these can be set in the FB_init method of the unit. Sequences in inactive states are usefull for observing things when the application/controled system is not active or not doing anything and waiting for the next action. This method sets the Sequence for its UnitStateMachineState.Fault state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetGoHomeSequence
METHOD SetGoHomeSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every state, it is possible to set a specific sequence. A Zeugwerk Template unit does this in its FB_init initialization, for every default active state This method sets the Sequence for its UnitStateMachineState.Automatic state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetIdleSequence
METHOD SetIdleSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every inactive state, it is possible to set a specific sequence. Usually A Zeugwerk Template unit does not have any sequence for default inactive states (e.g. Fault, Idle, Init) but these can be set in the FB_init method of the unit. Sequences in inactive states are usefull for observing things when the application/controled system is not active or not doing anything and waiting for the next action.
In idle state this sequence can be used to handle all safety door opening requests for example
This method sets the Sequence for its UnitStateMachineState.Idle state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetInitSequence
METHOD SetInitSequence (
[input] sequence : REFERENCE TO ZAux.Sequence)
For every inactive state, it is possible to set a specific sequence. Usually A Zeugwerk Template unit does not have any sequence for default inactive states (e.g. Fault, Idle, Init) but these can be set in the FB_init method of the unit. Sequences in inactive states are useful for observing things when the application/controlled system is not active or not doing anything and waiting for the next action. This method sets the Sequence for its UnitStateMachineState.Init state.
Inputs
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetLogger
METHOD SetLogger (
[input] logger : ZCore.ILogger)
Use this function to enable logging for this statemachine instance.
Inputs
loggerILoggerInterface to a Zeugwerk Framework compatible logging object
SetName
METHOD SetName (
[input] name : ZCore.ZString)
Sets the name of the Statemachine As default, in a Zeugwerk Template here the name of the Unit is set in FB_init
Inputs
nameZStringname of the statemachine as string
SetSequence
METHOD SetSequence (
[input] state : UnitStateMachineState,
[input] sequence : REFERENCE TO ZAux.Sequence)
Links the state of the statemachine to a sequence, which is executed once the statemachine has transitioned into this state To make a transition into a states, the UnitStateMachine state has to be in UnitStateMachineState.Idle.
Note
state has to be greater than 0 and equal or smaller than MaxStates. The first state index of
Userdefined States has to be equal or greater than UnitStateMachineState.Last.
UnitStateMachineState.Halt and UnitStateMachineState.Halt are special states, which can not be mapped to a sequence.
Trying to do so results in booting error!
Inputs
stateUnitStateMachineStatePredefined or userdefined state of the statemachine
sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible Sequence
SetState
METHOD SetState (
[input] state : UnitStateMachineState) : BOOL
This method can be used to force the statemachine into a specific state. In contrast to TransitionAsync the change is abruptly and will not stop an optionally already running sequence. In contrast to SetState this method only accepts states defined in UnitStateMachineState.
This method should only be used to set the initial state of the statemachine and not for controlling a sequential flow. For the later purpose, TransitionAsync should be used, which performs a "clean" transition from one state to another.
In the Zeugwerk Template this method is used to set the initial state to Boot.
Inputs
stateUnitStateMachineStateenumeration which defines the Zeugwerk Template standard states
Returns
- BOOL
SetStoppedSequence
METHOD SetStoppedSequence (
[input] sequence : ZCore.ISequence)
For every inactive state, it is possible to set a specific sequence. Usually A Zeugwerk Template unit does not have any sequence for default inactive states (e.g. Fault, Idle, Init) but these can be set in the FB_init method of the unit. Sequences in inactive states are usefull for observing things when the application/controled system is not active or not doing anything and waiting for the next action. This method sets the Sequence for its UnitStateMachineState.Stopped state.
Inputs
sequenceISequenceZeugwerk compatible Sequence implementing the ISequence interface for the UnitStateMachineState.Stopped
SetStopSequence
METHOD SetStopSequence (
[input] sequence : ZCore.ISequence)
For every state, it is possible to set a specific sequence. A Zeugwerk Template unit does this in its FB_init initialization, for every default active state This method sets the Sequence for its UnitStateMachineState.Automatic state.
Inputs
sequenceISequenceZeugwerk compatible Sequence implementing the ISequence interface for the UnitStateMachineState.Stop
SetUserdefinedActionSequence
METHOD SetUserdefinedActionSequence (
[input] stateIndex : INT,
[input] name : ZCore.ZString,
[input] sequence : REFERENCE TO ZAux.Sequence)
Adds a userdefined action for the Zeugwerk Template UnitStateMachine. Actions can be used to execute sequences when the statemachine is in any non-active state
- Init
- Fault
- Stopped
- Idle
This is useful if a unit has an error or can not be homed for whatever reason and for example someone wants to call a sequence for opening a clamping system.
Note
stateIndex has to be greater or equal to Last and smaller than MaxStates.
Calling this method with with an invalid stateIndex results in a BootingError.
Inputs
stateIndexINTIndex of a userdefined action, has to be equal or greater than UnitStateMachineState.Last
nameZStringName of the sequence as it is displayed in StateAsString, if the parameter is set this will internally call
sequence.SetName(name)sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible sequence object
SetUserdefinedStateSequence
METHOD SetUserdefinedStateSequence (
[input] stateIndex : INT,
[input] name : ZCore.ZString,
[input] sequence : REFERENCE TO ZAux.Sequence)
This method adds a userdefined state and a sequence to the statemachine. Its similar to SetSequence but this method sets the UserdefinedState flag per default.
Note
stateIndex has to be greater or equal to Last and smaller than MaxStates.
Calling this method with with an invalid stateIndex results in a BootingError.
Inputs
stateIndexINTindex to a userdefined state
nameZStringName of the sequence as it is displayed in StateAsString, if the parameter is set this will internally call
sequence.SetName(name)sequenceREFERENCE TO SequenceInterface to a Zeugwerk Framework compatible sequence object
State
METHOD State () : UnitStateMachineState
Return the current state index of the state machine. The method returns 0 if an invalid state has been set
Returns
StateDecoded
METHOD StateDecoded (
[input] state : INT) : ZCore.ZString
Allows to map a state enumeration to strings, which are then used for logging.
Inputs
stateINTstate as definied with UnitStateMachineState or a userdefined state/action.
Returns
TraceErrorStack
METHOD TraceErrorStack (
[input] trace : ZCore.IErrorTrace)
This method is used internally when recording an error trace.
Inputs
traceIErrorTrace
TransitionAsync
METHOD TransitionAsync (
[input] startToken : ZCore.IStartToken,
[input] state : UnitStateMachineState) : BOOL
to state, but only if the transition is allowed.
The state change is not performed instantly, but only in the next call of the Cyclic method, which is usually automatically called by an object manager or the parent of this object.
If the transition is successfully executed this method will return TRUE otherwise FALSE.
Inputs
startTokenIStartTokenstateUnitStateMachineStateenumeration which defines the Zeugwerk Template standard states
Returns
- BOOL