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?
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.
- Combine conditions with and, or and not, and know their order (not, then and, then or).
- Turn a worded safety rule into a Boolean expression and test it on every combination.
- Use a chained comparison such as 0 <= temp_c <= 45 for a range.
Predict
guard_closed = True, operator_present = False, fault = False. What is guard_closed and operator_present and not fault?
- True
- False
- 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.
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) # TrueThe 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
'or' means 'one or the other but not both'.
✓ Python's or is True when either or both are True. 'One but not both' is exclusive or, written a != b for bools.x == 1 or 2 checks whether x is 1 or 2.
✓ It means (x == 1) or 2, which is always truthy. Write x == 1 or x == 2.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.