Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nilayp/3e7675dee11bb2c5a8f945c2c8b48002 to your computer and use it in GitHub Desktop.
Save nilayp/3e7675dee11bb2c5a8f945c2c8b48002 to your computer and use it in GitHub Desktop.
This auto mode should move forward, turn left and go until the robot gets within 12 inches of an object
@Autonomous(name = "My FIRST Java OpMode", group = "Autonomous")
public class MyFIRSTJavaOpMode extends LinearOpMode {
// Declare hardware variables for the drive motors
private DcMotor backLeftDrive;
private DcMotor backRightDrive;
// Declare color and distance sensors
private ColorSensor color1;
private DistanceSensor distanceSensor;
@Override
public void runOpMode() {
// Initialize hardware for the motors
backLeftDrive = hardwareMap.get(DcMotor.class, "backLeftDrive");
backRightDrive = hardwareMap.get(DcMotor.class, "backRightDrive");
// Initialize the color sensor
color1 = hardwareMap.get(ColorSensor.class, "color1");
// Initialize the distance sensor
distanceSensor = hardwareMap.get(DistanceSensor.class, "distance1");
// Set motor directions with reversed configuration
backLeftDrive.setDirection(DcMotor.Direction.REVERSE);
backRightDrive.setDirection(DcMotor.Direction.FORWARD);
// Set zero power behavior
backLeftDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
backRightDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// Reset and set encoder modes
backLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Display initialization message
telemetry.addData("Status", "Initialized");
telemetry.addData("Distance (inches)", distanceSensor.getDistance(DistanceUnit.INCH));
telemetry.update();
// Wait for the start button to be pressed
waitForStart();
// Autonomous actions start here
if (opModeIsActive()) {
// Drive forward for 1 second
driveForward(0.5, 1000);
// Turn left using a timed duration of 6.5 seconds
turnLeftTimed(0.5, 6500); // Power 0.5, duration 6500 ms (6.5 seconds)
// Drive forward until 12 inches from an object
driveUntilDistance(0.5, 12.0);
}
}
// Method to drive forward
public void driveForward(double power, int duration) {
backLeftDrive.setPower(power);
backRightDrive.setPower(power);
// Display motor power, encoder values, and distance sensor reading in telemetry
telemetry.addData("Left Motor Power", backLeftDrive.getPower());
telemetry.addData("Right Motor Power", backRightDrive.getPower());
telemetry.addData("Left Encoder", backLeftDrive.getCurrentPosition());
telemetry.addData("Right Encoder", backRightDrive.getCurrentPosition());
telemetry.addData("Distance (inches)", distanceSensor.getDistance(DistanceUnit.INCH));
telemetry.update();
// Keep driving for the specified duration
sleep(duration);
// Stop the motors
backLeftDrive.setPower(0);
backRightDrive.setPower(0);
// Update telemetry after stopping
telemetry.addData("Left Motor Power", backLeftDrive.getPower());
telemetry.addData("Right Motor Power", backRightDrive.getPower());
telemetry.addData("Left Encoder", backLeftDrive.getCurrentPosition());
telemetry.addData("Right Encoder", backRightDrive.getCurrentPosition());
telemetry.addData("Distance (inches)", distanceSensor.getDistance(DistanceUnit.INCH));
telemetry.update();
}
// Method to turn left using a timed duration
public void turnLeftTimed(double power, int duration) {
// Set power for turning left: left motor moves backward, right motor moves forward
backLeftDrive.setPower(-power);
backRightDrive.setPower(power);
// Display motor power and distance sensor reading in telemetry while turning
telemetry.addData("Turning Left - Left Motor Power", backLeftDrive.getPower());
telemetry.addData("Turning Left - Right Motor Power", backRightDrive.getPower());
telemetry.addData("Distance (inches)", distanceSensor.getDistance(DistanceUnit.INCH));
telemetry.update();
// Sleep for the specified duration (6.5 seconds in this case)
sleep(duration);
// Stop the motors after the turn
backLeftDrive.setPower(0);
backRightDrive.setPower(0);
// Final telemetry update after motors stop
telemetry.addData("Left Motor Power", backLeftDrive.getPower());
telemetry.addData("Right Motor Power", backRightDrive.getPower());
telemetry.addData("Distance (inches)", distanceSensor.getDistance(DistanceUnit.INCH));
telemetry.update();
}
// Method to drive forward until an object is 12 inches away
public void driveUntilDistance(double power, double targetDistanceInches) {
backLeftDrive.setPower(power);
backRightDrive.setPower(power);
// Continue driving until distance sensor detects an object within the target distance
while (opModeIsActive()) {
// Get the distance in inches from the sensor
double distance = distanceSensor.getDistance(DistanceUnit.INCH);
// Check if the distance is less than or equal to the target distance
if (distance <= targetDistanceInches) {
backLeftDrive.setPower(0);
backRightDrive.setPower(0);
telemetry.addData("Status", "Object detected within 12 inches, stopping.");
telemetry.addData("Distance (inches)", distance);
telemetry.update();
break;
}
// Update telemetry with current distance
telemetry.addData("Distance (inches)", distance);
telemetry.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment