OOP methods of programming modern controllers

Strategy is a behavioral design pattern that defines a family of similar algorithms and puts each of them in its own class, after which the algorithms can be interchanged right at runtime.
We will copy and paste this definition to start our story. And after reading the definition, a thought creeps in, how can object-objective programming (OOP) be implemented on a PLC?

In fact, it's easy. And a lot of such patterns can be put on the rails of industrial programming.
To implement this madness, we need Codesys 3.x because he did not learn much in OOP. Let's transgress.

REALIZATION
What do smart articles on the internet offer us?
The Strategy pattern proposes to define a family of similar algorithms that are often changed or extended, and move them into their own classes, called strategies.
Instead of the original class executing this or that algorithm itself, it will act as a context, referring to one of the strategies and delegating the 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 the specific classes of strategies. On the other hand, you can change and add new kinds of algorithms without touching the context code.

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

Creating an interface
To create an interface, press RMB-> Add object-> Interface, then give it a name. I will have it "StrategyInt".
Then we add the method. Let's call it GetSTR. To add also right mouse button and add interface method.
We register only one output variable there.

  2.png

Description of interface method variables.

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

CONTEXT
This is the functional block that we will call various "strategies".
We create a regular function block.

  3.png

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

STRATEGIES
The next step is implementing strategies. First, let's make two. They won't be any different other than returning different values.
We create a functional block and inherit it from our interface.

  4.png

Function block creation window.

You can specify any name, I just ran out of fantasy.
I have created two such blocks, which will play the role of our strategies.

  5.png


Strategies created

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

  6.png


Implementation of the first strategy

  7.png


Implementation of the second strategy.

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

ASSEMBLY
And now we put it all together.


 8.png


Assembly of all components.

I broke it down a little in steps. For implementation we need:

Object that plays the role of 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 via __NEW. Well, the variables Out_1 and Out_2 for clarity.
The code works as follows. In the first step, we give our strategist POU_1 as input to the Object variable MyProc.

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


9.png


Execution result.

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

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


10.jpg

To connect to the PLC210 controller (89.23.36.110), follow the links, after downloading the Codesys distribution kit and the project archive:



The password is set on the archive - 159

The author of the article: Alexander Sudalin - sudalin.kms@gmail.com


#OOP, #CODESYS, #Onlineexample, #PLC210, #Strategy, #Design

