> 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/reference-examples-complete-copy-pasteable.md).

# Reference examples (complete, copy-pasteable)

### A complete competition TeleOp

```java
package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.CRServo;

@TeleOp(name = "Full TeleOp", group = "Competition")
public class FullTeleOp extends LinearOpMode {

    private DcMotor frontLeft, backLeft, frontRight, backRight;
    private DcMotor lift;
    private Servo claw;
    private CRServo intake;

    private static final double CLAW_OPEN   = 0.0;
    private static final double CLAW_CLOSED = 0.55;
    private static final int    LIFT_MIN    = 0;
    private static final int    LIFT_MAX    = 2200;

    private boolean clawClosed = false;
    private boolean lastRB     = false;

    @Override
    public void runOpMode() {
        frontLeft  = hardwareMap.get(DcMotor.class, "frontLeft");
        backLeft   = hardwareMap.get(DcMotor.class, "backLeft");
        frontRight = hardwareMap.get(DcMotor.class, "frontRight");
        backRight  = hardwareMap.get(DcMotor.class, "backRight");
        lift       = hardwareMap.get(DcMotor.class, "lift");
        claw       = hardwareMap.get(Servo.class,   "claw");
        intake     = hardwareMap.get(CRServo.class, "intake");

        frontLeft.setDirection(DcMotor.Direction.REVERSE);
        backLeft.setDirection(DcMotor.Direction.REVERSE);

        frontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        backLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        frontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        backRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        lift.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);

        claw.setPosition(CLAW_OPEN);

        telemetry.addLine("Ready. Press PLAY.");
        telemetry.update();
        waitForStart();
        if (isStopRequested()) return;

        while (opModeIsActive()) {
            double speed = gamepad1.left_bumper ? 0.35 : 1.0;

            double y  = -gamepad1.left_stick_y;
            double x  =  gamepad1.left_stick_x * 1.1;
            double rx =  gamepad1.right_stick_x;
            double denom = Math.max(Math.abs(y) + Math.abs(x) + Math.abs(rx), 1.0);

            frontLeft.setPower(((y + x + rx) / denom) * speed);
            backLeft.setPower(((y - x + rx) / denom) * speed);
            frontRight.setPower(((y - x - rx) / denom) * speed);
            backRight.setPower(((y + x - rx) / denom) * speed);

            double liftInput = -gamepad2.left_stick_y;
            int liftPos = lift.getCurrentPosition();
            if (liftInput > 0 && liftPos >= LIFT_MAX) liftInput = 0;
            if (liftInput < 0 && liftPos <= LIFT_MIN) liftInput = 0;
            lift.setPower(liftInput);

            boolean rb = gamepad2.right_bumper;
            if (rb && !lastRB) {
                clawClosed = !clawClosed;
                claw.setPosition(clawClosed ? CLAW_CLOSED : CLAW_OPEN);
            }
            lastRB = rb;

            if (gamepad2.right_trigger > 0.1) {
                intake.setPower(gamepad2.right_trigger);
            } else if (gamepad2.left_trigger > 0.1) {
                intake.setPower(-gamepad2.left_trigger);
            } else {
                intake.setPower(0);
            }

            telemetry.addData("Speed mode", gamepad1.left_bumper ? "SLOW" : "FULL");
            telemetry.addData("Lift pos", liftPos);
            telemetry.addData("Claw", clawClosed ? "CLOSED" : "OPEN");
            telemetry.update();
        }
    }
}
```

### A complete basic autonomous

