HCBBS Forum (English)
Submit Chemical Projects / Find Solutions
Amplify Your Requirements on a Broader Chemical Platform *Engineering · Technology · Equipment · Solutions*
Submit Request

Reposting an article: Object-Oriented Programming in PLCs – A True Classic

2009-03-24View Original

Thread Content

Author: Wu Xiangyang Source: Reads: 572 Publication time: 2007-10-19 10:56:00 Submitted by: Object-oriented programming is an advanced programming paradigm among high-level computer languages, and this design concept can also be applied in PLC programs for industrial control systems. Although we are unable to utilize many of the excellent features of object-oriented programming such as \"inheritance\", and it even doesn’t possess all the characteristics of object-oriented programming languages, the basic concepts of object-oriented programming are classes and instances of those classes (i.e., objects), and we only need to use these concepts. In computer programming, we need to abstract and generalize certain concepts in order to create classes. In industrial control systems, control objects such as motors and valves are clear examples of such categories; it is not necessary to abstract them in order to develop classes specifically for them. The following explanation will use Siemens’ Step7 programming language and Schneider’s Unity programming language to illustrate object-oriented programming in PLCs. I. Implementation method: Programming is carried out in Step7 using function blocks (i.e., FBs). When this is mentioned, people think of the modular programming concept proposed by Siemens. Indeed, it is this modular programming approach. However, terms such as modular programming, background data blocks, and multiple backgrounds introduced by Siemens do not help people understand and utilize this excellent design concept clearly. If one understands it from the perspective of object-oriented programming, this design pattern can be understood very well. “An FB block is regarded as a \"class\"; it can be seen as a summary of the code for similar control objects. For example, an FB block such as MtrMM440 can be created for an MM440 inverter. In object-oriented programming, this is referred to as a \"class\". When it is necessary to program the control of a specific motor, a background DB block can be assigned to it – in object-oriented programming, this is known as the implementation of the class (that is, creating an instance of the class). When controlling multiple motors, different background DB blocks can be assigned to this FB block, thereby creating multiple instances of the class. In Step7, there is another type of program block, namely the FC block. Programming that makes use of FC blocks is referred to as structured programming in Siemens; this can be compared to procedural programming in computer programming, which is programming that focuses primarily on functions. Schneider’s Unity software programming helps to better understand object-oriented programming. Its DFB definition includes input/output parameters, private/shared variables, and code implementation – which are precisely the basic elements of a \"class\" in object-oriented programming. Creating instances (objects) of such classes is similar to creating ordinary \"boolean\" variables; one simply needs to define these class variables within the \"Function Blocks\". Both Step7 and Unity can use procedural and object-oriented programming approaches, and the difference between these two approaches is similar to that between the C language and C++ language in high-level computer programming. In the following explanation, the FBs in Step7 and the DFBs in Unity will be referred to as \"classes\", while the instances of FBs plus background DBs in Step7, as well as the DFBs in Unity, will be called \"objects\". II. Object-Oriented Programming Architecture: What was explained above are the implementation details, while programming concepts are based on the program architecture. Just because an object-oriented approach is used in a certain part of a program does not mean that such programming can be called object-oriented programming. This type of programming requires starting from the following aspects: 1. Structured circuit design. Here, the focus is mainly on automated production lines; for individual machine tools, it can be considered as a simplified version of such systems. The automated line layer represents the highest level – it features a main PLC that controls the various areas under its jurisdiction. The engineering layer has an independent power distribution system, but no PLC; instead, it relies on distributed modules controlled by the automated lines. As the name implies, it has a high degree of independence and can be designed and manufactured as a separate engineering project; this level can be omitted when the assembly line is relatively small. Function group layer: Based on the manufacturing process, the equipment in those sections that perform a specific process function are grouped into a function group. This function group belongs to the engineering layer; when the engineering layer is omitted, it belongs to the assembly line layer. Object-oriented programming does not necessarily require the use of the structures mentioned above, but a good architectural structure facilitates object-oriented programming. 2. All control object logic is implemented within “classes”. To achieve this, it is necessary to analyze information related to the control object; for example, for a motor, the following relevant information needs to be taken into consideration: input information, as well as circuit protection information such as the motor’s circuit breaker and thermal relay. Functional protection information, such as the limit switches for motor drives, the pressure switches for fans, and the oil level switches for oil pumps, etc. Start and stop conditions: Both circuit protection and functional protection can lead to the cessation of motor operation, and reset can also result in restart. However, the conditions mentioned here refer to those for starting and stopping normal operation, such as the steps in a sequential control process. Control mode: such as manual and automatic, etc. Fault reset: Restart by using the reset information. Output information: Control outputs, such as the main contactor that controls the motor. Status information output, fault output. . . Status storage information: intermediate variables used in the code implementation, as well as status variables that can be read by the human-machine interface. All of this information is integrated into a single class, with an effort to standardize the parameters of that class. However, there are still some differences compared to high-level programming languages. For Step7, the standards to be followed are as follows: the program structure is implemented using FCs, while object control is handled by FBs; it follows a structural framework of this kind (whose electrical structure is described above). This is merely a rough outline of a PLC program architecture, and a good architecture should be more comprehensive and systematic. 3. Plan the data structures carefully. The definition of data structures is quite important; strive to standardize these structures, and don’t worry about storage space – today’s PLCs have sufficient memory to hold large amounts of data. It should be noted that in Step7, try not to define data structures (UDTs) outside the class, but rather define them within the class. Although this leads to repeated definitions of the same structure across different classes, it enhances the independence of the classes. III. Advantages 1. Standardization: By using this design pattern, program development can be divided into two phases: the development of standard libraries and basic frameworks, and the design at the practical application level. Among these, the standard library and basic architecture form the basis for establishing program standards, while the application layer design is aimed at programming specific control systems. This allows program designers to be divided into two categories: those responsible for standard development, who are senior programmers, and those responsible for application design (which includes program debugging), who are ordinary programmers who have received standardized training. Through such a division, the awkward situations faced in China’s industrial automation sector can be addressed. In traditional Chinese industries, program development was done by a single person, who was also responsible for on-site debugging. Programmers with extensive experience usually reached this stage in their lives after the age of 30, by which time they had already started families. Clearly, frequent business trips are detrimental to family life, and many talented programmers are forced to change careers out of consideration for their families – either by taking on management roles or working in manufacturing plants as maintenance technicians. This represents a serious loss of talent. Undoubtedly, by using the design process outlined above, we can have experienced programmers handle the design of standard libraries and architectures, while allowing those who are new to this industry to focus on application design and debugging. This not only allows experienced programmers to continue with their work without affecting their families, but it also enables young programmers to gain experience through hands-on debugging and increase their earnings. This might cause some people to worry, wondering whether young programmers can participate in on-site debugging It is certain that programs without standardized support cannot be developed even by young programmers, and debugging them on-site will bring many problems. But with good standardization, programmers with over one and a half years of work experience should be able to handle automated production lines independently. The core of object-oriented programming in PLCs is black-box programming. For Step7, we use FBs to handle the control of each object; the control logic, alarm handling, and signal exchange are all carried out within these FBs. Application designers do not need to understand the underlying code implementation – they only need to be aware of the functions of these FBs and how to use them effectively. This reduces the requirements regarding programming skills; programming essentially involves following the architecture, copying code, and modifying the input and output conditions. What about debugging? Many people believe that the most annoying aspect of using FB programming is the difficulty in diagnosing the code after it has been called multiple times. Technically, this is indeed the case; apart from checking information in the background database, it’s not possible to monitor the code after it has been called several times. But as I’ve said, this is a form of \"black box\" programming – we don’t need to diagnose such code; we only need to know what kind of inputs and parameter settings lead to certain outputs. The logic and functionality of the code are the responsibility of the standard library developers, who must test the functional blocks they design under different conditions to ensure their correctness. They also need to create comprehensive documentation so that application designers can understand these blocks. A standard architecture isn’t something that remains fixed once it’s established; it needs to be continuously improved and revised to accommodate the ever-changing requirements of projects. This is also an area where engineering companies can truly build up their knowledge base. The program is not only intended for use by debuggers, but users (equipment maintenance personnel) also need to understand it. Providing them with the complete standard library documentation could pose a risk of technical leakage; on the other hand, without such documentation, it may be difficult for them to diagnose the equipment. Therefore, standard developers need to create another set of documents, namely equipment maintenance documents, whose content is limited to what is necessary for users to be able to use the program for diagnosis purposes. 2. Reusability and ease of management: The advantages of object-oriented programming in computers include reusability and ease of management. This is also true for PLCs; taking Step7 as an example, it is necessary to discuss the differences between FCs and FBs. Looking at the data types, FB has an additional \"STAT\" type compared to FC. FB requires a background DB for use, while FC does not; this difference means that FB has its own independent data storage space, whereas FC’s data storage must rely on public variables (such as intermediate variables M or shared DBs). There is a rule stating that the greater the independence of a program block, the better its reusability, the lower the likelihood of data access conflicts, and the easier it is to manage. For some PLCs produced by certain companies, their programming language does not have features similar to FB; in such cases, an alternative approach using \"FC + shared DB\" can be employed, but its independence is then **reduced**. The independence of the same code is an important aspect in standard development; it is hard to imagine a functional block that is intricately linked to other functional blocks being able to be used repeatedly as a standard block in different projects. Looking at the development of computer languages, programming was terrifying in its early days, while today’s programming methods offer great convenience, with many ready-made standard libraries available. This allows programmers to focus more of their efforts on implementing the actual functions. PLC programming should also move in this direction; more people should be able to work on the application-level design. Those standard functional blocks should not be developed repeatedly by different individuals. Although various PLC manufacturers have created numerous program libraries, industrial control systems come in many different forms, and each industry should have its own set of program libraries. The reusability of code is key to evaluating the quality of these functional blocks. 3. Advancement of design concepts: In circuit diagram design, we have already been using a drawing approach tailored for control objects. After establishing the basic main distribution circuits and PLC configurations, we create circuit diagrams for each individual field control object, such as motors, valves, cylinders, etc. These objects draw their power from the main distribution circuits, while their control and feedback functions are connected to the PLC. The hardware interconnections are adjusted according to actual conditions. Each control object is organized and arranged in a manner similar to building blocks. Similarly, programming can be done by using appropriate standard control blocks for each control object, thereby effectively encapsulating the details of program control and making the program appear simpler and easier to maintain. A good design enables a strong connection between the schematic diagrams and the programs, sometimes even achieving a one-to-one relationship; for example, a control object depicted in a schematic diagram can correspond to an FB call in the program, thus enabling true object-oriented programming. Some people may wonder that PLC programming is mostly step-based programming, with each standard block being designed for a specific control object; so how is the control sequence implemented? This requires writing specialized sequential control FB blocks, or using Siemens’ ready-made Graph7 to achieve it, which is not much different from regular programming. Conclusion: In the field of industrial control today, there are many skilled programmers who are proficient in algorithms and have their own programming philosophies. When I discuss standardization with some of them, they recognize its efficiency, but believe that it does not reflect their programming skills. Indeed, as mentioned above, programming at the application level does not require a very high level of skill. But think about it – can a person spend their entire life doing on-site debugging? To demonstrate your value, you can engage in standard programming. I would prefer them to spend some time studying program architecture; in every field, the true masters are those who design system architectures, with programming tricks serving merely to enhance a good architecture. May China’s industrial automation soon move beyond the era of small-scale workshops! System category: PLC/PAC | User category: General discussions | Source: Reposted

Submit a Project

**Looking for Chemical Technology, Equipment & Solutions?** No Registration Required Broader Platform Exposure | Global Chemical Service Provider Connections

Submit Request — Free Consultation

Disclaimer

This is an automated machine translation of the original thread. Some technical terms may have inaccuracies; the original text shall prevail. Click "View Original" at the top right to access the source page, which supports IP-based automatic real-time language translation. Please watch out for contact details and sales inducements to prevent fraud. All content and translations are for reference only, representing solely the poster's personal views. For enquiries, email service@hcbbs.com.