More answers on PLC projects, programming best practices

In a September 14 webcast on PLC projects and programming best practices, Automation Consulting’s Frank Lamb discussed the history of PLC technology, and how it relates to current best practices when structuring code and developing standards.

By Control Engineering November 6, 2023


Learning Objectives

  • Understand where (types of applications) PLC-based programming can be effective.
  • Learn best practices related to code structuring, decomposition, reuse, encapsulation and abstraction.
  • Observe benefits of standard libraries for logic, motion, safety and communication.
  • See examples of how these methods can help improve quality, lower cost and ease the ability to offer custom functionalities and technologies.

Programmable logic controllers (PLCs) remain a strong choice for many automation applications, and programming software and practices that use standards has huge advantages over other options. Documentation is important, along with change management, and understanding use of standards to more efficiently program for certain applications. This includes the ability to bridge from operational technology (OT) applications to information technology (IT) connections. By using standards, code will be easier to update and optimize from current to future operations.

In a September 14 Control Engineering webcast, “PLC projects, programming best practices,” Frank Lamb, founder of Automation Consulting LLC., discussed the history of PLC technology, and how it relates to current best practices when structuring code and developing standards. Questions submitted by the audience that were not answered during the live event are addressed below.

What are some best practices for complex math calculation, and what is the best way to use ‘sample code’ in your project?

Answer: It depends on the platform. Structured Text is generally best for complex math, as formulas can be entered as written. Some customers or platforms won’t have access to ST editor, especially platform A. Fortunately, platform A has extended math functions.

Can you discuss the use of libraries among different projects?

Answer: Not all platforms have library capability. If not, you can simply save functions and code in a directory and import to a new project. If they do, create a list of resources and add them to the library as new code is created.

Can I use templates when creating a new PLC programming project?

Answer: You should use templates if available, as it will keep code consistent. If you don’t have one, you can create your own from a previously completed project. It’s just sample code and organization.

What are strategies that can be used to best organize and assemble code that makes interpretation understandable by trained technicians?

Answer: Simplify and comment extensively. Use descriptive names, rung and routine comments.

Can you discuss tag naming conventions? Do you use a traditional JSA (Job Safety Analysis) for programming best practices? Example: Each step has a hazard and mitigation.

Answer: Keep tags between 12-30+ characters, use CamelCase, and Hungarian notation only if required. Use only common abbreviations such as HMI, PX, PB etc. Use hyphens only for long tag-names to make more readable.

Do you use a traditional JSA (Job Safety Analysis) for programming best practices? Example: Each step has a hazard and mitigation.

Answer: No. Safety is generally handled in a risk assessment. Safety is generally hardwired by regulation. I avoid addressing safety functions in a PLC unless it is for a SafetyPLC, then I follow the results from the risk assessment, treating the safety code as if it was hardwired (dual-channel). I never write safety code into operational sequences, as it can lead to lawsuits on failure. It is best to leave safety to the component manufacturers.

Would IL look like SOR XIC OTE EOR, or is it a push and pop kind of thing?

Answer: XIC, OTE are “platform A” instructions, SOR and EOR are not. If you write LAD on that platform, double-click the rung and you will see the resulting IL format appear at the top.

Are tags the same as variables?

Answer: Tags are data containers with text names. They may be variables, but could also be made constants or be connected to I/O via aliasing or mapping.

Please describe Camel-case.

Answer: Camel Case has embedded capital letters to make a tag more readable. Example: CylinderExtendedProx, MasterControlRelay.

What is driving the move to structured text?

Answer: Students learn high-level code such as C# or Python in school and become adept at it. Very few schools teach Ladder techniques well.

How will artificial intelligence (AI) and machine learning affect the PLC market?

Answer: AI will eventually learn to write platform specific PLC code as well as it does Python now, even in graphical languages. It can already do Structured Text. It will still always need to be checked by humans, but may save time.

Any comments on how to write a specification for programming practices/style without knowing which platform might be used?

Answer: Stick to definitions of things that apply to all platforms, don’t use brand specific names (like XIC, OTE, AOI above). When describing units of code don’t call the POUs, OBs, etc. Words like subroutine, function, or function block are okay because they are generally understood definitions across platforms.

Is there an industry guide spec for PLC software design that can be incorporated into construction contract specifications, or does a firm have to develop their own?

Answer: Generally you would have to develop your own, but large automotive and other manufacturing company specs look remarkably similar.

Are there other methods to assist programing similar to State Diagrams?

Answer: SFC is basically state diagrams. Ladder or FBD can be made to look that way with proper rung formatting. There is no substitute for reading lots of programs and learning by doing.