Basic PLC Coding Primer

Basic PLC Coding Primer


 


 


                     Basic Customized PLC Coding Applications

                                    Written by Herbert Tonello



                                                        Foreword

When visiting customer locations, no doubt we have all been confronted with questions on a new control applications our customers wanted to implement. In the past, customers developing Programmable Logic Controller (PLC) code and its related visualization on OEM (Original Equipment Manufacturer) machines was a time-consuming process requiring technical support and machine downtime.

With today’s advances in software PLC’s, graphical PLC programming software, Human Machine Interface (HMI) development software, and visualization software packages, these new control application development times have dramatically decreased while the complexity has been minimized to the point where our customers can develop many applications on their own. The use of offline emulators and coding/debugging tools has helped to minimize development time and deployment issues even further.

This posting is intended to help our customer’s begin developing basic PLC application code, debug their code, and implement software changes. Please note that visualization linking of PLC code is beyond the scope of this posting and will be addressed in a future automation blog. This blog will conveniently show the programming, debugging, and implementation behind one of the more useful control coding pieces of logic that our customers use, the PID.

PID stands for Proportional-Integral-Derivative control, which uses mathematics to control a process action. The PID is used in most industrial control applications at some point where closed-loop control is needed. In this example, I will be using IEC-1131-3 coding language, specifically ST (Structured Text), FBD (Function Block Diagram), and the LD (Ladder Diagram) language which is the most popular language in the USA. The IEC-1131-3 standard dictates that the five major PLC Programming language (ST/LD/SFC/FBD/IL) should be similar across all PLC manufacturers. In this example, I am using a popular software-based PLC program compiler using the fore-mentioned ST, FBD, and LD languages. It should be mentioned that not all code can be converted between each other’s different languages and symbology.


 



Customizing your own PLC Code and the Problem with Standard Libraries and Function Blocks


Standard variable libraries and standard function blocks from manufactures often are not suitable for customized needs of our customer’s applications. Fortunately, most PLC manufactures know this and support User Defined PLC variable, Libraries, and Function Blocks.

In this example we will be developing separate function blocks for the PID’s Integration and Derivative values using a combination of math operators and algorithms. The derivative function block in this example can be used specifically on its own, for example, to detect a rapid rate-of-change like oil flow data during a break in an oil transfer pipe. This is what makes the PID and its components so powerful in automation programming. 

Once the Integral and Derivative Function Block are created, we will create the whole PID in the FBD language.


Here we have an INTEGRAL FB written in ST. The variables are declared in the upper right and the code is developed in the bottom right. With today’s compilers, you will usually be told exactly where the problem lies in your code when having compiler errors. Always make comments in your code (in green above) so others can troubleshoot in the future.


 


 

Here we have an INTEGRAL Function Block written in FBD. Notice the nice graphical example of this language, similar to the LD (Ladder Diagram) language that we know so well. Also notice that I am using the ST logic I developed earlier (the INTEGRAL_EXAMPLE FB) within this FBD.




Here we have a handy DERIVATIVE Function Block Written in ST. Notice the TIME_TO_REAL operator in the body. Watch that your data types match when they need to (math functions, etc.).

 


 

                                                           THE PID CODE


 



Variables used in the PID. This is the declaration section. You need to declare variables so the Computer/PLC/Visualization know what is what. This is a little trickier than it seems as there are Global Variables (used throughout an entire project) and Local Variables (used within one part of the code only). It is also possible that local variables with the same name are used in multiple programs within the code


 

Here is a PID written in FBD. Most manufacturers will give you a single neat Function Block, but they may lock it down so you cannot customize. I made some comments in each rung in this case to help future debugging.

(This is an example for illustrative purposes only and usually you want to do this with less rungs).


 


 


 


 


                                                          Debugging Your Code


 


With today’s compilers troubleshooting is made easy. Often one can use emulator software to debug offline, or use Watch Windows, Break Points, Step Overs, in the code to troubleshoot online. Simply going online with a compiled PLC program after downloading and watching the variable values will help find an issue.

But what if your code doesn’t even compile and has many errors?

It is best to not get to that point, so compiling the project often and saving the project avoids hundreds of errors after you are finally finished with an application. Often, the many compile errors are just due to a cascading effect where the one error causes many other errors. Always go to the first error and troubleshoot down.


Notice the message window in the bottom right of the image above. This is the error message window telling me that in Line 7 of the variable declaration section I made an error. I can go right to this line and find the problem. Notice I have two errors. The second error is caused by the first error!




I had a syntax error in Line 7 of the Declaration section of some LD (Ladder Diagram) language code; I fixed it, and recompiled. Problem fixed.


 


 


                                                           Further Debug Techniques


 


Many PLC manufacturers use something called TRACE or SCOPE to track down a problem with a physical input to a PLC, but these tools can also be used with software Input/Outputs. Your PLC program executes within a few hundred milliseconds usually, so catching a bad bit or transition is nearly impossible with the naked eye.


Let’s say for an example you have an output turning itself off for no reason, but you can’t see the issue because it happens so fast. Let’s say for an example you have (3) inputs that need to be satisfied to operate that output. One of these inputs has electrical bounce. You can simply write into your PLC code using any of the IEC-1131-3 languages:

 

              Input1 BOOLEAN FALSE> SET OUT1_Debug

              Input 2 BOOLEAN FALSE> SET OUT2_Debug

              Input 3 BOOLEAN FALSE> SET OUT3_Debug


This will set the faulty OUT Debug bit telling you which input has your problem.


 


------------------------------------------------------


A problem I have with the ST Language is that some parts of the code, you can not tell if the program is actually executing. The ST language is written like Pascal/C++ or any other high-level text-based computer program, so often you do not even know if the program is executing during initial debugging.

A handy tool I find useful is to simply write in the variable declaration part of the program I am having issue with:


      X := X+1;


Here, we have an assignment for the variable x to increase in value by 1 every PLC scan. If you go online with the project, you will see x increase in value if the code is actually executing.


 



                                                                   Conclusion


The automation and IOT worlds are increasing their influence over society exponentially, leading to OEMs and their customers to constantly upgrade/install and modify their factory equipment. Hopefully, basic but useful information blogs by automation professionals around the world on PLC programming continues to be posted on the internet. A great resource for this information is YouTube, LinkedIn, and other social media-sites where people from all walks of life and all levels of experience can gain knowledge and insight into the world of automation.


 


 


 

要查看或添加评论,请登录

Herbert Tonello的更多文章

  • PID Control Explained

    PID Control Explained

    Introduction to PID Control Foreword: Every electrical/electronic control system uses some form of feedback to govern…

  • Gate and Trigger Signaling in Automation

    Gate and Trigger Signaling in Automation

    Gate and Trigger Signaling Explained Herbert Tonello Foreword: One of the challenges in any machine design is the gate…

社区洞察

其他会员也浏览了