[attachment polisher.pdf]
I think there is a problem with this code, and it may be because of a misunderstanding about the PLC scan cycle works.
The point is that the a PLC scan cycle evaluates all the rungs in the program in order, and then the next PLC scan cycle evaluates them all again.
So if we look at rungs 2-6 in your program:
Rung 2 transitions from sequence #1 to sequence #2 when
X_ENC ≥ X_max:
Rungs 3-5 transition through sequences #2, #3, #4, and back to #1 based on various conditions:
Rung 6 transitions from sequence #1 to sequence #5 when several conditions are met, but the first condition is when
X_ENC ≥ X_max:
However, that last transition on Rung 6 will never happen, because on the PLC scan cycle when
sequence == #1 and
X_ENC ≥ X_max, when the scan cycle evaluates Rung 2,
which happens before evaluation Rung 6, Rung 2 will transition from sequence #1 to sequence #2.
Then later in that
same scan cycle when evaluating Rung 6, the
sequence value will no longer be #1, so the first comparison
sequence == #1 on Rung 6 will evaluate to False, so even if the second comparison
X_ENC ≥ X_max and all the subsequenc comparisons on Rung 6 evaluate to True, the rung state will
already be False, and so the final Store instruction will
never assign a value of #5 to the sequence tag.
There is a similar problem in Rungs 7-11: sequence will never transition from #5 to #1 when any scan cycle evaluates Rung 11 because it will have already transitioned from #5 to #6 earlier on the same scan cycle when evaluating Rung 7.
PLCs are very simple, and we have to think simply to program them. The only thing worse than a PLC not doing what you
want it to do, is when it does exactly what you
tell it to do.
I suspect that why this was coded like this is that our protagonist @SonusI has experience in procedural programming (C/C++, Python, etc.), and because Rung 6 is immediately after Rung 5, they had a mental model of Rung 6 being evaluated after Rung 5 with
no other rungs evaluated in between, and did not consider that Rung 2 would be evaluated,
many times, after the Rung 5 evaluation that transitioned sequence back to #1. The hope was probably that Rungs 2-6 could be re-used to repeat the X cycle after reaching sequence #5. That may or may not be the reason this happenen, but either way the code as written is unlikely to work.
One solution may be quite simple: move current Rung 6 to just before Rung 2, and current Rung 11 to just before Rung 7. That way, when the X cycle is complete, whether the sequence goes from #1 either to #2 or to #5, and from #5 either to #6 or to #1, depends on the current Y position.
But a better solution would be to refactor the sequence numbers to take into account whether Y has been increasing or decreasing. The Rungs 2-6 and 8-10 can still be re-used, but they would have two sequence comparisons ORed (i.e. in parallel) as an initial compound condition.