> For the complete documentation index, see [llms.txt](https://docs.cb21829.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cb21829.org/ftc-java-advanced-guide/feedback-control-pid-and-pidf.md).

# Feedback control: PID and PIDF

Everything advanced builds on feedback control. You measure the current state, compare it to the target, and command an output that reduces the error.

### The idea

A PID controller has three terms:

* **P** — reacts to current error.
* **I** — accumulates error over time.
* **D** — reacts to how fast the error changes.

```
output = Kp·error + Ki·∫error·dt + Kd·(d error / dt)
```

### A reusable PID controller

```java
import com.qualcomm.robotcore.util.ElapsedTime;

public class PIDController {
    private double kP, kI, kD;
    private double integralSum = 0;
    private double lastError = 0;
    private boolean hasRun = false;
    private final ElapsedTime timer = new ElapsedTime();

    public PIDController(double kP, double kI, double kD) {
        this.kP = kP; this.kI = kI; this.kD = kD;
    }

    public double calculate(double target, double state) {
        double error = target - state;

        if (!hasRun) {
            lastError = error;
            timer.reset();
            hasRun = true;
        }

        double dt = timer.seconds();
        double derivative = (error - lastError) / dt;
        integralSum += error * dt;

        double output = (kP * error) + (kI * integralSum) + (kD * derivative);

        lastError = error;
        timer.reset();
        return output;
    }

    public void reset() {
        integralSum = 0; lastError = 0; hasRun = false;
    }
}
```

Example use on a lift:

```java
PIDController liftPID = new PIDController(0.01, 0.0, 0.0004);

while (opModeIsActive()) {
    double power = liftPID.calculate(targetTicks, lift.getCurrentPosition());
    lift.setPower(power);
}
```

### Adding F with feedforward

Feedback reacts after error appears. Feedforward predicts the needed output before the error grows.

Common feedforward terms:

* `kG` for gravity
* `kV` for velocity
* `kA` for acceleration

```java
double power = liftPID.calculate(target, lift.getCurrentPosition()) + kG;
lift.setPower(power);
```

### Practical tuning

1. Set `kI = 0` and `kD = 0`.
2. Raise `kP` until the mechanism reaches target quickly.
3. Add `kD` to reduce overshoot.
4. Add a little `kI` only if steady-state error remains.

Use FTC Dashboard for live tuning when possible.

`RUN_TO_POSITION` is fine for simple cases. Use your own PIDF when you need custom behavior, feedforward, or motion profiling.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cb21829.org/ftc-java-advanced-guide/feedback-control-pid-and-pidf.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
