Introduce OOP programming of TC3 to automation engineers




Practice and summarize in work, share and discuss in blog, and write practical blog

Object Oriented Programming, or OOP programming, is one of the new features of the TwinCAT 3 programming tool and is also a new addition to the third edition of IEC61131-3.
TwinCAT3 PLC(Photo:Beckhoff inc.)


In TwinCAT 2.0 PLC programming, users can write a function block (FB) for code that needs to be reused.The most typical example is to encapsulate the internal logic of a type of device into a FB, such as a shaft control FB, a temperature control FB, and a light control FB. Each FB has its own input variables, output variables, and intermediate variables.The logic of how the input variables affect the output variables is implemented in code.The number of instances of the same FB is determined by the number of devices of the same type.The way each instance interacts with the outside world is through variables.Among them, Input variables are readable and writable from the outside, while Output variables and intermediate variables are only readable from the outside and cannot be written.



In TwinCAT 3.0, internal variables of FB are not visible to the outside world.However, in addition to input and output variables, two new types of content are visible to the outside world: Method and Property.This idea of "Method" and "Property" is not like traditional PLC, but more like object-oriented programming in high-level languages. This is TC3 OOP.According to the concept of object-orientation, FB not only has Method and Property, but also can inherit.Between two FBs with inheritance relationship, the update of the parent object will automatically update the child object.In addition, TC3 also introduces a concept: Interface.In Interface, there are only interface definitions of Method and Property, but no code.For users who are accustomed to high-level languages, these concepts are not difficult to understand, but for traditional PLC engineers and electrical engineers, this is a big leap forward.
Beckhoff PLC(Beckhoff Official website)


It needs to be clarified that OOP programming is a new feature of TC3, but it is not a must-use feature.In TC3, not only Method, Property, and Interface are supported, but also Input and Output variables and Action code.There is no strict requirement on when to use Input and Output variables and when to use Method and Property.Users can also use them together according to the project, or even ignore the new features of OOP and only use Input and Output variables to complete the project.



For many electrical engineers, it is already difficult to get used to structured text programming, and OOP programming is even more daunting.Based on my own confusion and overcoming of details during my understanding and practice of OOP programming as an automation engineer, this article summarizes my insights into OOP programming.Taking the example of building the most commonly used "axis control function" in automation work, it leads readers to practice OOP programming from the basics to advanced levels, and reminds them of the details that need attention during the process.