top of page
Search

# The Math Behind PID Control

## What is PID Control?

PID stands for Proportional Integral Derivative. It is one of the most important puzzle pieces for robot control and movement. Basically, it's a way of controlling something, like a wheel or an arm, using information gathered by the robot's surroundings. Data for PID is gathered by sensors, like encoders, range sensors, light sensors, ultrasound sensors, and gyros. The robot uses this data to determine what to do.

## A Read The Docs example on PID Control:

Let’s say you have a cold room, like 10 degrees. You want to warm it up to a nice 23 degrees (Celsius). Luckily you have a heater. You set it’s thermostat to 23, and it starts heating. It heats as fast as it can, and quickly gets to 23 degrees. It immediately turns off. However, the coils on the heater are still warm, and continue to heat the air for a while after. The room heats up to 25, before the coils cool down, and the room loses heat to the environment. It dips down to 23 degrees, and the heater turns on - but it takes time for the coils to turn on, and during this time the room cools down to 21 degrees. This oscillation around the set point slowly dies out, over a long period of time.

PID is designed to intelligently approach the target to reach it as quickly as possible. So in this example, the heater would have turned off before it hit 23, say at 21 degrees, such that it naturally warms up to 23.

In robotics, the same concept can be applied. Many teams use PID control to drive during autonomous, using encoders as their sensor, shooting, using cameras as their sensor, or rotating, using gyros as their sensor.

## Calculating PID

The PID equation is:

## Proportional

Proportional control uses a constant (kP) to control how much something can move. We can re-arrange this equation to be P = 1/(error^2). (Note: P is 1/error^2 because you want the output to be 1/error. )

As you can see here, the distance from the target is getting closer to zero. Generally for FRC P is sufficient for most cases, going further is a bit overkill, but might be necessary in some situations.

## Integral

When controlling a motor with just P, you may find that it oscillates. This happens because it has too much power when it gets to the set point, and it overshoots. Then when it tries to correct, it overshoots again, and this cycle continues. One way to reduce this is to lower P. This could have some bad side effects though. By reducing P, your motor may not get all the way to where you want it to. It may be off by a few degrees or rotations. To overcome this steady-state error, an Integral gain is introduced.

If you’ve taken calculus, you know the integral is the area under a curve or line. It’s the same with PID. The Integral gain is the sum of all the past error. This means the gain will increase more and more the longer the motor isn’t where it’s supposed to be.

## Derivative

Derivative gain works by calculating the change in error. By finding this change, it can predict future system behavior, and reduce settling time. It does this by applying a brake more or less. This can be useful if it is imperative that you don’t overshoot. This isn’t even used in FRC much, but if you find yourself with long settling times, it may help to introduce a Derivative gain, but generally this is overkill.

## Basic use for FRC

I will not go into depth on implementing PID here, but all you need to know for now is that there are two ways of using PID, doing the calculations on your own or using WPILib’s PIDController.