Plcopen Oop Guidelines Version 0.99 RFC
Plcopen Oop Guidelines Version 0.99 RFC
Training
DISCLAIMER OF WARANTIES
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 DOCUMENT. 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.
The following paper is a document created within the PLCopen Promotional Committee 2 – Training.
It summarizes the results of the PLCopen Promotional Committee meetings, containing contributions of its
members as well as external sources:
Name Company
Rene Simon Hochschule Harz
Wolfgang Doll Codesys
Yves de la Broise Interval Zero
Anders Lekve Brandseth Framo
Daniel Wall Eaton
Filippo Venturi SACMI
Georg Rempfler Wyon
John Dixon ABB
Dominik Franz ABB
Ralf Dreesen Beckhoff
Saele Beltrani SACMI
Yo Takahashi Mitsubishi Electric
Juliane Fischer TUM
Contents
1 INTRODUCTION TO THIS DOCUMENT ............................................................................. 5
1.1. GOALS OF THIS WORKING GROUP ......................................................................................... 5
2 INTRODUCTION IN THE OOP FEATURES OF THE 3RD EDITION OF THE IEC 61131-3
STANDARD. ...................................................................................................................................... 6
2.1. THE 3RD EDITION OF IEC 61131-3......................................................................................... 6
2.2. DEFINITIONS IN THE IEC 61131-3 STANDARD ...................................................................... 6
2.3. DIFFERENCES IN OOP LANGUAGES ...................................................................................... 8
3 GENERIC APPROACH ............................................................................................................ 9
3.1. GENERAL ............................................................................................................................. 9
3.2. MODULES AND COMMANDS ................................................................................................. 9
4 THE BASIC EXAMPLE: BOILER DEMO ........................................................................... 12
4.1. OVERVIEW OF THE DIFFERENT PHASES ............................................................................... 12
4.2. APPLICATION DESCRIPTION ................................................................................................ 12
4.3. THE EXAMPLE IN A CLASSICAL PROGRAM STYLE ................................................................ 13
4.4. CONVERTING THE PROGRAM TO A PLCOPEN COMPLIANT VERSION .................................... 14
4.5. ADDING ERROR BEHAVIOUR .............................................................................................. 17
4.6. ADDING A SYSTEM ALARM LIST .......................................................................................... 25
5 ADDITONAL CONSIDERATIONS ....................................................................................... 28
5.1. FUNDAMENTALS FOR OOP GUIDELINES – CONSIDERATIONS.............................................. 28
5.2. NOTES ON PERFORMANCE CONSIDERATIONS ...................................................................... 28
5.3. NOTES FOR FURTHER INVESTIGATIONS ............................................................................... 29
List of figures
Figure 1: Representation of IModules and ICommands............................................................................. 10
Figure 2: Implementation of LConC .......................................................................................................... 11
Figure 3: State Machine of LConC ............................................................................................................ 11
Figure 4: The boiler demo. ......................................................................................................................... 12
Figure 5: Layout of the POUs .................................................................................................................... 13
Figure 6: Function block interface and state diagram of LConC ............................................................... 15
Figure 7: Same interfaces for different devices .......................................................................................... 16
Figure 8: Overview of the states in a simulation ........................................................................................ 17
Figure 9: The AttachDevice Method of the Error handler ......................................................................... 18
Figure 10: The Error Handler FB with the list of the devices (FBs)......................................................... 19
Figure 11: Initialization from the Error Handler in FB_Boiler ................................................................. 19
Figure 12: The structure for Error Handler ............................................................................................... 20
Figure 13: Main program with error handling .......................................................................................... 20
Figure 14: Example of the error message for one FB ............................................................................... 21
Figure 15: Overview of the architecture ................................................................................................... 21
Figure 16: Example of a cyclic action linked to the IActionControl ........................................................ 22
Figure 17: Implementation of the Set action of the Behaviour Model ..................................................... 23
Figure 18: The needed changes in the Device FB..................................................................................... 23
Figure 19: Corresponding changes to the attached devices ...................................................................... 24
Figure 20: UML Diagram of the new design ............................................................................................ 24
Figure 21: Implementation of the GetName in the Base Class ................................................................. 25
Figure 22: Implementation of the delegation of the GetModelState......................................................... 25
Figure 23: String array for the error handler ............................................................................................. 26
Figure 24: Example of an error string ....................................................................................................... 26
Figure 25: Example of creating the string in the error message ............................................................... 26
Figure 26: The function FC_ErrorCode .................................................................................................... 27
List of abbreviations:
FB Function Block conform IEC 61131-3
FC flow control
LC level control
OOP Object-Oriented Programming
OT Operation Technology
PID Proportional, Integral Derivative control algorithm
PLC Programmable Logic Controller
As example, a boiler demo will be used to represent the different forms of programming.
2 Introduction in the OOP features of the 3rd edition of the IEC 61131-3
standard.
2.1. The 3rd edition of IEC 61131-3
Modern programming environments refer in many cases to OOP which is included in Python, C++,
Objective-C, Smalltalk, Delphi, Java, Swift, C#, Perl, Ruby and PHP.
Important aspects of the 3rd Edition of the IEC 61131-3 standard are the integration of object-oriented
features. This includes Classes, including Methods and Interfaces, OOP features for Function blocks, and
Namespaces.
The 3rd edition of standard IEC 61131-3 – Programming Languages is approved as International Standard.
This means that this edition is now official and is available as International Standard at www.IEC.ch . This
standard is fully upwards compatible to IEC 61131-3, 2003 (2nd edition).
The 3rd edition has a long list of changes and enhancements. Important aspects are the integration of OOP
features. These include Classes, Methods and Interfaces, OOP features for Function Blocks, and
Namespaces.
The main reason to include these extensions is to link better to the scarce resources of engineers in the
future. With preferences to writing apps for the mobile world, we should better adapt to them than vice
versa.
Note that references are made to the IEC 61131-3 standard itself as is available at www.IEC.ch. These used
references are for explanation only and for completeness the standard itself should be bought.
2.2. Definitions in the IEC 61131-3 standard
With these OOP extensions, there are several changes or new definitions in comparison to the 2nd edition.
For instance, a program organization unit, originally defined as function, function block, or program, is
extended to include a category “class”.
Function blocks and classes may contain methods. This means that also a class and a derived class must be
defined, similar to derived data types and derived function blocks, as well as methods.
The most important new definitions are listed here:
Name Description
base type data type, function block type or class from which further types are
inherited/derived.
call language construct causing the execution of a function, function block, or
method.
class program organization unit consisting of:
• the definition of a data structure,
• a set of methods (like subroutines) to be performed upon the data structure
A class is an implementation— a concrete data structure and collection of
subroutines— while a type is an interface.
derived class class created by inheritance from another class.
Note 1 to entry: Derived class is also named extended class or child class.
Note 2: this is of course very much in line with derived data type and derived
function block type as defined in the IEC 61131-3-2003.
dynamic situation in which the instance of a method call is retrieved during runtime
binding according to the actual type of an instance or interface.
inheritance creation of a new class, function block type or interface based on an existing
class, function block type or interface, respectively.
input variable variable which is used to supply a value to a program organization unit except
for class.
instance individual, named copy of the data structure associated with a function block
type, class, or program type, which keeps its values from one call of the
associated operations to the next.
interface language element in the context of OOP containing a set of method prototypes.
Example: it is similar to a motor flange: it describes holes diameter, distance,
shaft size, but it is not a motor.
method language element similar to a function that can only be defined in the scope of
a function block type and with implicit access to static variables of the function
block instance or class instance.
Example: a boiler can have a Fill method, a HeatUp method, each one
performing a specific task.
override keyword used with a method in a derived class or function block type for a
method with the same signature as a method of the base class or function block
type using a new method body
output variable variable which is used to return a value from the program organization unit
except for classes.
program function, function block, class, or program.
organization
unit
signature set of information defining unambiguously the identity of the parameter
interface of a METHOD consisting of its name and the names, types, and order
of all its parameters (i.e., inputs, outputs, in-out variables, and result type).
Multi languages + + - - -
OOP/procedural mixed - + + - -
Classes ~ (FB) + + + +
Methods ~ (Actions) + + + +
Interfaces - + - + +
Polymorphism - + +/- + +
Properties - - - - +
3 Generic Approach
3.1. General
The PLCopen document "Guidelines for usage of Object Orientation" defines the currently generally
accepted rules for good OOP design in the context of the IEC61131-3 3rd Edition.
It is advised to read the document PLCopen Software Creation Guidelines: Creating PLCopen Compliant
Libraries, Version 1.0 of May 4, 2017, which is downloadable from the PLCopen website. As part of this,
we will use the SOLID principles in the automation technology environment. SOLID stands for:
• A module represents a part of an automation application including their software function and can
also be nested hierarchically as required. These kinds of objects are implemented with the keyword
CLASS.
• A command represents a concrete action of a module. It is assigned to one module or to a group of
modules they implement the same interface. These kinds of objects are implemented with the
keyword FUNCTION_BLOCK.
Every command will implement a generic ICommand interface and will follow the design guidelines for
PLCopen conforming function blocks.
The axis related commands provide an input of type IAxis to get the reference to a related axis instance they
will act upon.
Example of LConC from the document “Creating PLCopen compliant libraries”. LConC stands for Level
Controlled, not time limited and with a continuous behaviour.
We will not use the different detailed FBs directly, but we will use our abstract Interface IBehaviourModel
(see Figure 7:Same interfaces for different devices). This interface gives access to the Outputs from the
function blocks which include xDone, xBusy, xError, xAborted, iErrorID. This is the idea from the Interface
Segregation Principle, and with this, all the FBs from the Common Model Library can now be connected.
This is not corresponding to only one FB-type, nor to one Behaviour Model. All described Behaviours and
types can be connected without any adaptation to this ErrorHandler.
The idea is to map all the function blocks to an array which is included in the error handler, to enable an
iteration through all the function blocks. In this Array we will not have a Pointer to the different FBs on
detail level, but we will have the basic interface IBehaviour Model only. With the Method GetModelState
we can monitor the required information from all FB. See Figure 10:The Error Handler FB with the list of
the devices (FBs).
The Error Handler has two methods to link it to the devices (FBs): AttachDevice and DetachDevice. The
error handler has these two methods for attach and detach, reflecting it in the device list. The error handler
references to this list (see Figure 12:The structure for Error Handler).
We can now monitor all FBs with the same Method GetModelState and build a structure with Status
Information so that one can see the status from all the function blocks.
Figure 10: The Error Handler FB with the list of the devices (FBs)
The definition of the Attach and Detach Methods is done in an Interface which is implemented from the
Error Handler. This interface can now be used by the Boiler to attach all the instances to the Error Handler.
Using inheritance as a base design for FBs gives a nice abstract control option but shows some limitations.
We are no longer able to extend the detailed device FB on an abstract Level. The reason for this limitation is
that one cannot use inheritance on the abstract Level BehaviourModelBase as it uses inheritance already.
This is the point to check, if we can optimize this design by using composition instead of inheritance for the
Behaviour Model. For this, we must show how we can define the Behaviour Modell as a member of our FBs
but define the Method for the different states in the same way as before. (Open / Close Principle).
A feature from the Common Behaviour Model can help us to implement this design. The FBs in the Library
support the connection of an Action Controller, which allows a FB which implements the IActionController
to do a Method call instead of the methods which come from the Behaviour Model.
The idea is now to create a new Abstract Device Base Class, which has as a new feature ‘Name’ from the
FB as a complete Path. To support now the Composition from Behaviour Model, our Base Class implements
the IActionController. To follow the Open/Close Principle it is recommended to implement the
IActionProvider Interface also. This allows the detailed Device FB to work with the behaviour
implementation on the same way as before.
In the Method from the Action Controller, one calls a Method, which comes from the Action Provider.
The following example shows the cyclic action.
To attach our FBs to the Error Handler we use our property so that the interface to the Error Handler can
still be the same.
This is implemented in a way that all errors which happen are presented in a string array, which users can
read.
5 Additonal considerations
On the following pages some information from previous discussions are listed. Currently they have not the
right quality to be included in the main text.
5.1. Fundamentals for OOP Guidelines – considerations
Within this document, some key aspects shall be taken into consideration:
• Good design but bad performance -- Which details should be considered in order not to influence the
performance too much?
• Avoidance of dependencies
The introduction of dependencies at runtime (Dependency injection) plays a central role in the
discussion whether the composition should be given a priority over inheritance.
It is very important to note that this is only a discussion.
The purpose of this discussion is not necessarily to find the "optimal" way to design a class, but to
provide food for thought on how to deal adequately with the decision for or against the use of
inheritance or composition.
When designing a controller today, it is no longer just a question of how much time 1024 IL instructions
require!
▪ Compiler design
The code generator for a PLC must ensure that the code parts of an application are in relation to the cache
structure in the neighborhood. So, code and data which are used cyclically can be processed very fast
without a new transfer from the main memory. The implementation of interfaces, methods, and properties
but also references must be adapted to the needs of a PLC. So, use cases like OnlineChange and
Debugging must be considered early and well. The allocation of memory on the stack (local variables)
must not create a problem when using methods. The initialization of extensive data structures can become
a problem (e.g., when cyclically called) and should be accordingly controllable by the user.
▪ Software design
The keywords ABSTRACT, FINAL, PRIVATE, ... must be used consciously.
This allows the developer to influence essential aspects of the runtime of his software at a very early stage.
The developer must keep an eye on whether and when the initialization of his local variables must take
place. The PLC development environment should provide a way to prevent the initialization of a set of
specific variables so that "much" time can be saved here.