PID
What is PID?
PID is one of the most common examples of a closed loop control system (feedback controller).
The name PID comes from its parts: P, I, and D which stand for proportional, integral, and derivative. If you've done any calculus, these values should look somewhat familiar. Though, you do not need to know any calculus to understand how PID works.
P (Proportion)
This is the main part of PID. The larger the error is, the more the robot moves to compensate.
For some applications, this is the only part of PID that is necessary. However, this can lead to oscillations around the target point since the robot will overshoot and then have to correct itself back and forth.
D (Derivative)
We're going to skip I for now and come back to it later.
The D in PID stands for "derivative" (aka - how fast is our error moving). This is used to slow the robot down if it's moving too fast. The faster the error is changing, the more the robot will slow down the output to compensate. This is useful to prevent overshooting and oscillations around the target point.
I (Integral)
With just P and D, the robot may struggle to reach the target point due to a phenomenon called steady state error. This is due to the fact that as the robot gets closer to the target point, the error gets smaller, which means the P gain has less and less effect on the robot's movement.
Lets say you have a flywheel that you want to maintain at a certain speed. The error in this case is the difference between the target speed and the actual speed. As the flywheel gets closer to the target speed, the error gets smaller, which means both P and D approach zero. Since the flywheel is fighting friction, air resistance, and other forces, it'll likely never be able to reach the target speed, let alone maintain it.
This is where the I gain comes in.
The I gain is used to accumulate the error over time, which allows the robot to compensate for steady state error. The longer the robot struggles to reach the target point, the more the I gain will accumulate, which will eventually allow the robot to reach the target point.
Be careful when using the I gain, as it can lead to another phenomenon called integral windup. This is where the I gain accumulates too much error and causes the robot to overshoot and oscillate uncontrollably.
If you're finding that the mechanism is struggling to reach the target point after tuning, perhaps you should be using a feedforward controller?
Example
// Make a PID controller with the following gains:
// P_gain = 0.006
// I_gain = 0
// D_gain = 0.01
PIDController pidController = PIDController(0.006f, 0.0f, 0.01f);
void opcontrol() override
{
while (true) {
// Define the expected position of the motor encoder (in ticks)
float expectedPosition = 1000.0f;
// Get the actual position of the motor encoder (in ticks)
float actualPosition = sampleMotor.getPosition();
// Error is the difference between these two
// (How far away are we from our expected position)
float error = expectedPosition - actualPosition;
// Update the PID Controller
float output = pidController.update(error);
// Move the motor based on the PID output
sampleMotor.move(output);
// ...
}
}
PID Tuning
This is just one of many strategies for tuning a PID controller, but it is by far the most common. Depending on your situation, this guide may or may not work for your specific application.
1. Set P, I, and D gains to 0
2. Gradually increase the P gain until you see oscillation
Initially, you should see the mechanism slowly approaching its target speed/position. As you increase the P gain more, it'll approach more quickly. This results in the mechanism oscillating back and forth around the target as it repeatedly overshoots.
If the mechanism is oscillating uncontrollably, then the P gain is too high.
3. Gradually increase the D gain until the oscillations are eliminated
Remember, the D gain will slow the mechanism down if its going too fast.
If you see the mechanism oscillate before it reaches its target point, then the D gain is too high.
4. Gradually increase the P and D gains until the mechanism quickly reaches its target point without oscillating
Make sure to test different starting points and different target points. What might work for one position may not work for other positions.
5. Evaluate whether or not you need I gain
The I gain may not be applicable for the majority of situations. If you're finding that the mechanism is struggling to reach the target point after tuning, perhaps you should be using a feedforward controller?