Sie sind auf Seite 1von 15

AP P L I CATI O N EX AM P L E

AC500 V3 OBJECT ORIENTED


PROGRAMMING
WORKING WITH INTERFACES, METHODS
AND PROPERTIES
Contents
1 Disclaimer .................................................................................................................................... 3

2 Introduction ............................................................................................................................... 4
2.1 Scope of the document ............................................................................................................. 4
2.2 Compatibility ............................................................................................................................... 4

3 Object Oriented Programming ..................................................................................................5


3.1 Inheritance ....................................................................................................................................5
3.2 Interfaces ...................................................................................................................................... 6

4 Object Oriented Programming in Codesys ............................................................................. 6


4.1 Function blocks ........................................................................................................................... 6
4.2 Interfaces ...................................................................................................................................... 8

5 Example Description ................................................................................................................. 8


5.1 Interfaces ...................................................................................................................................... 9
5.2 Implementation of the function blocks and their instance calls. ...................................10
5.2.1 SimpleRoom ..............................................................................................................10
5.2.2 Bathroom ...................................................................................................................10
5.2.3 TempRoom ................................................................................................................ 11
5.3 Using the light interface of all function block instances .................................................. 12
5.4 Visualization ................................................................................................................................ 12

2 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

1 Disclaimer
A. For customers domiciled outside Germany /

Für Kunden mit Sitz außerhalb Deutschlands

„Warranty, Liability:

The user shall be solely responsible for the use of this products described within this file. ABB
shall be under no warranty whatsoever. ABB's liability in connection with application of the
products or examples provided or the files included within this products, irrespective of the
legal ground, shall be excluded. The exclusion of liability shall not apply in the case of inten-
tion or gross negligence. The present declaration shall be governed by and construed in ac-
cordance with the laws of Switzerland under exclusion of its conflict of laws rules and of the
Vienna Convention on the International Sale of Goods (CISG)."

„Gewährleistung und Haftung:

Der Nutzer ist allein für die Verwendung des in diesem Dokument beschriebenen Produkte
und beschriebenen Anwendungsbeispiele verantwortlich.

ABB unterliegt keiner Gewährleistung. Die Haftung von ABB im Zusammenhang mit diesem
Anwendungsbeispiel oder den in dieser Datei enthaltenen Dateien - gleich aus welchem
Rechtsgrund - ist ausgeschlossen. Dieser Ausschluss gilt nicht im Falle von Vorsatz oder gro-
ber Fahrlässigkeit. Diese Erklärung unterliegt Schweizer Recht unter Ausschluss der Verwei-
sungsnormen und des UN-Kaufrechts (CISG)."

B. Nur für Kunden mit Sitz in Deutschland

„Gewährleistung und Haftung:

Die in diesem Dokument beschriebenen Anwendungsbeispiele oder enthaltenen Dateien be-


schreiben eine mögliche Anwendung der AC500 bzw. zeigen eine mögliche Einsatzart. Sie
stellen nur Beispiele für Programmierungen dar, sind aber keine fertigen Lösungen. Eine Ge-
währ kann nicht übernommen werden.

Der Nutzer ist für die ordnungsgemäße, insbesondere vollständige und fehlerfreie Program-
mierung der Steuerungen selbst verantwortlich. Im Falle der teilweisen oder ganzen Über-
nahme der Programmierbeispiele können gegen ABB keine Ansprüche geltend gemacht wer-
den.

Die Haftung von ABB, gleich aus welchem Rechtsgrund, im Zusammenhang mit den Anwen-
dungsbeispielen oder den in dieser Datei enthaltenen Beschreibung wird ausgeschlossen.
Der Haftungsausschluss gilt jedoch nicht in Fällen des Vorsatzes, der groben Fahrlässigkeit,
bei Ansprüchen nach dem Produkthaftungsgesetz, im Falle der Verletzung des Lebens, des
Körpers oder der Gesundheit oder bei schuldhafter Verletzung einer wesentlichen Vertrags-
pflicht. Im Falle der Verletzung einer wesentlichen Vertragspflicht ist die Haftung jedoch auf
den vertragstypischen, vorhersehbaren Schaden begrenzt, soweit nicht zugleich ein anderer
der in Satz 2 dieses Unterabsatzes erwähnten Fälle gegeben ist. Eine Änderung der Beweis-
last zum Nachteil des Nutzers ist hiermit nicht verbunden.

Es gilt materielles deutsches Recht unter Ausschluss des UN-Kaufrechts."

3ADR010525, 1, en_US 3
2 Introduction

2.1 Scope of the document


This document should give the user an idea how Object Oriented Programming (OOP) can be
realized in a PLC with CoDeSys 3.5.

