ROBT 308
Industrial Automation
Lecture 22 Sequential Function Chart (SFC)
programming in CODESYS
7 April 2015, Thursday
Industrial Automation (ROBT308) - Spring 2016
Topics
Todays Topics
Sequential Function Chart (SFC) programming
Cursor position in SFC
Working in the SFC Editor
SFC Element Properties
SFC Elements
Qualifier for actions in SFC
Implicit variables - SFC flags
Sequence of processing in SFC
SFC Editor in online mode
SFC examples: Traffic light control (one lane and two lane)
SFC exercises: Garage door control and ingredient mixing process
Industrial Automation (ROBT308) - Spring 2016
Sequential Function Chart (SFC)
programming
The Sequential Function Chart (SFC) is a graphically oriented language which allows to
describe the chronological order of particular actions within a program. These actions are
available as separate programming objects, written in any available programming language.
In a SFC the actions are assigned to "step" elements and the sequence of processing is
controlled by transition elements.
SFC is a high-level language to
describe control sequences in
graphical form.
SFC is a tool for the top-down
analysis and representation of a
control sequence.
Industrial Automation (ROBT308) - Spring 2016
Cursor position in SFC
A possible cursor position in a SFC diagram in the SFC Editor is indicated per default by a
grey shadow when moving with the cursor over the elements.
There are two categories of cursor positions: Texts and element bodies. See the possible
positions indicated by a grey shaded area as shown in the following pictures:
1. Texts
When you click on a text cursor
position, the string will get editable:
Industrial Automation (ROBT308) - Spring 2016
Cursor position in SFC
2. Element bodies
When you click on a shadowed area, the
element will get selected. It gets a dotted frame
and is displayed red-shaded :
Industrial Automation (ROBT308) - Spring 2016
Working in the SFC Editor
By default a new SFC POU contains an init step and a subsequent transition.
Navigating: Jumping to the next and previous element in the chart is possible by using
the arrow keys.
Insert elements
The particular SFC elements can be inserted via the respective commands which by
default are available in the "SFC" menu. A double-click on an already inserted step,
transition or action element, which does not yet reference a project object, will open a
dialog for assigning one.
Select elements
An element and text field might be selected by a mouse-click on a possible cursor
position. The selection might also always be given to an adjacent element by using the
arrow keys. The element will change color to red.
Multiple selection is possible by the following:
1. Keep the [ Shift ] -key pressed and subsequently click on the particular elements to be
selected. 2. Press the left mouse-key and draw a rectangle (dotted line) around the
elements to be selected. 3. Use command Select All , by default from the Edit menu.
Industrial Automation (ROBT308) - Spring 2016
Working in the SFC Editor
Edit texts
By a mouse-click on a text-cursor-position at once the edit field opens, where you can edit
the text. If a text area has been selected via the arrow keys, the edit field must be opened
explicitly by using the [ Space ] -bar.
Edit associated actions
A double click on an step (entry, active or exit) or transition action association opens the
associated action in the corresponding editor. For example perform a double click on the
transition element or on the triangle indicating an exit action in a step element.
Cut, Copy, Paste elements
Select the element(s) and use command Cut, Copy resp. Paste (by default from the
Edit menu) or the corresponding keys.
Notice the following behavior which is different to CODESYS V2.3 :
When you paste one or several cut or copied element(s), the content of the clipboard will be
inserted before the currently selected position. If nothing is currently selected, the
element(s) will be appended at the end of the currently loaded chart.
If you paste a branch while the currently selected element is also a branch, the pasted
branch elements will be inserted left to the existing ones.
Industrial Automation (ROBT308) - Spring 2016
Working in the SFC Editor
If you paste an action (list) at a currently selected step, the actions will be added at the
beginning of the action list of the step resp. an action list for the step will be created.
Incompatible elements when cutting/copying: If an associated action (list) and additionally
an element, which is not the step to which the action (list) belongs, are selected, a message
box will appear: "The current selection contains incompatible elements. No data will be
filed to the clipboard." The selection will not be stored and you cannot paste or copy it
somewhere else!
Incompatible elements when pasting: If you try to paste an action (list) while the currently
selected element is not a step or another association, an error box will appear: "The current
clipboard content can not be pasted at the current selection." If you try to paste an element
like a step, branch or transition when currently an associated action (list) is selected the
same message box will appear.
Delete elements:
Select the element(s) and use command Delete or the [ Del ] -key. Notice the following:
Deleting a step also deletes the associated action list.
Deleting the init step automatically sets the following step to be the initial one, that is
option 'Initial step' will be activated in the properties of this step.
Deleting the horizontal line preceding a branched area will delete all branches.
Deleting all particular elements of a branch will delete the branch.
Industrial Automation (ROBT308) - Spring 2016
SFC Element Properties
The properties of a SFC element can be viewed and edited in the "Element properties"
window. This window can be opened via command 'Element Properties', which by default is
part of the 'View' menu.
It depends on the currently selected element which properties are displayed. The properties
are grouped and the particular group sections can be opened or closed by using the plusand minus signs.
Notice that in the 'View' tab of the SFC-Editor options you can configure whether the
particular types of properties should be displayed next to an element in the SFC chart.
See in the following all possible properties:
Industrial Automation (ROBT308) - Spring 2016
SFC Element Properties
See in the following all possible properties:
Industrial Automation (ROBT308) - Spring 2016
10
SFC Element Properties
Industrial Automation (ROBT308) - Spring 2016
11
SFC Elements
The graphic elements usable for programming in the SFC editor window can be inserted by
using the commands by default available in the SFC menu.
The elements Step, Transition, Action, Branch (Alternative), Branch (Parallel), Jump, Macro
are available and described in the following:
Step-Transition
Steps and transitions are basically inserted in combination, via command Insert steptransition and Insert step-transition after.
A step is represented by a box primarily containing an automatically generated step name. It is
connected to the preceding and subsequent transition by a line. The box frame of the first step
within a SFC, the init step, is double-lined.
The transition is represented by a small rectangle. After inserting primarily it has a default name:
Trans<n>, whereby n is a running number.
The step and transition names can
be edited inline.
Industrial Automation (ROBT308) - Spring 2016
12
SFC Elements
Step names must be unique in the scope of the parent POU. Notice this especially when using
actions programmed in SFC
Notice that each step - by command Init step or by activating the respective step property can be transformed to an init step, that is to that step, which will be executed first when the
IL-POU is called.
Each step is defined by the step properties.
After inserting a step, the action(s) to be performed when the step is active (processed) are
to be associated.
Transitions:
A transition must provide the condition on which the subsequent step shall get active as
soon as the condition value is TRUE.
So a transition condition must have the value TRUE or FALSE. It can be defined in the
following two ways:
Industrial Automation (ROBT308) - Spring 2016
13
Transitions:
SFC Elements
1. Directly ("inline condition"): Replace the default transition name by either a boolean
variable, a boolean address, a boolean constant or an instruction having a boolean result
(e.g. (i<100) AND b). It is not possible here to specify programs, function blocks or
assignments!
2. Using a separate transition or property object ("multi-use condition"): Replace the default
transition name by the name of a transition or property object (
,
) available in the
project (this allows multiple use of transitions. The object like an inline transition can
contain a boolean variable, address, constant or instruction, but also multiple statements
with arbitrary code.
It is the user's responsibility to assign the desired expression to a transition variable, if a
transition produces multiple statements!
Transitions which consist of a transition or a property object are indicated by a small
triangle in the upper right corner of the rectangle.
Industrial Automation (ROBT308) - Spring 2016
14
Transitions:
SFC Elements
Transition object in POUs tree (multiple
use transition)
1. Transition conditions entered directly
2. Transition condition_xy programmed in ST
Industrial Automation (ROBT308) - Spring 2016
15
Transitions:
SFC Elements
A transition condition will be entered
according to the following:
Syntax:
<transition name>:=<transition
condition>;
(for example" trans1:= a=100")
or just
<transition condition>;
(for example a=100)
"Multiple use conditions" (transitions or
properties) are indicated by a triangle
Industrial Automation (ROBT308) - Spring 2016
16
Actions:
SFC Elements
An action can contain a series of instructions written in one of the valid programming
languages. It is assigned to a step and in online mode it will be processed according to the
defined sequence of processing .
Each action to be used in SFC steps must be available as a valid POU within the SFC POU
Example of an action written in ST
Industrial Automation (ROBT308) - Spring 2016
17
Actions:
SFC Elements
There are the following types of actions:
1. IEC conforming step action ("IEC action"):
This is an action according to standard IEC61131-3 which will be processed according to its
qualifier when the step has got active and a second time when it has got deactivated. In case
of assigning multiple actions to a step, the action list will be executed from up to down.
Different qualifiers can be used for IEC step actions in contrast to a "normal" step action.
A further difference to the "normal" step actions is that each IEC step action is provided with
a control flag, which allows to make sure that - even if the action is called also by another
step - the action will get executed always only once at a time. This is not guaranteed with the
normal step actions.
An IEC step action is represented by a bipartite box, connected to the right of a step via a
connection line. In the left part it shows the action qualifier, in the right part the action
name. Both can be edited inline.
IEC step actions get associated to a step via the Insert action association command. One or
multiple actions can be associated to a step. The position of the new action depends on the
current cursor position and the command. The actions must be available in the project and
be inserted with a unique action name (for example plc_prg.a1).
Industrial Automation (ROBT308) - Spring 2016
18
Actions:
SFC Elements
2. IEC-extending step actions:
These are actions extending the IEC standard: The actions must be available as objects below
the SFC object. The action names must be unique.
- "step entry" action (step activated):
This type of step action will be processed as soon as the step has become active and before
the "step active" action.
The action is associated to a step via an entry in the "step entry" field of the step properties.
It is represented by an "E" in the lower left corner of the respective step box.
- "step active" action (step action):
This type of step action will be processed when the step has become active and after a
possible "step entry" action of this step has been processed. However in contrast to an IEC
step action it is not executed once more when it gets deactivated and it cannot get assigned
qualifiers.
The action is associated to a step via an entry in the "step active" field of the step properties.
It is represented by a small triangle in the upper right corner of the respective step box.
Industrial Automation (ROBT308) - Spring 2016
19
Actions:
SFC Elements
2. IEC-extending step actions:
- "step exit" action (step deactivated):
An exit action will be executed once when the step has got deactivated. Notice however
that this execution will not be done in the same, but at the beginning of the subsequent
cycle.
The action is associated to a step via an entry in the "step exit" field of the step properties.
It is represented by an "X" in the lower right corner of the respective step box.
The "Step active", "Step Entry"
and "Step Exit" actions are
defined in the step properties.
Industrial Automation (ROBT308) - Spring 2016
20
Actions:
SFC Elements
Example: Difference between IEC matching / extending step actions
The main difference between step actions and IEC actions with qualifier N is the following:
The IEC action is at least executed twice: First time when the step is active, second time
when the step gets deactivated. See the following example:
Action Action_AS1 is associated to step
AS1 as a step action (left), and as an IEC
action with qualifier N (right). Due to the
fact that in both cases two transitions are
used, it will take two PLC cycles each
before the init step is reached again.
Assuming that a variable iCounter gets incremented in Action_AS1, after a re-activation of
step Init, iCounter in the left example will have value 1. In the right one however it will have
value 2, because the IEC action - due to the deactivation of AS1 - has been executed twice.
Industrial Automation (ROBT308) - Spring 2016
21
SFC Elements
Branches:
A sequential function chart can diverge, that is the processing line can be branched into two
or several further lines ("branches"). Parallel branches will be processed parallel (both at a
time), in case of alternative branches only one will be processed depending on the
preceding transition condition. Each branching within a chart is preceded by a horizontal
double (parallel) or simple (alternative) line and also terminated by such a line or by a jump.
A parallel branch must begin and end with a step.
Parallel branches can contain alternative branches or
other parallel branches.
The horizontal lines before and after the branched area
are double-lines.
Example Parallel Branch
Industrial Automation (ROBT308) - Spring 2016
22
Branches:
SFC Elements
An alternative branch must begin and end with a
transition. Alternative branches can contain parallel
branches and other alternative branches.
The horizontal lines before and after the branched area
are simple lines.
If the step which precedes the alternative beginning
line is active, then the first transition of each
alternative branch will be evaluated from left to right.
The first transition from the left whose transition
condition has value TRUE, will be opened and the
following steps will be activated.
Example Alternative Branch
Industrial Automation (ROBT308) - Spring 2016
23
Jump:
SFC Elements
A jump is represented by a vertical connection
line plus a horizontal arrow and the name of
the jump target.
A jump defines the next step to be processed
as soon as the preceding transition is TRUE.
Jumps might be needed because the
processing lines must not cross or lead
upward.
Besides the default jump at the end of the
chart a jump may only be used at the end of a
branch. It gets inserted via command Insert
jump when the last transition of the branch is
selected.
The target of the jump is specified by the
associated text string which can be edited
inline. It can be a step name or the label of a
parallel branch.
Example Jump
Industrial Automation (ROBT308) - Spring 2016
24
Macros:
SFC Elements
A macro is represented by a bold-framed box
containing the macro name.
It includes a part of the SFC chart, which thus
is not directly visible in the main editor view.
The process flow is not influenced by using
macros, it is just a way to "hide" some parts of
the program, for example in order to simplify
the display.
A macro box is inserted by command 'Insert
macro (after)'. The macro name can be edited
inline.
To open the macro editor, perform a doubleclick on the macro box or use command Zoom
into macro. You can edit here just as in the
main editor view and enter the desired section
of the SFC chart. To get out use Zoom out of
macro.
Macro editor view for Macro1
Industrial Automation (ROBT308) - Spring 2016
25
Qualifier for actions in SFC
In order to configure in which way the actions should be associated to the IEC steps, some
qualifiers are available, which are to be inserted in the qualifier field of an action element.
These qualifiers are handled by the SFCActionControl function block of the IecSfc.library,
which automatically is included in a project by the SFC plug-ins.
The available
qualifiers:
The qualifiers
L, D, SD, DS
and SL need a
time value in
the TIME
constant
format.
Industrial Automation (ROBT308) - Spring 2016
26
Implicit variables - SFC flags
Each SFC step and IEC action provides implicitly generated variables for watching the status
of steps and IEC actions during runtime. Also variables can be defined for watching and
controlling the execution of a SFC (timeouts, reset, tip mode). These variables also might be
generated implicitly by the SFC object.
Basically for each step and each IEC action an implicit variable is generated. A structure
instance, named like the element, for example "step1" for a step with step name "step1".
Notice the possibility, to define in the element properties, whether for this flag a symbol
definition should be exported to the symbol configuration and how this symbol should be
accessible in the PLC.
The data types for those implicit variables are defined in library IecSFC.library. This library
will automatically be included in the project as soon as an SFC object is added.
Step and Action status and Step time
Basically for each step and each IEC action an implicit structure variable of type
SFCStepType and SFCActionType is created. The structure components (flags) describe the
status of a step and action or the currently processed time of an active step.
The syntax for the implicitly done variable declaration is:
<stepname>: SFCStepType; or _<actionname>:SFCActionType;
Industrial Automation (ROBT308) - Spring 2016
27
Implicit variables - SFC flags
The following boolean flags for step or IEC-action states are available:
Step
<stepname>.x: shows the current activation status.
If <stepname>.x = TRUE, the step will be executed in the current cycle.
<stepname>._x: shows the activation status for the next cycle.
If <stepname>._x = TRUE and <stepname>.x = FALSE, the step will be executed in the
following cycle, that is <stepname>._x gets copied to <stepname>.x at the beginning of a
cycle.
IEC action
_<actionname>.x is TRUE, when the action is executed.
_<actionname>.x is TRUE, when the action is active.
Symbol generation
In the SFC Element Properties of a step or an action you can define, whether for the step
or action name flag a symbol definition should be added to a possibly created and
downloaded symbol application. For this purpose make an entry for the desired access right
in column Symbol of the element properties view.
Industrial Automation (ROBT308) - Spring 2016
28
Implicit variables - SFC flags
Time via TIME variables
The flag "t" gives the current time span which has passed since the step had got active; this is
only for steps, no matter whether there is a minimum time configured in the step attributes
or not.
For steps: <stepname>.t
For actions: The implicit time variables are not used.
Control of SFC execution (timeouts, reset, tip mode)
Some implicitly available variables, also named SFC flags, can be used to control the operation
of an SFC, for example for indicating time overflows or enabling tip mode for switching
transitions. In order to be able to access these flags and to get them work, they must be
declared and activated. This can be done in the Project Settings, SFC dialog for a single object
of project wide in SFC.
Manual declaration, is only necessary to enable write access from another POU. In this case
however regard the following: If you declare the flag globally, you must deactivate the
Declare option in the SFC Settings dialog. Else, an implicitly declared local flag is used
instead of the global one! Keep in mind, that the settings for a SFC POU done in
View Properties... SFC Settings Flags initially are determined by the project wide
definitions set in Project Project Settings SFC Flags .
Industrial Automation (ROBT308) - Spring 2016
29
Implicit variables - SFC flags
The following implicit variables (flags) can be used. For this purpose they must be declared and
activated in the SFC Settings:
Industrial Automation (ROBT308) - Spring 2016
30
Implicit variables - SFC flags
The following implicit variables (flags) can be used. For this purpose they must be declared and
activated in the SFC Settings:
Industrial Automation (ROBT308) - Spring 2016
31
Implicit variables - SFC flags
Example of some SFC error flags in online mode of the editor
A timeout has been detected in step s1 in SFC object POU by flag SFCError
Industrial Automation (ROBT308) - Spring 2016
32
Accessing flags:
Implicit variables - SFC flags
For enabling access on the flags for the control of SFC execution (timeouts, reset, tip mode),
the flag variables must be declared and activated as described above.
Syntax for accessing:
... from an action or transition within the SFC POU: <stepname>.<flag> and
_<actionname>.<flag>
... from another POU: <SFC POU>.<stepname>.<flag> and <SFC POU>_<actionname>.<flag>
Industrial Automation (ROBT308) - Spring 2016
33
Implicit variables - SFC flags
Regard that in case of write access from another POU the implicit variable additionally must
be declared explicitly as a VAR_INPUT variable of the SFC POU or globally for example in a
GVL.
Industrial Automation (ROBT308) - Spring 2016
34
Sequence of processing in SFC
In online mode the particular action types will be processed according a defined sequence as
below:
Following terms are used:
Active step: A step, whose step action is being executed, is called "active". In Online mode
active steps are filled with blue color.
Initial step: In the first cycle after a SFC POU has been called, the initial step automatically gets
active and the associated step action is executed.
IEC actions are executed at least twice: The first time when they have got active, the second
time - in the following cycle - when they have been deactivated.
Alternative Branches: If the step preceding the horizontal start line of alternative branches is
active, then the first transition of each particular branch will be evaluated from left to right.
The first transition from the left whose transition condition has value TRUE will be searched
and the respective branch will be executed, that is the subsequent step within this branch will
get active.
Parallel Branches: If the double-line at the beginning line of parallel branches is active and the
preceding transition condition has the value TRUE, then in all parallel branches each the first
step will get active. The branches now will be processed parallel to one another. The step
subsequent to the double-line at the end of the branching will become active when all
previous steps are active and the transition condition after the double-line has the value TRUE.
Industrial Automation (ROBT308) - Spring 2016
35
Sequence of processing in SFC
Processing order of elements in a sequence:
Industrial Automation (ROBT308) - Spring 2016
36
Sequence of processing in SFC
Processing order of elements in a sequence:
Industrial Automation (ROBT308) - Spring 2016
37
SFC Editor in online mode
In online mode the SFC-Editor provides views for monitoring and for writing and forcing the
variables and expressions on the controller. Debugging functionality (breakpoints, stepping
etc.) is not yet available.
Notice that the editor window of an SFC object also includes the Declaration Editor in the
upper part.
In case of having declared implicit variables (SFC flags) via the SFC Settings dialog, those will be
added here, but will not be viewed in the offline mode of the declaration editor.
Also please notice the sequence of processing of the elements of a sequential function chart.
Consider the possible use of flags for watching and controlling the processing of an SFC.
Monitoring
Active steps are displayed filled blue-colored. The display of step attributes depends on the
currently set SFC Editor options.
Industrial Automation (ROBT308) - Spring 2016
38
SFC example: Traffic light control
Creating States and transitions:
Create a new project,
selecting SFC as the
programming language.
Change the transition for
the Init state to True (1).
1
2
Click on the Init state and
then insert step-transition
state by clicking on the
corresponding icon (2).
Industrial Automation (ROBT308) - Spring 2016
39
SFC example: Traffic light control
Creating States and transitions:
Insert two more similar states and
change the name of the states to Green,
Yellow, and Red (1).
Change the label after the last state to
Green, in order to jump to Green after
executing Red (2).
Change the transition between the
states to Green.t> t#5s, Yellow.t>
t#2s, and Red.t> t#7s (3).
State.t gives the amount of time in
which that state is being executed.
1
3
1
Run the program and check the functionality.
Industrial Automation (ROBT308) - Spring 2016
3
40
SFC example: Traffic light control
Adding actions:
Click on the Green
state, then select
Add entry action
icon from the
toolbar (1).
Industrial Automation (ROBT308) - Spring 2016
41
SFC example: Traffic light control
Adding actions:
You can specify a
name for the action
(1), and choose an
implementation
language for the
action from the list
(2). Select LD for
this case. Then add
the action.
Industrial Automation (ROBT308) - Spring 2016
42
SFC example: Traffic light control
Adding actions:
In the editor window for implementing the action, insert a coil and
assign GreenLight variable for that (1). Note that the created action
appears under the main program in the project tree view (2). Select
LD for this case. Then add the action.
3
Click on the Green state, then select Add exit
action icon from the toolbar (3).
Industrial Automation (ROBT308) - Spring 2016
3
43
SFC example: Traffic light control
Adding actions:
This time, select ST as the
implementation language of the
action and add the action (1).
The aim of this action is to turn
the green light off (2).
Industrial Automation (ROBT308) - Spring 2016
44
SFC example: Traffic light control
Adding actions:
Add similar entry and exit actions
for the Yellow and Red states (1).
Add a visualization and run the
program to check the
functionality (2).
1
2
Industrial Automation (ROBT308) - Spring 2016
45
SFC example: Traffic light control using
implicit variables
Now, we can redo the same example using
the implicit variables. In this case there is no
need to declare GreenLight, YellowLight and
RedLight variables. We also do not need to
add entry and exit actions for the steps. The
only thing that we need to do is to assign the
implicit variables Green.x, Yellow.x and
Red.x to the pilot lamps (1).
Save your project as a new one to
access it later. Delete all the
actions and change the variable of
the pilot lamps as described. Run
the program and check the
functionality.
Industrial Automation (ROBT308) - Spring 2016
46
SFC example: Two lane Traffic light control
The aim of this example is to
control the traffic lights in the
intersection. As the first step, add
an action for the Init step (1),
inside which turn all the lights off
(2).
1
1
Insert another step after Init (3). You will jump to this
step afterwards
Industrial Automation (ROBT308) - Spring 2016
47
SFC example: Two lane Traffic light control
Now we want to add a parallel branch to
our chart. To do so, select Branch from
the toolbox (1), and drag it onto the
chart. The possible nodes on the chart
will turn Gray. Click on the node just
before the Green01 step (2).
Then that node will turn red (3), waiting
for the next node to be selected. Click on
the node just after the Red01 step (4).
Afterwards, the branch will appear (5).
Industrial Automation (ROBT308) - Spring 2016
48
SFC example: Two lane Traffic light control
Modify the program as shown here (1).
In the visualization, add two sets of
traffic lights for each direction and assign
the variables for the lights in a way to
have the traffic lights working properly
(2). You may need to add some actions
to the steps.
Industrial Automation (ROBT308) - Spring 2016
49
SFC exercise: Garage door control
Industrial Automation (ROBT308) - Spring 2016
50
SFC exercise: Garage door control
Garage door state logic
Industrial Automation (ROBT308) - Spring 2016
51
SFC exercise: Ingredient mixing process
Design a PLC program to implement the process of mixing two
ingredients, A and B. The mixing cycle can be summarized as
follows:
Ingredient A is sent to the tank first by energizing solenoid No. 1.
The flow meter gives one pulse for every gallon of flow. Solenoid
valve No. 1 will be open (energized) until 200 gallons have poured
in.
After ingredient A is in the tank, 300 gallons of ingredient B
should be added. The process of adding ingredient B follows the
same procedure as that of adding ingredient A.
After ingredient B is in the tank, the mixer motor starts and runs
for 5 minutes
After the mixing is complete, solenoid No. 3 should open and let
the mixed batch go into a finished tank.
When the tank is empty (as indicated by the NC empty liquidlevel switch), solenoid No. 3 should close and stop the cycle.
Industrial Automation (ROBT308) - Spring 2016
Process flow diagram
52