Skip to content
Tactiverge

School/△ Builder/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

1.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.

2.Predict

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

Guess first. Being wrong here is how the surprise works.

3.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?

Explorer 05 — Digital logic

Build answers out of ones and zeros

Every digital chip is built from a handful of simple gates. Flip the inputs to watch signals travel through a circuit, then wire your own gates to meet a target truth table.

ONE GATE · 1 GATE DEEP0A0BAND= 0G10Y
  • 1 (high)
  • 0 (low)
  • Unknown
  • Inverting output
Mode
Starting circuit

A single two-input gate. Change its type and watch the truth table change.

Inputs

Gates (1 of 8)

  1. G1 = 0

Add a gate

Outputs

Gates
1
Depth
1
Longest chain of gates a signal passes through
Rows
4
2^2 input combinations
Check
—
Truth table
Set inputsABY
000
010
100
111

State

Inputs A = 0, B = 0. Outputs Y = 0.

Inputs A = 0, B = 0. Outputs Y = 0.

How to read this

Signals flow left to right. Circles on the left are the inputs, boxes are gates and squares on the right are outputs. A thick bright wire carries a 1, a thin grey wire carries a 0, and each gate shows the value it outputs. A small circle on a gate's output means it inverts (NOT, NAND, NOR, XNOR).

The truth table lists every possible input combination and what the circuit outputs for each, so it describes the circuit completely. Two circuits with the same truth table are equivalent, however differently they are built. That is why NAND alone can make any other gate. Choose a row to set the inputs to it.

Assumptions & limitations
  • Ideal logic: every wire is exactly 0 or 1. Real gates use voltage ranges (for example near 0 V and near 3.3 V or 5 V) and an input between them is undefined.
  • Gates switch instantly here. Real gates take nanoseconds each, so a deeper circuit is slower, and outputs can glitch briefly while signals race through different paths.
  • Only combinational circuits are allowed: no output may feed back into its own input. Feedback is how memory (latches and flip-flops) works, but it needs timing this model does not simulate, so loops are reported as an error.
  • Gates here have one input (NOT) or two inputs. Real chips also have three- and four-input gates, and limits on how many inputs one output can drive (fan-out).
  • Gate counts are a simple measure of size; real designs also weigh speed, power and the number of transistors per gate.
Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

guard, operator and fault are each True or False (8 combinations). For how many of the 8 combinations is this Python condition True: guard or operator?

rows

Practice 2

Which condition is True exactly when temp_c is from 0 to 45 inclusive?

5.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

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.

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.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

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?

7.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.

Open BUILD 003: Create an Intelligent Sorting Machine →

8.Reflect

Concepts: Decisions and loops · Logic gates and Boolean algebra