PID Controlling

I’ve been looking into PID control and I’m absolutely lost. How could I learn to use PID control?

3 Likes

Learn to make it work or learn how it works?

Yes, I’ve looked at some documents and I know that my drones use PIDs as well and I’m absolutely lost.

If you are using the TalonSRX for PID control, you should definitely look at this Stryke Force Talon SRX Motor Training Course by 2767. And even if you aren’t using Talons, you can still watch “Chapter 4, Control Loops” for an explanation of how PID works in the Talons, which you can then apply to other PID control loops. This may be a lot to swallow all at once, but I think it would be worth your time to at least skim over some of it.

1 Like

Basically a PID controller is three parts.
The P for proportional: This adjusts your output (usually a motor voltage) based on how far away you are from the target. The more your error is, the more aggressively it will counter.
IIRC, the I or integral helps boost the output the more time you spend just short of the target and the D or derivative boosts the output depending on how fast the error is changing.

I have found that this interactive demo is decent at demonstrating how PID works at a high level.

1 Like

Our team used this Youtube video (step by step) to implement PID.

Also there is very thorough 9 video series from team 2767 Stryke Force that is extremely helpful on you tube.

2 Likes

Here is a good white paper from NI. The first half is an excellent explanation of the theory. If you happen to use Labview, the second half will show you how to tune.

https://frc-pdr.readthedocs.io/en/latest/control/pid_control.html

The tl;dr is that given some sensor input and some desired sensor value (setpoint), your PID controller will output a weighted sum of the current error, the past error, and the change in error.

3 Likes

Alright, so if you haven’t been able to follow anything else, I’ll try my take at it.

PID control is short for proportional, integral, and derivative control. These three mathematical terms are a function of the error in the system. For a PID system to work, you must have a sensor with a current state and a desired state.

The first thing you want to do is calculate the error caused by your current state and your desired state. The way to calculate this is as follows.
error = desired state - current state

Now that an equation for the error exists, it can be used in the following proportional, integral, and derivative equations.

A surprising amount of FRC systems can be controlled with just a proportional control method, and it’s also the simplest! Using the proportional method, the equation has a constant § which you can tune to adjust the aggressiveness of the response the system has to changes in error. The way to calculate the proportional output is as follows.
output_P = P * error
As the name implies, the output is directly proportional to the error in the system. This allows the system to slow as it gets nearer to the desired state or gain speed as it gets farther from the desired state.

The next letter in PID is I, which stands for integral. The integral in the PID system is generally used for what’s called steady-state error. Steady-state error occurs when a force such as friction or gravity is keeping your system from reaching the desired state. Once again, the constant I will be used, but this time multiplied the sum of the error. The sum of the error can be obtained with this equation.
errorSum = errorSum + error
As is probably obvious, the sum of the error could build quite quickly, which would make the system require an I-zone. However, out of simplicity, I am going to skip over I-zone.
The final equation for the integral term becomes:
output_I = I * errorSum

The final term in PID control is D, or derivative. As the name implies, the derivative term takes the change in error to account for the system overshooting the desired state. Once again, we use the constant, D and multiply it this time by the change in error over time. You can calculate the change in error over time as follows.
Δerror = (current_error - previous_error) / Δtime
Implementing this into a method, you must store your current error into a previous error variable to use it in the equation. To find Δtime, it is just the speed the loop runs at (.02 seconds is what I believe FRC uses). Otherwise, Δtime = previous time - current time.
The final equation for the D term is:
output_D = D * Δerror

Finally, all of the terms have been solved and can be inserted into the final equation.
output = output_P + output_I + output_D
The final output can be applied to whatever you’re controlling with your system such as a motor. Remember to tune the constants to improve the system.

If you have any questions, either reply to me or direct message me and I should receive an email. There are also lots of other helpful people on Chief Delphi that would be happy to help as well. If someone catches a mistake I may have made, reply or direct message me and I will try to fix it as soon as I get the chance.

Good luck with your ventures! PID is always exciting and fun to get working!

Edit 1: Fixed error equation.

2 Likes

If you like reading, here’s a some total self promotion:

Overview of “control systems” in general

Understanding system behavior through math

The Math of a PID controller

Interactive shooter wheel tuning example

Interactive arm tuning example

2 Likes

You got the equation for error backwards, unless you like providing negative controller gains.

2 Likes

Whoops, thank you! fixed.

The official FRC documentation has quite a bit to say about PID: https://docs.wpilib.org/en/latest/search.html?q=pid&check_keywords=yes&area=default