# Module 4.2: PID Control Intuition and Practical Tuning

- Course: [Course 4: Feedback Control & Robot Dynamics](https://roboticsconcepts.dev/courses/feedback-control-robot-dynamics.md)
- HTML Page: https://roboticsconcepts.dev/courses/feedback-control-robot-dynamics/pid-control-intuition-and-tuning/
- Key Concepts: Proportional (spring) & Derivative (damper), Integral action & windup, Overshoot, settling time & sensor noise

PID control has been around for over a century, and honestly, it's still the workhorse running underneath most robots you've ever seen. Once you stop treating Kp, Ki, and Kd as mysterious tuning knobs and picture them as a physical spring, a shock absorber, and a patient memory bank, tuning stops feeling like guesswork.

## P is a spring, D is a shock absorber

The Proportional term (Kp · e) acts like an invisible rubber band or coil spring tied between the arm and the target angle. Crank Kp up and the spring gets stiffer: the arm snaps toward the target faster and sags less under gravity. Crank it too high without anything to slow the arm down, though, and the arm flies right past the target and bounces up and down like a car with blown shocks.

That's where the Derivative term (Kd · ė) comes in. Instead of looking at where the arm is, D looks at how fast the error is changing — essentially how fast the joint is moving. It behaves like a viscous oil damper or shock absorber, resisting rapid motion so the arm glides onto the target without ringing.

## I cleans up steady-state droop — until it winds up

Even a well-damped PD controller still sags slightly against constant gravity, because a spring has to stay stretched to hold weight. The Integral term (Ki · ∫ e dt) fixes that by keeping a running sum of past error over time. As long as the arm sits even half a degree low, that sum keeps growing and quietly adds extra upward torque until the steady-state error reaches zero.

The catch shows up when something blocks the arm or the motor hits its torque limit. While the arm is pinned, the integral sum keeps piling up like a coiled spring — a headache called integral windup. The moment the obstacle moves, that huge stored integral blasts the arm into a nasty overshoot.

## Interactive Sandbox: Gravity-Loaded Single-Joint Arm PID Tuner

Try tuning a classic PID response in the sandbox below: set Kp = 32, Kd = 0, Ki = 0 and notice the bouncy overshoot. Slowly raise Kd toward 6–8 until the ringing disappears. Then watch the steady-state error readout, add Ki = 12, and see how it pulls the settled angle cleanly onto the dashed target.

## Why cranking D too high hurts on real encoders

In a clean simulation, derivative gain feels like free stability — crank Kd up and oscillations vanish. On real hardware, joint encoders measure position in discrete ticks and carry electrical jitter. Taking a numerical derivative (Δθ / Δt) amplifies high-frequency sensor noise by 1/Δt.

If your loop runs at 1 kHz and Kd is too aggressive, the motor starts buzzing, heating up, and vibrating against phantom velocity spikes. That's why practical controllers low-pass filter the velocity estimate and keep Kd just high enough for clean damping.

## Parallel PID control law

```text
τ = Kp · e(t)  +  Ki · ∫ e(τ) dτ  +  Kd · (de/dt)
```

P pulls proportionally to current error (spring), I accumulates persistent error over time (droop remover), and D opposes fast motion (damper).

## Check Your Intuition

**Question:** Your robot arm reaches its target angle quickly, but overshoots and rings back and forth five times before settling. Which gain adjustment fixes that ringing first?

- [ ] Increase Ki so the integral term pushes harder
- [✓] Increase Kd to add velocity damping, or slightly lower Kp
- [ ] Set Kd to zero so friction isn't fought

**Explanation:** Ringing and overshoot mean the virtual spring (Kp) has too little damping (Kd). Adding derivative damping absorbs kinetic energy as the arm approaches the target.

## Where You'll See This in Modern Robotics

Most quadrupeds and manipulators trained with reinforcement learning actually output target joint angles into a fast PD loop (Ki = 0, moderate Kp and Kd) so the robot stays compliant instead of fighting contacts with a winding integrator.

## Companion Python Notebook: 02 · Gravity-Loaded PID Tuning & Windup Lab

Tune Kp, Ki, and Kd on a gravity-loaded pendulum arm, observe integral windup under torque saturation, and test anti-windup clamping.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-4/02_pid_control_tuning.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-4/02_pid_control_tuning.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-4/02_pid_control_tuning.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-4/02_pid_control_tuning.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-4/02_pid_control_tuning.py`
