> 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-beginner-guide/autonomous-encoders-the-imu-and-heading-correction.md).

# Autonomous: encoders, the IMU, and heading correction

Autonomous means the robot must move known distances and turn to known angles without a driver.

### Encoders

Every FTC motor has a built-in encoder that reports tick counts.

To convert ticks to distance:

```
ticks per inch = (ticks per motor revolution × gear ratio) / (wheel circumference in inches)
```

Common encoder setup:

```java
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
```

Use `RUN_TO_POSITION` for simple position moves:

```java
int target = 1000;
motor.setTargetPosition(target);
motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motor.setPower(0.5);

while (opModeIsActive() && motor.isBusy()) {
    telemetry.addData("pos", motor.getCurrentPosition());
    telemetry.update();
}
motor.setPower(0);
```

### The IMU

The IMU reports robot orientation. The heading value you use most is yaw.

```java
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;

IMU imu = hardwareMap.get(IMU.class, "imu");

IMU.Parameters parameters = new IMU.Parameters(
    new RevHubOrientationOnRobot(
        RevHubOrientationOnRobot.LogoFacingDirection.UP,
        RevHubOrientationOnRobot.UsbFacingDirection.FORWARD
    )
);
imu.initialize(parameters);
imu.resetYaw();
```

Read heading like this:

```java
double heading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES);
```

The hub orientation must match the physical mounting.

### Angle wrapping

Headings wrap at `±180°`. Normalize the error so the robot turns the short way.

```java
public double angleWrap(double degrees) {
    while (degrees > 180)  degrees -= 360;
    while (degrees < -180) degrees += 360;
    return degrees;
}
```

### Heading correction with a P controller

```java
public void turnToHeading(double targetDeg) {
    double kP = 0.02;

    double error = angleWrap(targetDeg - getHeading());

    while (opModeIsActive() && Math.abs(error) > 1.0) {
        error = angleWrap(targetDeg - getHeading());

        double turnPower = kP * error;
        turnPower = Math.max(-0.5, Math.min(0.5, turnPower));

        frontLeft.setPower(-turnPower);
        backLeft.setPower(-turnPower);
        frontRight.setPower(turnPower);
        backRight.setPower(turnPower);

        telemetry.addData("target", targetDeg);
        telemetry.addData("current", getHeading());
        telemetry.addData("error", error);
        telemetry.update();
    }

    frontLeft.setPower(0);
    backLeft.setPower(0);
    frontRight.setPower(0);
    backRight.setPower(0);
}

private double getHeading() {
    return imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES);
}
```

Tune `kP` until the turn is crisp without oscillation.

### Driving straight while holding heading

```java
double targetHeading = getHeading();
double kP = 0.02;

double error = angleWrap(targetHeading - getHeading());
double correction = kP * error;

frontLeft.setPower(drivePower - correction);
backLeft.setPower(drivePower - correction);
frontRight.setPower(drivePower + correction);
backRight.setPower(drivePower + correction);
```

This combination gives you a strong first autonomous foundation.


---

# 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-beginner-guide/autonomous-encoders-the-imu-and-heading-correction.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.
