# INTRO

In this article, I'm going to explain PID Controller - what is it, why do we use it, and how it works - as briefly as I can, so that you can have at least a basic understanding for tuning our FRC robot. I will try my best to explain like you are 5 years old. There might be mistakes so plz point them out and leave a comment below.

# Explanation

We are not going to start with the complicated math formula but rather a simple and easy-to-understand situation.

# Proportional Controller

Imagine you have a drone. And you want raise it all the way up to 50 meters high. Sounds pretty easy huh? But currently we are not even controlling the drone. So what should we do to the propeller motors?

Let it spin.

So we can construct a proportional(P) controller to approach our goal. What the proportional controller does is that it's going to set the out put to e(t) * Kp. "e" stands for error which is the distance between the drone and 50 meters in this case, and Kp is a constant we set up after tuning it a lot of times times. So theoretically the error will be eliminated over time, like this:

Start with an error of 50 meters.
Say we have a Kp of 0.5.
When t = 0s, e = 50m, v = 0.5 * 50 = 25m/s;
When t = 1s, e = 25m, v = 0.5 * 25 = 12.5m/s;
When t = 2s, e = 12.5m, v = 0.5 * 12.5 = 6.25m/s;
...

So we should get a e(t) graph like this:

I'm graph!

What a beautiful curve. It seems like it is enough to reach our goal with only a proportional controller. So far so good.

However, once we apply it to a real drone, weird things will happen. If you record the e(t) graph of a real P-controller-only drone, it will end up like this:

I'm graph!

It looks ugly isn't it. So now we have to talk about what happened to our drone, or even, if there is something we're missing.

Our first graph looks perfect because we assumed the drone was in a non-physical world instead of a real world environment, therefore we ignored two serious problems: overshooting, which is generally caused by inertia, and steady-state. Let's talk about steady-state first.

# Steady-State

Consider this question: what will happen after the drone was lifted to 50 meters high? Since the error is 0 meters, the output v will become 0 * 0.5 = 0m/s. Compare this to a plane at a height of 5,000 meters above the ground and suddenly you turn off the engine. Well it's a little bit different, because once the error is not 0 our drone will go back up. We still don't want this to happen though. We have to make sure the output is non-zero when it reaches the goal.

This is when we need to add another controller.

# Integral Controller

The integral(I) controller capable of eliminating steady-state. What an integral controller does it that it's going to add the accumulation, which is the integral of 1/Ki * e(t) to the output. Since the error is always a positive number before reaching the goal, the output should also be a positive when it reaches the goal. However, a I controller is not able to resolve overshooting. Actually, it is going to aggravate overshooting if you have a really big Ki or Kp.

# Overshooting

As I mentioned above, overshooting is caused by inertia, which is a very very basic physical concept that you should have learned in elementary school. If you have never heard of it, you really should blame your elementary school teacher. I'm going to explain it here anyways. Imagine you are in a car at a velocity of 100m/s(this is really fast I have to say), and suddenly it decelerates to 0m/s and coincidentally you forget to buckle up your seat belt. What will happen? You are probably going to smash the windshield with your poor head and fly out of the car at a really high speed. This tells us the necessity of buckling up the seat belt that objects has the natural tendency to resist changes in its state of motion. Same with our drone, we did not decelerate the motors before it reaches the goal so it will continue to go up a bit.

Therefore, we have to add one last controller to our drone.

# Derivative Controller

The Derivative(D) controller is used to address our second problem - overshooting. Derivative controller adds a third term, Kd * d/dt * e(t) to our main controller. Let me explain derivative first.

# Derivative(or instant rate of change)

Derivative is really just a fancy word for representing the instant rate of change of a function. If you have taken grade 9 math then you should be familiar with slope - and yeah the slope is the derivative of a linear function. Here is the general formula for calculating the derivative of every functions:

ps: I wrote a derivation of the power rule in another article xxx, plz go and check it if you are interested.

# Calculation

So the calculation of D term should be:

PID Derivative Term

\[ u_d(t) = K_d \cdot \frac{e(t_1) - e(t_0)}{\text{aSmallAmountOfTime}} \]

"aVerySmallAmountOfTime" depends on the time between control loop iterations. Before reaching our goal, the term is always a negative number which will decrease the output speed. Also the faster the drone approaches the goal, the smaller the number is, so that it can effectively neutralize overshooting.

# Definition: what is PID

We went through P, I and D separately and problems each of them solved. Now we are going to have an official definition of PID, and its math formula:

A proportional–integral–derivative controller (PID controller or three-term controller) is a feedback-based control loop mechanism commonly used to manage machines and processes that require continuous control and automatic adjustment.

PID Formula

\[ u(t) = K_p e(t) + K_i \int_0^t e(\tau)\, d\tau + K_d \frac{de(t)}{dt} \]

And now with PID controller we get a perfect e(t) graph:

I'm graph!

What a beautiful curve.