Paul's Tutorials - logo3-3PID Part 2: Implementing a PID



Now that you know what a PID is, you will learn how to apply it to the robot.

Safety Note

The most important thing with robotics in general is safety, as you well know by now. One important thing to remember is that the more automated the machine becomes, the more dangerous it can be. As a PID automates the robot to a certain degree, you should always be VERY CAREFUL when you are testing one for the first time (although you should always be very careful anyway).

PIDController

WPILib comes with a builtin PID controller class, named, quite creatively, PIDController. Declare it like you would any other robot component (as a pointer).

Initialization

The PIDController constructor has 5 basic parameters for initialization.

PIDController::PIDController(float p, float i, float d, PIDSource* source, PIDOutput* output)
float p
the value of P, or the proportional constant
float i
the value of I, or the integral constant
float d
the value of D, or the derivative constant
PIDSource* source
the sensor input to be used with the PID controller. All sensors inherit the PIDSource class
PIDOutput* output
The motor controller output to be used with the PID controller. All motor controllers inherit the PIDOutput class

Use

Firstly, the PIDController has an on/off switch, which is off to start. In order to use it, it must first be enabled.

void PIDController::Enable()

Similarly, it can be disabled:

void PIDController::Disable()

Once enabled, all you need to do is set the setpoint. As soon as the robot reaches that point in the code, it will automatically move the output such that the input matches the setpoint.

void PIDController::SetSetpoint(float setpoint)

You can also check what the current setpoint is, as well.

float PIDController::GetSetpoint()

Tuning a PID

Writing a PID is one thing. Making it work is another thing.

Tuning a PID can be dangerous on any number of levels, as you have no idea of how the robot will respond. When you are tuning a PID, you should ALWAYS have your finger on the Enter key so that, when things go wrong, you can stop the robot before it does any serious damage.

PID Setpoints

You may remember the PIDGet() function from lesson 3-1. Funny how it begins with P-I-D, huh? That's because this is the function that the PIDController reads from when it runs the calculations. This means that all of your setpoints need to match the value returned by PIDGet().

To get these values, simply write the output of the PIDGet() function for whatever sensor you are using to the driver station, and then use those values for the setpoints.

Prerequisites

Before you start tuning a PID, make sure your code does the following:

Setting the setpoint and enabling the PID controller should be run when a button on the controller is pressed. This means that when the operator lets go of the button, the PIDController should be disabled.

You should start with the following PID constants:

Finding the right values

When you are tuning the PID, you need to ask yourself the following questions. Below each question is a potential solution for a problem that you might see.

Knowing how much to change a P, I, or D value is a learned skill and does not really follow a definite algorithm. As such it cannot be taught through a tutorial. If you need help, ask someone who has tuned a PID before.

Activity

The content in this lesson could be tested, but most of the PID-capable subsystems on available robots require that you know the content from lesson 10.

If you do have access to a robot with a system that is ready to run a PID that only uses one sensor and one motor, then write code to implement a PID on that system. Otherwise, continue to the next lesson.


← 3-2 PID Part 1: PID Theory 3-3 PID Part 2: Implementing a PID 3-4 PID Part 3: Advanced PID Programming →