CODESYS PLC Programming: Tips for Maximizing Performance
Enhance the PLC code efficiency within CODESYS.Hello and welcome back to another article from RealPars, the world’s largest online learning platform for cutting-edge industrial technologies. In this article, you will learn about CODESYS PLC programming and how to optimize your PLC code for performance.
Before we talk about how to optimize your PLC code, let’s quickly define what optimization is in the context of PLC programming.
Introduction to optimization
In this article, you will see how we can refactor, or update, a CODESYS PLC application to optimize it for performance.
If you want to learn more about programming PLCs using CODESYS, check out the course CODESYS 1 – Introduction to PLC Programming.
In PLC programming, performance optimization usually aims to lower PLC cycle times and reduce the memory usage of the application.
Low cycle times are important because we want to control processes at near real-time. High cycle times may lead to the loss of control of a process which can create dangerous situations.
Low memory usage is important because PLCs have limited memory resources. Unlike computers which have a lot of memory available, a typical PLC, like the WAGO CC100, has only 32 megabytes available to store an application.
Now that we know what optimization involves, let’s look at the application that we will optimize in this article.
Application introduction
In this article, we will optimize a CODESYS application that monitors a temperature and controls a fan.
When the temperature reaches an upper limit, the fan turns on, and when the temperature reaches a lower limit, the fan turns off.
This sounds like a simple application but it will highlight how many opportunities there are to optimize even the easiest of applications.
Let’s take a look at the current code and see how we can optimize it.
Optimization
Optimization starts with an analysis of the current code.
The aim here is to identify parts of the code that we can refactor to simplify calculations, consolidate logic, and improve memory usage by reducing the number of variables used.
Streamlining complex calculations
By simplifying calculations and removing duplicate logic from an application, we can significantly reduce the cycle time of the application.
To start, we can see that there is a very complex calculation spread across three rungs.
Since the calculation, which converts the temperature in Celsius to Fahrenheit is spread out, it uses two intermediate variables rTempInC1x9 and rTempInC1x9Div5 to store intermediate values for the calculation.
This is a waste of memory and can be optimized by placing the mathematical operators in series.
This is easier to do in Function Block Diagram, so I will create a new POU in FBD to replace the existing one.
In this new POU, I update the logic as shown here, as you can see we have eliminated several rungs from the application and a number of intermediate variables.
Looking at the new program, we can see another chance to optimize it. Why should we scale the analog input to a value in degrees Celsius and then convert that value to degrees Fahrenheit?
Instead, we can scale the analog input directly to a value in degrees Fahrenheit and eliminate 3 instructions from our program to reduce the cycle time.
Removing duplicate calculations
Switching back to the original program, we can see that the same calculation is done twice. The temperature is calculated on rungs 1 – 4 and again on rung 6.
By removing this duplicate calculation, we can eliminate another instruction and intermediate variable from our program.
Finally, when determining if the fan should run, our original program uses two intermediate variables to control the fan.
Once again, if we place our instructions in series, we can eliminate two intermediate variables from our program as shown here.
The end result is a program that is optimized for performance and is, in my opinion, easier to understand, debug, and maintain.
In this example, we optimized the program for performance by re-writing the logic in a more suitable language, optimizing calculations, and eliminating redundant variables.
In other applications, you may also optimize performance using more advanced methods like by re-organizing the POUs in the application, reducing communication overhead, or reducing data processing.
Now that we are done with our optimization, we can test and benchmark the results.
Test and benchmark
After optimizing an application, you should check how much of an impact your changes have made to understand the value of the optimization and if the performance targets were reached.
The first thing to check is the reduction in memory use. You can see the memory use of an application when you build the application. to do this, click on Build > Generate Code and check the memory statistics in the Messages pane as shown here.
You can also check the reduction in cycle time by downloading the project to a PLC and opening the Monitor tab of the Task Configuration object.
This tab gives some statistics about the cycle time of the PLC including the last cycle time, average cycle time, max cycle time, and min cycle time.
Conclusion
In this article, you learned how to analyze an application to identify opportunities for performance optimization, implement them, and test the effectiveness of the optimization.
Optimization is very useful in high-speed or memory-critical applications where scan time and memory usage are really important, however, extreme optimization can lead to code that is more complex and harder to follow.
When you are optimizing your PLC applications, you should always think about the trade-off between performance and complexity. In general, new programmers should focus on writing code that is easy to understand and maintain over code that is completely optimized for performance.
Ken Bourke
Automation Engineer
Posted on Sep 4th, 2023
Ken Bourke
Automation Engineer
Posted on Sep 4th, 2023
If you would like to learn more about effective PLC programming with CODESYS, check out our course CODESYS 1 – Introduction to PLC Programming
5 Actionable Tips for Getting a PLC Programming Job with NO Experience
In this blog post, you’ll learn about the mindset that helped me getting a PLC programming job with NO experience. This is my personal experience as someone who searched for a job in this field and as an employer who reviews resumes and interviews candidates for a variety of projects. So let’s get started!
Ladder Logic Debugging: Solving Problems in PLC Programs
Hello and welcome back to another article from RealPars, the world's largest online learning platform for cutting-edge industrial technologies. In today’s article, I will show you how to debug common problems that you will encounter while programming, testing, and...
Trouble-Free PROFIBUS Network: Tips to Ensure Robust Infrastructure
Today, we're diving deeper into making your PROFIBUS DP network run without hitches. Have you ever felt frustrated because your Wi-Fi was slow? Now, magnify that frustration for large industries. But fear not! With these 5 tips, you'll be a step ahead in preventing...
Become a pro member
Pro includes access to all the courses on the RealPars learning platform as well as the new lessons that are added weekly.
Yearly Pro Membership
Save 30% Compared to the Monthly Plan
One-Time Yearly Payment of €300
Exclusive Access to the Pro course library
Unlimited Support from Proven PLC Programming Experts
Earn Certifications of Completion
Stay Up-To-Date with New Regularly Released Course
Monthly Pro Membership
Hassle-Free Monthly Payment of €35
Exclusive Access to the Pro course library
Unlimited Support from Proven PLC Programming Experts
Earn Certifications of Completion
Stay Up-To-Date with New Regularly Released Courses
Learn at Your Own Pace
RealPars is the world's largest online learning platform for cutting-edge industrial technologies.
Questions?
[email protected]
+31 10 316 6400
Mon - Fri 8:30 am to 5:30 pm (CET)
Rotterdam Science Tower,
Marconistraat 16,
3029AK Rotterdam, The Netherlands
COMPANY
About
Service
Partners
Terms of Service
Privacy
Cookies
LEARN
Courses
Blog
Sign in
HELP
Help & Support
Refund & Cancellation Policy
© 2023 RealPars B.V. All rights reserved.
Created with coffee and tea in Rotterdam.