Skip to content

Conditions

For an introduction to the DSL vocabulary, see Core Concepts.

These forms go inside rung(...). All of them can be mixed freely.

Fault                          tag is truthy
~Fault                         tag is falsy
MotorTemp > 100                comparison  (==  !=  <  <=  >  >=)
Fault, Pump                    comma = implicit AND
Fault, MotorTemp > 100         implicit AND with comparison
And(Fault, Pump, Valve)        explicit AND (same as commas)
Or(Low, High, Emergency)       explicit OR
Or(Start, And(Auto, Ready))    nested AND inside OR

Normally open (examine-on)

with rung(Button):          # True when Button is True
    out(Light)

Normally closed (examine-off)

with rung(~Button):      # True when Button is False
    out(FaultLight)

Rising and falling edge

with rung(rise(Button)):    # True for ONE scan on False→True transition
    latch(Motor)

with rung(fall(Button)):    # True for ONE scan on True→False transition
    reset(Motor)

As on a CLICK, each rise() or fall() contact has its own memory of the bit. The memory updates on every scan that reaches the contact, whether the rung has power or not. The memory starts as off at RUN. This has these results:

  • A bit already on at the first scan rises on that scan.
  • Two rise(Button) contacts each see the edge, wherever they sit.
  • An edge that happens while the rung is off is spent, not saved for later.
  • A rise() above a rung that writes its bit sees the write one scan later.
  • Inside a forloop() body, a rise() compares with the previous iteration. In a subroutine called twice a scan, it compares with the previous call.

Multiple conditions (AND)

# Comma syntax — all must be True
with rung(Button, ~Fault, AutoMode):
    out(Motor)

# And() — explicit AND
with rung(And(Button, ~Fault, AutoMode)):
    out(Motor)

OR conditions

# Or() — at least one must be True
with rung(Or(Start, RemoteStart)):
    latch(Motor)

Nested AND/OR

with rung(Or(Start, And(AutoMode, Ready), RemoteStart)):
    latch(Motor)

Comparisons

with rung(Step == 0):
    out(InitDone)

with rung(Temperature >= 100.0):
    latch(OverTempFault)

with rung(Counter != 5):
    out(NotAtTarget)

A TXT compared with a literal of more than one character compares the TXTs that follow it too, as a CLICK does. For example, Txt[1] == "AB" is true when TXT1 is "A" and TXT2 is "B".

pyrung refuses <, <=, > and >= on a TXT when the program is built, because a CLICK compares text only with == and !=. To order characters, copy each one into an Int tag with convert=to_ascii and compare the Int tags.

A comparison may read a block element addressed at scan time, through a pointer or an index expression:

with rung(DS[Step] == 0):
    out(StepEmpty)

with rung(Setpoint < DS[Step + 1]):
    out(NextIsHigher)

A pointer may be a whole side of the comparison. DS[Step] + 1 > 5 raises TypeError when you build the program. copy() the element into a tag first.

An address outside the block, or in a gap of a sparse block, reads as its type's zero value. No fault flag is set. copy() and math() treat a source below the block differently. They set system.fault.address_error and write nothing (see copy). A CLICK accepts a pointer only in Copy. The CLICK validator refuses a pointer in a contact (pointer-outside-copy).

INT truthiness

INT tags are True when non-zero:

with rung(Step):                    # True if Step != 0
    out(StepActive)

with rung(Or(Step, AlarmCode)):
    out(AnyActive)

Inline expressions

with rung((PressureA + PressureB) > 100):
    latch(HighPressureFault)

Inline expressions work in simulation. The CLICK dialect validator will flag them if targeting CLICK hardware. Rewrite them as math() instructions instead.

An inline expression computes only with numbers. pyrung refuses an expression that holds a TXT or text when the program is built, and also a compare of an expression with a TXT or text. Copy the text into a number tag first, with copy(Txt[1], DS[1], convert=to_value).

An inline expression is permissive. If an operation in it causes a math error (a division by zero, or a math function given an operand outside its domain), the operation gives 0 and sets no fault flag. See Math outside math().