Moscow, Azovskaya 14
+7 (495) 310-97-15
Mo-Fr: 9.00 - 18.00 (Moscow time)
Order a call
Your name *
Enter phone *
Your Email *
Call me back
OOP programming methods of modern controllers

OOP for PLC. Codesys and Strategy

A strategy is a behavioral design pattern that defines a family of similar algorithms and places each of them in its own class, after which the algorithms can be interchanged right during program execution.
This is the definition we will copy and paste to begin our story. And after reading the definition, the thought creeps in, but how can object-objective programming (OOP) be implemented on a PLC?

In fact, it's easy. And a lot of these patterns can be shifted to the rails of industrial programming..
To implement this madness, we need Codesys 3.x because he learned a little in OOP. Let's start.

What do smart articles on the Internet return to us?
The Strategy pattern proposes to define a family of similar algorithms that are often changed or extended, and put them into their own classes, called strategies.
Instead of the original class itself executing this or that algorithm, it will act as a context, referring to one of the strategies and delegating work to it. To change the algorithm, it will be enough for you to substitute another strategy object into the context.
It is important that all strategies have a common interface. Using this interface, the context will be independent of concrete strategy classes. On the other hand, you can change and add new kinds of algorithms without touching the context code.

An interface, according to the Codesys documentation, is an object-oriented programming tool. The ITF object ("Interface") describes a set of prototype methods and properties. In this context, prototype means that methods and properties only contain declarations and do not contain implementations.
This means that in the interface we describe methods. We give them names, a list of input and output data, but do not touch the implementation in any way.

Codesys 3.5 ООП 

Creating an interface
To create an interface, click RMB->Add object->Interface, then give it a name. For me it will be "StrategyInt".
Then we add a method. Let's call it GetSTR. To add also right click and add interface method.
We write there only one output variable.

  Codesys 3.5 ООП

Description of interface method variables.

Now, when we create functional blocks inherited from this interface, we will already have the GetSTR method with the output variable OUT in it.

This is the functional block that will call various "strategies" for us.
Create a common function block.

  Codesys 3.5 ООП

A functional block that plays the role of a context.
We can send any object that will be inherited from the StrategyInt interface to the input of the functional block. In the implementation, we see that when calling this function block, we will call the GetSTR method, and return the result to the output variable Result.

The next step is the implementation of the strategies. Let's start with two. They will not differ in anything other than returning different values..
We create a function block and inherit it from our interface.

  Codesys 3.5 ООП

Function block creation window.

You can specify any name, I just ran out of imagination.

I created two such blocks, which will play the role of our strategies.

  Codesys 3.5 ООП

Created Strategies

Each has two methods, the implementation of which must be written.

  Codesys 3.5 ООП

Implementation of the first strategy.

  Codesys 3.5 ООП

Implementation of the second strategy.

Yes, you can directly return values through a method, but I don’t want to.

And now we put it all together.

 Codesys 3.5 ООП

Assembly of all components.

I've broken down the steps a bit. To implement we need:

An object that plays the role of a context - 1 pc. Our strategies. Here I included them in the block of variables, but you can also make a pointer to the object of the strategy we need, and create it in the code using __NEW. Well, the variables Out_1 and Out_2 for clarity.
The code works as follows. At the first step, we give our strategy POU_1 as input to MyProc variable Object.

Then the context is called and it works, writing the result of its work to the Out_1 variable.
Then we give the POU_2 strategy as input to the same context, followed by output, the result is written to the Out_2 variable.
Turn on emulation.

Codesys 3.5 ООП 

Execution result.

In this way, you can spread the logic of the work of objects that are similar, but slightly different in implementation. Returning a string variable is just an example here)

Additionally, to support this article, I paid for a static IP and created a port forwarding scheme on the Mikrotik router. You can connect and use.

Codesys 3.5 ООП

The project archive: 

CODESYS V3.5 SP14 Patch 3

Project archive ПЛК210 - Nov_190621s.projectarchive

The password for the archive is 159

Article author: Alexander Sudalin - 

#OOP, #CODESYS, #OOPOnlineexample, #PLC210, #Object-oriented programming

Be the first to comment

You comment add