1# no code yet:2# first, model the motor1V = 10.01 # volts, set once2# never reads the speed1e = 1.0 - speed2V = ? # the next steps1e = 1.0 - speed2V = 100*e1e = 1.0 - speed2I += e*dt3V = 100*e + 200*I1e = 1.0 - speed2I += e*dt3D = (e - e_old)/dt4V = 100*e + 200*I + 10*D1e = 1.0 - speed2I += e*dt3V = 20*e + 40*I1 rad/s0.0 V—0.000 rad/sdrag 0 N·me = +1.0000.000
setpoint 1 rad/sdrag on →0123400.51time (s)rad/sSHAFT SPEEDopen loopPPIPIDtuned PIdriver limit ±24 V↑ 10 100 V for 1 ms (off scale)00.20.40.6-100-24024100time (s)VDRIVER VOLTAGE ASKED FORtextbook PIDtuned PI
Lit wire with moving dashes: a signal, power or torque is flowing along itBlocks: setpoint, driver, motor, flywheel and encoder; the controller is the code cardPlots: shaft speed and driver voltage over time, computed from the motor modelThe pill travelling back to the controller is the encoder reading: feedbackA DC motor speed loop: setpoint, controller (a short Python rule run every millisecond), driver (actuator), motor and flywheel (plant) and encoder (sensor), with a brake on the flywheel as the disturbance. The plots below are simulated from the motor model as the steps play: the textbook PID meets its specs on paper but asks for volts no driver has; a PI tuned for a 24 V driver meets them for real. If a 24 V driver simply clipped the textbook PID's demand, the integral would keep growing while the voltage is stuck at the limit and the speed would overshoot. That is integrator windup, the subject of "Actuator limits and integrator windup".

The plant

  1. A small DC motor spins a flywheel. Our job: hold it at 1 rad/s (radians per second, about 9.5 turns a minute), even when something drags on the shaft. We go the whole way: model, tune, then the snags textbooks skip.

Textbook tuning

Real hardware

What is happening

# step 1/11 · job
# about: Setpoint 1 rad/s, DC motor (plant), Flywheel + load

setpoint   1 rad/s  (1 rad/s = 60/2π ≈ 9.5 rpm)
plant      DC motor + flywheel
actuator   driver, −24 to +24 V
sensor     encoder on the shaft
disturbance  a drag torque on the load

Model → transfer function → specs →
tune → check what real hardware allows.

The motor values (J = 0.01 kg·m², b = 0.1 N·m·s, K = 0.01 N·m/A, R = 1 Ω, L = 0.5 H) are an illustrative small motor, the set many university tutorials use for this example, not a datasheet. Every curve and number in the figure is computed from them. The controller runs every 1 ms and holds its voltage in between, as in the lesson "Writing a digital PID loop". Between updates the motor equations are stepped with fourth-order Runge-Kutta, accurate far beyond the digits shown. The controller itself is discrete: the 10 100 V kick exists because the derivative is taken over one 1 ms sample; a continuous PID would give an impulse instead.