Python to replace PLC

I have seen many discussions about this.

Almost all of the conclusion is No. The python code can not replace PLC due to realtime requirements.

But If the realtime requirements is not critical. If the control can be extended to 1 second or even 2 second. I think it is possible.

Actually I have done a small control system of 1 sec scan time on about 100 in/out signals using python and mysql, using RPI, so far no issue. But I haven't try to create a PID loop which maybe a challenge.

The next scale up is whether I can create a complete python code with DI, DO, AI, AO, VFD, PID, Timers, Ramp all setup to control a process plant, 1 sec scan time, using RPI or industrial PC.

Welcome everyone to discuss and share your experience on it.

Are you saying that python, as a language, can not be used due to realtime requirements?

I am a purely python user, don't know the fundamental structure of python language, but a lot of saying is that compare with C or other language, python does not meet the realtime requirements like the PLC does.

for example, siemens 1200 PLC can scan at rate of 5-10ms, rockwell compactlogix can go at similar rate, but if the program is heavy, it may go up to 100-500ms, that's it.
But for the little project I did using python on RPI to read data, calculation in the code, send data to the DO, it takes around 300ms already.
This is just a simple control system, no batch, no sequences, maybe my coding structure is not ideal, maybe it can be improved to dramatically reduce the scan rate, but I don't think python is efficient enough to handle 5-10ms scan rate logic requirements.

A lot of this is because there is a whole OS 'between' your code and the hardware

Yes, it has been discussed by many people. I like the one below:

My thinking that, compare with the CNC system, which requires ms feedback, the process control system usually does not have strong real time requirements, the python may have better chance to replace PLC. Of cause, the code needs to be well written to protect the site in case, a lot of exceptional handle logic to add in.

The key to this topic is a language's memory management. Scripting languages in general, and java in particular, typically use "garbage collection" to determine when memory can be reclaimed and then recycled. This requires pauses while the language infrastructure examines all of the objects to determine which ones have been discarded. Depending on the language and the garbage collection algorithm, these pauses may be tens or hundreds of milliseconds, or more. Java (and therefore Ignition), when both memory and CPU are plentiful, can be tuned to keep these pauses in the low tens of milliseconds.

Other languages use other techniques to track memory usage and release resources in a more timely fashion--possibly even completely manually (like C). If you can match an application's requirements to a language's maximum pause time, you can do control in that language.

FWIW, PLCs typically don't even try to manage memory--every bit of memory used by the program is pre-allocated before starting RUN mode and there are no pauses in the PLC's infrastructure at all.

For most control applications, the issue is reliable timing in the single-digit millisecond range, or perhaps a bit faster. Servo and robotic applications typically have positioning control loops operating in the hundred-microsecond range.

2 Likes

I think this may be tangentially related - https://micropython.org/

I've personally never used it but I've seen it mentioned a bunch in other discussions about embedded solutions using python.

1 Like

:face_vomiting: LOL, I do my embedded stuff in Rust :slightly_smiling_face:

3 Likes

How is rust going vs Golang?

We are trying Golang to read Siemens 1200, it takes 300ms just to read one DB of 100bytes.

Right now, I only use Rust for side projects in the cellular IoT space. I haven't tried to use it for any PLC/automation stuff. I just don't have the time right now. Also, I have zero experience with Golang, so I can't offer any comparisons there.

You can look for Rust crates(libraries) on www.crates.io to see if there is anything already out there that you could use or build on, for example profirust. It looks very new and incomplete but might be a starting point.

You could also check out the ethercrab project for inspiration. It's a pure Rust EtherCAT master.

If you are using Ignition, is there a reason you don't just use the Siemens driver from IA?

1 Like

Just to update, using different coding structure, go can read siemens PLC 200byte in 5ms.

But it is still a long way to go to try it out as a software PLC system.

there's a lot of discussion of Rust vs Go as embedded control system language.

I might just stay with Go for now, if Go can do a good job replacing some of the PLC functions, I am sure Rust will be the same and do a better job in memory management section.

The discussion around using Python for industrial control systems is indeed an interesting and evolving topic. While traditional PLCs are designed for real-time control and are widely used in industrial settings, there are cases where non-critical real-time requirements might make Python a feasible option, as you've experienced in your small control system. At the same time, Python can be a desirable option for some applications due to its flexibility and ease of use, particularly when timing constraints are not as important.
Thanks