If you are a programmer, an OEM, or end user that utilizes IEC 61131-3 compliant PLCs, you
should MUST read this article.
In the 3rd part of my series on the IEC61131-3 programming standard (you can check out the previous part 1 and part 2), I will explain the huge benefits of structured programming and why Function Blocks are the greatest thing ever....seriously they are awesome and worth using in every PLC program.
So what is a Function Block anyway?
A Function Block is an encapsulated piece of reusable code that consists of Input variables, Output variables, Through variables, Internal variables, and Internal logic.
For anyone who is familiar with Object Oriented Programming (OOP), a Function Block is just like a class or an object. In order to actually use a Function Block in your program, you create an instance of the Function Block. An Instance is the term used to describe the allocation of the memory for a particular Function Block. Each unique instance runs the same logic as the others but has its own internal memory.
If I just lost you, don't worry, it's actually not that complicated... and I guarantee it is worth the investment of your time!
Believe it or not, anyone who has programmed a PLC has probably used Function Blocks without knowing it. The workhorse of the PLC, the ubiquitous Timer Block is, in fact, a function block.
It has inputs, outputs, internal variables, and internal logic....and you can create as many as you like (within the memory limits of the PLC).
Typically, you would never think of writing your own Timer Block. You just expect every PLC to have them and you really never need to look inside the block because its functionality is well understood. Since it is already done for you, you can focus on the higher level logic and not on the creation of a low-level timer block. This same concept can be applied to user defined Function Blocks as well. The idea is to encapsulate your low-level code into a Function Block so that you can focus your energy on the higher level control.
Here are the top 5 reasons why you should use Function Blocks:
Encapsulation - Reusable Function Blocks can be used to consolidate duplicate code.
If you frequently find yourself copying and pasting code, you will benefit from encapsulating this code into a reusable function block.
After isolating the duplicated code and encapsulating it into a reusable Function Block, you can replace the original code with instances of your new block.
Maintenance - Code is defined and managed in one spot.
Normally you will create a reusable function block because you plan to use many instances of the block throughout your program. When you need to make changes or enhancements to the block (and you will), these changes are managed in one place and will propagate to all of the instances. This is a HUGE time saver, especially when you have hundreds of instances throughout your program.
Standardization - Create a code library of standard function blocks
You should create modular, reusable Function Blocks for all of your commonly used devices (Valves, Cylinders, Motors, Robots, Cameras, etc.). Standardized Function Blocks will allow you to maintain consistency among all systems in your organization. As long as you put enough thought into the interface and internal logic design of each block, you should be able to modularize the functionality such that the internal code is rarely viewed. This reduces the need for future programmers to fully understand all of the intricacies and internal workings of the block, which reduces development time and ultimately saves money.
Collaboration - multiple programmers seamlessly working on the same project
Function Blocks allow you to structure and modularize your program in such a way that easily lends itself to collaboration. Each programmer can be working on a separate function block. Since the blocks communicate to outside logic via the interface (inputs, outputs, etc.), you just need to know the interface structure of the block in order to use it. Someone can be working on the logic for a low-level function block while another person assembles the low-level blocks into a higher level program. We find this type of collaboration to be crucial for large projects requiring multiple programmers.
Mix & Match Languages - Use the right language for the task at hand
When you organize your code into separate reusable function blocks, you can more easily incorporate different IEC61131-3 languages into your project. We have many customers that prefer to write everything in ladder (which is perfectly fine). However, once in a while there is a need for a function that requires using another language like Structured Text. It may be a complex math routine, a communication block, or may have some complex array indexing, which means ST is the only good option. The simple solution is to encapsulate the Structured Text code into a Function Block that can be called in Ladder. Problem solved!...and the customer still only sees ladder in the main program.
So is there any downside to using Function Blocks?
In my (admittedly biased) opinion, the answer is NO, but feel free to comment below if you have some insight on the matter. I've mentioned before in previous posts that we can typically program an IEC 61131-3 compliant PLC in half the time it takes to program on a noncompliant PLC. Function Blocks are one of the major reasons for this enhanced efficiency. We write code once and then call as many instances of each block as needed. Copying and pasting code is old-school!
So get ready to unleash the power of your PLC!
If you are an OEM, a library of modular, reusable function blocks will speed your development time and allow you to better manage your code base. Or, if you are an end-user, you can provide your standard libraries to your OEMs when purchasing equipment. This will ensure that your standards are properly followed. And if you are an integrator, then you probably already know about the tremendous benefits of using Function Blocks and they are part of every project you deliver.
Learn more about DMC's PLC programming expertise.