Back to Tutorials
FTCIntermediateProgramming18 min read

PIDF Tuning for Turrets

Written by William

Turrets are high-speed rotational mechanisms that demand precise closed-loop control. A well-tuned PIDF controller keeps your turret on target instantly with minimal overshoot. This guide walks through the full tuning process.

1

Why PIDF for Turrets?

A simple power command can't account for varying loads, motor inconsistency, or external forces. PIDF (Proportional-Integral-Derivative-Feedforward) continuously corrects error between your target angle and actual angle.

For turrets specifically, the F (feedforward) term compensates for gravity or friction at different angles, and the D term prevents oscillation around the target.

2

Hardware Setup

Use a motor with a encoder connected to your expansion hub/control hub. Make sure the encoder is plugged in the same slot as the target motor.

Set your motor to RUN_USING_ENCODER mode so you can adjust the PIDF without issue

code
PIDFCoefficients pidfNew = new PIDFCoefficients(20.3025, 0, 0, 20.7020); //f=9
                flywheelMotor.setPIDFCoefficients(DcMotor.RunMode.RUN_USING_ENCODER, pidfNew);
3

Implementing the PIDF Loop

In our flywheel loop, we set the velocity to be a certain number, the PIDF will then account for other factors.

code
if (flywheelMode == 1) {
                            flywheelMotor.setVelocity(HIGH_VELOCITY);
                            flywheeling = true;
                            gamepad2.setLedColor(255, 0, 255, Gamepad.LED_DURATION_CONTINUOUS);
4

Tuning P and F values

We recomend making a seperate file to tune your PIDF, the simplest way to make this file is to follow Brogan M. Pratt's video on making the file. We have made our own tuning file, but I have personally tested his file and works fine.

P is the value that changes the recovery time or how fast the turret will recover after something is shot. Increasing P will make the turret recover faster and decreasing will make it recover slower.

The F value is how powerful the shot will be, or how close the shot power will be to the velocit of the turret. Be weary when chaning this number.

Change the values in small increments, this is what we recomend below.

code
double[] stepSizes = {10.0, 1.0, 0.1, 0.001, 0.0001};
5

Using Panels for Live Tuning

Install Panels and expose your PIDF constants as @Config fields. This lets you adjust values in real time from a browser without re-deploying code.

Graph your error and motor output in Panel's telemetry panel. A good tune shows error dropping.