Plcopen Rules SFC Version 1.0
Plcopen Rules SFC Version 1.0
PLCopen
for efficiency in automation
DISCLAIMER OF WARANTIES
The name ‘PLCopen®’ is a registered trade mark and together with the PLCopen logos owned by
the association PLCopen.
This document is provided on an ‘as is’ basis and may be subject to future additions, modifications,
or corrections. PLCopen hereby disclaims all warranties of any kind, express or implied, including
any warranty of merchantability or fitness for a particular purpose, for this moment. In no event will
PLCopen be responsible for any loss or damage arising out or resulting from any defect, error or
omission in this document or from anyone’s use of or reliance on this document.
Name Company
Bert van der Linden ATS
Bernhard Werner Codesys
Hiroshi Yoshida Omron
Barry Buxton Omron
Arnulf Meixner Phoenix Contact
Andreas Weichelt Phoenix Contact Software
Eelco van der Wal PLCopen
Version Content
V 0.1 Sept. 11, 2014. Basic document provided by Mr. Bonfatti et al. and basic editing by EvdW
V 0.2 Oct. 2, 2014. With comments from Barry Buxton. Provided to the group
V 0.3 Nov 28 – incl. comments and objective from Andreas Weichelt and Hiroshi Yoshida
V 0.4 Dec.3 2014 – during the Face2Face meeting in Frankfurt
V 0.5 March 2017 – as result of meeting with Bert van der Linden in Jan. and addition of PackML
V 0.6 June 29, 2017 as result of the webmeeting on May 31 and June 29
V0.6a July 31, 2017 as a result of the webmeeting on July 6
V 0.7 August 24, as result of the webmeeting and the addition of Ch 3.6 on final scan
V0.8 As result of the webmeeting January 25, 2018. Basis for V0.99 Release for Comments
V0.99 Release for Comments, published on Feb. 28, 2018
V 1.0 Official release
TABLE OF CONTENTS
1 Introduction to this document .................................................................................................... 6
1.1 Software Modularity and the role of SFC ............................................................................. 6
2 Introduction SFC ......................................................................................................................... 8
2.1 Steps and transitions .............................................................................................................. 8
2.2 Actions................................................................................................................................... 9
2.3 Qualifiers ............................................................................................................................. 10
2.4 SFC evolution rules ............................................................................................................. 10
2.5 Divergence and convergence............................................................................................... 11
3 Structural properties of SFCs .................................................................................................. 13
3.1 Process Structure ................................................................................................................. 13
3.2 Simultaneous sequences ...................................................................................................... 15
3.3 Parallel sequences................................................................................................................ 16
3.4 Action Blocks ...................................................................................................................... 17
3.4.1 Introduction .................................................................................................................. 17
3.5 Qualifiers ............................................................................................................................. 18
3.5.1 Introduction .................................................................................................................. 18
3.6 Execution control ................................................................................................................ 25
3.6.1 ACTION_CONTROL function block ......................................................................... 25
3.6.2 Final Scan ..................................................................................................................... 26
4 Coding rules SFC....................................................................................................................... 30
4.1 Convergence and Divergence do’s and don’ts .................................................................... 30
4.2 Linearization in SFC ........................................................................................................... 32
4.3 Mutually exclusive transition conditions ............................................................................ 33
4.4 Do not use priorities for the different transitions ................................................................ 36
4.5 Dependence on the previous state ....................................................................................... 36
4.6 Advanced use of parallel sequences .................................................................................... 38
4.7 Action independence ........................................................................................................... 40
4.8 Rules for S/R qualifiers’ usage ............................................................................................ 42
4.9 Rules for Step variables ....................................................................................................... 43
4.10 Rules for Actions ................................................................................................................. 43
5 Introduction State Diagrams .................................................................................................... 44
6 Examples with state diagrams .................................................................................................. 48
6.1 Example 1: Simple motor control ....................................................................................... 48
6.1.1 Introduction .................................................................................................................. 48
6.1.2 States and Transitions: ................................................................................................. 48
SFC is a suitable language (in IEC 61131-3) to support the initial and more crucial phases of the
PLC software development life cycle. Reasons include:
High expressive power. The SFC language has the same expressive potential as state
diagrams and, it is comparable to Petrinets in facing concurrent problems. State diagrams
and Petrinets are considered the most appropriate tools to model dynamics and they are
extensively used in many fields. Thus, we can say that the SFC language is intrinsically
capable of modelling the behaviour of a system.
Graphic formalism. SFC is not the only language with graphic primitives made available by
the standard, but with respect to the other two graphic languages (LD and FBD) it offers
higher level characteristics to describe system dynamics. Thanks to its graphic syntax, it is
very easy to learn and use. Moreover, it results particularly suited to represent the process at
different detail levels.
Support of preliminary design. The SFC graphic formalism can be used right from the
beginning, to give a first formal representation of the system behaviour. Therefore, it is a
precious tool in the preliminary analysis and early design phases, when many aspects are
still not well defined or even unknown to the designer. Using SFC we avoid adding the
ambiguity of natural language description to the approximate specifications available to the
designer. In this way, the number of misunderstandings between customer, designer and
programmers is substantially reduced.
Support of detailed design. The SFC scheme produced in the early design phase can be
progressively specified and refined as new information becomes available. Thus, the desired
level of detail is reached stepwise.
Natural connection with the other languages. It is quite evident that the SFC language can
be used in combination with the other languages of the standard, particularly suited to
describe control details such as transition conditions and elementary actions. The possibility
to use the right language at the right moment increases the global efficiency of software
development and betters the performances of the resulting executable code.
Support of software partitioning. Using the SFC language makes the partitioning of code
into portions to be executed at different cycle scans easier. This is one of the possible
techniques to reduce the maximum execution time of the cycle. The advantage given by
SFC schemes is that they represent such partitions explicitly and, above all, they clarify
which are the conditions affecting the execution order of the various software portions.
2 Introduction SFC
STEP Si:
Si actions
Si (* action declarations *)
END_STEP
TRANSITION Tij:
Tij
(* transition condition *)
END_TRANSITION
2.2 Actions
Zero, one or more actions may be associated with each step. A step with no associated actions is
providing a WAIT function, which is, waiting for the following transition condition to become true.
An action declaration consists of the action name (of type string) and the action body. The action
body can be a Boolean variable, a collection of instructions in IL, a collection of statements in ST, a
collection of rungs in LD, a collection of networks in FBD or a sequential function chart (SFC) in
its turn. Actions are declared and associated with steps via textual step bodies or graphical action
blocks. The scope of the declaration of an action is local to the POU containing the declaration.
IEC 61131-3 3rd edition:
Table 60 – Action control features
No. Description Reference
1 With final scan per Figure 22 a) and Figure 23 a)
2 Without final scan per Figure 22 b) and Figure 23 b)
These two features are mutually exclusive, i.e., only one of the two shall be supported in a given
SFC implementation.
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 9/59
®
PLCopen
for efficiency in automation
2.3 Qualifiers
The control of actions is defined by action qualifiers. Possible action qualifiers are those listed in
the following table. Qualifiers specify the execution of actions at every execution cycle in relation
to the states of their associated steps. Actions using the qualifier N or none are executed as long as
their associated steps are active. The L, D, SD, DS, and SL qualifiers require an additional
associated parameter of type TIME for the delay or the limitation. See 3.5 Qualifiers for further
explanations of action qualifiers.
3 Start Timer
4 Stop motor M1
4 Tix
Parallel divergence and convergence are represented by double horizontal lines as in the example of
Figure 4. Criteria for a correct use of simultaneous sequences are proposed in 4.3 Mutually
exclusive transition conditions, together with the analysis of the most frequent modelling errors.
1 Initialize
1 Run
3 True
Sequence of steps An SFC program in its simplest form: “one shot” behaviour.
The system moves from one step to the other as soon as the
Start triggering condition associated to the transition between the two
states is verified. More precisely, when the condition associated to
T01
T01 becomes true, the system moves from the Start step, which
consequently becomes inactive, to the S1 step, which becomes
S1
active. The same occurs for T12, closing S1 and starting S2, and
T12 for T2S that brings the system from S2 to the Stop step. Since
there are no leaving paths from the final step, the system remains
S2 in that state until a warm or cold start happens.
T2S
Stop
Cyclic sequence of steps The most common situation is that realising a cyclic execution of
the control. In the scheme here we imagine to return to the S1 step
Start
after having reached S3 through S2.
T01 In this case we still have an operator turning on the system in step
Start, but then the system keeps on running cyclically through
S1 steps S1, S2 and S3 until an external (and therefore not
T12 represented) event stops it.
One can interpret this on-going cycle as part of an SFC scheme
S2 with an action associated to a step belonging to an upper level
T23 SFC scheme. This action can be stopped (that is, no more
executed) by simply closing (making inactive) the upper level step
S3 it is associated to.
T31
Cyclic sequence of steps with In the scheme to the left a cyclic behaviour with a controlled
controlled termination exit condition is represented. This condition is associated to
the transition T2S bringing from step S2 to step Stop, as an
Start alternative to transition T21 bringing back the system to S1
T01 from S2. In this situation a smooth conclusion of the
execution is introduced. In practice, the PLC software
S1
includes the functions which are required to keep under
T12 control the system shut down sequence.
This can represent for example, the behaviour of a machine
S2
tool which is turned on (Start), take a piece (S1), process the
T21 T2S piece (S2) and then take another piece to process. The
machine has a command that can stop it after the processing
Stop
of the last piece has been terminated, but it cannot move
directly from S1 to Stop.
Cyclic sequence of steps with Divergence corresponds to operative situations where the
divergence behaviour of the controlled system should differ according to
the conditions that occurred in conclusion of the step
Start
immediately preceding the divergence.
T01 The figure on the left shows a divergence after step S1 to
S1
either steps S21 or S22. After them, the system converges
towards step S3 before reaching the final choice (again a
T121 T122
divergence) of coming back to S1 or exiting to the Stop step.
Concerning transitions, we observe that conditions T121 and
S21 S22
T122 should be disjoined to determine unambiguously the
T213 T223 choice between the two alternative steps S21 and S22. On the
contrary, conditions T213 and T223, leaving respectively S21
S3
and S22, are independent of each other: they are individually
T31 T3S related to the steps they leave, thus, in general, each of them
Stop
has no relation with the step which has not been chosen at the
divergence.
Divergence and transition conditions This scheme shows a divergence of sequence case: it is
equivalent to the representation of the three transitions
Sa
linkingSastep Sa respectively to steps Sx, Sy and Sz. Since
the leaving transitions from Sa are three, there are three
triggering conditions
T Tax, Tay and Taz, with necessary
Tax Tay Taz
disjunction to obtain a deterministic algorithm.
Sx Sy Sz Sx Sy Sz
Divergence
(a) correctof sequence
divergence (b) wrong divergence
Convergence and transition conditions On the left a convergence case: it is equivalent to the
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 14/59
®
PLCopen
for efficiency in automation
Sx Sx
Convergence of sequence
(a) correct convergence (b) ambiguous divergence
Sequence loop and transition conditions In the figure on the left, with the ending of a sequence
loop, gives the representation of the (implicit)
convergence, where a triggering condition is
Sa associated to each transition.
Sa
Tab T
Sb Sb
Sequence loop
(a) correct sequence loop (b) ambiguous sequence loop
SFC scheme with action blocks The complete description of a control program
modelled as an SFC scheme requires making
Qs As Vs explicit the effects of step activation in terms of
Start
actions. Each effect is described by an action
TS1 block associated to the step. The frequency of
Q11 A11 V11
action execution is chosen by means of proper
S1 qualifiers. The example here shows an SFC
Q12 A12 V12
scheme with four steps and six associated actions.
T12 If we refer to the generic i-th action, Qi represent
S2
Q2 A2 V2 the qualifier, Ai identifies the action, Vi is the
name of an optional Boolean variable chosen to
summarise the results of the action.
T21
Stop
Qp1 Ap1 Vp1 Some of the steps may have no associated actions.
S1
Qp2 Ap2 Vp1 They represent waiting states, where no activity is
performed. In such states the system simply waits
for the occurrence of events, typically detected as
variations of input variables, which turn true the
exit condition
3.5 Qualifiers
3.5.1 Introduction
The execution of an action block is related, through its qualifier, to the activation of the steps
invoking it and the duration of their staying in an active condition. For this reason, two special
variables are updated by implicit actions associated to all the steps. One is a Boolean variable,
called step active flag (and denoted as stepname.X) which represents the activation state of the step:
its value is 1 in every execution cycle where the step is active and 0 otherwise. The other is a TIME
variable, representing the step elapsed time (and denoted as stepname.T): it is set to zero whenever
the step becomes active. Both these variables may be tested in transition conditions and in actions
associated to other active steps for synchronisation purposes. The standard refers to several
qualifiers, representing alternative ways to associate actions to steps (Table 59 in IEC 61131-3):
If the transition condition becomes TRUE there are 2 (implementation specific) options: with a final
scan or without a final scan of the related ACTION block. For details see Error! Reference source
ot found. Execution control.
Sx
Step activity
Execution
5s 5s
N Ax
Action Ax:
if Sx.T >= t#5s then Num:=Num+1
end_if
end_action
Effect of the SD qualifier The SD (Stored and time Delayed)
qualifier, used in combination with the R
(reset) qualifier, stores the action but
SD
Sx t#5s
Ax Vx delays its execution of a given time
Action Ax:
interval. Therefore, the action starting
Num:= Num+1 may occur when the step with the SD
end_action
...
qualifier is no more active. More
Sy precisely, it may occur during the
activation of any of the following steps,
but before reaching that with the R
R Ax Vx
Sz qualifier. In the figure on the left, action
Ax has to start 5 second after its storing.
During the first execution of the sequence
Sx there is time enough to start the action (in
Step activity
particular, while Sy is active), but this
Sy does not hold during the second
execution as the R qualifier is reached too
Sz
soon.
Note: To represent the same behaviour
5s 5s
using only P and N qualifiers, it is
Execution
necessary to remember that the starting of
action Ax execution is conditioned by the
elapsed time calculated from the
activation time of step Sx. Unfortunately,
the value of the Sx.T variable is not
P Ax1
Action Ax1:
TONx (IN:=true, PT:=t#5s)
end_action
N Ax2
Action Ax2:
if TONx.Q then Num:=Num+1
end_if
end_action
Sy
Sz
5s 5s
Execution
The same behaviour as DS but with only the P and N Note: To get the same behaviour as
qualifiers DS using only the P and N qualifiers,
it is necessary to split Sx into two
steps, and use two versions of Sy (and
of all the other steps possibly
Sx1 preceding Sz), as it is shown in the
figure on the left (the relational
operators depend on the particular
Txy and Sx1.T<t#5s Sx1.T>=t#5s implementation of the DS qualifier).
The actions associated to the steps in
Sy Sx2
this solution are so characterised:
Tyz Txy Sx1 has associated all the P and N
type actions of Sx, but not Ax
Sy’ Sx2 has associated all the N type
actions of Sx, and the action [N,
Tyz
Ax]
Sy remains as before (it has not
Sz associated the Ax action)
Sy' has associated the same actions
as Sy, and the action [N, Ax]
( if other steps exist before Sz,
they are distinguished as Sy and
Sy')
Sz does not mention the Ax action.
P Ax1
Action Ax1:
TONx (IN:=true, PT:=t#5s)
end_action
N Ax2
Action Ax2:
if not TONx.Q then Num:=Num+1
end_if
end_action
It is very important that the programmer puts much care in using the stored qualifiers, as they can
cause effects difficult to see and control. The major problem with the stored qualifiers is their
hidden association to the steps preceding the R declaration. A clear and strong principle should
inspire the programmer activity: make coding as explicit as possible so as to obtain a more
readable control software although that may be longer.
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 24/59
®
PLCopen
for efficiency in automation
In order to understand the execution logic of an SFC it is necessary to keep in mind, that there is no
direct link between the active state of a Step and the execution of an associated action.
Associated with each action is an implicit ACTION_CONTROL function block (or a functional
equivalent), which is in control of the execution of the action. An active step sets an input of the
ACTION_CONTROL block and the evaluation of the ACTION_CONTROL block controls
whether or not the action is executed in the current cycle.
This is important if an action is associated to different STEPs and even more, if the action is
associated to these steps with different qualifiers.
For example, if an action is associated with two parallel steps like in the following figure, then the
action will only be executed once in a cycle, even if both steps are active:
The specifics of this implementation may have significant impact on the semantics of the SFC.
If bTest1 is TRUE for one cycle, then ACT will be executed in the next two cycles, even if Step1 is
active only in the next cycle.
Now once again it is important to remember, that the Action is not directly linked to the state of the
Step, but to the state of the ACTION_CONTROL block.
Consider the following slightly different situation:
If bTest1 is TRUE for one cycle, then ACT will be executed in the next three cycles, there is no
final scan between the active states of Step1 and Step2, since the associated ACTION_CONTROL
block does not detect a falling edge.
If an action is associated exactly to one step, and if it is associated with the N-qualifier, then the
final scan can be identified by checking the Step-Flag of the associated step. The execution is in the
final scan, if the Step is not active.
In the upper example, the Action ACT could be implemented in the following way:
ACTION ACT
IF Step2.x OR Step1.x THEN
// do cyclic things;
;
ELSE
// do final things;
;
END_IF
END_ACTION
Some implementations of the SFC will offer in addition to the Step flags (and as an extension to the
standard) Action flags that allow access to the state of the ACTION_CONTROL Block. Then the
final scan can be determined independent from any step flags.
Due to the final scan, it is a common situation, that two actions are executed in the same cycle, even
if there is no parallel branch in the graph. To discuss the problems that may arise due to the final
scan, consider the following SFC graph:
The two Actions ACT1 and ACT2 shall have the following implementations:
ACTION ACT1
x := x + 1;
END_ACTION
ACTION ACT2
y := x;
END_ACTION
The following table shows the states of the upper Graph in the following cycles after bTest1 has a
rising edge:
Cycle Step1 Step2 Step3 ACT1 ACT2 x y
Thus, in cycle 2 and 3, both actions get executed, which may be unexpected. Note furthermore, that
the standard does not require any specific order of execution for the actions. An order of execution
according to the topology in the graph may be expected, but is not possible, since the action may be
associated to different steps at several locations in the graph. ACT1 is above and below ACT2 in
the same graph.
Therefore, after cycle 2 and 3 the value of y may be equal to x or equal to x-1, depending on the
implementation of the SFC, and depending on the order of execution of the actions. If ACT2 is
always executed after ACT1, the sequence of values for y would be 0, 2, 3, 3. If ACT2 is always
executed before ACT1, the sequence of values for y would be 0, 1, 2, 2.
Some implementations of the SFC may execute all final scans before executing all other actions, but
this is an implementation dependent feature and not required by the standard. In this case the order
of execution of the two actions would be different in cycle 2 and 3. In cycle 2 ACT1 would be
executed first, in cycle 3 ACT2 would be executed first. In this case, the sequence of values for y
would be 0, 2, 2, 2.
Every parallel sequence must have only one initial step and only one final step.
Wrong SFC scheme Every parallel sequence must have only one
initial step and only one final step.
Ti
As example shown on the left a wrong
situation is when one of the parallel sequences
included between a simultaneous divergence
Su Sx and the following convergence presents two or
more final steps. Since only one step of such
sequence is active at a time, the final steps
cannot be all contemporarily active. It means
Tuv Tuw
that, in the example, the final steps Sv and Sw
of the left-hand sequence are never both
Sv Sw active, therefore the simultaneous
convergence will never operate.
Tj
A sequence leaving a simultaneous divergence should never be directed towards two or more
simultaneous convergence.
Wrong SFC scheme A sequence leaving a simultaneous divergence
should never be directed towards two or more
Ti
simultaneous convergence.
Combining a simultaneous convergence and
divergence with a simple divergence, as in the
Sw Sx example on the left, can introduce unreachable
steps. The problem does not arise from having
nested parallel sequences, as this is a natural
Txy Txz
use of the SFC language. Rather, the mistake is
Sy Sz
caused by the fact that the nesting does not
respect the established rules. In the example,
the simple divergence forces to choose between
Tzk
Tj
the execution of transitions Txy and Txz, so
that either step Sy or step Sk can finally be
Sj Sk activated, but not both. This means that one
simultaneous convergence is never reached: the
consequence is again a blocking of the control
function.
Ts
Tzk
Tj
Sj Sk
The IEC 61131-3 standard is deterministic: it ensures that in all cases only one branch in a
divergence of sequence can become active. In absence of additional information it is implicitly
assumed that the branch to activate is the leftmost one (among those which present in that moment a
true condition). Otherwise, the programmer can assign an explicit priority value (the lower this
value, the higher the priority) to the branches with overlapping conditions.
The IEC 61131 standard allows the overlapping of conditions associated to transitions leaving the
same state. Nevertheless, even if more than one condition could be true at the same time, only one
is executed and consequently we have only one step activated next. When the triggering event
cannot determine alone the step swap, further information is required to decide. The SFC syntax
suggests that the choice should be made according to a priority parameter, implicitly or explicitly
defined.
In absence of additional information it is implicitly assumed that the branch to activate is the
leftmost one (among those which present in that moment a true condition). Otherwise, the
programmer can assign an explicit priority value (the lower this value, the higher the priority) to the
branches with overlapping conditions.
This idea of priority is still ambiguous and can cause some mistakes. First of all, the word priority is
misleading in itself as it could induce to think that all the overlapping transitions will be executed in
the order given by the established priorities. Besides, the risk of mistakes is very high when the
overlapping transition conditions affect many diverging branches, and various priority relations are
defined on them. These are the reasons why many authors strongly suggest, as we do, to use
mutually exclusive transition conditions only.
Sx
For example, consider a control structure close to that shown in Figure 6 and suppose to establish
the following priority relations (Tij denotes the condition of the transition leaving step Si and
reaching step Sj):
This situation is conveniently represented by means of an oriented graph, with nodes corresponding
to the transition conditions and branches starting from the lower priority node and ending to the
higher priority node (see Figure 6a). If we move along the graph starting from the lowest priority
nodes (Txd in this case) it is easy to assign the correct numerical values to priorities:
priority (Txd) = 4
priority (Txb) = 3
priority (Txc) = 2
priority (Txa) = 1
Txa Txa
Txc Txc
The oriented graph technique is particularly useful to find out those cases where priority numerical
values cannot be used. The graph represented in Figure 6a is not cyclic, in that it is not possible to
start from one node and reach the same node moving along the oriented branches. Obtaining an
acyclic graph is a necessary and sufficient condition to assign numerical values to priorities.
Instead, Figure 6b shows a cyclic graph, derived from the previous one by adding the further
relation:
This condition generates the cycle involving nodes Txb, Txc e Txd. If the graph resulting from the
given relations is cyclic it is not possible to define a set of priority values satisfying all the
conditions. In that case, the only thing to do is modifying the transition conditions in such a way to
remove overlaps. Better, let us see how to achieve the recommended solution constituted by
mutually exclusive transition conditions starting from a non-deterministic SFC scheme.
We examine first a case with only two overlapping conditions Tx e Ty. Figure 7a shows, in form of
a Venn diagram, the truth domains of the two conditions, where the common part is indicated as
Txy. We can say that the Txy domain corresponds to the variable values that force true both
conditions. If we want to disjoint the two conditions without losing the priority of, say, Ty over Tx
(i.e. Ty precedes Tx), we have to redefine the truth domains as follows:
Ty precedes Tx and Tz
Tx precedes Tz
and, nevertheless, remove the ambiguities it is necessary to redefine the transition domains
according to the following expressions:
Tx Ty Tx’ Ty’
Tz Tz’
The result is shown in Figure 8b. In terms of transition conditions, the Tx and Tz predicates are thus
rewritten:
For the sake of completeness, let us consider again the non-deterministic SFC scheme reported in
Figure 8. In order to make it deterministic and taking into account the assigned priorities reported
on the scheme branches, the transition conditions must be modified as follows:
that is:
Txa' = (V1 > 3) and not ((V1 = 5) and (V2 <> 0))
Txb' = (V1 = 5) and (V2 <> 0)
Txc' = ((V2 < 10) or (V3 < 10)) and not ((V1 > 3) or
((V1 =5) and (V2 <> 0)) or ((V1 =0) and (V3 =0)))
Txd' = (V1 = 0) and (V3 = 0) and not ((V1 > 3) or
((V1 = 5) and (V2 <> 0)))
Si Qi Ai Vi Sj Qj Aj Vj
Tix Tjx
Sx Qx Ax Vx
Txy
Sy Qy Ay Vy
Figure 9 shows a hypothetical case of dependence from the preceding state. In fact, we can assume
that in the text of action Ax, associated to step Sx, it is required to differentiate the code in
consideration of the fact that the control comes from either step Si or step Sj.
If the differentiation is minimal, in that the most part of the code is shared in both cases, the easiest
solution is that shown in Figure 10. The coming from steps Si or Sj is marked by the Boolean
variables FromSi and FromSj which are turned true respectively from the actions Ai and Aj (both of
them have been previously reset). So, the code of action Ax can be properly conditioned.
Si Qi Ai FromSi Sj Qj Aj FromSj
Tix Tjx
Sx Qx Ax Vx
Action Ax:
Txy ...
If FromSi then...
... else
If FromSj then...
end_If
FromSi:=0
FromSj:=0
end_action
If, on the contrary, the behaviour of Ax in the two cases is quite different, the best solution is that
shown in Figure 11, where step Sx is split into two steps, namely Sxi and Sxj. The Axi and Axj
actions are the versions of Ax for the left and the right branches, respectively. Note that this
solution presents the absolutely general advantage of rendering explicit the dependence of the Sx
behaviour from the reaching path. In this way the distinction appears clear simply examining the
graph topology, while in the case of Figure 10 it can be seen only when going through the action
code.
Si Qi Ai Vi Sj Qj Aj Vj
Tix Tjx
Txy Txy
Sy Qy Ay Vy
Sx S Axy Vx
Sx ...
Txy Ss N Axy Vx
Txy
Sy ...
Sy ... Tx
Tyz
Sz R Axy Vx
Tyz
Sz ...
Then, we observe that all the cases of temporally constrained stored actions generate the same
structure of Figure 12b. The difference is represented by the way how the actions take into account
the step elapsed times:
SD (Stored and time Delayed). At any scan cycle, action Axy compares the given time delay with the measure of the elapsed time
from the activation of steps Sx and Ss. This can be done with a proper Axy timer or, more simply, by watching to the value of the
Sx.T variable and, if necessary, to the value of the sum Sx.T + Sy.T. If the delay threshold is reached, the core action body begins
to be executed.
DS (time Delayed and Stored). In this case, action Axy compares the given time delay only with the measure of the variable Sx.T. If
Sx stays active for a time interval longer then the delay threshold, the core action body begins to be executed.
SL (Stored and time Limited). As in the SD case, the Axy action compares the given time delay with the measure of the elapsed
time from the activation of steps Sx and Ss. The difference is that the core action body begins to be executed at the activation of the
divergence, and the delay threshold determines the end of its execution.
Combined cases. Besides the possibility offered by the standard qualifiers, it can be useful to define actions where the execution
interval ranges between two time instants defined by the programmer. This is equivalent to combine the delay option with the time
limit option. While this situation is difficult to realise in the framework given by the standard, it is immediately obtained with the
approach described above. It can be noted that the scheme structure remains always the same represented in Figure 12b.
It is worth observing that step Ss plays a supervision role on the evolution of the control from Sx to
Sy. Thus, each of these steps is no more required to be aware of the behaviour of the other, that is,
the programmer can develop either step autonomously. This makes the resulting code much more
reusable and the step Ss itself, as supervisor of the interactions between steps Sx and Sy, could
become in its turn a reusable module.
A generalised use of a supervision (synchronisation) step is that shown in Figure 13.
S1x S2x
S2y.X=True
S1y S2y
T2xy
S1z S2z
S1x S2x
T1xy
S1y S2y N Ax
Action Axy:
T1xy:= (S2y.X=true)
end_action
S1z S2z
The scheme of Figure 13a presents two parallel sequences where the activation of one step of the
former (S1y) is conditioned by the activation of one step of the latter (S2y). By introducing the
synchronisation step Ss (see Figure 13b) the two parallel sequences no longer need to know the
evolution of each other. In fact, action Axy performs the task of assigning the enabling value to the
Boolean variable T1xy at the right moment.
IEC standard gives no rule on the execution order of actions. In practice, every PLC environment
adopts its own execution strategy which is often unknown to the programmer.
N Ax1 C
Sx
Action Ax1:
Txy ...
C:= C+10
...
Sy end_action
N Ax2 C
Action Ax2:
...
C:= C*2
...
end_action
Thus, after one cycle the following different results could be obtained:
C = C1 = 15 if Ax1 is executed after Ax2
C = C2 = 10 if Ax2 is executed after Ax1
C = C1 * 2 = 30 if Ax2 uses the result of Ax1
C = C2 + 10 = 20 if Ax1 uses the result of Ax2,
and the difference increases in the subsequent cycles.
The case of Figure 14 is a typical symptom of the low communication level between the
programmers that developed Ax1 and Ax2, as variable C is used without paying attention to its
evolution in time. This kind of situation can be singled out by producing cross reference tables and
tracing the changes of system variables. However, a more general and effective rule states that
conflicts do not arise if the contemporarily active actions are mutually independent.
Let E be the set of the actions simultaneously executed during a given scan cycle. A sufficient
condition to avoid conflicts among them is:
Ai, Aj :
(i) Mi Mj =
(ii) Ui Mj =
(iii) Uj Mi =
where Ui and Mi are the sets of variables respectively used and modified by action Ai, and Uj and
Mj the sets of variables respectively used and modified by action Aj. The condition is only
sufficient since situations may be found where the condition is not true and nevertheless there are
no conflicts (for example, in the cases when the used variables do not alter the computation of the
modified variables).
The violation of condition (i) corresponds to the case where both actions try to modify the value of
the same variable. If this is not a programming bug, it is the evidence of a low action cohesion. In
fact, there is an aspect in the control, represented by the variable in conflict, whose management is
in charge of two different actions. In this case we suggest to assemble all the operations dealing
with strongly related variables in to a single action, and to code elsewhere the tasks that correspond
to other control aspects.
A more frequent case is that causing the violation of conditions (ii) and (iii). Not considering code
bugs, it can be interpreted as the programmer intention to modify
Sx
a given
N variable
Ax C many times,
relying on a precise actions execution order. Since, as we have said,Action
such Ax1:an order is in many cases
Txy ...
unpredictable, the result may be out of control. This case results in a strong
C:= C+10 action coupling, which
can be corrected by making explicit the desired execution sequence. ...C:= C*2
...
Figure 15 shows two possible solutions to the problem described in end_action
Figure 14. In Figure 15a it is
suggested to gather the actions in conflict (or, at least, the specific instructions in conflict) in a
unique action fixing the execution order. In Figure 15b, actions Ax1 andactions
(a) unifying Ax2 are associated to steps
Sx1 and Sx2 resulting from splitting Sx. The jump instruction following Sx2 causes the iterated
execution of the two actions in the established order, until the occurring of the exit condition Txy.
N Ax1 C
Sx1
true
N Ax C N Ax2 C
Sx Sx2
Action Ax1:
Txy ...
C:= C+10
... not Txy Txy
C:= C*2
... Sy
end_action Sx1
N Ax1 C
Obviously, the two solutions are
Sx1 not perfectly equivalent: the former guarantees that the two
Overall it is advised not to use S and R qualifiers due to less readable and less understandable code.
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 42/59
®
PLCopen
for efficiency in automation
However, if an action block uses an action with the ‘S’ qualifier then another action block shall use
the same action with the ‘R’ action qualifier (antivalent), or vice versa.
The execution model of the automaton requires that it has always assigned a state value that changes
time by time. At the beginning, the automaton is placed in the initial state, and there it remains until
one of the events that cause a state transition occurs. The condition under which the transition takes
place is the existence, in the set T, of the rule <si, eij, sj>, where si is the current state (at the
beginning, the initial state) and eij is the occurred event. In such a case the automaton moves to the
new state sj where it remains up to a new transition triggering event. If we think at the automaton as
an algorithm, we see that it receives, as input, a sequence of events and generates, as output, the
indication of the reached final state or that of an abnormal termination.
This execution model implies that a further condition, still not stated, should be satisfied: the
transitions leaving each of the states are enabled by disjoint events, so as to guarantee that the new
state to be reached is uniquely identified. In other words, the two transition rules:
<si, ex, sj> and <si, ex, sk>
cannot belong to the set T simultaneously, otherwise the same event ex could, leaving si, bring to
either sj or sk. An automaton satisfying such a condition is called deterministic. A non-deterministic
automaton is not necessarily wrong, but it requires a more complex and less efficient execution
model.
state
A
b a
initial state
d
D c B
final state
e f
C
transition
State diagrams represent finite state automata as oriented graphs, obtained starting from the
graphical primitives shown in Figure 16a: states are circles, the initial state is a circle with the
starting arrow, final states are double line circles, transitions are oriented (arrowed) branches going
from the current state to the next one and carrying the indication of the enabling event. Figure 16b
shows a simple diagram with four states, two of which are final, and six transition rules. Its formal
definition is the following:
S = {A, B, C, D}
E = {a, b, c, d, e, f, g}
I = A
F = {B, D}
T = {<A, a, B>, <A, b, D>, <D, c, B>, <B, d, A>, <D,
e, C>, <C, f, B>}
The first and most important aspect to consider in defining an automaton is the choice of the states
that better represent the behaviour of the modelled system. In most cases it is not easy to select the
significant states among all the possible states that the system can assume. For instance, suppose
that the system behaviour is described by N variables, and that each of them can assume on the
average M different values. The number of the possible combinations of these values is then MN.
Even if some of the combinations cannot be reached, typically because of incompatibilities among
variable values, the potential number of states still remains very high.
It is for this reason that we are interested to those states we consider significant. Let us imagine a
shuttle moving from point to point in a warehouse. During its motion, the variable expressing its
position assumes continuously changing values. However, if we observe the motion from the PLC
viewpoint, the principal task is verifying that the path actually followed by the shuttle coincides
with the desired one. This control operation has to be executed exactly in the same way at every
cycle, in correspondence of each of the points reached by the shuttle. This means that we can
resume the whole motion states into a unique system state (say, the shuttle moving state) whose
associated action is the path control.
An effective way for identifying the significant states of a system is to start from detecting the
events causing communications between the entities involved in the system control. Two of these
entities are always the controlled system and its control software. Coming back to the shuttle
example, we imagine that another entity could be the operator, who assigns missions to the shuttle.
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 45/59
®
PLCopen
for efficiency in automation
In order to represent the events it is comfortable to use an event trace diagram, based on these
simple rules:
Figure 17 shows the event trace diagram describing some of the possible events affecting the shuttle
behaviour. At first, the operator sends to the system control a message specifying a new mission for
the shuttle. The control, after executing possible correctness checks, gives the shuttle the command
to move and, at the same time, sends an acknowledgement message to the operator. The shuttle
executes the command and, once reached the new position, sends a confirmation message to the
control. As a consequence, the control informs the operator that now it is possible to work on the
material transported by the shuttle. As this activity terminates, the operator informs the control that
the shuttle is free again, and the control commands the shuttle to go back to its stand-by position.
Once the stand-by position is reached, the shuttle informs the control, and the control sends a
message to the operator to confirm that the shuttle is ready carry out a new mission.
operator control shuttle
mission request
destination reached
shuttle in position
work finished
return command
shuttle ready
Starting from this representation, some of the significant states of the shuttle behaviour are easily
identified, namely:
ready, if it is available to perform a new mission (at the stand-by site)
moving, while it is changing its position in the warehouse
arrived, if it has reached the destination (other than the stand-by site)
working, while the transported material is handled
free, when it can go back to the stand-by site.
Other states, not considered in the example, could be those corresponding to the different error
conditions (wrong positions along the trajectory, troubles on the motor, and so on).
The relationship to establish between system behaviour and control software functions is now likely
easier to understand. The state diagram representing the system dynamics must be translated into a
control algorithm, say an SFC scheme, with as many steps as the states of the diagram. Each step
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 46/59
®
PLCopen
for efficiency in automation
will include the actions (signal analysis, comparisons, computations, command issuing) to perform
whenever the system is in the corresponding state.
Although state diagrams are very useful in the design of PLC software, as the corresponding SFC
schemes present identical structures, it is important to separate the two concepts. On the one hand,
we have a system changing its state as the consequence of internal events or commands sent by the
control. On the other hand, we have a software program which keeps under continuous control the
state of the system so as to react in the shortest possible time. Even though the state change is
forced by the control, this cannot move to the next step until a confirmation of the occurred
transition comes from the system.
The rules that the programmer has to follow to implement correct control algorithms arise from the
comparison between state diagrams and SFC schemes:
steps and transitions are interleaved
each transition has a unique origin step
each transition has a unique destination step
a step can be both origin and destination of a transition
for each transition an enabling event is defined
a single step can have more than one transition reaching it
a single step can have more than one transition leaving it
transitions leaving different steps may adopt the same enabling event
transitions reaching a given step may adopt the same enabling event
transitions leaving a given step must adopt disjoint enabling events.
The syntactical differences between state diagrams and SFC schemes can lead the non-expert
programmer to some mistakes.
Note: /MoveLeft and /MoveRight is the transition with releasing the button.
Couple to the states MotorTurningLeft and MotorTurningRight are action blocks that issue the
commands to the underlying system. Such an action block can be calling an MC_MoveVelocity
with the relevant direction and parameters. As an extension one can add the state Error, on the right
side of MotorTurningRight and StandStill, which will be accessed from all the states in case of an
error. A reset procedure for the error can be switching the power off and on.
The Error handling is not included in this example. How this can be handled is shown in 6.3.3 Error
handling via the loops Stop and Abort.
A Wait State (Orange in picture) is used to identify that a machine has achieved a defined set of
conditions.
An Acting State (Green in picture) is one which represents some processing activity.
Dual state (Blue) is defined as a machine actively executing in the chosen mode.
The states in orange and blue are stable states, meaning that they can be valid for a longer period of
time.
PLCopen Software Construction Guidelines: July 3, 2018 © PLCopen (2018)
Structuring with SFC: do’s and don’ts Version 1.0 Official Release page 52/59
®
PLCopen
for efficiency in automation
The states in green are states that are only valid for a certain period of time and transfer to the next
state without intervention from an operator. The transition is automatically done if the state is
complete (SC = State Complete).
Shown above is the full state diagram with the state Execute (in blue) the producing state. The loop
underneath, via Suspended, is a waiting loop for material to be worked upon. The loop above, via
Held, is the loop where the operator holds the system out of the producing state.
After all products are made, the producing state Execute is left via Complete, and ready for a new
production order.
At power on, the state Stopped is valid. After a Reset it moves to the state Idle via Resetting.
Issuing ‘Start’ gets the unit to ‘Execute’ via ‘Starting’.
The PackML state diagram leaves its normal loop via either Abort or Stop. The Abort is coupled to
the error handling from every state. The Stop is for the operator interface.
SA1
ErrorAll Appl2Cond
A1
SA2
ErrorA1
ErrorAll Appl3Cond
ErrorApplication SA2
SA3
ErrorOff
ErrorAll Appl4Cond
SError
NoErrors
The main states in PackML for the normal production process (executing) looks as follows:
STOPPED
RESET AND
NOT ABORT
RESETTING
PRESET AND
NOT ABORT
IDLE
START AND
NOT ABORT
STARTING
NOT ABORT
EXECUTE
COMPLETING
AND NOT ABORT
COMPLETE
The next figure, Figure 21, shows a basic implementation of the full PackML state diagram,
including the ‘Abort’ and ‘Stop’ loops, and on the lower right side the ‘Hold’ and ‘Suspend’ loops,
which loop back to the ‘Execute’ state. For the error handling option 1 - centralized is in this case
shown via the ‘Abort’ loop. In the top middle, the abort sequence is specified, with the ‘Abort’
entry point on top. All other abort loops refer to this starting point. Also the ‘Stop’ loop is identified
there.
STOPPED
RESET AND
ABORT STOP
NOT ABORT
STATE STATE
ABORT
COMPLETED COMPLETED
STATE COMPLETED
ABORT STOP
AND NOT ABORT
ABORTING
ABORTING ABORT STOPPING STOPPED
IDLE ABORTED
CLEAR
START AND
ABORT STOP
NOT ABORT
ABORTING STOPPING
STARTING CLEARING
STATE ABORT
COMPLETED
STATE COMPLETED
ABORT STOP
AND NOT ABORT
ABORTING
ABORTING STOPPING STOPPED
EXECUTE
ABORTING STOPPING
COMPLETE HOLDING SUSPENDING
UN-SUSPEND
UN-HOLD AND STOP AND NOT ABORT STOP
ABORT
NOT ABORT ABORT
Concerning the transition conditions one has to note that the order also could be (from left to right)
first Abort, then Stopping and then the transition condition in the normal production process loop.
For readability this is changed in this example. Note that the conditions are mutually exclusive.
loop is not made available, limiting the feed-in of products to on one product at a time only for
checking purposes. In the Setup mode there is no production, so the ‘Execute’, ‘Suspending’, and
‘Holding’ loops are not available. This is coupled to the function blocks in the SFC program above.
For instance, the Starting and Execute states are only accessible if the SetUpMode is not set, and the
‘Holding’ loop if SemiAutoModeSelected is not SET so the system is in Automatic mode.
In parallel, the safety program takes care that in the setup mode the drives are in a safe state. For
this the ‘Safely Limited Speed’ functionality can be used, combined with the ‘EnableSwitch’
functionality, with which the operator can move the machine at a reduced speed. The functional
application program defines the safely limited speed, while the safety application checks that the
limit set is not exceeded.
In addition, one can couple the emergency switch to different stop categories per motor.
A small overview of the applicable safety function blocks is shown in the next drawing. For details
on the PLCopen Safety specification, check the website www.PLCopen.orgff under TC5 Safety.
SF_ModeSelector SF_SafelyLimitedSpeed
SF_ModeSelector SF_SafelyLimitedSpeed
Activate Ready Activate Ready
S_Mode0 S_Mode0Sel S_OpMode S_SafetyActive
S_Mode1 S_Mode1Sel S_Enabled Error
S_Mode2 S_Mode2Sel AxisID DiagCode
S_Mode3 S_Mode3Sel MonitoringTime
S_Mode4 S_Mode4Sel Reset
S_Mode5 S_Mode5Sel
S_Mode6 S_Mode6Sel SF_SafeStop1
S_Mode7 S_Mode7Sel
SF_SafeStop1
S_Unlock S_AnyModeSel
Activate Ready
S_SetMode Error
S_StopIn S_Stopped
AutoSetMode DiagCode
AxisID Error
ModeMonitorTime
MonitoringTime DiagCode
Reset
Reset
SF_EmergencyStop SF_SafeStop2
SF_EmergencyStop SF_SafeStop2
Activate Ready Activate Ready
S_EStopIn S_EStopOut S_StopIn S_Standstill
S_StartReset Error AxisID Error
S_AutoReset DiagCode MonitoringTime DiagCode
Reset Reset