Programming

Programming is an essential part of control systems, as it allows engineers and developers to create the instructions and algorithms that control the behavior of the system. In control systems, programming is used to specify the desired behavior of the system and the conditions under which it should operate. Control systems can be programmed using a variety of languages, including C, C++, and Python. The specific language used will depend on the type of control system and the requirements of the application. Programming is used in control systems to: Specify the input and output variables of the system: This includes defining the sensors and actuators that will be used to gather data and perform actions within the system. Define the control algorithms: This involves creating the logic and rules that will be used to make decisions and control the system based on the input and output variables. Test and validate the control system: This involves using simulation and prototyping tools to test the control system and ensure that it behaves as expected. Monitor and maintain the control system: This involves using programming to monitor the performance of the system and make any necessary updates or adjustments to ensure it continues to operate properly. Overall, programming is a critical component of control systems, as it allows engineers and developers to specify and control the behavior of the system in order to achieve the desired outcomes.

Programming Articles

PLC programming dos and don'ts to consider

Programmable logic controller (PLC) programming is often done to resolve an immediate problem, but this can lead to long-term issues, particularly if the original programmer isn't around.

PLC Programming Insights

  • Programmable logic controller (PLC) programmers have a tendency to write code for immediate solutions rather than the long-term, which can be a problem for anyone picking up their work down the road.
  • Some PLC programming don’ts include copy/pasting repetitive logic and using indecipherable tag names with no labeling.
  • Some PLC programming dos include looking for opportunities to reuse useful code and using descriptive tag names so things are easier to find.

Production is down and the client is losing $100k an hour and they’ve called a programmable logic controller (PLC) programmer to fix it as quickly as possible. The programmer fires up a virtual private network (VPN) while looking at last minute flights. An hour later, the programmer is staring at a new (to them) program, thousands of rungs of ladder logic, no tag descriptions, unclear naming conventions, code that’s been copy and pasted a hundred times and it’s all in one, massive routine. This might lead to the programmer wondering what the original programmer was thinking.

Eight dos and five don’ts of PLC programming

The average PLC programmer has a tendency to write code for themselves for the immediate solution. It’s easy to forget about the poor sap that has to maintain this in the future. If we’re not mindful, we may very well become the reason someone is shouting expletives at their screen. Here are some simple tips on how to not be that programmer.

The average programmable logic controller (PLC) programmer has a tendency to write code for themselves for the immediate solution, which can cause long-term headaches. Courtesy: Breen Machine Automation Services

DON’T – Copy and paste repetitive logic. Let’s say there are two coils you want to activate in sequence. A rung to turn on the first one, maybe a delay timer, and then a rung to turn the next one on. Aside from changing the tag name from “CoilOne” to “CoilTwo” the rungs are identical. We all have code like this because usually that’s all it is, just a couple rungs. But what happens when you’ve got 50 coils? Before you go hammering on Ctrl+V…

DO – Look for opportunities to reuse code. Loops are your friend. AOIs, subroutines, and even basic arrays can speed up development time, keep code cleaner, and make future maintenance easier. Logic change? You don’t have to paste 50 fixes, just one small change to your subroutine and you’re done. What? The client wants 50 coils to now be 100? If you did it right you should literally just have to change a tag, “Coil_Count” or whatever, from 50 to 100.

DON’T – Use indecipherable tag names with no labeling
“tmrdelay” – “Timer” and “delay” are redundant. What’s this delay for? Are we using this to flash a light or wait a safe amount of time before lowering a heavy press?
“AB_XGI:I.Data[1]”, Obviously, this is a data structure for some connected device but referencing it like this in your main routine is throwing away an opportunity for self-explanatory code.
“fireRobotMove”, Which robot? Which move? Do I need a fire extinguisher? These tag names aren’t useless, per se, but without context they don’t mean much.

DO – Use descriptive tag names
The name should say what the tag is for. Care should also be taken with formatting. Even “tmrDelay” or “tmr_delay” would be better. Nobody should have to guess at the word separation.

DO – Add descriptions to tags and rungs
A simple buffer routine or alias can turn “AB_XGI:I.Data[1]” into something more useful like “partXPos”. “tmrdelay” could become “tmrDrivesReady.” Even better would be a description on the tag or rung that explains what it’s for.

DO – Use proper spelling. Ever tried to find all the tags dealing with position data and one of them is spelled “poistion”? Yeah…

DON’T – Neglect program structure. Nobody wants to sift through 200 rungs of a routine called “Main” that covers everything from input/output (I/O) to process flow.

DO – Use routines and user-defined data types (UDTs) (or “structs” depending on manufacturer) to stay organized. Simply breaking code up into a few routines called “Camera”, “InputBuffer”, and “Faults” automatically makes things more readable. No sifting through 50 rungs of unrelated logic – if you need camera logic search the “Camera” routine.

