## FANDOM

A PID is a control algorithm that takes a desired end position for something (a motor for example) and determines the most efficent way to reach it from the current position. It stands for Proportional Integral Derivative.

The difference between the desired output value and the input value is the 'error'. The PID algorithm determines a factor to multiply the error, and adds it to the input. Over time this will act to reduce the error, bringing the output value to the input. The PID determines the multiplying factor by adding three terms.

Proportional: The proportional term equals some constant times the error. (Kp * Error)

Integral: The integral term adds the previous errors, then multiplies by a constant. (Ki * Integral of Error)

Derivative: This term is the derivative of the error multiplied by a constant. (Kd * dError / dt)

Total adjustment = Error * [ (Kp * Error) + (Ki * Integral of Error) + (Kd * dError / dt)]

The links below explain mroe clearly why there is a need for three terms. In general, the proportional term causes overshoots in adjustment, the integral term prevents the adjustment from getting stuck (ie it may get stuck at a very low error, meaning d/dt is zero, and the proportional adjustment may not be able to overcome friction). The derivative adjustment simply means the input is adjusted more for large errors, and then the adjustment reduces as the error goes down.

## Useful Links Edit

Very good article explaining the theory behind a PID http://www.pacontrol.com/PID.html

## Simple pseudocode for programming a PID Edit

Here is a simple software loop that implements the PID algorithm in its 'ideal, parallel' form:

```previous_error = 0
integral = 0
start:
error = setpoint - actual_position
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*error) + (Ki*integral) + (Kd*derivative)
previous_error = error
wait(dt)
goto start
```

## Useful Links Edit

Good article explaining PID basics, with good graphs http://www.freestudy.co.uk/plc/pid.pdf

Very good article explaining the theory behind a PID http://www.pacontrol.com/PID.html