The document is not describing the basic for an AC500 PLC. To understand
the Ideas and the Application example basic knowledge in object oriented
NOTICE programming is beneficial

2.2 Compatibility
The application example explained in this document have been used with the below engineer-
ing system versions. They should also work with other versions, nevertheless some small ad-
aptations may be necessary, for future versions.

• AC500 V3 PLC

• Automation Builder 2.2.4 or newer

4 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

3 Object Oriented Programming


Characteristic for OOP is the encapsulation into different objects/ classes. This should re-
duce the programming effort and avoid mistakes. A class is a datatype which contains Attrib-
utes and Methods. Attributes are the state of the class. Methods represent the behavior of
the class.

As an example, the simple class “Light” is shown below.

Light

+isOn(get)

+switchOn()

+switchOff()

A light like shown has only one attribute which has the status if the light is on or not. The
class light has furthermore two methods. switchOn() is used to power the light. SwitchOff()
disables the light. The + in front of the attribute and the methods is showing that they are
public. This means the method and property can be called from each program part. The prop-
erty isOn has a get method. The property can be read but not written as there is not write
method.

3.1 Inheritance
Each class can inherit the attributes and methods from another class.

Light

Dimmable Flashing
Light Light

Lounge Light Blue Light Stroboscope

As shown in the picture above the class “Light” is on top. This class has the property and
methods as described in the chapter before. The class “Dimmable Light” inherits all attrib-
utes and methods from “Light”. The class “Lounge Light” inherits the class “Dimmable Light”.
In case the Class “Dimmable Light” has the attribute Brightness with a get and a set method
the user can read and write LoungeLight.Brightness. In the class Stroboscope or any instance
of this class it is not visible what the method switchOn() is doing exactly. Due to the OOP the
method can just be used as it is defined in the class Light.

3ADR010525, 1, en_US 5
Each class can extend one other class. This class is called father. Each class can have only one
father. So “Stroboscope” has the father “Flashing Light”. The class “Flashing Light” itself has
two children “Blue Light” and “Stroboscope”.

3.2 Interfaces
An interface defines the attributes and methods a class has. In contrast to the inheritance an
interface has only the definition. The implementation is done in the class which is implement-
ing the interface.

Interface
Light
switchOn()

Class Room Class Car


switchOn() switchOn()

In the example above the Interface Light has the Method switchOn(). The Interface itself has
no implementation for this Method. Both Classes Room and Car are implementing the Inter-
face Light. In both Classes the Method switchOn() has to be implemented. But the Methods
can be different in both classes. This has the advantage that the behavior of the method
switchOn() can be exchanged in any class. This has no influence on the other classes which
are also implementing the interface.

Another advantage of interfaces is, that a class is able to implement more than one interface.
For example, the class Car can implement the interfaces Vehicle, Radio and Light. This means
the Car behaves from an external point of view like a Vehicle, Radio and Light.

From the programmer side it makes no difference if the light in a room is switched on or the
light in a car is switched on because the interface to the user is looking the same way in both
cases.

4 Object Oriented Programming in Codesys

4.1 Function blocks


Function blocks can be used as classes in Codesys. By adding a function block, it is possible
to extend the function block. Then this function block inherits from another function block.

In addition to the Extend a function block can also implement other interfaces.

The Keywords “Final” and “Abstract” are not described in this document. In the Access speci-
fier drop down menu internal and public can be selected. By selecting internal all local varia-
bles can not be accessed from the code. The variables of a public function block can be read
and written from any other place of the project.

6 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

3ADR010525, 1, en_US 7
4.2 Interfaces

Interfaces can be added to the project. To each interface methods and properties can be
added. Each Property has by default a setter and a getter method. In case a read or a write
should not be allowed this Method can be deleted.

5 Example Description
In this chapter the example project “OOP_Example_Rooms_AB224 is described. In the project
rooms have been used as objects for the object oriented programming. In this project three
interfaces and three function blocks are used. The Interfaces are Light, DimmingLight which
extends Light and Temperature.

Three rooms are used as objects.

• SimpleRoom is implementing Light which means the room has only a light.

• Bathroom is extending the SimpleRoom. The Bathroom has a ventilation in addition


to the Light

• TempRoom is a Room with a Temperature control. The user can set a temperature
and the room will heat up and cool down to this temperature. Furthermore, this room
has a dimmable light

An overview of this is shown in the picture below.

8 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

5.1 Interfaces

3ADR010525, 1, en_US 9
The interfaces are shown in the picture above.

The Light interface has two Methods. SwitchOn and SwitchOff which are used to toggle the
light. With the property isOn the user can read whether the light is switched on or not. The
user has no possibility to write this variable.

