The Basics of PID Controllers

Jan 28, 2020

If you are new to FRC, you may hear a lot about PID controllers. You may see intimidating flow charts that branch and reconnect, and have absolutely no idea what is going on. In this post, I will try to help you understand what a PID controller is, why you would want one, and hopefully make it be less intimidating.

The Vocabulary

When you read about PID Controllers, you likely will hear a lot of vocabulary. This section will briefly define some of the key terms.

PIDF

A PIDF Controller is an algorithm that uses a sensor and runs math on it to account for errors that may occur while driving.

Proportional

The Proportional gain will make changes that are proportional to the error. This means that the bigger the error, the bigger the change.

Integral

The Integral gain accounts for the sum of all of the past-experienced errors. It is very effective at accounting for long-term errors, but isn't as helpful for immediate errors.

Derivative

The Derivative gain accounts for the change from the previous error to the current error. It is very effective with making tiny changes to your loop, but is ineffective at major changes.

Feedforward

The Feedforward gain is added to the PID loop after all other computations are done. It takes your kF Constant and multiplies it by your set-point to stay on track. This is more helpful for drive-train situations rather than a position control (such as an elevator).

Gain

A gain is the word used to describe one of the components of a PID loop. They are passed in to the function as constants. You may hear them referred to by their first letter and gain (such as p gain), by the variable name (such as kP), by their letter (such as P) or, usually less frequently, the full word (Proportional).

Set-point

A set-point is the target that you want your robot to achieve. It can be in a variety of forms, but usually will be a velocity or position. It is easier to think about this as a target.

Error

The error is the difference between a sensor's current value and its setpoint. The error is what is fed into the PID loop for compensation.

Oscillation

Oscillation is the word that describes a behavior that a PID controller shows as when there is too much p gain. What happens is that the robot will over-shoot and then over-correct. The end result is a behavior where the robot makes erratic movements which hopefully resolve themselves.

Different P values produce different effects. The purple line represents oscillation, while the green is close to ideal

What is a PID Controller?

Now that the vocabulary is out of the way, I will go into more detail about what a PID Controller is. At its core, a PID controller takes an error and does math to help correct a path.

There are many other kinds of controllers, but a PID controller is very easy to implement and is widely used in FIRST Robotics. When you see documents about PID diagrams, you will probably see something like this

Controlador PID en lazo cerrado by Arturo Urquizo on Wikimedia Commons

I don't like these diagrams because, while the math can be helpful and is very simple once you have taken Calculus AB, it can be a huge deterrent to younger people and, at least in FRC, the math doesn't really matter. Talon SRX/FX and Spark MAX motor controllers have built in PID controllers, and WPILib also has their own method. Instead, I prefer a diagram like the one below, since it is much more approachable.

Basics of a PID Controller

This diagram helps show how the controller works: we take the error, plug our gains into the math, and we get a new output with a new error. As we approach our set-point, our error decreases so the math applied to the gains is reduced, and robot will eventually reach its set-point and stop.

Why Do I Care?

PID Controllers can help make significantly more reliable robots, especially with elevators, arms, and autonomous.

Arms and Linear Controls

With a PID controlled elevator, the robot will correct any errors it may experience and keep the elevator at the same level. During testing last year, we were able to apply a fairly strong amount of force to our PID controlled elevator and it resisted the downwards force and stayed in place. The same can happen with arms. With arms, the downwards forces change as the arm moves. Therefore, trying to control it with open-loop controls can be very difficult. PID allows it to automatically adjust to any physical differences and stay in the same place regardless of external situations.

Drivetrains

Autonomous driving is critical to making the most out of the first 15 seconds of a match. PID can make your robot drive consistently and it can use encoder feedback to make sure that your robot stays on track. If you have read my guide to WPILib Trajectory, you will have seen that PID is critical to our autonomous success, especially when using Talon SRX, Talon FX, or Spark MAX motor controllers

WPILib Trajectory Autonomous Guide
One of our projects for the start of Build Season was learning the WPILib Trajectory feature which acts as a path planning and following library. In this post, I will cover what we did to achieve path planning using WPILib Trajectory. The example code is at the repo below: alexander-beaver/TalonTr…

Tips and Tricks

Don't Max Out the Controller

Your PID controller needs to have the ability to adjust to problems it encounters. If you tell your controller to go at the max velocity, it won't be able to adjust and will continue to have error. For this reason, my team usually caps our velocity control at around 80-85% of the max velocity. You probably can run it in the mid 90 percent, but we feel more confident in our systems if there is a good amount of padding where error correction can take place.

Tune Right

Tuning can take a lot of time in the beginning, but as you become used to tuning, you can be much more efficient. I am now able to tune our drivetrain in about 15 minutes, after spending hours trying to tune as a rookie. The steps are as follows:

  • If your robot needs f-gain (anything with velocity control), start by setting kF so that it gets very close
  • Set a different set-point, and continue tuning kF
  • Once you are close, start setting kP. Start with a tiny amount, and begin to increment or decrement by powers of 10 until you get close.
  • Continue changing your kP value by gradually smaller factors until it reliably gets incredibly close to your set-point without significant oscillation.
  • It should be good enough at this point. If not, try adding tiny amounts of kD until you get the behavior you want

Use SmartDashboard

SmartDashboard/Shuffleboard can be an invaluable tool for PID tuning. When tuning our robot, we get the P, I, D, and F values from Smart Dashboard and then set the control loops to follow those values. We also put velocity and error on a graph so that we can easily see what is happening in detail.

If you want an even better process, you can use Shuffleboard recording to view back the data later.

You can learn more about Shuffleboard on the WPILib Docs.

Use Motor Controllers to Your Advantage

Some of the newer motor controllers, including Talon SRX, Talon FX, and Spark MAX, allow you to run a PID controller on the motor controller rather than on the RoboRIO. These usually run faster, are more reliable, and do not eat up RoboRIO resources, so I would highly recommend running your PID controller on the motor controller, rather than on the RoboRIO.

Most motor controllers with PID support allow for velocity, position, and motion profile controls, which means that you can do almost anything you would ever need to do just by setting a setpoint.

Alex Beaver

Hi! I'm Alex, and I am on leadership and software on Team 100. I want to help others through the FRC process by sharing tips and tricks on how to be effective in FRC.