Generative AI in Packaging and Processing Equipment: Can PLC Programmers Learn Python?
As part of our semi-occasional series on incorporating AI into your packaging and processing equipment, this column focuses on how realistic it is for your engineers to learn Python, a key skill in your generative AI journey.
I’ve written several columns about the unique opportunity packaging and processing OEMs have to incorporate artificial intelligence into their machines by focusing on small language models (SLMs). The potential benefits are enormous and far-reaching: operators and technicians at CPGs would be able to communicate with your machine in plain language — and potentially multiple languages. Generative AI built into your equipment, perhaps more than anything else, can help CPG customers mitigate labor shortages and skill gaps with their current workforce.
Small language models, unlike the large language models powering ChatGPT and Google’s Gemini, run locally on a PC, such as the ones you’re likely already embedding in your machines, such as an HMI. Some are even small enough to run on a local Raspberry Pi single-board PC.
And in case you missed it, these AI models will run on a machine without an Internet connection, bypassing arguments with customers about maintaining connections from their factory floor to the cloud.
While Big Automation is certainly working out where generative AI fits into crowded product roadmaps, playing around with SLMs on your own could be incredibly rewarding to those who are able to clench a first-mover advantage. But it will take some effort. It’s a totally different world than the deterministic, locked-down world of industrial machine control. The goal of this series is to walk you through different aspects of incorporating AI into your machines.
Customizing and fine-tuning language models requires knowledge of the Python programming language. For this column, I’m going to focus on the question of how easy is it for someone who knows how to program PLCs to learn Python. (In the sidebar at the end of this article, I cover how to actually find and download actual language models to start experimenting.)
If you're familiar with PLC (Programmable Logic Controller) programming, particularly the IEC 61131-3 standardized programming languages (such as Ladder Diagram (LD), Structured Text (ST), Function Block Diagram (FBD), Sequential Function Chart (SFC), and Instruction List (IL)), learning Python can be relatively straightforward. Here’s why:
1. Logic and Control Structures
PLC programming languages, especially Structured Text (ST), have similarities to traditional c programming languages like Python. ST, for example, has constructs like `IF`, `FOR`, `WHILE`, and `CASE`, which are directly comparable to Python’s control structures (`if`, `for`, `while`, etc.).
Familiarity with logical thinking, control flow, and the concept of variables, loops, and conditionals from PLC programming will translate well to Python.
2. Procedural and Object-Oriented Programming Understanding
If you have experience with Function Block Diagram (FBD) or Structured Text (ST), you’re already accustomed to thinking in terms of functions and modular blocks. Python builds on these concepts with both procedural and object-oriented programming (OOP), making it easier for you to grasp Python's structure and design.
3. Syntax Simplicity
Python is known for its simplicity and readability. It uses clear and concise syntax, which is often more straightforward than the syntax used in some PLC programming languages. Python’s syntax avoids the verbosity of languages like C/C++ or Java, making it more accessible for programmers who are not deeply experienced in general-purpose programming.
4. Libraries and Functionality
Python has an extensive library ecosystem that can replicate many PLC functionalities (e.g., data handling, control, automation). The ease of using these libraries makes Python a powerful tool, especially for data analysis, machine learning, and AI, which are increasingly relevant in industrial automation.
5. Error Handling and Debugging
While PLC programming involves specific diagnostic and debugging tools, Python’s error messages and exception handling (`try`, `except`) are generally more descriptive, which can help programmers quickly identify and resolve issues.
6. Community and Documentation
Python has a vast and active community with extensive documentation, tutorials, and forums that make learning the language easier. Many resources are geared towards beginners, offering step-by-step instructions and examples that can benefit someone coming from a PLC programming background.
Challenges you might face
There may be a few road bumps on your way to learning Python:
1. Different Paradigm Adjustments
Transitioning from graphical languages like Ladder Diagram (LD) to a text-based language like Python can be an adjustment. However, since you're likely familiar with Structured Text (ST), this transition should be smoother.
2. Advanced Concepts
While basic Python concepts are easy to grasp, more advanced topics like asynchronous programming, decorators, or Pythonic idioms might take some time to understand if you're accustomed to more linear programming styles in PLCs.
3. Dynamic Variable Typing
Python is dynamically typed, which can be a shift if you’re used to more strictly typed languages or environments in PLC programming. This means variables in Python don’t need to be declared with a specific type and can change type dynamically, which can lead to different types of bugs.
Learning Path for PLC Programmers Transitioning to Python
1. Start with Basics
Learn basic Python syntax and structure (variables, control structures, functions, classes). Many of these concepts will feel familiar if you already know Structured Text (ST).
2. Use Analogies
Draw analogies between Python and the IEC 61131-3 languages you already know. For example think of Python functions as function blocks in FBD or SFC steps as Python functions or classes.
3. Work on Simple Projects
Start with small automation scripts, simple data processing tasks, or even emulate PLC logic in Python to practice.
4. Explore Libraries
Learn Python libraries like numpy, pandas, matplotlib for data manipulation and visualization, and pyserial or pymodbus for interfacing with industrial hardware. These libraries will demonstrate Python’s power in areas beyond traditional PLC programming.
5. Understand Python’s Ecosystem for Industrial Automation
Explore Python-based frameworks for automation, such as pyScada, which might provide some familiarity with industrial automation concepts but using Python.
Conclusion
For someone already experienced in PLC programming, especially with knowledge of IEC 61131-3 languages like Structured Text, learning Python is generally straightforward. The core concepts of logic, control structures, and modular programming are already ingrained in your experience, and Python’s simplicity and extensive resources will make the learning curve manageable.
And worst case, if you’re stuck, ask ChatGPT, Gemini, Claude or your favorite large language Model-based AI. It can save hours of effort. In fact, all of the enumerated lists in this article were written by ChatGPT and verified/validated by me for sense. (Normally I take pride in writing my own articles but what better source to find out about AI programming than an AI itself. My promise is to always disclose who wrote what!)
In future columns I’ll focus on how to gather relevant content and data to train your model, normalize that content/data, annotate it, and other hands-on steps. We’ll also cover customizing and fine-tuning the model to actually start making progress with incorporating AI into your equipment. I'll also write about a couple of more off-the-shelf tools that could simplify much of your generative AI journey.
OEM Magazine is pleased to inaugurate this semi-occasional column tracking the rapid advances in AI and how packaging and processing machine builders can leverage them to build next-generation equipment. Reach out to Dave at [email protected] and let him know what you think or what you’re working on when it comes to AI.
There are a handful of websites where you can download these models, but the two big ones are Hugging Face and Ollama. There are many more but they tend to be specialized for certain models, applications, or eco-systems that aren’t as pertinent to packaging and processing OEMs.
Let’s begin with Hugging Face, which many consider to be one of the most popular platforms for hosting and sharing machine learning models, especially in natural language processing (NLP). It provides an extensive library of pre-trained models, datasets, and tools for model training, evaluation, and deployment. You’ll find all of the models we researched for our list on Hugging Face. The site has a strong community via its user forums, and has extensive documentation and tutorials.
Ollama is similar to Hugging Face in that you can download multiple models, though the selection is not as good as Hugging Face. (It only has about half of the models on our list.) That’s because Ollama is more about ease of use and running models on a local machine. In fact, it provides for streamlined processes to get models up and running quickly, with easy commands to download and use models. Similar to Docker, Ollama uses a command-line interface (CLI) to pull, run, and manage models, making it user-friendly for developers comfortable with CLI tools.