Array
(
    [ID] => 508
    [IBLOCK_ID] => 1
    [NAME] => OOP for PLCs
    [IBLOCK_SECTION_ID] => 
    [IBLOCK] => Array
        (
            [ID] => 1
            [~ID] => 1
            [TIMESTAMP_X] => 14.10.2021 23:54:30
            [~TIMESTAMP_X] => 14.10.2021 23:54:30
            [IBLOCK_TYPE_ID] => EN
            [~IBLOCK_TYPE_ID] => EN
            [LID] => s2
            [~LID] => s2
            [CODE] => articles
            [~CODE] => articles
            [API_CODE] => 
            [~API_CODE] => 
            [NAME] => Articles
            [~NAME] => Articles
            [ACTIVE] => Y
            [~ACTIVE] => Y
            [SORT] => 10
            [~SORT] => 10
            [LIST_PAGE_URL] => /articles/
            [~LIST_PAGE_URL] => /articles/
            [DETAIL_PAGE_URL] => /articles/#ELEMENT_CODE#.html
            [~DETAIL_PAGE_URL] => /articles/#ELEMENT_CODE#.html
            [SECTION_PAGE_URL] => 
            [~SECTION_PAGE_URL] => 
            [CANONICAL_PAGE_URL] => https://lapshinvr.com/articles/articles.html
            [~CANONICAL_PAGE_URL] => https://lapshinvr.com/articles/articles.html
            [PICTURE] => 
            [~PICTURE] => 
            [DESCRIPTION] => 
            [~DESCRIPTION] => 
            [DESCRIPTION_TYPE] => html
            [~DESCRIPTION_TYPE] => html
            [RSS_TTL] => 24
            [~RSS_TTL] => 24
            [RSS_ACTIVE] => Y
            [~RSS_ACTIVE] => Y
            [RSS_FILE_ACTIVE] => N
            [~RSS_FILE_ACTIVE] => N
            [RSS_FILE_LIMIT] => 
            [~RSS_FILE_LIMIT] => 
            [RSS_FILE_DAYS] => 
            [~RSS_FILE_DAYS] => 
            [RSS_YANDEX_ACTIVE] => N
            [~RSS_YANDEX_ACTIVE] => N
            [XML_ID] => 
            [~XML_ID] => 
            [TMP_ID] => 
            [~TMP_ID] => 
            [INDEX_ELEMENT] => Y
            [~INDEX_ELEMENT] => Y
            [INDEX_SECTION] => N
            [~INDEX_SECTION] => N
            [WORKFLOW] => N
            [~WORKFLOW] => N
            [BIZPROC] => N
            [~BIZPROC] => N
            [SECTION_CHOOSER] => L
            [~SECTION_CHOOSER] => L
            [LIST_MODE] => 
            [~LIST_MODE] => 
            [RIGHTS_MODE] => S
            [~RIGHTS_MODE] => S
            [SECTION_PROPERTY] => N
            [~SECTION_PROPERTY] => N
            [PROPERTY_INDEX] => N
            [~PROPERTY_INDEX] => N
            [VERSION] => 1
            [~VERSION] => 1
            [LAST_CONV_ELEMENT] => 0
            [~LAST_CONV_ELEMENT] => 0
            [SOCNET_GROUP_ID] => 
            [~SOCNET_GROUP_ID] => 
            [EDIT_FILE_BEFORE] => 
            [~EDIT_FILE_BEFORE] => 
            [EDIT_FILE_AFTER] => 
            [~EDIT_FILE_AFTER] => 
            [SECTIONS_NAME] => 
            [~SECTIONS_NAME] => 
            [SECTION_NAME] => 
            [~SECTION_NAME] => 
            [ELEMENTS_NAME] => Элементы
            [~ELEMENTS_NAME] => Элементы
            [ELEMENT_NAME] => Элемент
            [~ELEMENT_NAME] => Элемент
            [REST_ON] => N
            [~REST_ON] => N
            [EXTERNAL_ID] => 
            [~EXTERNAL_ID] => 
            [LANG_DIR] => /
            [~LANG_DIR] => /
            [SERVER_NAME] => lapshinvr.com
            [~SERVER_NAME] => lapshinvr.com
        )

    [LIST_PAGE_URL] => /articles/
    [~LIST_PAGE_URL] => /articles/
    [SECTION_URL] => 
    [CANONICAL_PAGE_URL] => https://lapshinvr.com/articles/oop-for-plcs-codesys-and-strategy.html
    [SECTION] => Array
        (
            [PATH] => Array
                (
                )

        )

    [IPROPERTY_VALUES] => Array
        (
            [ELEMENT_PREVIEW_PICTURE_FILE_NAME] => oop for plcs
            [SECTION_META_TITLE] => Automation articles 
            [SECTION_META_KEYWORDS] => Automation, Articles 
            [SECTION_META_DESCRIPTION] => I met something worthy of attention during the work. It is difficult to assess the necessity of posting this or that article, I tried to clean up the obvious garbage
            [SECTION_PAGE_TITLE] => Here I published articles that may be useful to someone in the production of work
            [ELEMENT_META_TITLE] => Object oriented programming

            [ELEMENT_META_KEYWORDS] => Object Oriented Programming, OOP, OWEN
            [ELEMENT_META_DESCRIPTION] => Alexander Sudalin shared with us his experience of using Codesys OWEN as an example of object-oriented programming
            [ELEMENT_PAGE_TITLE] => OOP methods of programming modern controllers
        )

    [TIMESTAMP_X] => 21.11.2021 18:57:21
    [META_TAGS] => Array
        (
            [TITLE] => OOP methods of programming modern controllers
            [BROWSER_TITLE] => Object oriented programming

            [KEYWORDS] => Object Oriented Programming, OOP, OWEN
            [DESCRIPTION] => Alexander Sudalin shared with us his experience of using Codesys OWEN as an example of object-oriented programming
        )

    [PREVIEW_TEXT] => A lot of these patterns can be shifted onto the rails of industrial programming. To implement this madness, we need Codesys 3.x because he did not learn much in OOP. Let's transgress.
    [PREVIEW_PICTURE] => Array
        (
            [ID] => 2014
            [TIMESTAMP_X] => Bitrix\Main\Type\DateTime Object
                (
                    [value:protected] => DateTime Object
                        (
                            [date] => 2021-11-21 18:57:21.000000
                            [timezone_type] => 3
                            [timezone] => Europe/Moscow
                        )

                )

            [MODULE_ID] => iblock
            [HEIGHT] => 428
            [WIDTH] => 537
            [FILE_SIZE] => 51912
            [CONTENT_TYPE] => image/jpeg
            [SUBDIR] => iblock/a97
            [FILE_NAME] => px0kw5p7z1vo9t28gbzthhwa6bkusj1x.jpg
            [ORIGINAL_NAME] => oop for plcs. codesys and strategy.jpg
            [DESCRIPTION] => 
            [HANDLER_ID] => 
            [EXTERNAL_ID] => 75912ab9d57afe7a8cf744fe6825bbcc
            [~src] => 
            [SRC] => /upload/iblock/a97/px0kw5p7z1vo9t28gbzthhwa6bkusj1x.jpg
            [UNSAFE_SRC] => /upload/iblock/a97/px0kw5p7z1vo9t28gbzthhwa6bkusj1x.jpg
            [SAFE_SRC] => /upload/iblock/a97/px0kw5p7z1vo9t28gbzthhwa6bkusj1x.jpg
            [ALT] => OOP for PLCs
            [TITLE] => OOP for PLCs
        )

    [DETAIL_PAGE_URL] => /articles/oop-for-plcs-codesys-and-strategy.html
)
Ваш комментарий добавлен

Возврат к списку