Automation / PLC

Why Your Siemens Automation Has Ghostly Behavior? (And How to Fix It)

F
Franck G♥INI
July 1, 202612 MIN READ
34
Why Your Siemens Automation Has Ghostly Behavior? (And How to Fix It)

This article explores "ghost bugs" in industrial programmable logic controllers (PLC), often caused by race conditions. It explains the mechanism of these errors, their link to the PLC scan cycle, and interactions with HMI/SCADA/interrupt tasks. Practical solutions for TIA Portal (Siemens) are presented to ensure code robustness.

The Enigma of the Ghost Bug in the Factory

Imagine the scene: your production line is running smoothly, humming with precision for weeks. Sensors are active, motors are starting, and cylinders are extending with the regularity of a Swiss clock. Then, without warning, an unexpected event occurs. A cylinder extends at the wrong time, a motor refuses to start, or a valve opens when it should remain closed. The culprit? Invisible. The symptom? Fleeting. A simple restart of the automation, and the problem disappears, like magic. Impossible to reproduce, isolate, or track.

This frustrating situation is the nightmare of every automation professional. We suspect the mechanics, the electricity, and then desperation sets in. But what if I told you that often, it's neither worn-out mechanics nor a failing electrical component? The real culprit is much more insidious: it's a logical problem, a malfunction in the temporality of your program execution. Welcome to the world of race conditions.

What is a "Race Condition"? The Simple Theory

To understand a race condition, let's consider a simple everyday situation. Imagine two people trying to write on the same whiteboard at exactly the same moment, without coordination. The first person erases a message, the second tries to write over it. The final result on the board will be unpredictable: an illegible mix, a missing part of the message, or one of the writings overriding the other. This is exactly what happens when a computer system attempts to access and modify the same shared data by multiple concurrent entities (tasks, programs, processes) at the same time and without adequate synchronization. The result depends on the order of execution, which is random, making the behavior non-deterministic.

It's essential to distinguish quickly between a race condition and a "deadlock". In a deadlock, two or more processes wait indefinitely for each other to release a resource, which has the effect of completely freezing the system. A PLC in a deadlock will be stuck, without movement. A race condition, on the other hand, does not necessarily block the system; it leads to incorrect, unpredictable, and often intermittent results. The PLC continues to run but makes mistakes, making it much harder to diagnose.

Why is the PLC World Concerned?

Programmable Logic Controllers (PLC) are designed to be robust and deterministic. Their operation is based on a fundamental principle: the scan cycle. This cycle takes place in three main repetitive steps: reading of physical inputs, processing of the user program with these inputs and internal memories, and then writing of physical outputs. This mechanism, via the "image memory" of inputs/outputs (PII/PQI), offers intrinsic protection: while the program is being executed, the values of the inputs are frozen, and the outputs are only updated at the end of the cycle, ensuring internal consistency of the program in the face of real-world fluctuations.

However, this beautiful protection shows its limits as soon as data access sources are introduced outside of the main scan cycle or cut it. Two classic scenarios create vulnerability windows where race conditions can appear: firstly, when an HMI (Human-Machine Interface) or SCADA (Supervisory Control And Data Acquisition) system writes into a data block (DB) of the automation in the middle of the main program scan cycle. The program may then read partial or modified data while using it. Secondly, interrupt tasks (like OB30, OB35, OB40 in Siemens), which literally interrupt the main program to execute priority routines. If this interrupt task modifies a variable that the main program was reading or calculating, it's an open door to inconsistencies and ghostly behavior. For more details on the general operation of automations, you can consult resources like the page of Siemens Industry Online support.

How to Protect Yourself on TIA Portal (Siemens)?

The good news is that strategies exist to shield your code against race conditions, notably in Siemens' TIA Portal environment. One of the most effective tricks is the concept of a "buffer" for variables coming from an HMI or SCADA. Instead of using the HMI variable directly in your logic, copy it into a local variable (a sort of frozen copy for the cycle) at the very beginning of your OB1 or the function that will use it. This ensures that all readings and calculations of this cycle are done on consistent data, even if the HMI modifies it in the background during the cycle execution.

CODE
// Example SCL: Using a buffer for an HMI variable
FUNCTION_BLOCK "FB_GestionMoteur"
VAR_INPUT
Moteur_Commande_IHM : Bool; // Variable HMI, subject to change at any moment
END_VAR
VAR_OUTPUT
Moteur_Actif : Bool;
END_VAR
VAR
Moteur_Commande_Local : Bool; // Local buffer, read once per cycle
END_VAR
BEGIN
// Step 1: Buffering the HMI variable at the beginning of the cycle
// This local variable is read only once and remains stable for this cycle.
Moteur_Commande_Local := Moteur_Commande_IHM;

// Step 2: Using the local variable for the motor logic
IF Moteur_Commande_Local THEN
// Motor start logic
Moteur_Actif := TRUE;
ELSE
// Motor stop logic
Moteur_Actif := FALSE;
END_IF;

// Other safety logic, interlocking, etc., using Moteur_Commande_Local
END_FUNCTION_BLOCK

Another technique to protect critical sections of your code against interruptions is the use of interrupt masking instructions. In Siemens, you can use the `DIS_INT` (Disable Interrupts) and `EN_INT` (Enable Interrupts) functions around a block of sensitive code. This ensures that during the execution of these lines, no interrupt task (OB3x, OB4x, etc.) can run and potentially corrupt the data you are working on. However, these instructions should be used sparingly and with discernment, as they can affect the overall response time of the automation if used too extensively or for too long. To understand the operation of the scan cycle in a more general way, you can consult explanations like those provided by AutomationDirect.

Finally, a classic beginner's trap to avoid at all costs to not short-circuit the safety of the scan cycle is direct access to peripherals (the famous `%I0.0:P` or `%Q0.0:P`). This syntax forces the direct reading or writing of a physical input/output, bypassing the image memory. Although it may seem to offer a fresher reading of an input, it eliminates the temporal protection and consistency offered by the scan cycle, making your program vulnerable to intermittent and incorrect readings if the state of the input changes exactly when the direct reading is made. Always prefer access via the image memory (for example, `%I0.0`) which is updated consistently at the beginning and end of each cycle.

Conclusion: The Secret to Robust Industrial Code

In summary, a good automation professional does not just write a program that works on the test bench or during initial tests. They design code that is resilient over time, anticipating asynchronisms and concurrent accesses. Understanding race conditions and knowing how to mitigate them is the mark of mature and reliable industrial code. It's the guarantee that your machine will not just run, but will do so with the predictability and robustness expected of an industrial system.

Eliminating ghost bugs means transforming the random into the deterministic, and the anxiety of inexplicable failures into the satisfaction of a smooth and surprise-free production. It's the true secret to robust industrial code, capable of withstanding the temporal challenges.

34

Commentaires

Laisser un commentaire

0/2000

* Les commentaires sont modérés avant publication.

Chargement des commentaires...