Class AutoDrive
An encoder is a sensor on the motor that counts how far it has turned (in "ticks"). The IMU is the sensor in the Control Hub that knows which way the robot is facing (its "heading").
Standard (tank) drive can only move forward and back (direction 0 or 180) and turn;
sideways moves and moveWithTurn(double, double, double, double, double) throw an error, because the robot cannot do them.
BLOCKING ON PURPOSE: each move/turn method WAITS until the move ends, so use these only
in a LinearOpMode autonomous, never in TeleOp. Your mechanisms keep working during a move
through setLoopHook(java.lang.Runnable) (or AutoRoutine, which does that for you). Each one stops when it is done, when it is stuck,
when it times out, or when the OpMode stops. The motors are always stopped at the end.
No wheel ever gets more than DriveConfig.powerLimit.
Why waiting is fine here and nowhere else: docs/guides/non-blocking.md.
AutoDrive auto = new AutoDrive(this, driveConfig, autoConfig, imu);
waitForStart();
auto.move(24, 0, 0.6); // 24 inches forward
auto.move(12, -90, 0.5); // 12 inches right (90 would be left)
auto.turn(90, 0.5); // turn 90 degrees left
auto.turnTo(0, 0.5); // face the starting direction again
Every angle counts counterclockwise, like the IMU: in move(double, double, double), 0 = forward, 90 = left,
180 = back, -90 = right; in turns, positive = left. The full sign rules: docs/conventions.md.
Docs: docs/modules/drive.md (section "AutoDrive"). Tutorial 09 builds a first autonomous with it.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDrives to a point on the field and turns to a heading at the same time, using odometry (mecanum only). -
Constructor Summary
ConstructorsConstructorDescriptionAutoDrive(AutoDriveConfig cfg, boolean tank, double powerLimit, com.qualcomm.robotcore.hardware.DcMotorEx fl, com.qualcomm.robotcore.hardware.DcMotorEx fr, com.qualcomm.robotcore.hardware.DcMotorEx bl, com.qualcomm.robotcore.hardware.DcMotorEx br, DoubleSupplier headingCcwDeg, Runnable headingReset, BooleanSupplier isActive, Runnable idle, Consumer<String> log, LongSupplier clockNanos) For tests and simulations (no robot needed): everything AutoDrive needs, given directly.AutoDrive(com.qualcomm.robotcore.eventloop.opmode.LinearOpMode opMode, DriveConfig driveConfig, AutoDriveConfig autoConfig, com.qualcomm.robotcore.hardware.IMU imu) Sets up the four drive motors (encoders reset, RUN_USING_ENCODER, brake, power 0).AutoDrive(com.qualcomm.robotcore.eventloop.opmode.LinearOpMode opMode, DriveConfig driveConfig, AutoDriveConfig autoConfig, com.qualcomm.robotcore.hardware.IMU imu, BooleanSupplier isActive) LikeAutoDrive(LinearOpMode, DriveConfig, AutoDriveConfig, IMU), with your own "keep going" check: for an OpMode that runs inside another one (a finder inside "Tune and check"), whose ownopModeIsActive()is always false. -
Method Summary
Modifier and TypeMethodDescriptiondouble[]displacementIn(int[] from, int[] to) How far the robot moved between two encoder readings, robot-relative: {forward inches, right inches}.driveTo(Supplier<FieldPose> pose, double xIn, double yIn, double headingDeg, double maxPower, double toleranceIn) driveTo(Supplier<FieldPose> pose, BooleanSupplier ready, double xIn, double yIn, double headingDeg, double maxPower, double toleranceIn) driveTo(java.util.function.Supplier, double, double, double, double, double), checking every loop that the odometry works: ifreadyturns false, the robot stops at once (NO_ODOMETRY).followRoute(Supplier<FieldPose> pose, BooleanSupplier ready, List<double[]> points, RouteDriver.LineCheck clear, double headingDeg, double maxPower, double toleranceIn) Drives a route of field points without stopping at the points on the way: a route fromFieldMap.route(double, double, double, double), in the odometry's frame.int[]Encoder counts, in the order front-left, front-right, back-left, back-right.doubleThe robot's heading, IMU degrees (counterclockwise positive).move(double distanceIn, double directionDeg, double maxPower) Drives a distance in a straight line, robot-relative, without turning.moveWithTurn(double distanceIn, double fieldDirectionDeg, double targetHeadingDeg, double maxPower, double maxTurnPower) Drives a distance in a FIELD direction while turning to a heading at the same time.voidSets the current heading to 0.voidsetLoopHook(Runnable hook) Runs your code once per loop during every move and turn.voidsetStopCondition(BooleanSupplier condition) An extra reason to stop a move early: when this is true, the move ends withSTOPPED.voidstop()Stops all four wheels (brake).turn(double degrees, double maxPower) Turns in place by an amount.turn(double degrees, double maxPower, double timeoutSec) turn(double, double)with its own time limit, for a step that has a time limit of its own.turnTo(double headingDeg, double maxPower) Turns in place to face a heading.turnTo(double headingDeg, double maxPower, double timeoutSec) turnTo(double, double)with its own time limit, for a step that has a time limit of its own.
-
Field Details
-
ODOMETRY_LOST_SEC
public static final double ODOMETRY_LOST_SECDrives to a point on the field and turns to a heading at the same time, using odometry (mecanum only). It keeps correcting with the odometry's pose every loop, so a bump or wheel slip does not matter: it ends where the field point is, not where the wheels think it is.The frame is the odometry's: x inches right, y inches away from the drivers' wall, heading 0 = facing away, counterclockwise +.
BLOCKS (on purpose): returns when it arrives, gets stuck or runs out of time.
- See Also:
-
-
Constructor Details
-
AutoDrive
public AutoDrive(com.qualcomm.robotcore.eventloop.opmode.LinearOpMode opMode, DriveConfig driveConfig, AutoDriveConfig autoConfig, com.qualcomm.robotcore.hardware.IMU imu) Sets up the four drive motors (encoders reset, RUN_USING_ENCODER, brake, power 0). Brake means the wheels hold still at power 0 instead of rolling, so the robot stops where the move ends. Encoders are reset so every autonomous starts counting from 0.- Parameters:
opMode- your autonomous OpMode, usuallythisdriveConfig- motor names, directions and power limit (the same one TeleOp uses)autoConfig- tuning;calibrationConfirmedmust be trueimu- a started IMU, e.g. fromImuSetup.get(com.qualcomm.robotcore.hardware.HardwareMap, java.lang.String, com.qualcomm.hardware.rev.RevHubOrientationOnRobot.LogoFacingDirection, com.qualcomm.hardware.rev.RevHubOrientationOnRobot.UsbFacingDirection)- Throws:
IllegalStateException- ifautoConfig.calibrationConfirmedis false
-
AutoDrive
public AutoDrive(com.qualcomm.robotcore.eventloop.opmode.LinearOpMode opMode, DriveConfig driveConfig, AutoDriveConfig autoConfig, com.qualcomm.robotcore.hardware.IMU imu, BooleanSupplier isActive) LikeAutoDrive(LinearOpMode, DriveConfig, AutoDriveConfig, IMU), with your own "keep going" check: for an OpMode that runs inside another one (a finder inside "Tune and check"), whose ownopModeIsActive()is always false. Moves end with STOPPED as soon as it gives false.- Parameters:
isActive- gives true while the moves may run, false once they must stop
-
AutoDrive
public AutoDrive(AutoDriveConfig cfg, boolean tank, double powerLimit, com.qualcomm.robotcore.hardware.DcMotorEx fl, com.qualcomm.robotcore.hardware.DcMotorEx fr, com.qualcomm.robotcore.hardware.DcMotorEx bl, com.qualcomm.robotcore.hardware.DcMotorEx br, DoubleSupplier headingCcwDeg, Runnable headingReset, BooleanSupplier isActive, Runnable idle, Consumer<String> log, LongSupplier clockNanos) For tests and simulations (no robot needed): everything AutoDrive needs, given directly. The motors must already be set up (directions, encoder mode). For a 2-motor tank drive,blandbrare null.- Parameters:
headingCcwDeg- gives the heading in degrees, counterclockwise positiveisActive- gives false once the OpMode should stopclockNanos- gives the time now in nanoseconds (a fake clock in tests)
-
-
Method Details
-
setLoopHook
Runs your code once per loop during every move and turn. Use it to keep other mechanisms working while the robot drives (for example a lift PID or a flywheel). The code must be quick. Pass null to remove it. -
setStopCondition
An extra reason to stop a move early: when this is true, the move ends withSTOPPED.AutoRoutineuses it to end the main part of autonomous on time for parking. Pass null to remove it. -
move
Drives a distance in a straight line, robot-relative, without turning. Full speed until the last part, then slows down (seetaperStartFractionandslowDownIn). At the distance the wheels brake and it waits (at most 0.3 s, mechanisms still updating) until the robot has stopped.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
distanceIn- inches. A negative distance goes the opposite way.directionDeg- robot-relative direction, counterclockwise like a heading: 0 forward, 90 left, 180 back, -90 rightmaxPower- 0.1 to 1 (0.3 to 0.7 is usual). Never more than the DriveConfig power limit.- Returns:
- how the move ended: DONE, STALLED, TIMED_OUT or STOPPED
- Throws:
IllegalArgumentException- on a standard (tank) drive, for any direction except 0 or 180
-
moveWithTurn
public AutoDrive.Result moveWithTurn(double distanceIn, double fieldDirectionDeg, double targetHeadingDeg, double maxPower, double maxTurnPower) Drives a distance in a FIELD direction while turning to a heading at the same time. The direction stays fixed on the field even while the robot turns. If the move finishes (DONE) but the heading is not there yet, it finishes the turn, inside the same time limit. After a stall it does NOT turn.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
distanceIn- inches (negative = opposite way)fieldDirectionDeg- direction on the field, counterclockwise from where heading 0 points (like a heading): 0 forward, 90 left, 180 back, -90 righttargetHeadingDeg- heading to end at, IMU degrees (counterclockwise positive)maxPower- translation power, 0.1 to 1maxTurnPower- turn power, 0.1 to 1- Returns:
- how the move ended: DONE, STALLED, TIMED_OUT or STOPPED
- Throws:
UnsupportedOperationException- on a standard (tank) drive
-
driveTo
public AutoDrive.Result driveTo(Supplier<FieldPose> pose, double xIn, double yIn, double headingDeg, double maxPower, double toleranceIn) -
driveTo
public AutoDrive.Result driveTo(Supplier<FieldPose> pose, BooleanSupplier ready, double xIn, double yIn, double headingDeg, double maxPower, double toleranceIn) driveTo(java.util.function.Supplier, double, double, double, double, double), checking every loop that the odometry works: ifreadyturns false, the robot stops at once (NO_ODOMETRY). Without it the robot would drive on a frozen pose until the time runs out. -
followRoute
public AutoDrive.Result followRoute(Supplier<FieldPose> pose, BooleanSupplier ready, List<double[]> points, RouteDriver.LineCheck clear, double headingDeg, double maxPower, double toleranceIn) Drives a route of field points without stopping at the points on the way: a route fromFieldMap.route(double, double, double, double), in the odometry's frame. It slows down near each one, and goes on as soon as it is withinRouteDriver.PASS_POINT_INor has slid past it, if the line on from there is clear (clear). So a park around a box costs little time.BLOCKS (on purpose): returns when it arrives, gets stuck or runs out of time. Each line of the route has its own
actionTimeoutSec.- Parameters:
pose- the odometry's pose, read each loop (the loop hook updates it)ready- false while the odometry cannot be trusted: it holds still, and after 0.2 s it endspoints- the points, each {x, y}, inches; the last one is where it endsclear- the map's line check, in the odometry's frame (RouteDriver.checkLines(com.crossbotics.ftchelper.drive.RouteDriver.LineCheck)), or nullheadingDeg- the heading to turn to on the way (counterclockwise +; 0 = facing away)maxPower- 0.1 to 1toleranceIn- close enough at the last point, inches (1 is usual)- Returns:
- DONE, NO_ODOMETRY, STALLED, TIMED_OUT or STOPPED
-
turn
Turns in place by an amount.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
degrees- positive = left (counterclockwise), negative = rightmaxPower- 0.2 to 1 (0.3 to 0.6 is usual)- Returns:
- how the turn ended: DONE, TIMED_OUT or STOPPED
-
turnTo
Turns in place to face a heading.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
headingDeg- target IMU heading, degrees (counterclockwise positive, 0 = start direction)maxPower- 0.2 to 1- Returns:
- how the turn ended: DONE, TIMED_OUT or STOPPED
-
turn
turn(double, double)with its own time limit, for a step that has a time limit of its own.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
degrees- how far to turn, counterclockwise positivemaxPower- 0.2 to 1timeoutSec- seconds it may take; never more than the drive's ownactionTimeoutSec- Returns:
- how the turn ended: DONE, TIMED_OUT or STOPPED
-
turnTo
turnTo(double, double)with its own time limit, for a step that has a time limit of its own.BLOCKS (on purpose): returns when the move ends. Autonomous only; never in TeleOp.
- Parameters:
headingDeg- target IMU heading, degrees (counterclockwise positive, 0 = start direction)maxPower- 0.2 to 1timeoutSec- seconds it may take; never more than the drive's ownactionTimeoutSec- Returns:
- how the turn ended: DONE, TIMED_OUT or STOPPED
-
headingDeg
public double headingDeg()The robot's heading, IMU degrees (counterclockwise positive). -
resetHeading
public void resetHeading()Sets the current heading to 0. -
getWheelTicks
public int[] getWheelTicks()Encoder counts, in the order front-left, front-right, back-left, back-right. On a 2-motor tank drive, the back values repeat the front ones. -
stop
public void stop()Stops all four wheels (brake). -
displacementIn
public double[] displacementIn(int[] from, int[] to) How far the robot moved between two encoder readings, robot-relative: {forward inches, right inches}. Sideways wheel travel is divided bystrafeCorrection(strafeCorrectionLeftfor travel to the left, when set), because mecanum wheels turn more than the robot moves sideways. The Move Tuner uses it to measure how far a move slid past its end.- Parameters:
from- wheel counts fromgetWheelTicks()at the startto- wheel counts fromgetWheelTicks()at the end- Returns:
- {forward inches, right inches}, robot-relative
-