> For the complete documentation index, see [llms.txt](https://yall.yassrobotics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yall.yassrobotics.com/documentation/tuning/pid-tuning-for-aiming-and-ranging.md).

# PID Tuning for Aiming & Ranging

Once tracking itself is stable (pipeline tuned, [crosshair calibrated](/documentation/tuning/crosshair-calibration.md)), this is the last step: closing the loop between `tx`/`ty` and drivetrain output. This only applies to direct visual servoing off `tx`/`ty`/`ta` (`limelight.getData().targetData.getHorizontalOffset()` / `getVerticalOffset()` / `getTargetArea()`, as in [Basic Setup](/documentation/tutorials/basic-setup.md)); if you're driving off a fused `PoseEstimate` instead, this doesn't apply, so tune your pose estimator's trust filters ([Pose Estimation & Ambiguity](/documentation/understanding/pose-estimation-and-ambiguity.md)) instead.

## Starting gains

Limelight's own aiming/ranging example uses simple proportional-only control as its starting point, a reasonable baseline before you add integral/derivative terms:

| Term            | Starting value | Drives                                                                                                                                                             |
| --------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `kP_aim`        | `-0.1`         | Turn rate, from `tx` (heading error)                                                                                                                               |
| `kP_distance`   | `-0.1`         | Forward/back speed, from `ty` (range error)                                                                                                                        |
| `minAimCommand` | `0.05`         | Deadband floor: the minimum command applied once outside the deadband, so small errors don't get commanded a near-zero output that never overcomes static friction |

The negative sign matters: a positive `tx` (target to the right) should produce a *negative* turn command if your convention is "positive turn = counterclockwise." Get the sign backwards and the loop actively drives away from the target instead of toward it.

## Deadband

Only apply the aim correction once `|tx| > 1.0` degrees (this exact threshold is a starting point, not a hard rule). Without a deadband, the loop chases sensor noise once it's essentially on-target, and oscillates instead of settling.

```java
double tx = limelight.getData().targetData.getHorizontalOffset();
double steer = 0.0;
if (Math.abs(tx) > 1.0) {
    steer = kP_aim * tx;
}
```

## Tuning order

1. **Aim (`tx`) alone first**, with ranging disabled. Confirm the robot settles on heading without oscillating before adding range control.
2. **Range (`ty`/`ta`) alone next**, with aim disabled, same idea: isolate one loop before combining them.
3. **Combine both** once each is independently stable. Combined aim+range is more sensitive to gain that's slightly too high, since heading and distance errors can compound.
4. **Increase `kP` gradually** from the starting values above until the robot responds promptly without overshoot/oscillation at your target distance; back off \~20% from the point oscillation starts.

## See Also

* [Tuning Workflow Overview](/documentation/tuning/tuning-workflow-overview.md)
* [Crosshair Calibration](/documentation/tuning/crosshair-calibration.md)
* [Pose Estimation & Ambiguity](/documentation/understanding/pose-estimation-and-ambiguity.md): the equivalent tuning problem when driving off a fused pose instead of raw `tx`/`ty`.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://yall.yassrobotics.com/documentation/tuning/pid-tuning-for-aiming-and-ranging.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