DO – UDTs are incredibly useful. They let you group and name data, even in arrays.  For example, if you have a lot of position data coming back from your vision system you could keep it organized by creating a “Position” UDT with “X”, “Y”, and “Z” tags. “point1” with sub tags is far better than “point1X”, “point1Y”, and “point1Z”. Easier to rename, easier to cross reference, easier to stuff in an array and iterate through.

DON’T – Be optimistic:
“This project will only take a few months”
“Client knows exactly what they want”
“Nobody will ever see this but me”
or my personal favorite:
“I’ll remember why I did that.”

DO – Remember Murphy’s Law: “Anything that can go wrong will go wrong.” This point is really to highlight the necessity of all the others. A positive attitude is rarely a bad thing but if nothing ever went wrong we probably wouldn’t have jobs. Things break, plans change, accidents happen.  Scalable, readable, maintainable code is Murphy’s mortal weakness.

The best thing we can do to prepare for an unknown future is be mindful of the above DOs. By using data structures, organization, consistent naming styles, and descriptive comments, we write code that is maintainable and flexible. This makes it easier for every single person who has to look at the project in the future.

Your client will thank you when they need to add a new push button. Your coworkers will thank you for having an easy to follow structure. But in my experience the person you help most is yourself. Because honestly, 50% of the time that I’m complaining about code, it’s my own.

DON’T – Just make it work.

DO – Take the time to do it right. Work smart now to make work easier later.

Breen Machine Automation Services is a CFE Media and Technology content partner.

Programming FAQ

  • What programming languages do control systems use?

    • Structured text (ST): A high-level programming language used in control systems, specifically in PLC (programmable logic controller) systems.
    • Ladder logic (LD): A graphical programming language used in PLC systems, typically used to describe simple control systems.
    • Function block diagram (FBD): A graphical programming language used in PLC systems, typically used to describe more complex control systems. (ST, LD and FBD are among the IEC 61131-3 programming languages.)
    • C: A high-level programming language used in control systems, including DCS (distributed control systems) and SCADA (supervisory control and data acquisition) systems.
    • C++: An object-oriented programming language used in control systems, including DCS and SCADA systems.
    • Python: A high-level programming language used in control systems, including DCS and SCADA systems, due to its versatility and ease of use.
  • What software is used with control systems?

    Control systems use a variety of software, including:

    • Human Machine Interface (HMI) software: A software application used to monitor and control automated processes, typically through a graphical user interface (GUI).
    • Programmable logic controller (PLC) software: A software application used to program and control PLC systems, typically using programming languages for control systems, such as structured text, ladder logic, or function block diagram.
    • Distributed control system (DCS) software: A software application used to monitor and control automated processes in a distributed control system, often through a supervisory control and data acquisition (SCADA) system.
    • Supervisory control and data acquisition (SCADA) software: A software application used to monitor and control automated processes in a SCADA system, typically through a graphical user interface (GUI) or other type of HMI.
    • Computer-aided design (CAD) software: A software application that can be used to help design and model control systems, typically used by control engineers to develop control system designs and schematics.
    • Simulation software: A software application used to simulate control systems, allowing control engineers to test and evaluate control strategies before implementation.
    • Cybersecurity software: A software application used to protect control systems from cyber-attacks. Cybersecurity software can include firewalls, intrusion detection systems and anti-virus software.
  • What skills are required to program a control system?

    Programming a control system requires a combination of technical and soft skills, including:

    • Technical knowledge: A deep understanding of control systems, including control theory, automation and programming languages used in control systems such as Structured Text, Ladder Logic, Function Block Diagram, C, C++ and Python.
    • Analytical skills: The ability to analyze and solve complex control system problems, including understanding control algorithms, process dynamics and control strategies.
    • Attention to detail: The ability to pay close attention to detail, including detecting and correcting errors in control system programming and design.
    • Communication skills: Good communication skills, including the ability to work effectively with team members, stakeholders and customers to understand their requirements and develop control systems that meet their needs.
    • Problem-solving skills: The ability to think creatively and critically to solve complex control system problems, including developing and testing new control algorithms and strategies.
    • Project management skills: The ability to effectively manage control system projects, including defining project requirements, tracking project progress and ensuring that projects are completed on time and within budget.
    • Familiarity with industry regulations: Knowledge of industry regulations and standards related to control systems, including safety and cybersecurity regulations.
  • What is ladder logic?

    Ladder logic is a graphical programming language widely used in the automation industry to program programmable logic controllers (PLCs) and control industrial automated processes. It is based on the electrical ladder diagrams used in control panels and is designed to be easily understood by electrical technicians and engineers who are familiar with relay logic.

    In ladder logic, programs are represented as diagrams that resemble electrical ladder diagrams, with the rungs of the ladder representing individual instructions. The instructions in the ladder logic program control the flow of electrical current, just as in a physical control panel. The instructions can be used to control inputs and outputs, perform mathematical operations, and make decisions based on conditions.

Some FAQ content was compiled with the assistance of ChatGPT. Due to the limitations of AI tools, all content was edited and reviewed by our content team.