Tutorial 12: Organize a whole robot
In the five files: Robot.java lists the parts, and the library makes, updates and stops them for
you (The five files).
Build it by hand to see what that structure does inside: a Robot class and one class per mechanism
(a “subsystem”), shared by TeleOp and autonomous.
You will build: a whole robot program (drive, lift, claw) split into small classes, with a TeleOp and an autonomous that share them.
You need: Tutorials 08-10 done. A drivetrain, a lift (lift, optional lift_bottom) and a claw
servo (claw).
Full programs: the robot folder:
Robot, Lift, LiftHeights, Claw, MainTeleOp, MainAuto (and RobotSettings from Tutorial 09).
1. Why organize?
Section titled “1. Why organize?”By now one OpMode has the drive, the IMU, the lift with its limits and PID, the claw, the buttons and telemetry. Autonomous needs most of it AGAIN. Copies drift apart: someone fixes the lift limit in TeleOp and forgets autonomous.
The fix: each mechanism gets its own class that knows everything about it. OpModes only say WHAT to do (“lift to HIGH”, “open the claw”), never HOW.
robot/ LiftHeights.java the set heights (no hardware: testable, Tutorial 13) Lift.java lift motor, limits, PID, modes Claw.java claw servo and its positions Robot.java makes all the mechanisms, updates them, stops them MainTeleOp.java buttons -> robot actions (short!) MainAuto.java a route -> robot actions (short!)RobotSettings.java drive config, auto tuning, IMU (Tutorial 09)In your TeamCode, put these in a folder robot (package org.firstinspires.ftc.teamcode.robot).
2. The set heights: LiftHeights
Section titled “2. The set heights: LiftHeights”public enum LiftHeights { BOTTOM(0), LOW(800), MIDDLE(1600), HIGH(2400);
public final int ticks; public LiftHeights up() { return this == HIGH ? HIGH : values()[ordinal() + 1]; } public LiftHeights down() { return this == BOTTOM ? BOTTOM : values()[ordinal() - 1]; } public static LiftHeights closestTo(int position) { ... }}An enum is a fixed list of named values. LiftHeights.HIGH is clearer than 2400 all over the
code, and the numbers live in ONE place. up()/down() step through the list. ordinal() is a value’s
place in the list (BOTTOM = 0, LOW = 1, …), and values() is the whole list. The enum uses no
hardware, so Tutorial 13 can test it.
3. A subsystem: Lift
Section titled “3. A subsystem: Lift”The Lift class holds everything from Tutorial 08:
public class Lift { private final SafeMotor motor; private final PidController pid = ...; private LiftHeights target = null; // null = by hand private double handPower = 0;
public Lift(HardwareMap hardwareMap) { ...MotorSetup, SafeMotor, optional switch... }
public void goTo(LiftHeights height) { target = height; pid.reset(); } public void stepUp() { ... } public void stepDown() { ... } public void manual(double power) { handPower = power; if (power != 0) target = null; }
public void update() { // once every loop motor.update(); if (motor.isWaitingForHoming()) target = null; if (target == null) motor.setPower(handPower); else motor.moveTo(target.ticks, pid); }
public boolean isAtTarget() { ... } public void stop() { ... } public void addTelemetry(Telemetry telemetry) { ... }}The real file adds these:
MAX_TICKS = 2600is above HIGH (2400) and below the real top, so the lift never runs into its physical stop under power.- The constructor resets the encoder, so turn the robot on with the lift DOWN. Nothing moves in a constructor, so it is safe in INIT.
update()runsmoveToevery loop, also at the target: that holds the lift up against gravity. Before homing (Tutorial 08), the zero is not known, soupdate()cancels a set height.- After hand control,
stepUp()steps from the set height closest to the lift (LiftHeights.closestTo). stop()also forgets the target and the hand power, so the nextupdate()does not start the lift again.
The pattern for every subsystem:
- Constructor (the method with the class’s name, run by
new Lift(...)): get and set up the hardware. - Commands that only change state (
goTo,manual): quick, safe to call from a button. update(): the one place that talks to the motors, once per loop.- Questions (
isAtTarget) andaddTelemetry. stop().
Claw is the same idea, smaller: open(), close(), toggle(), isOpen(). Its servo is private,
so no other class can send it a wrong position. A servo cannot report where it is, so Claw remembers
what it last told the servo.
4. The whole robot: Robot
Section titled “4. The whole robot: Robot”Other classes can read public final fields (robot.lift), but they can never swap them for another
object.
public class Robot { public final IMU imu; public final Lift lift; public final Claw claw;
public Robot(HardwareMap hardwareMap) { BulkReads.auto(hardwareMap); imu = RobotSettings.imu(hardwareMap); lift = new Lift(hardwareMap); claw = new Claw(hardwareMap); }
public void update() { lift.update(); } public void stop() { lift.stop(); } public void addTelemetry(Telemetry telemetry) { lift.addTelemetry(telemetry); claw.addTelemetry(telemetry); }}The drivetrain is NOT in Robot: TeleOp drives with MecanumDrive, and autonomous with AutoDrive.
Both read RobotSettings.drive(), so they still match. Only the lift needs update(), for its PID.
5. TeleOp becomes short
Section titled “5. TeleOp becomes short”Robot robot = new Robot(hardwareMap); // the IMU setup makes the way the robot faces NOW heading 0MecanumDrive drive = new MecanumDrive(hardwareMap, RobotSettings.drive());
ControllerBindings controls = new ControllerBindings() .bindPress(Pad.G1, Btn.BACK, robot.imu::resetYaw) .bindPress(Pad.G2, Btn.DPAD_UP, robot.lift::stepUp) .bindPress(Pad.G2, Btn.DPAD_DOWN, robot.lift::stepDown) .bindPress(Pad.G2, Btn.A, () -> robot.lift.goTo(LiftHeights.BOTTOM)) .bindPress(Pad.G2, Btn.Y, () -> robot.lift.goTo(LiftHeights.HIGH)) .bindPress(Pad.G2, Btn.RB, robot.claw::toggle);The button list IS the driver’s control sheet. robot.lift::stepUp is short for () -> robot.lift.stepUp().
The loop runs controls.update, robot.lift.manual(stick), robot.update(), the drive and telemetry.
6. Autonomous uses the same classes
Section titled “6. Autonomous uses the same classes”robot = new Robot(hardwareMap);AutoDrive auto = new AutoDrive(this, RobotSettings.drive(), RobotSettings.auto(), robot.imu);auto.setLoopHook(robot::update); // the lift keeps working during every move...robot.claw.close();robot.lift.goTo(LiftHeights.HIGH); // rises during the next move (loop hook)auto.move(30, 0, 0.6);waitForLift(2.0); // at most 2 s: a stuck lift cannot use up the autorobot.claw.open();pause(0.3); // let the piece fallThe loop hook runs robot.update() again and again during every move. Without it, the lift gets no
updates (no PID) while the robot drives.
waitForLift and pause keep calling robot.update() while they wait (Tutorial 10), so the lift keeps
holding its height. Do not use sleep() while a mechanism must hold position: sleep stops all
your code, including the lift’s PID.
7. Hand-over from autonomous
Section titled “7. Hand-over from autonomous”TeleOp sets up the IMU again, and that makes “0” the way the robot faces now. So MainAuto saves its
heading with PoseStore.save(...) in finally, and MainTeleOp’s DriverHeading adds it back. Then
field-centric “forward” is right from the start of TeleOp. Start autonomous with the robot facing
straight away from the driver (DriverHeading).
Run it
Section titled “Run it”- Copy the
robotfolder andRobotSettingsinto TeamCode (fix the package lines). - Run Main Auto: the claw closes, the lift rises while the robot drives, the claw opens, and the lift lowers on the way back.
- Run Main TeleOp: telemetry shows “Heading: from autonomous”. The D-pad steps the lift through LOW, MIDDLE, HIGH. The right bumper toggles the claw. The stick takes over the lift.
If something goes wrong
Section titled “If something goes wrong”| Problem | Fix |
|---|---|
| Lift does nothing in TeleOp | The loop does not call robot.update(). |
| Lift sags during auto moves | auto.setLoopHook(robot::update) is missing. |
| Lift drops while the claw opens | The code uses sleep() instead of pause(). |
| Two OpModes behave differently | An OpMode sets up something that belongs in Robot/RobotSettings. Move it there. |
Try this
Section titled “Try this”- Add an
Intakesubsystem within(),out(),stop(). Bind it on gamepad 2. - Give
LiftascoreHeightFor(...)that picks a height from the game situation. - Make
Robot.addTelemetryshow only the lines the driver needs. Add adebugflag for the rest.
Next: Tutorial 13: Test your code without a robot.
Look it up: Safety module (SafeMotor), Control module,
Drive module: DriverHeading, Auto module (AutoDrive).
Something wrong on this page? Tell us with a short GitHub form.