Search requires an index. Run npm run build once, then restart dev.
Search needs internet. Offline lessons: use the sidebar or download a track under Reading preferences (Aa).
Basic Autonomous Programming (OnBot Java)
intermediate50 min
Autonomous period
For the first 30 seconds of each match (length may vary by season), the robot runs without gamepad input. Your @Autonomous OpMode runs during this time.
Autonomous OpMode structure
Extend LinearOpMode, annotate with @Autonomous, and put actions after waitForStart().
@Autonomous(name="MyFirstAutonomous")public class MyFirstAutonomous extends LinearOpMode { @Override public void runOpMode() { // Initialization code here telemetry.addData("Status", "Initialized"); telemetry.update(); // Wait for the game to start (driver presses PLAY) waitForStart(); // Autonomous actions go here while (opModeIsActive()) { // Example: do nothing telemetry.addData("Status", "Running"); telemetry.update(); } }}
Practice Exercise: Your First Autonomous OpMode
Write an autonomous OpMode that drives a motor for 2 seconds, then stops. Use telemetry to display status before, during, and after movement.
Map a DC motor using hardwareMap.
Use waitForStart() to wait for the match to begin.
Set the motor power to drive forward for 2 seconds.
Stop the motor and update telemetry.
Map a DC motor using hardwareMap.
Map a DC motor using hardwareMap.
// Map the motor in your OpMode
DcMotor driveMotor = hardwareMap.get(DcMotor.class, "drive_motor");
// Make sure the name matches your robot configuration!
Use waitForStart() to wait for the match to begin.
Use waitForStart() to wait for the match to begin.
// Wait for the game to start
waitForStart();
Set the motor power to drive forward for 2 seconds.
Set the motor power to drive forward for 2 seconds.
// Drive forward for 2 seconds
driveMotor.setPower(0.5); // 50% power
sleep(2000); // Wait for 2 seconds (2000 ms)
Stop the motor and update telemetry.
Stop the motor and update telemetry.
// Stop the motor and show status
driveMotor.setPower(0.0);
telemetry.addData("Status", "Stopped");
telemetry.update();
Combine all tasks into one working OpMode.
Combine all tasks into one working OpMode.
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
@Autonomous(name = "DriveForwardAuto")
public class DriveForwardAuto extends LinearOpMode {
private DcMotor driveMotor;
@Override
public void runOpMode() {
// Map the motor
driveMotor = hardwareMap.get(DcMotor.class, "drive_motor");
telemetry.addData("Status", "Initialized");
telemetry.update();
// Wait for the game to start
waitForStart();
// Drive forward for 2 seconds
driveMotor.setPower(0.5);
telemetry.addData("Status", "Driving");
telemetry.update();
sleep(2000);
// Stop the motor
driveMotor.setPower(0.0);
telemetry.addData("Status", "Stopped");
telemetry.update();
// Optionally, keep the OpMode alive until STOP is pressed
while (opModeIsActive()) {
idle();
}
}
}