TactivergeLite

Lite / Building Machines That Respond / Week 5

and, or, not: safety interlocks in code

A press may run only if the guard is shut, an operator is present and there is no fault. Can one line of Python say all that?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Interlocks are the code that keeps people safe around machines. They are nearly always several conditions joined together. Writing them clearly, and checking every combination, is one of the most important habits a controls engineer has.

Predict

guard_closed = True, operator_present = False, fault = False. What is guard_closed and operator_present and not fault?

  1. True
  2. False
  3. An error
Guess first, then reveal

False. and needs every part to be True, and operator_present is False. The press stays off: exactly what the rule intends.

Discover

In the logic lab, build guard AND operator AND (NOT fault). Toggle the three inputs through all 8 combinations and record which ones light the output. Then build (guard OR operator) AND NOT fault and compare: why would that be a dangerous interlock?

What the interactive shows (text version)

The logic lab lets you connect AND, OR and NOT gates to switches and an output lamp. For guard AND operator AND NOT fault, only one of the 8 input combinations (guard on, operator on, fault off) lights the lamp. For (guard OR operator) AND NOT fault, three combinations light it, including guard off with operator on, which is unsafe.

You can also open the interactive on the full site when you have a faster connection.

Understand

The three operators

a and b is True only if both are True. a or b is True if at least one is True. not a flips True and False. Python works out not first, then and, then or, so not fault or guard means (not fault) or guard. Use brackets whenever you mix them.

A rule in one line

guard_closed = True
operator_present = True
fault = False
can_run = guard_closed and operator_present and not fault
print(can_run)   # True

The variable name says what True means. Test all 8 combinations: can_run must be True in exactly one of them.

Ranges and short-circuits

0 <= temp_c <= 45 is Python's chained comparison: the same as 0 <= temp_c and temp_c <= 45. Python also short-circuits: in a and b, if a is False it never looks at b. That lets you write count > 0 and total / count > 50 without dividing by zero.

Explain it another way

Intuitive

and is a door with several locks: every key must turn. or is a room with several doors: any one will do. not swaps yes and no.

Technical

Python's Boolean operators return one of their operands and evaluate lazily. Precedence is not > and > or, all lower than comparisons, so x > 0 and y > 0 needs no brackets.

Mathematical

In Boolean algebra, AND is multiplication and OR is addition (with 1 + 1 = 1). De Morgan's laws: not (a and b) = (not a) or (not b); not (a or b) = (not a) and (not b).

Applied

Industrial machine guards combine several sensors in exactly this way, and safety standards require showing what happens for every combination, including a sensor that fails.

Worked example 1

Rule: 'The vent opens if it is hotter than 28 °C, or the humidity is above 85 % while it is not raining.' Write it.

open_vent = temp_c > 28 or (humidity_pct > 85 and not raining). Brackets make the second clause one unit. Check: 30 °C in the rain → True; 25 °C, 90 %, raining → False; 25 °C, 90 %, dry → True.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

rows

Practice 2. Which condition is True exactly when temp_c is from 0 to 45 inclusive?
Practice 3. Why is (guard or operator) and not fault a dangerous interlock?
Practice 4. How does Python read not fault and guard or override?
Practice 5. Why does count > 0 and total / count > 50 never divide by zero?

Apply it

Write the interlock for a laser cutter: it may fire only if the lid is closed, the extractor fan is on, the temperature is from 5 °C to 40 °C and no emergency stop is pressed. Write it as one expression, then print a table of a few important combinations and check each.

Reflect

When a machine has a safety rule, why test every combination rather than just the usual ones?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Decisions from measurements: if, elif, elseTest the edges: boundary values →