Race condition - will FBD-Light detect this?
Posted: 21 Mar 2018, 23:34
Hi,
I just received my RevPi and started testing with Logi.cals
I used the FBD editor from OpenPLC to create a simple test program from my Function Block Diagram:
From this FBD I created the following Structured Text:
The idea is that the relay output is active for 2s when the switch is pressed. This is true independent of the length of time the switch is pressed (shorter or longer than 2s should not matter) thanks to the R_TRIG.
But now a fun thing happens: I am able to reproduce a situation where I press the switch again just at the moment when the relay is switched off. After this, the output may get stuck forever and the relay will not switch off again.
I found the reason to be the location of the RS1/Ton0 blocks in the structured text.
There seems to be a race condition in the feedback. If I hit the key at the exact moment when the relay is switched off, Ton0.Q is still TRUE when Trig.Q becomes TRUE (for one cycle).
Unfortunately Ton0.Q is TRUE for a complete cycle, resulting in an immediate reset of RS1. This results in Ton0.Q never being set again (since Ton0.IN is FALSE).
So RS0 is never reset and the relay is never switched off(RS0.Q stays TRUE).
In this case the problem was not hard to locate and the fix is easy: change the order of the lines to remove the race condition.
But this does arise a question: I created the ST by hand, walking through the diagram from left to right. I also tested the compiler in PLCOpenEditor and that gives the same order of lines as my original program.
What happens when I do this in the Logi.cals FBD editor? Is that one able to detect such race conditions - or at least give a warning?
and yes, there are likely to be other solutions for this problem, this is just a very simple program to play around a bit with Logi.cals.
I never programmed a PLC using ST or FBD and the only PLC experience I have is with Ladder Diagrams in the pre 1984 era...
Regards,
Rob
I just received my RevPi and started testing with Logi.cals
I used the FBD editor from OpenPLC to create a simple test program from my Function Block Diagram:
From this FBD I created the following Structured Text:
Code: Select all
PROGRAM Test
VAR
Ton0 : TON;
RS0 : RS;
RS1 : RS;
Trig : R_TRIG;
END_VAR
VAR_EXTERNAL
Switch : BOOL;
Lamp1 : BOOL;
relais1 : BOOL;
END_VAR
Trig(CLK := Switch);
RS1(S := Trig.Q, R1 := Ton0.Q);
Ton0(IN := RS1.Q1, PT := T#2s);
RS0(S := Trig.Q, R1 := Ton0.Q);
relais1 := RS0.Q1;
END_PROGRAM
But now a fun thing happens: I am able to reproduce a situation where I press the switch again just at the moment when the relay is switched off. After this, the output may get stuck forever and the relay will not switch off again.
I found the reason to be the location of the RS1/Ton0 blocks in the structured text.
There seems to be a race condition in the feedback. If I hit the key at the exact moment when the relay is switched off, Ton0.Q is still TRUE when Trig.Q becomes TRUE (for one cycle).
Unfortunately Ton0.Q is TRUE for a complete cycle, resulting in an immediate reset of RS1. This results in Ton0.Q never being set again (since Ton0.IN is FALSE).
So RS0 is never reset and the relay is never switched off(RS0.Q stays TRUE).
In this case the problem was not hard to locate and the fix is easy: change the order of the lines to remove the race condition.
But this does arise a question: I created the ST by hand, walking through the diagram from left to right. I also tested the compiler in PLCOpenEditor and that gives the same order of lines as my original program.
What happens when I do this in the Logi.cals FBD editor? Is that one able to detect such race conditions - or at least give a warning?
and yes, there are likely to be other solutions for this problem, this is just a very simple program to play around a bit with Logi.cals.
I never programmed a PLC using ST or FBD and the only PLC experience I have is with Ladder Diagrams in the pre 1984 era...
Regards,
Rob