Which IEC 61131-3 Programming Language is best? Part 2
With many programmable logic controller (PLC) programming languages and standards from which to choose, select the best language for specific automation and controls applications. Part 2 focuses on function block diagram (FBD), continuous function chart (CFC) and structured text (ST).
- Function block diagram (FBD) is a graphical language which appeals to engineers’ affinity for graphical languages.
- Continuous function chart (CFC) programming, a super-set of FBD, is a similar but more flexible extension to IEC 61131-3.
- Structured text (ST) is based on the PASCAL programming language and has enough similarities to VB to make use of the more widely available VB tutorials.
Many programming language choices exist for programmable logic controllers (PLCs), and the IEC 61131-3 Programming Languages standard has several options. It’s more efficient and effective to use the strength of each rather than learn the tricks and workarounds required to force-fit one language to all applications. The programmer should select the best language for the application.
Programming languages: LD, SFC, FBD, CFC and ST
“Which IEC 61131-3 Programming Language is best? Part 1” discussed the strengths and best applications for each language, has a graphic showing all five languages, and provides details on ladder diagram (LD) and sequential function chart (SFC).
“Which IEC 61131-3 Programming Language is best? Part 2” (this article) discusses function block diagram (FBD), continuous function chart (CFC), structured text (ST) and how they can be mixed and matched for optimal results.
Function block diagram (FBD) and continuous function chart (CFC)
Function Block Diagram (FBD) is a graphical language which appeals to engineers’ affinity for graphical information. The Codesys integrated development environment (IDE), from Codesys GmbH (previously called 3S-Smart Software Solutions), offers two flavors of FBD, the traditional IEC 61131-3 FBD and the similar but more flexible continuous function chart (CFC) which is not formally part of IEC 61131-3. CFC is a super-set of FBD and has two areas of strength:
- Block-based functional programming (performing Boolean and mathematical operations)
- Hierarchical designs (calling other functions and function blocks).
Block-based programming with CFC
Block-based designs consist of assemblies of pre-defined functionality which are wired together to carryout Boolean logic, mathematical calculations, or a combination of the two as shown in Figure 1. CFC block programming is excellent when the outputs are strictly a function of the current inputs (in other words, when there are no state variables such as Boolean feedback, set/reset coils, or flags). When there is a combination of state-less and state-based functionality, use a combination of CFC and SFC.
Because connectivity is represented with lines between the function blocks, it is easy to understand the flow of information and how the information is modified along its path. If the functionality begins to become complex, it should be split into smaller, more manageable sub-blocks as described in hierarchical designs below. If there are a significant number of connections between blocks, the lines can be consolidated into pins that are declared as a data structure type as shown in Figure 2.
Three advantages of structured pins
Structure pins have three advantages:
- They remove unnecessary detail from this level of the design hierarchy so that the forest isn’t lost in the trees (to paraphrase the saying).
- Keeps the block size small so that the overall flow of information can be viewed on one page
- Allows information to flow upstream and downstream (to further reduce the unnecessary detail).
Hierarchical designs with CFC
Hierarchical design is the practice of creating a design from building blocks, which are built from simpler building block, which are built from simpler building blocks and so on. Small building blocks have many advantages over the traditional large flat designs, including being easier to:
- Test and perfect
- Understand and maintain
- Be much more likely to be reused.
The building block technique is easier to design and understand because it enables and encourages the practice of maintaining a consistent level of detail at each level of the hierarchy. In this way, upper levels of the design are not cluttered with confusing and unnecessary detail that is important at the lower levels of the design. The analogy is an automobile engine which has a starter, which has an armature, which is wound with copper wire, which is extruded from copper, which is mined throughout the world. The automobile designer doesn’t need to be an expert or be concerned with how the copper in the engine is mined. This level of detail is left to the lowest levels of the design, where that detail is important. Hierarchical CFC designs should be designed with a consistent level of detail on each level of the hierarchy.
Two downfalls of using LD as a building-block language
This reveals the downfall of the traditional method of using LD as a building-block language. Specifically:
- The low density of information requires LD designs to be spread-out over dozens of pages, making it difficult or impossible to see and understand the big picture.
- High-level details and low-level details are intermingled on the same level leading to clutter, which makes the design difficult to understand and maintain.
Hierarchical design in CFC using structure-pins solves both of these problems, while maintaining the graphical nature of LD. It’s a no-compromise solution with the best of both worlds.
Benefits of using CFC and hierarchical design
Hierarchical design is a key feature of object-oriented industrial programming (OOIP). Figure 3 shows an OOIP design of a control system for a racing sailboat. The top-level of the design shows the boat control consists of inputs and outputs (I/O), services, winches, cylinders, and hydraulics. Double-clicking any of those blocks reveals the next level of detail such as the overview of the cylinders and the hydraulic system. Further double-clicking on any blocks in those reveal progressively further levels of details, each implemented in the best language for the job. Notice the careful attention to keeping the CFCs one page and of consistent levels of detail so the functionality is easily understood at each level. These objects are then configured with a .CSV file as shown in the OOIP article and video referenced at the end of this article.
With the ability to place and configure user-defined blocks, CFC programming begins to closely resemble programming/configuring for a distributed control system (DCS), except with IEC 61131-3, the programmer is in control of the underlying blocks. If a block needs a minor enhancement, the programmer has the power to do this without the expense and time required to involve the DCS vendor.
A complete hierarchical PLC design can be thought of as a tree with hierarchical CFC making up the trunk and branches (the block diagram); and block-based CFC, SFC, LD, and ST filling in the detailed functional design at the leaves of the tree. Together, the IEC61131-3 language forms a team which is more powerful than any of its individual parts.
What operations should be programmed with structured text?
While LD, SFC, and FBD/CFC are great tools for their purposes, there are some operations that do not lend themselves to graphical languages.
Some operations that favor use of ST programming include:
- Iterative operations such as For, While, and Repeat
- Complex conditionals and selections such as IF-THEN, and CASE
- Bit manipulations such as bitwise-AND, Shift, Rotate
- Text string manipulation
- State machines with unusual requirements or complexity
- Pure object-oriented programming (calling methods, setting properties, etc.)
There are many good tutorials available for engineers transitioning from other text-based languages to ST. A Visual Basic tutorial is a good starting point for those completely new to text-based languages. While structured text is based on the Pascal programming language, it has enough similarities to VB to make use of the more widely available VB tutorials.
Know programming vocabulary for best results
To be able to communicate effectively in the English language it’s important to have the right vocabulary and to know how to use that vocabulary effectively. Similarly, to be able to create effective industrial controls programs, it is important to have the right languages and know how to use those languages effectively. IEC 61131-3 provides the languages, and this article has provided guidance on their effective use. Use the tips on LD, SFC, FBD, CFC and ST in this two-part series to go forth and program effectively with IEC 61131-3 Programming Languages.
KEYWORDS: IEC 61131-3 Programming Languages, FBD, CFC and ST
Use the right IEC 61131-3 programming language for the application.
For more, see:
More programming downloads from Object Oriented Industrial Programming Foundation.
Codesys integrated development environment (IDE) from Codesys GmbH, used for examples in this article can be downloaded at no charge, including a software-based PLC that will run for 2 hours between resets.
For a Control Engineering article with programmable logic controller programming instructions, terms, logic circuits and more, see “Support-focused enterprise controls: PLC Basics.”
Author biography: Who is Gary Pratt?
Gary L. Pratt, P.E. is president of ControlSphere Engineering. Pratt’s career began with Chevron Corporate Engineering in 1982 and recently retired as the president of the Codesys Corp. of North America. He holds patents in industrial controls and now concentrates on sharing his knowledge and experience with the next generation through IEC 61131-3 Programming Languages and Codesys integrated development environment (IDE) consulting and training classes.