ENع
←The PID controller
Lesson 39 · PID control and compensators

The PID controller

14 min11 steps5 questionsAdvanced
About this lesson

A PID controller adds three forces: one for the error now (P), one for the error added up over time (I) and one for how fast the output is moving (D). Follow a cart on a spring through one step command. P alone swings and stops short. With all three terms the cart arrives fast, barely overshoots and stays on target. The three gains also set the closed-loop poles.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 39 · Interactive
01 / 11

A cart on a spring

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.

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.
Loading figure…
or ← →
Read the lesson step by step
  1. A cart on a spring

    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.

    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.
  2. P alone: swing and offset

    With proportional control alone, u = 300·e. The cart swings to 1.31 m and settles at 0.94 m, short of 1 m: P pushes only while an error remains, so some error must stay to hold the spring (see "Proportional control and its offset").

    Kp, Ki, Kd, dt = 300, 0, 0, 1e-4
    y = v = I = 0.0
    for n in range(15000):   # 1.5 s
        e = 1.0 - y            # P: the error now
        I += e * dt            # I: the error so far
        u = Kp*e + Ki*I - Kd*v # D: brake on the speed
        a = u - 10*v - 20*y    # cart: y'' = u - 10y' - 20y
        v += a * dt
        y += v * dt
    print(round(y, 2))         # 0.94
    
    resting point: Kp·e = k·y
    300·(1 - y) = 20·y  →  y = 300/320 = 0.9375 m
    peak 1.31 m at 0.18 s (40 % over its final value)
  3. Three terms, one law

    PID joins the three terms from "Adding integral and derivative action": u = Kp·e + Ki·∫e dt + Kd·de/dt (our D uses y, step 7). P sees the error now, I the error so far, D how fast it changes. Gains: 300, 200, 20.

    u(t) = Kp·e(t) + Ki·∫₀ᵗ e dτ + Kd·de/dt
    
    P   the present   error now
    I   the past      error added up so far
    D   the future    trend: where it is heading
    
    Kp = 300 N/m   Ki = 200 N/(m·s)   Kd = 20 N·s/m
    
    the card runs the same law once every dt:
    e, the running sum I, the slope D, then u
  4. C(s): one pole, two zeros

    In Laplace form C(s) = Kp + Ki/s + Kd·s = (20s² + 300s + 200)/s. That is a pole at s = 0, the integrator, plus two zeros, here at −0.70 and −14.30. Three gains place two zeros and the overall gain.

    # zeros of C(s) = (Kd s² + Kp s + Ki) / s
    import cmath
    Kp, Ki, Kd = 300, 200, 20
    d = cmath.sqrt(Kp**2 - 4*Kd*Ki)
    z = [(-Kp + d) / (2*Kd), (-Kp - d) / (2*Kd)]
    print(f"{z[0].real:.2f} {z[1].real:.2f}")   # -0.70 -14.30
    
    # pole at s = 0: the integrator. It makes the
    # loop type 1, so a step leaves no error.
  5. P: the error now

    P pushes in proportion to the error right now. At t = 0 the error is the full 1 m, so P pushes 300 N, the biggest kick of the run. As the cart closes in, the error shrinks and P fades towards zero.

    uP = Kp·e = 300 · (1 - y)
    
    t = 0      e = 1.000 m   uP = 300 N
    y → 1      e → 0         uP → 0
    
    P alone cannot hold the cart: once e = 0
    it pushes nothing. That job goes to I.
  6. I: the error so far

    I adds up the error over time, so it keeps growing while any error is left. It ends at 19.8 N, heading for exactly the 20 N the spring needs. With I holding that force, P no longer needs an error.

    uI = Ki·∫e dt = 200 · (area under e)
    
    while e > 0   the area grows, uI climbs
    when e = 0    the area stops growing, uI holds
    
    t = 1.5 s   uI = 19.8 N   (→ 20 N = k · 1 m)
    
    The pole at s = 0 is what removes the offset.
  7. D: brake on the trend

    D reacts to how fast the cart moves: −20 × its speed. Racing towards the target, D pushes back, down to −142 N at 0.060 s, so the cart barely overshoots. It uses dy/dt, not de/dt, so the step command makes no spike.

    uD = -Kd·dy/dt = -20 · speed
    
    most negative  -142 N at t = 0.060 s
    at rest        0 N
    
    why dy/dt, not de/dt?
    e = r - y jumps when r steps, so de/dt would be
    an impulse ("derivative kick"). Since r is
    constant after the step, -dy/dt = de/dt there.
  8. Add them, push the cart

    The three terms add into one force, u = P + I + D, and that single force pushes the cart. Early on P dominates, D brakes the rush, and at the end I alone holds the 20 N.

    Kp, Ki, Kd, dt = 300, 200, 20, 1e-4
    y = v = I = 0.0
    for n in range(15000):   # 1.5 s
        e = 1.0 - y            # P: the error now
        I += e * dt            # I: the error so far
        u = Kp*e + Ki*I - Kd*v # D: brake on the speed
        a = u - 10*v - 20*y    # cart: y'' = u - 10y' - 20y
        v += a * dt
        y += v * dt
    print(round(y, 3), round(Ki*I, 1))   # 0.999 19.8
  9. PID against P alone

    The result: the PID cart peaks at only 1.005 m (0.5% overshoot), stays within 2% of 1 m after 0.24 s and ends on target. P alone swung to 1.31 m and stopped at 0.94 m.

                 P only        PID
    peak         1.31 m        1.005 m
    final        0.94 m       1.000 m
    settles (2%) 0.77 s        0.24 s
    
    I removed the offset, D removed the swing,
    P kept it fast.
  10. Three gains, three poles

    Closed-loop poles solve s³ + (10 + Kd)s² + (20 + Kp)s + Ki = 0, here s³ + 30s² + 320s + 200: −0.67 and −14.67 ± 9.24j. Each gain sets one coefficient, so the three gains can place all three poles.

    1 + C(s)·G(s) = 0 with derivative on y:
    s(s² + 10s + 20) + Kd s² + Kp s + Ki = 0
    s³ + (10+Kd)s² + (20+Kp)s + Ki = 0
    s³ + 30s² + 320s + 200 = 0
    
    poles  -0.67
           -14.67 ± 9.24j
    
    P alone: s² + 10s + 320, poles -5 ± 17.18j
    (lightly damped: the swing you saw).
    The slow pole at -0.67 sits next to the zero of
    Kp s + Ki at -0.67, so it barely shows in y.
  11. Real PID: forms and limits

    Industry often writes Kp(1 + 1/(Ti·s) + Td·s): here Ti = 1.50 s, Td = 0.0667 s. Real controllers also filter D against sensor noise and stop I growing when the motor is at its limit. "Ziegler-Nichols tuning" picks gains by rule.

    standard (ISA) form   Kp·(1 + 1/(Ti s) + Td s)
    Ti = Kp / Ki = 1.50 s    (integral time)
    Td = Kd / Kp = 0.0667 s  (derivative time)
    
    in practice
      D is low-pass filtered: Kd s / (1 + Td s / N)
      I is frozen when u saturates (anti-windup)
      D often acts on y, as here
    
    choosing the gains by rule: next lesson,
    "Ziegler-Nichols tuning".

Quiz Unlocks after the last step.