Comparing ladder logic and object-oriented programming
Many young people in science, technology, engineering, and math (STEM), in FIRST, and other high school technical programs have used or programmed Raspberry Pi, Arduino, or similar microcontrollers. Most, by default, are learning to program using object-oriented programming (OOP) of some type, and thus gravitate towards it when they move into the real world.
However, industrial automation has relied on ladder logic, also called ladder diagram, for nearly 50 years. Programmers with decades of industrial programming experience like it. But they won’t be here forever, and the industry is getting this influx of younger workers who are already familiar with OOP. How can we introduce these new workforce entrants to ladder logic? While OOP has advantages and disadvantages, it’s a good starting point for ladder logic.
OOP has a longer history than ladder logic, appearing in the early 1960s or before. Current hardware and software available in the "maker" world is adding to its popularity. Leading microcontrollers, such as Arduino and Raspberry Pi, are examples (Figure 1).
Arduino microcontroller hardware typically uses a basic form of C programming. Raspberry Pi is a Linux-based system with many OOP programming platforms such as Python and Java. These low-cost microcontrollers and Microsoft’s .NET platform have made OOP available to those who want to learn it.
Compared to ladder logic, OOP’s advantages include:
- Code is portable and easy to reuse
- Is easy to work with math, loops, etc.
- Is taught in nearly every computer programming course
- Code can be run on various hardware platforms.
OOP uses objects; it’s necessary to understand the concept of objects and their use to grasp OOP. Once an object or modular class has been written, it is easy to reuse and call it multiple times. For example, when an object is created to control a motor—it takes care of all inputs, outputs, and faults. This single object can be used multiple times as instances when several motors are need to be controlled. It’s called when needed and creates an instance when used.
Each instance, for each motor, has its own characteristics such as motor off, motor running, motor RPM, motor overload, etc. Much of the programming work is done when the object is first created. It’s a different way of thinking than ladder logic, and it is more powerful because once an object is built, it’s easy to use and reuse.
OOP also makes it much easier to do complex math, loop computations, arrays, and nested subroutines. It is also taught in nearly every computer programming course in high school, college, and online. The code created is portable and can be run on various hardware platforms.
Compared to ladder logic, OOP drawbacks include:
- Higher overhead
- Steeper learning curve
- Not as easy to troubleshoot for maintenance personnel
- Usually must be compiled so source code can be uploaded to the processor.
OOP can have higher overhead than ladder logic because it tends to need more memory and processing power. The learning curve for an OOP programming language can be longer. Significant time, practice, testing and application, likely in a classroom, may be needed to grasp core concepts.
OOP must often be studied by a programmer to follow the code with a tracer or debugger to follow the logic. With this type of high-level programming, it can be difficult to perform monitoring in real-time. The source code must be compiled before it is downloaded to the controller. Often, the source code is not present in the processor memory. This means the source code must be carefully backed up because the compiled code usually is not editable.
With OOP, libraries need to be connected to other resources used during program compiling. If the connections and resources are not understood, it will be difficult to get the program to run.
Learning ladder logic
Ladder logic is a very simplistic and self-documenting method of coding, and some even debate whether it is even a programming language. It follows the format of ladder-type electrical diagrams used in relay-based control systems, and most people can learn it quickly. It was the only industrial-strength programming language for decades in the machine automation arena and it’s still the main programming language used in the automation industry (Figure 2).
Over time, as different people enter the industry from different areas and backgrounds, different languages have been added to the industrial automation toolbox. These include function block programming, structured text, state programming and sequential function chart. These four languages and ladder logic constitute the IEC 61131-3 standard for programming languages from the International Electrotechnical Commission (IEC).
The idea behind IEC 61131 is if each vendor follows the standard, at least to some extent, a person can learn the five languages once and easily move among different platforms from different vendors. However, the reality is different.
Basic ladder logic—such as the use of relay contacts and coils-is the same, but the syntax and the user experience when programming, and the details of how to use the programming platform, must be learned for each vendor.
Despite this lack of standardization, ladder logic advantages over OOP include:
- Well suited for both machine and process control
- Easy to understand because it’s naturally self-documenting
- Easy to use for troubleshooting the controlled system
- Easy to debug
- Source code that can often be stored in the processor.
Ladder logic is well suited for machine and process control, particularly for automating systems with lots of discrete input/output (I/O). Ladder logic has been modified over the years to also handle analog I/O, making it a good fit for many process control applications, which tend to have a higher proportion of analog I/O as compared to machine control applications.
A wide range of skilled, technical, and engineering personnel can learn ladder logic quickly because it’s easier to use than OOP. The logic is very systematic and orderly, making it simple to follow and understand due to its self-documenting nature. Each line of code must be true before it turns something on. If there are five motors, there are typically at least five lines of code, providing a high degree of simplification.
Easier to learn
Ladder logic is intuitive for electrical engineers and maintenance personnel. While ladder logic requires a different mindset than OOP, it can be learned quickly, and it takes less time to understand code written by others. It is very clear when the logic is true. Contacts on or off, energized coils, comparing variables, and common math functions are easy to follow-even by someone with limited programming experience (Figure 3).
Its ease-of-use simplifies troubleshooting and debugging as well. It is straightforward and easy to see what is happening when monitoring the logic. No software degree or advanced programming skills are necessary. With ladder logic, it’s easy for maintenance and engineering personnel to follow the flow and understand what is going on. Ladder logic can be thought of as a truth table. If the logic on the left is true, the logic on the right is solved or turned on.
Ladder logic source code and descriptors are often stored in the controller. This can eliminate the frustration a programmer experiences when trying to understand a compiled program without access to the source code, a common issue with OOP.
However, when compared to OOP, ladder logic can:
- Be unfamiliar to computer programmers, information technology (IT), etc.
- Be difficult for math, text, and data handling
- Be dependent on scan time
- Require specialized hardware to execute, such as a programmable logic controller (PLC).
Ladder logic is a symbolic language unfamiliar to computer programmers and IT personnel because it’s not something they typically learn in school. Handling math, text strings, and data in ladder logic can be difficult because ladder logic was not originally conceived to deal with these functions.
Ladder logic is also scan time dependent. Larger programs take longer to scan and solve the logic. With ladder logic, the inputs are read, the logic is scanned, the data table and outputs are updated, the overhead communication is performed, and then the cycle repeats. Functions such as interrupts and other programming techniques can be implemented to ensure certain rungs are executed quicker. Although software-based PLCs with ladder logic can run on a PC, often the hardware, such as a PLC, is matched to the programming software, with both purchased from the same vendor. This ensures compatibility, but hinders switching vendors.
In addition to comparing pros and cons of ladder logic and OOP, users should evaluate the existing use of these languages within the environment where they will be deployed. If the plant or facility has standardized on ladder logic, the use of OOP will be discouraged, even if it’s a better fit for the application. OOP use is growing, however, and it’s expected to co-exist with ladder logic for decades to come. An aspiring automation professional would do well to learn both languages.
KEYWORDS: PLC programming, ladder logic, OOP
- Ladder logic and object-oriented programming
- PLC programming strategies
- Learning ladder logic and OOP will help.
Do your PLC programmers have adequate training?