1force = 1250 # N, set once2# never reads the speed1error = goal - speed2force = 350 * error1error = goal - speed2integral += 40 * error * dt3force = 350*error + integral1# cruise is off:2# the driver's foot3# holds 1000 N25 m/s1000 N—20.0 m/sflaterror +5.00 m/s20.0 m/s
set 25 m/shill starts →4 % grade0306090120150121518212427time (seconds)m/sCAR SPEEDopen loopPPI
Lit wire with moving dashes: a signal or force is flowing along itBlocks: set-speed dial, engine, car, speed sensor and hill; the controller is the code cardPlot: car speed over time for open loop, P and PI, computed from the modelThe pill travelling back to the controller is the speed reading: feedbackA cruise-control loop: the set speed, the controller (a short Python rule), the engine (actuator), the car (plant) and a wheel-speed sensor, with a 4 % hill as the disturbance. The plot below is simulated from the car model as the steps play: open loop loses speed on the hill, proportional control leaves a gap, and PI control returns to 25 m/s. Real aerodynamic drag grows with the square of speed (½·ρ·Cd·A·v²). The straight-line b·v is a linearisation that holds near the cruising speed; the lesson “Linearising a nonlinear system” shows how to get it.

The car

  1. A 1000 kg car cruises at 20 m/s. The driver switches on cruise control and sets 25 m/s (90 km/h). From now on a controller must move the car there and hold it, flat road or hill.

Open loop

Feedback

PI design

What is happening

# step 1/11 · goal
# about: Set speed, Car (plant)

car mass        1000 kg
speed now       20 m/s (72 km/h)
set speed       25 m/s (90 km/h)

The design: model the car, try open loop,
close the loop, add integral action, pick
the gains, then check the practical bits.

The car is a simple linear model with illustrative numbers: 1000 kg, drag and rolling loss of 50 N per m/s, engine force 0 to 3000 N. Every curve and number in the figure is computed from that model. Its transfer function G(s) and 20 s time constant are explained in “The transfer function of a system” and “First-order response and the time constant”; what happens when the engine hits its limit is in “Actuator limits and integrator windup”. The Python on each step is plain Python 3 with the math module. It updates the controller every 0.01 s and moves the car forward with the exact solution of its equation between updates, just as the figure does.