Program Control
For an introduction to the DSL vocabulary, see Core Concepts.
Programs
Two equivalent ways to define a program:
# Context manager
with Program() as logic:
with rung(Start):
latch(Running)
# Decorator
@program
def logic():
with rung(Start):
latch(Running)
Both produce a Program you pass to PLC. See Core Concepts — Programs for details.
Subroutines
Context-manager style
with Program() as logic:
with subroutine("startup"):
with rung(Step == 0):
out(InitLight)
with rung(AutoMode):
call("startup")
Decorator style
@subroutine("init")
def init_sequence():
with rung():
out(InitLight)
with Program() as logic:
with rung(Button):
call(init_sequence) # auto-registers and calls
Ending a scan early
When Paused is on, the scan stops at end_early(). The rungs below it do not run on that scan. This is CLICK's conditional END. It works on the first scan too.
Call end_early() only in the main program. Call return_early() to leave a subroutine early. It is CLICK's RET. The CLICK export adds the unconditional END that every CLICK main program ends with, and the closing RET of each subroutine.
A rung that end_early() skips keeps its state:
Motorkeeps its last value, on or off.- A timer keeps its accumulator and its done bit. When its rung runs again, the timer goes on from that accumulator.
- A counter misses the pulses that come while its rung is skipped. If its input was off when the skip began and is on when it ends, the counter counts once.
For loops
forloop repeats a block of instructions N times within a single scan:
The count can be a positive whole number or a tag, read once when the loop starts.
A constant count with a decimal point, such as forloop(5.0), raises when you
build the program. As on CLICK, an Int count is read as unsigned 16-bit, so -1 runs 65535
iterations (a scan of a second or more on a CLICK). A Dint count is read whole
and signed, so 65536 runs 65536 and -1 runs none. A literal is read whole, so
forloop(70000) runs 70000. A count of 0 runs none.
For CLICK, the count must be a whole-number constant or a DS, DD, TD,
CTD or SD tag. CLICK validation reports for-count-bank for a count in any
other bank. Copy the count into a DS or DD tag and loop on that.
Use loop.idx for indirect addressing inside the loop body:
End and return instructions aren't needed. Python indentation handles scope.
Body rungs
The loop body is a run of rungs between For and Next. A bare instruction is a
body rung with no contacts. A body rung with contacts is a with rung(...)
inside the loop. Its contacts are evaluated on every iteration, against the
values the earlier body rungs just wrote:
with rung(Enable):
with forloop(10) as loop:
with rung(Src[loop.idx] == 0):
continue_loop()
copy(Src[loop.idx], Dst[loop.idx])
with rung(Fault):
break_loop()
continue_loop() skips the rest of the current iteration. break_loop() leaves
the innermost loop. The scan carries on after its Next. Both need a body rung
with at least one contact, as in CLICK.
When the loop's own rung is false, the body does not run at all, as on CLICK.
Its out() coils keep their values. Its timers and counters neither time,
count nor reset. Its one-shots do not re-arm (only the For's own
oneshot=True does). To turn a body coil off when the loop stops, reset() it
from a rung of its own.
You cannot use branch() inside a loop. A body rung cannot hold another
with rung().
Nested loops
Loops nest up to three deep. Each one has its own loop.idx, and
break_loop() / continue_loop() act on the innermost:
with rung(Enable):
with forloop(Rows) as row:
with forloop(Cols) as col:
math(Total + Grid[row.idx * 8 + col.idx], Total)
A nested loop is a body rung of the outer loop, so it can have contacts:
with rung(Ready): with forloop(3): ... inside the outer loop.