1error = goal - reading2throttle = min(max(error, 0), 1)25 m/s0–3000 N—20.0 m/serror +5.0 m/spush
goal 25 m/s0246810122025time (seconds)m/sCAR SPEEDgood sensorbad sensor: truebad sensor: reading
Lit wire with moving dashes: a signal is flowing along itBlocks: set-speed dial, engine, car and wheel sensor; the controller is the code cardPlot: the car's speed over time, computed from the modelThe travelling pill is the signal on that wire; it changes kind at every partA cruise-control loop: the set speed (setpoint), the controller (a two-line Python rule), the engine (actuator), the car (plant) and the wheel-speed sensor. The pill on the wires changes what it carries at every part. The plot below is simulated from the car model as the steps play, with a good sensor and with one that reads 10 % low. Feedback drives the reading, not the true speed, to the goal. A sensor that reads 10 % low therefore makes the car run about 10 % fast (27.5 m/s instead of 24.8 m/s). Control textbooks make the same point: the loop can be no more accurate than its sensor.

The parts

  1. Cruise control holds a car at the speed the driver sets. The driver dials in 25 m/s (90 km/h); the car is doing 20 m/s (72 km/h). Four parts share the work, and each has one job.

The controller

Round the loop

What is happening

# step 1/10 · job
# about: Set speed (setpoint), Car (plant)

setpoint (goal)   25 m/s  = 90 km/h
car right now     20 m/s  = 72 km/h

m/s = metres per second.
Multiply by 3.6 to get km/h.

The four parts: plant, actuator,
sensor and controller.

The car is a simple model with illustrative numbers: 1200 kg, drag of 30 N per m/s (real drag grows faster than this, roughly with speed squared), and an engine that can push up to 3000 N. 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.05 s and moves the car forward with the exact solution of its equation between updates, just as the figure does.