The DimmingLight Interface extends the Light interface. Only the Property LightLevel is
added to this interface. The methods to switch the light on and off will be inherit from the
Light interface.

The Temperature interface has only the RoomTemperature property. This property can be
read and written by the user.

5.2 Implementation of the function blocks and their


instance calls.
5.2.1 SimpleRoom

The simple room is defined as internal. As it is implementing the Light interface it automati-
cally has the shown Methods and Property. The Implementation of these have to been added.

The SimpleRoom has one local variable LightIsOn. By calling the Method METH_SwitchOn this
variable is set to true. The Method SwitchOff sets this variable to false. By reading the Prop-
erty isOne the status is outputted: Prop_isOn := lightIsOn;.

The function block itself has no code inside.

In the PLC_PRG the instantiation of the function block can be found. Room1: SimpleRoom;

In the code the Light is on Property is read and stored in a local variable. The methods switch
on and switch off are called from the visualization.

5.2.2 Bathroom
The Bathroom is extending the SimpleRoom. In addition to the Light the Bathroom has a Ven-
tilation. The ventilation is depending on the light.

The function block has the local variable ventilationIsOn which can be read via the property.
The value ventilation is on is set 5 seconds after the light is switched on and reset 20 seconds
after the light has been switched off. To realize this, two timers are used, and a short code is
written into the function block body.

In The PLC_PRG the second room is instantiated the same way.

10 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

As Bathroom is extending SimpleRoom the light Property can be read the same way.

5.2.3 TempRoom

The function block TempRoom is implementing the interface DimmingLight which is extend-
ing Light. The methods switchOn and SwitchOff as well as the Property isOn is working in the
same way as in the SimpleRoom. In addition to that the function block has the property light-
Level. Here a local variable can be set and read which is dimming the light. The get Method is
a bit extended to check whether the light is on. In case the light is switched off the Property
returns 0.

Furthermore, the Room has a Temperature Control. The user can set and read the Room Tem-
perature. If the user sets the RoomTemperature Property an intern setpoint variable is set. In
case the user reads the RoomTemperatore Property another internal variable is outputted.
On the interface to the user only one variable is visible which is the temperature. Intern two
variables are used. The current temperature and the setpoint temperature.

Here the Setting of the property is shown. The inputted variable has to be between -20 and
80. If it is outside -20 or 80 are used.

By reading the temperature the currentTemperature Variable is returned.

Intern the function increases or decreases the current Temperature to the setpoint.

3ADR010525, 1, en_US 11
5.3 Using the light interface of all function block
instances
As all three rooms implement the Light interface all instances can call the methods SwitchOn
and SwitchOff. Even if there could be different behaviors of these Methods the interface is
looking the same. This can be used to access all function blocks in the same way for example
in a loop. In the code the array aAllLights is defined.

The three entries are assigned to the different rooms. All Entries of this array have to imple-
ment the Interface Light.

Then a loop can be used to switch the light in all rooms on.

5.4 Visualization

This project has a little visualization. The three rooms are visible. All rooms have the buttons
On and Off to turn on and off the light. The Bathroom has the ventilation which is starting if
the light is turned on for at least five seconds. The ventilation remains 20 seconds after the
light has been turned off. The third room has a slider which can be used to change the bright-
ness of the light. In the right bottom corner, the temperature is shown. By clicking to the text
field another temperature can be inputted. The room is simulating its current temperature

12 3ADR010525, 1, en_US
AC5 00 V3 OB J ECT ORI E NT ED PR OGR AMM ING

and will start changing the temperature by cooling or heating. The limits of the inputted tem-
perature are -20 °C and +80 °C In case a value outside this range is inputted the setpoint is
set to the limits.

With the buttons All On and All Off all lights can be switched on or off. This is implemented
inside the PLC_PRG with an array containing all three rooms

3ADR010525, 1, en_US 13
__ __

ABB Automation Products GmbH We reserve the right to make technical We reserve all rights in this document and
Eppelheimer Straße 82 changes or modify the contents of this in the subject matter and illustrations con-
69123 Heidelberg, Germany document without prior notice. With re- tained therein. Any reproduction, disclo-
Phone: +49 62 21 701 1444 gard to purchase orders, the agreed par- sure to third parties or utilization of its
Fax: +49 62 21 701 1382 ticulars shall prevail. ABB AG does not ac- contents – in whole or in parts – is forbid-
E-Mail: plc.support@de.abb.com cept any responsibility whatsoever for den without prior written consent of ABB
www.abb.com/plc potential errors or possible lack of infor- AG.
mation in this document. Copyright© 2020 ABB. All rights reserved