P —r = 1 me = r − yI —u —1 / (s²+10s+20)D —y 0.00 m1e = r - y2I = I + e * dt3D = -(y - y_old) / dt4u = Kp*e + Ki*I + Kd*DPIDU
r = 1 m00.511.500.51time (s)position y (m)STEP RESPONSE y(t)P onlyPID20 N holds the spring00.511.5−1500150300time (s)force (N)CONTROLLER TERMS (N)P = Kp·eI = Ki·∫eD = −Kd·ẏu = sum
Lit wire with moving dashes: a signal or force is flowing along itBlocks: command, compare, P, I, D, adder, cart and position; the code card holds the PID updatePlots: step response (P alone and PID) and the three controller terms, integrated from the modelPills travelling into the adder carry each term's force; the last one carries their sum u to the cartA PID loop for a cart on a spring: the error feeds a proportional (P) and an integral (I) block, the measured position feeds a derivative (D) block, and the three terms add into one force. The plots are integrated from the closed loop: P alone swings and stops short; with PID the cart reaches 1 m quickly, barely overshoots and stays there because the integral ends up holding the spring's 20 N. Here P and I act on the error but D acts on the measured position (often called PI-D). The closed-loop poles are the same as for the textbook form Kp + Ki/s + Kd·s; only the zeros differ, and the command step no longer produces an infinite derivative spike.

One term is not enough

  1. The plant: a 1 kg cart on a spring (20 N/m) with a damper (10 N·s/m), pushed by a force u. So G(s) = 1/(s² + 10s + 20). We ask it to move 1 m; holding it there takes 20 N against the spring.

The three terms

All together

The working

# step 1/11 · cart
# about: Command r, Cart, Position y

plant   m·y'' + b·y' + k·y = u
        m = 1 kg, b = 10 N·s/m, k = 20 N/m
        G(s) = Y/U = 1 / (s² + 10s + 20)

command r = 1 m (a unit step at t = 0)

to hold y = 1 m at rest the spring pulls back
with k·y = 20 N, so the controller must keep
pushing with u = 20 N for ever.

The cart (1 kg, 20 N/m, 10 N·s/m) and the gains are round illustrative numbers, not a measured machine. Every curve is integrated from the closed loop (RK4, 0.1 ms steps) and every number in the text is measured on those curves. The Python is plain Python 3 with a simple Euler loop at 0.1 ms; it prints the same numbers as the RK4 model to the digits shown. With python-control, ct.step_response(ct.tf([300, 200], [1, 30, 320, 200])) gives the same PID curve.