Ladder logic: Strengths, weaknesses
Anyone who has ever worked on a project knows that one of the differences between an amateur and a professional is using the right tool for the job. This holds true whether repairing a faucet or programming a measurement and control system. Recognizing this need, software vendors often allow multiple programming methods to be used with their software. Tips, diagrams follow.
Anyone who’s ever worked on a project knows that one of the differences between an amateur and a professional is using the right tool for the job. This holds true whether repairing a faucet or programming a measurement and control system. Recognizing this need, software vendors often allow multiple programming methods to be used with their software. These fundamentally different models of computation let programmers select the most appropriate tool for their tasks. The five most popular programming methods for control and measurement software are: ladder diagram, textual, function block/data flow, state chart, and simulation and modeling.
The ladder diagram evolved in the 1960s when the automotive industry needed a more flexible and self-documenting alternative to physical relay and timer cabinets. Later, a microprocessor-based system was introduced, and the software for the system was designed to mirror the electrical diagrams used to build the relay control panels. This software, called ladder logic or a ladder diagram, looks and executes similarly to an electrical diagram.
The illustration of “One rung of ladder logic” shows that the left rail is the power bus and the right rail is the ground bus. Power flows through a series of normally open or normally closed contacts and powers a coil. Open contacts block power and closed contacts pass power. In ladder logic, each contact and coil is linked to a Boolean memory location. Series contacts act like an “and” and parallel contacts act like an “or.” Execution is left to right and top to bottom.
A rung of ladder logic mirrors electrical circuits.
Inherent to ladder logic is an unseen scanning engine and memory management stack. Physical inputs are read and stored in a memory table. The ladder logic is run by reading and writing from the memory table for inputs, outputs, and intermediate values. At the end of the logic cycle, all the physical outputs are updated with the values from the memory table. (Many ladder representations can perform immediate reads and writes, rather than wait until the end of the execution cycle.)
To handle more complex tasks, such as math, timer functionality, counters, or PID, all ladder representations today have the ability to call functions on the ladder rung. These functions typically have an input terminal to enable the function execution (EN) and an output terminal when the function has finished successful execution (ENO). Functions may be dropped onto a ladder diagram, and they will execute according to ladder diagram power flow.
Strengths of ladder logic
Ladder diagram has a number of strengths.
Intuitive and self-documenting. A ladder diagram features an excellent graphical representation based on well-understood circuit design concepts. Because of the familiarity of the environment, the learning curve to get started with a ladder diagram is very low; basic programming skills develop quickly. This has made the ladder diagram particularly popular for applications that need to be debugged or maintained by personnel without software training, including some electricians or plant technicians.
Excellent debugging tools. Modern online debugging tools available in a ladder diagram provide an animation showing live “power flow.” This makes it very easy to understand the diagram logic and to debug faults.
Efficient representation for discrete logic. Because a ladder diagram is designed to mirror electrical circuits, it naturally is a good way to represent discrete logic. For digital logic, a ladder diagram is almost intuitive.
Weaknesses of ladder logic
The PLC scanning architecture reads inputs into memory, executes ladder logic, and writes outputs from memory.
Hierarchical data and logic encapsulation. One limitation of a pure ladder diagram (without functions) is the inability to encapsulate code for reuse. If you only programmed in a ladder diagram, programs would be unmanageably long, which makes coding, debugging, and editing very difficult. Because hierarchical programming is a fundamental requirement of modern software design, almost every major ladder diagram package today includes functions and function blocks that can be called from the ladder rung. However, many ladder programs support a limited number of subroutines or program blocks, which can make it difficult to break a large program into manageable parts.
Poor data structure. In general, ladder diagrams address memory in single memory bits or registers and can read and write to variables anywhere in the program. This makes it difficult to protect data or to group data as a structure. Because data addressing is very low level, it is easy to accidentally access the wrong registers, causing unexpected behavior. This can also make it difficult to protect internal information and introduces the risk that internal data can be modified by faulty code elsewhere in the program. The use of named variables helps, but even when using variables, it is possible to create variables with overlapping memory locations. Most good editors have tools that show where memory registers are read and written within the program and check for conflicts.
Limited execution control. In a ladder diagram, the ladder rungs are executed in left-to-right top-to-bottom fashion, and timing is defined by the speed at which the programmable logic controller (PLC) can scan and execute. This easy-to-understand model works well for a large number of applications that primarily use discrete logic. However, it makes building multi-rate applications difficult. To change the execution structure, you can use “jump” commands to move to different parts of the diagram. But because the overall response time depends on the length and complexity of the ladder program, this can cause problems while designing systems that need to respond with particular timing. It can take time and effort to arrange the scheduling of different sections of code. This becomes even more problematic when performing analog or process control. Analog control, such as PID, depends on having consistent execution timing. When sections of code are swapped in and out, the cycle time from one execution time to the next varies and can cause problems with analog-based control.
Arithmetic operations. Traditionally, ladder logic has dealt only with discrete values and counter/timers, but the ladder diagram today also supports mathematical operations using function blocks. However, in ladder, unlike in function block editors, the function block inputs and outputs are not wired together. Instead, they are referenced to memory locations. For simple operations this is sufficient, but complex algorithms involving large numbers of variables and intermediate results can be very cumbersome to program, document, debug, and edit.
Programming methods: Ladder logic and other models of computation
Since the advent of the first software-based control and measurement systems in the 1960s, there have been numerous programming languages and tools introduced for engineers to program systems more efficiently. Selecting and using the right “tool” can make measurement and control systems faster to design, more efficient to operate, and easier to maintain and debug.
Researchers at the University of California at Berkeley who study real-time and embedded development call the different software representations “models of computation.” A model of computation is an academic term that refers to a particular way of representing the behavior of a system, such as a finite-state machine or a dataflow representation.
“Practically speaking, the models of computation concept can be used in a broad fashion to span multiple ways of representing and programming an industrial system. To frame this discussion, consider a model of computation as containing aesthetic properties and execution properties, such as how it looks and how it runs,” says Todd Walter, manager of the Industrial Measurement and Control Group at National Instruments.
Different programming models are valuable to engineers approaching a design from different perspectives, says Walter. For example, process control engineers benefit from a programming representation that can demonstrate concurrent operations, such as simultaneous PID loops, batch sequencing, operator interface, alarming, and so on. Likewise, an engineer designing a machine control system benefits from a programming model that handles discrete logic, sequential operations, and state transitions, she says.
The two designers may use the same hardware, but different programming tools, to design their systems. Each model of computation has benefits and shortcomings and may be more appropriate for one type of task and less so for another.
“The most-used language of the five in North America is ladder logic,” says Walter. “Engineers who work with PLCs and ladder every day develop expertise in ladder diagram programming, and through the employment of good programming practices, ladder is a very functional language. Very complex and full-featured applications may be programmed with ladder diagrams, especially applications that are primarily digital.“
The additional benefit of having code that is readable by electricians and technicians should not be underestimated. Ladder logic can be very effective at describing digital logic in a form that is easy to program and maintain. However, it can be cumbersome for applications involving algorithms, process control, or complex sequencing, says Walter.
Look for the series on programming methods in which engineers from National Instruments explain and compare various models of computation. Part 1, in March 2007, looks at ladder diagram. Future articles cover textual, function block/data flow, state chart, and simulation and modeling.
Todd Walter is the manager of the Industrial Measurement and Control Group at National Instruments in Austin, Texas. He holds a B.S. in mechanical engineering from Virginia Polytechnic Institute and State University in Blacksburg, Virginia.
Programming methods series
Ladder logic is the first in a series exploring different software programming methods how to choose the most appropriate model of computation based on system requirements and architecture. Future articles will cover textual, function block/data flow, state chart, and simulation and modeling methods.