Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 Β· Actuators and Control

PID Control

Learn feedback control and PID: proportional, integral and derivative terms, tuning, anti-windup and derivative filtering, with a runnable motor simulation.

A motor given 6 volts does not run at a known speed: load, friction and battery level all change it. Feedback control solves this by measuring the result and correcting. The most widely used feedback controller in robotics and industry is the PID controller: proportional, integral, derivative. It is simple enough to write in ten lines and strong enough to run most motor speeds, arm joints, thermostats and drone attitudes.

The feedback loop

flowchart LR
    R["Setpoint r"] --> E(("e = r - y"))
    E --> C["Controller<br/>PID"]
    C -->|"u"| P["Plant<br/>motor + load"]
    P -->|"y"| S["Sensor"]
    S -->|"measurement"| E

The controller sees only the error between the goal and the measurement.

The setpoint is the goal, the process variable the measurement y, the error e = r - y, and the control output u goes to the actuator (PWM duty, torque, voltage).

The three terms

u = Kp · e + Ki · ∫e dt + Kd · de/dt

  • P (proportional): push in proportion to the current error. Larger Kp reacts faster but eventually causes overshoot and oscillation. With P alone there is often a leftover steady-state error, because to keep applying force the error must stay non-zero.
  • I (integral): add up the error over time. A persistent small error grows the integral until the output removes it, which eliminates steady-state error. Too much Ki causes slow oscillation and windup.
  • D (derivative): react to the rate of change of the error. It acts like a brake or damper, reducing overshoot, but amplifies measurement noise.

A common implementation, in discrete time with step dt:

Python
error = setpoint - measurement
integral += error * dt
derivative = (error - previous_error) / dt
output = Kp * error + Ki * integral + Kd * derivative
previous_error = error
Python A PID class controlling motor speed

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

P alone settles below the target (steady-state error) and sags further when the load arrives at 2 s. Adding I removes both errors, and a larger Ki (third case) does it faster, at the price of overshoot and ringing in systems that are less forgiving than this motor. D, shown in the next example, reduces overshoot at the cost of noise sensitivity. This is a common pattern: PI for speed loops (the plant already damps itself) and PD or PID for position loops.

Practical details that matter

  • Integral windup. If the actuator saturates (the motor is at maximum voltage) the error persists and the integral keeps growing; when the setpoint is finally reached the huge integral pushes the output past it for a long time. Fix with anti-windup: clamp the integral or stop integrating while saturated (as in the code above).
  • Derivative noise. Differentiating a noisy measurement gives huge spikes. Low-pass filter the derivative, or take it from the measurement rather than the error, to avoid a derivative kick when the setpoint jumps.
  • Sample time. Run the loop at a constant rate, and use the real dt.
  • Output limits. Clamp the output to what the actuator can do, and don't forget the units.
  • Feedforward. If you know roughly what output holds the target (say gravity compensation for an arm), add it directly and let PID correct only the remainder.

Tuning

Tuning means choosing Kp, Ki, Kd. A practical manual recipe:

  1. Set Ki = Kd = 0. Increase Kp until the response is fast but starts to overshoot or oscillate a little.
  2. Add Kd to damp the overshoot.
  3. Add small Ki to remove the remaining offset, keeping it as low as possible.
  4. Test with the worst-case load and setpoint change, not just the easy one.

The Ziegler-Nichols method gives a starting point: raise Kp until sustained oscillation at gain Ku with period Tu, then use Kp = 0.6 Ku, Ki = 1.2 Ku / Tu, Kd = 0.075 Ku Tu. It is aggressive, so expect to reduce the gains. Auto-tuners and simulation do the same job more safely.

Python Effect of Kp: from sluggish to unstable

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The curves show the typical trade: low gain is slow, higher gain is fast but oscillates, and derivative action tames the overshoot. A delay in the loop (here 30 ms) is what makes high gains unstable, which is why sensor latency matters so much.

Beyond PID

PID is a great default, but it has limits: it is single-input single-output, linear, and ignores the plant model. Cascades (an inner speed loop inside an outer position loop) are common in robot joints. Model-based methods such as LQR and model predictive control (MPC) handle many inputs and constraints and are used for drones, legged robots and self-driving cars. They build on the same ideas of feedback and error.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Which PID term removes a constant steady-state error?

    The integral keeps growing while any error remains, so the output rises until the error is zero.
  2. 2. What is integral windup?

    The accumulated integral keeps pushing after the target is reached, causing big overshoot. Anti-windup prevents it.
  3. 3. Why is the derivative term sensitive to sensor noise?

    Noise changes quickly, so its derivative is large. Filter it.
  4. 4. You raise Kp and the response starts to oscillate with large overshoot. What is a good next step?

    Derivative action damps overshoot; lowering Kp also helps.

Summary

  • PID computes u = KpΒ·e + Ki·∫e + KdΒ·de/dt from the error between setpoint and measurement.
  • P gives speed, I removes steady-state error, D adds damping.
  • Real controllers need output limits, anti-windup, derivative filtering and a steady sample rate.
  • Tune step by step: P first, D for damping, small I last, under realistic loads.

Next: Trajectory Generation