WPILib PID Controller
WPILib PID Controller
WPILib has awesome documentation on how to use the PID controller. You can find it here.
The WPILib PID controller class can be used regardless of the motor controllers and sensors being used. It automatically handles the feedback calculation for you.
You can create a PID controller in a single line using the following code.
PIDController pid = new PIDController(kP, kI, kD);The kP, kI, and kD are the constants for the proportional, integral, and derivative terms of the PID controller.
Using this PID controller, you can set the setpoint and the input, and the PID controller will automatically calculate the output for you.
Spark motor = new Spark(0);Encoder encoder = new Encoder(0, 1);double targetSpeed = 5000;
PIDController pid = new PIDController(0.1, 0.0, 0.0);
public void periodic() { // Reads the current speed of the flywheel // Then calculates the output of the PID controller // To get it to the target speed motor.set(pid.calculate(encoder.getVelocity(), targetSpeed));}This example only uses the proportional term for ease of understanding.