# Proportional Integral Derivative (PID) Control

PID is used everywhere. PID describe how the error term is handled.

This video treats line following as PID for self-driving cars.

Good Lecture Series by Brian Douglas: https://www.youtube.com/playlist?list=PLn8PRpmsu08pQBgjxYFXSsODEF3Jqmm-y

Why make a simpler controller?

- Easy to implement
- Easy to test and troubleshoot
- Easy to understand

The Equation $u(t)=K_{p}e(t)+K_{i}โซ_{0}e(t_{โฒ})dt_{โฒ}+K_{d}dtdโ(e(t))$ where

- $u(t)$ is the control output (in our case is the steering angle we want the car to drive at)
- $K_{p}$,$K_{i}$ and $K_{d}$ are constants that determine how much weight each of the three components (proportional, integral, derivative) contribute to the
- $e(t)$ is the error term. This is the difference between the set point and the parameter we want to maintain around that set point.

Implementation in code (at least the super simple way):

- $e(t)$ is the measured error at time $t$
- $dtde(t)โโฮte_{t}โe_{tโ1}โ$, i.e. take the differences the error between a timestep divided by the time elapsed for a timestep
- $โซe(t)dtโโe(t)ฮt$, i.e sum the errors and multiple it by the time elapsed since the beginning measurement.

Other implementation things you need to think about:

- If your PID controller runs for a long time, $โe(t)โโ$
- Division by very small numbers if you $ฮt$ is very small

### Components of a PID Controller

**Proportional Control**: Steering of the car is proportional to the crosstrack error.**Derivative Control**: To avoid overshooting the car and oscillating around the centerline with a simple proportional control, we add derivative control to counterbalance.**Integral Control**: This is to counterbalance the stead-state error over time. The integral control corrects for the total error so far.

Combining all three of these create the PID controller.

### PID Tuning

There is a science behind tuning, but the most common method is manual tuning.

Ziegler-Nichols method is a way to tune, got it from CARLA

This is a really good guide: https://tlk-energy.de/blog-en/practical-pid-tuning-guide