1power = 1.33 # kW, set once2# never reads the room1error = goal - temp2power = 5 * error3power = min(max(power, 0), 3)20 °C0 kW—10.0 °C10 °Cerror +10.0 °C10.0 °C
goal 20 °Ccold snap →outside 10 → 4 °C060120180240300360101520time (minutes)°CROOM TEMPERATUREopen loopclosed loop
Lit wire with moving dashes: a signal or heat is flowing along itBlocks: goal, heater, room, thermometer and weather; the controller is the code cardPlot: room temperature over time, open loop and closed loop, computed from the modelThe pill travelling back to the controller is the thermometer reading: feedbackA control loop for a room heater: the goal, the controller (a short Python rule), the heater (actuator), the room (plant) and the thermometer (sensor), with the weather outside as a disturbance. The plot below is simulated from the room model as the steps play: open loop holds only while the weather matches what was assumed; feedback keeps the room near the goal. A household thermostat often just switches the heater fully on below the goal and off above it, with a small band in between. That is feedback too; this lesson uses a smoother rule so the heater power can be read at every moment.

The job

  1. A control system makes something behave the way we want, on its own, with nobody watching. Our job: keep a room at 20 °C while it is 10 °C outside. The value we ask for is the goal, also called the setpoint.

Open loop

Closed loop

What is happening

# step 1/11 · goal
# about: Goal 20 °C, Room (plant)

goal (setpoint)   20 °C
outside           10 °C
room right now    10 °C

A control system = something that keeps an
output where we ask, without a person
adjusting it by hand.

The room is a simple model with illustrative numbers: it heads towards the outside temperature plus 7.5 °C for every kW of heat, with a 40-minute time constant. Every curve and number in the figure is computed from that model. The Python on each step is plain Python 3 with the math module. It updates the controller every 0.1 minutes and moves the room forward with the exact solution of its equation between updates, just as the figure does.