```java
package org.firstinspires.ftc.teamcode;

import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;

@Autonomous(name = "Basic Auto", group = "Competition")
public class BasicAuto extends LinearOpMode {

    private DcMotor frontLeft, backLeft, frontRight, backRight;
    private IMU imu;

    private static final double TICKS_PER_REV = 537.7;
    private static final double WHEEL_DIAM_IN = 3.78;
    private static final double TICKS_PER_INCH =
            TICKS_PER_REV / (WHEEL_DIAM_IN * Math.PI);

    @Override
    public void runOpMode() {
        frontLeft  = hardwareMap.get(DcMotor.class, "frontLeft");
        backLeft   = hardwareMap.get(DcMotor.class, "backLeft");
        frontRight = hardwareMap.get(DcMotor.class, "frontRight");
        backRight  = hardwareMap.get(DcMotor.class, "backRight");

        frontLeft.setDirection(DcMotor.Direction.REVERSE);
        backLeft.setDirection(DcMotor.Direction.REVERSE);

        imu = hardwareMap.get(IMU.class, "imu");
        imu.initialize(new IMU.Parameters(new RevHubOrientationOnRobot(
                RevHubOrientationOnRobot.LogoFacingDirection.UP,
                RevHubOrientationOnRobot.UsbFacingDirection.FORWARD)));
        imu.resetYaw();

        telemetry.addLine("Auto ready.");
        telemetry.update();
        waitForStart();
        if (isStopRequested()) return;

        driveInches(24, 0.5);
        turnToHeading(90);
        driveInches(12, 0.5);
        turnToHeading(0);
        driveInches(-12, 0.5);
    }

    private void driveInches(double inches, double power) {
        int target = (int) (inches * TICKS_PER_INCH);

        for (DcMotor m : new DcMotor[]{frontLeft, backLeft, frontRight, backRight}) {
            m.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
            m.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        }

        double startHeading = getHeading();

        while (opModeIsActive() &&
               Math.abs(frontLeft.getCurrentPosition()) < Math.abs(target)) {

            double error = angleWrap(startHeading - getHeading());
            double correction = 0.02 * error;

            double dir = Math.signum(inches);
            frontLeft.setPower(dir * power - correction);
            backLeft.setPower(dir * power - correction);
            frontRight.setPower(dir * power + correction);
            backRight.setPower(dir * power + correction);

            telemetry.addData("target", target);
            telemetry.addData("pos", frontLeft.getCurrentPosition());
            telemetry.update();
        }
        stopDrive();
    }

    private void turnToHeading(double targetDeg) {
        double error = angleWrap(targetDeg - getHeading());
        while (opModeIsActive() && Math.abs(error) > 1.0) {
            error = angleWrap(targetDeg - getHeading());
            double turn = Math.max(-0.5, Math.min(0.5, 0.02 * error));
            frontLeft.setPower(-turn);
            backLeft.setPower(-turn);
            frontRight.setPower(turn);
            backRight.setPower(turn);
            telemetry.addData("target", targetDeg);
            telemetry.addData("current", getHeading());
            telemetry.update();
        }
        stopDrive();
    }

    private void stopDrive() {
        frontLeft.setPower(0);  backLeft.setPower(0);
        frontRight.setPower(0); backRight.setPower(0);
    }

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

    private double angleWrap(double deg) {
        while (deg > 180)  deg -= 360;
        while (deg < -180) deg += 360;
        return deg;
    }
}
```

### A reusable hardware class

```java
package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.Servo;

public class RobotHardware {

    public DcMotor frontLeft, backLeft, frontRight, backRight;
    public DcMotor lift;
    public Servo claw;

    public void init(HardwareMap hwMap) {
        frontLeft  = hwMap.get(DcMotor.class, "frontLeft");
        backLeft   = hwMap.get(DcMotor.class, "backLeft");
        frontRight = hwMap.get(DcMotor.class, "frontRight");
        backRight  = hwMap.get(DcMotor.class, "backRight");
        lift       = hwMap.get(DcMotor.class, "lift");
        claw       = hwMap.get(Servo.class,   "claw");

        frontLeft.setDirection(DcMotor.Direction.REVERSE);
        backLeft.setDirection(DcMotor.Direction.REVERSE);
    }

    public void drive(double y, double x, double rx) {
        double denom = Math.max(Math.abs(y) + Math.abs(x) + Math.abs(rx), 1.0);
        frontLeft.setPower((y + x + rx) / denom);
        backLeft.setPower((y - x + rx) / denom);
        frontRight.setPower((y - x - rx) / denom);
        backRight.setPower((y + x - rx) / denom);
    }
}
```

### Field-centric driving

```java
double y  = -gamepad1.left_stick_y;
double x  =  gamepad1.left_stick_x;
double rx =  gamepad1.right_stick_x;

double heading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);

double rotX = x * Math.cos(-heading) - y * Math.sin(-heading);
double rotY = x * Math.sin(-heading) + y * Math.cos(-heading);
rotX = rotX * 1.1;

double denom = Math.max(Math.abs(rotY) + Math.abs(rotX) + Math.abs(rx), 1.0);
frontLeft.setPower((rotY + rotX + rx) / denom);
backLeft.setPower((rotY - rotX + rx) / denom);
frontRight.setPower((rotY - rotX - rx) / denom);
backRight.setPower((rotY + rotX - rx) / denom);

if (gamepad1.options) imu.resetYaw();
```

### Snippet cheat sheet

```java
intake.setPower(gamepad2.right_trigger);

if (gamepad2.dpad_up)    wrist.setPosition(0.9);
if (gamepad2.dpad_left)  wrist.setPosition(0.5);
if (gamepad2.dpad_down)  wrist.setPosition(0.1);

if (gamepad2.y) {
    lift.setTargetPosition(2200);
    lift.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    lift.setPower(0.8);
}

double clamped = Math.max(-1.0, Math.min(1.0, someValue));

double input = gamepad1.left_stick_y;
if (Math.abs(input) < 0.05) input = 0;

double volts = hardwareMap.voltageSensor.iterator().next().getVoltage();
```


---

# 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/reference-examples-complete-copy-pasteable.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.
