2022PythonExamples/15 - PID Control
2022-12-26 14:40:10 -05:00
..
Challenge Adding a bunch more readme content 2022-12-26 14:40:10 -05:00
README.md Adding a bunch more readme content 2022-12-26 14:40:10 -05:00
robot.py Committing some more work 2022-12-13 20:35:53 -05:00

15 - PID Control

This is going to be an intermediate level topic that is not required for you to have a functional robot for competition. It can be a very powerful solution for managing the interaction between a sensor and a system (like a drivetrain, arm, elevator, shooter, etc.) but it isn't necessarily required for those systems to work.

What is PID?

PID stands for Proportional, Integral, and Derivative. These three "pieces" are combined together to create a form feedback control for a motor or collection of motors on your robot. Each piece plays a different role, and more often than not you won't see more than 2 of the pieces used at any given time in FRC. The explanation below of each of the pieces, and the additional components needed for functional PID control, is not going to be fully comprehensive, but I'll do what I can to ensure that I cover enough to get you exploring.

First thing to understand is the controller has to have an input. This input is defined by you. It could be the rate or the current angle from a Gyroscope, the rate or the current position of the robot as received from an Encoder, and so on.

The second thing to understand is the setpoint, this is the position or the speed you want the PID controller to help you achieve, the units of the setpoint come from the units of your input. Each component (the P, I, and D) plays a different role in getting to your desired state as represented by the setpoint. Note that each of the mathematical functions represented by the three letters are looking at the difference between the current setpoint, and the most recent input from the sensor into the controller.

The third thing to understand is the tolerance of your controller. PID controllers can be tuned to perform very accurate calculations but there is a limit to how close they can get you to being exactly on target. The tolerance is the amount of error you're ok with before the PID controller says it's at the desired setpoint and stops telling motors to do work. For example, if your setpoint is 60, and the toleraance is +/- 1, then the range of values that is considered "at setpoint" for the controller is 59-61. Tolerance is something you have to evaluate for yourself between the amount of time you want to spend tuning the PID versus how much error you're willing to have left over when the PID controller stops doing work.

The next 3 values will talk about are multipliers for the P, I, and D portions of the control loop. If the value for any of these is 0, then the result of that particularly mathematical process is ignored by the controller, and doesn't influence the overall output provided to motors.

P, or Proportional, is the amount of effort the controller puts in to close the gap between your desired setpoint, and your current input value. Sometimes, you can get away with just having a value for P, and the I and D values can be 0. You will basically always have a value for P.

I, or Integral, is a calculation performed to determine the area under a curve on a graph, but at a single point, rather than the range between two points. The I portion informs the controller of what you've been doing previously, and uses that information to influence what happens next. In FRC, this value is usually ignored, i.e. is 0.

D, or Derivative, is a calculation perform to determine the slope of a line, but at a single point, rather than the range between two points. The D portion informs the controller what might happen or need to happen next, and uses that information to influence the output of the controller. It's not uncommon to see teams have relatively small values for D. To help compensate for an issue called oscillation, which is where the P value is so large that it causes the system to constantly overshoot the setpoint, then overcompensate and overshoot in the opposite direction, overcompensate again and overshoot, and so on. Small amounts of D can suppress these osciallations fairly easily, with ample time spent on tuning, of course.

Where can I use PID Control?

Lots of different places. PID Control has been available to FRC for a long time which is why I show it here in these examples, it's fairly versatile and can handle the work of a lot of systems. Drivetrains, elevators, arms, shooters, you name it, PID control has probably done it. You're implementation and you're willingness to spend time on finding values for P, I, and D and tuning those values is what will make PID control useful to you.

How do I find values for P, I, D?

This is where things get tricky. It can be very hard to find values for P, I, and D depending on the system. I'll describe the manual way that can work for many systems, and then mention a more automated mechanism you can use and research on your own.

First, if you're manually tuning your PID controller, assume your robot is never going to drive at full speed when using PID control. Because of the manual effort in testing required to figure out the PID values, a safe speed is half speed in both directions, forward and back. In the example, you'll see that I've written a small function (method/behavior) called clamp. It takes in a value, plus a high/low range, if the value is higher than the high value, the high value is returned, if it's lower than the low value, then low is returned, if the value is between high and low, then the value itself is returned. This makes it so you can constrain your motors to a specific speed range and keep you and your robot safe (or at least safer) from out of control behavior.

For the tuning we'll be doing, you can ignore I (set it to 0), as I mentioned before, I is not often used in FRC and can be very tempermental to tune. Just leave it be.

While tuning, slowly begin increasing P (with I and D set to 0), adding small amounts each time. What constitutes a small amount varies from system to system, use your best judgement, but starting off between .01 and .1 is usually safe. If the system your tuning takes off and seems out of control, go smaller.

Overtime as you increase P, one of two things will happen, either your system will get to the setpoint you want, or it will begin to oscilate. If it gets to your setpoint, your done. Leave I and D at 0 and revel in your accomplishment. If it begins to oscilate, that's when you start adding small values to D. For many systems, adding .001 to .01 to start with is pretty safe. Continue to increase D until the oscillations stop and you're still able to achieve your setpoint. If you get to this point, you're done!

Sometimes increasing D can cause your system to constantly fall short of its setpoint, this is called steady state error. In this situation, you need to, very slowly, increase P, or decrease D, by very small increments, until the problem is solved. Depending on the scenario, you may need to increase P, or you may not. You should be able to go with either option, but make sure you write down the values that got you to the "steady state error" issue, so you can go back if your tinkering doesn't work out.

Now, I mentioned an automated mechanism. As of this moment, I wouldn't call this mechanism a full on replacement for manual tuning. But if you're willing to learn the software, you can get some very close values for P and D that may even work as well as you want them to immediately after running the program.

The software used for automating the calculation of the values is called SysID. It's not new software, but I wouldn't call it "mature" software either. It takes a bit of getting used to to get through it's sometimes errant quirkiness. I'm hoping that through the next several seasons it's ease of use will improve. But for the work that it does, the difficulties and learning curve can be overlooked.

The values that SysID calculates for P and D are estimates, based on SysID actually capturing real world data about the system you want to make a PID Controller for, and the parameters you provide for the calculation beyond the sampled data.

SysID will upload custom code to your robot based on the parameters you specify for what motor controllers to use, what sensors to observe, etc, and then have you run several routines on your robot to identify different characteristics about how the motors behave when certain amounts of voltage are applied. By observing these values, SysID can calculate P and D values that should work in most situations, even at full speed (although your mileage may vary).

SysID is more of an advanced topic, but you can learn more in the WPILib documentation, note that some portions of this part of the documentation are very dense.

Is PID Control the only option for controlling motors from sensor input?

Absolutely not, there are several others available to you that are useful for various types of control already available in WPILib. They aren't covered here, because they lean more towards the advanced end of robot programming (at least most of them do). If you want to explore more, you can start here in the WPILib documentation.