# PID control, by feel

Robotics foundations · Lecture 4 of 12 · 26 min

Source: https://shakaut.com/learn/pid-control-by-feel

## What this covers

- P, I and D as push, memory and brake
- Overshoot, settling time and steady error
- Tuning without a maths degree

---

Why the derivative term calms the overshoot, and how to tune it without a maths degree.

**Speaker notes.** Welcome back. Last time we closed the loop: we measured the motor instead of trusting a timer. Today we decide how hard to push, using three simple ideas.

## The job: hold a motor at 90°

- Push too hard and it overshoots, then swings back
- Push too softly and it never quite arrives
- So push based on the error, and keep adjusting

> error = where you want to be − where you are

**Speaker notes.** Everything today is about one number, the error. If the target is 90 degrees and we are at 60, the error is 30. The controller turns that error into a motor command.

## The loop we are tuning

*Diagram: The controller only ever sees the error. The sensor closes the loop.*

## P: push in proportion to the error

- u = Kp × error
- Bigger Kp: faster response, but more overshoot
- Kp alone often stops short under load (steady-state error)

> P is the push

**Speaker notes.** Proportional is the obvious one. Far away, push hard. Close, push gently. The catch: near the target the push gets too small to beat friction, so P alone can stall just short.

## Same motor, three controllers

*Diagram: P only stops short, PI gets there but wobbles, PID gets there calmly.*

## I: remember the error that’s left

- Adds up the error over time
- Slowly pushes away the last bit of steady error
- Too much I: slow swings and windup, so clamp it

> I is the memory

**Speaker notes.** Integral is memory. If we have been a little short for a while, the sum grows and pushes us the rest of the way. If the motor is stuck, the sum keeps growing. That is windup, and we clamp it.

## D: react to how fast the error changes

- Acts like a brake as you approach the target
- Calms overshoot and wobble
- Amplifies sensor noise, so filter it

> D is the brake

**Speaker notes.** Derivative looks at the slope. Rushing toward the target? Ease off early. It is the reason a tuned robot glides in instead of bouncing. Noisy sensors make D twitchy, so we smooth it.

## Your turn: tune it

*Interactive sim: open this slide in the browser to try it.*

**Speaker notes.** Play with the sliders. Start with Kd at zero and watch the overshoot. Then add Kd and see it calm down. Aim for under 10 percent overshoot and settling under 3 seconds.

## PID in twelve lines

```python
class PID:
    def __init__(self, kp, ki, kd, limit=1.0):
        self.kp, self.ki, self.kd = kp, ki, kd
        self.limit = limit
        self.i = 0.0
        self.prev = None

    def update(self, target, measured, dt):
        e = target - measured
        self.i = max(-self.limit, min(self.limit, self.i + e * dt))
        d = 0.0 if self.prev is None else (e - self.prev) / dt
        self.prev = e
        return self.kp * e + self.ki * self.i + self.kd * d
```

*Clamping the integral is the anti-windup trick from the I slide.*

**Speaker notes.** This is the whole controller. Notice the integral is clamped, and the derivative is skipped on the first call so we do not get a huge kick.

## Tuning by feel, in four steps

- Set Ki and Kd to zero
- Raise Kp until it oscillates, then halve it
- Add Kd until the overshoot calms down
- Add a little Ki to remove the last error

**Speaker notes.** This is the routine I use on real robots. It is not optimal, but it is safe and it works in a few minutes.

## Quick check

**Your robot overshoots and wobbles before it settles. What do you change first?**

- [ ] Raise Kp
- [x] Raise Kd
- [ ] Raise Ki
- [ ] Sample less often

D is the brake. More Kd reduces overshoot and wobble. Raising Kp or Ki usually makes it worse.

**Speaker notes.** Take a guess before revealing. The wobble is a sign we are arriving too fast, and that is the derivative term’s job.

## What to remember

- P pushes, I remembers, D brakes
- Tune P first, then D, then a little I
- Clamp the integral and filter the derivative

> Next: tuning PID on real hardware

**Speaker notes.** That is PID. Next lecture we do this on a real motor, where sample time, windup and noise stop being theory.

---

Free to teach from. A credit back to shakaut.com/learn is appreciated.
