Sample Code - Tank Drive

Tank Drive
This OpMode class extends LinearOpMode and provides the complete structure for running a tank drive with four motors. Remember to customize the motor object names (leftFrontMotor, rightFrontMotor, leftRearMotor, and rightRearMotor) and any other hardware mappings based on your robot's configuration.

You can save this class in your FTC project and use it as a basis for your tank drive program. Make sure to configure the motor ports in the hardwareMap.get() calls to match your actual hardware setup.

Don't forget to test the OpMode on your robot and make any necessary adjustments or additions to suit your specific requirements.


---code---

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;

public class TankDriveOpMode extends LinearOpMode {

    private DcMotor leftFrontMotor;
    private DcMotor rightFrontMotor;
    private DcMotor leftRearMotor;
    private DcMotor rightRearMotor;

    @Override
    public void runOpMode() {
        // Initialize motor objects
        leftFrontMotor = hardwareMap.get(DcMotor.class, "left_front_motor");
        rightFrontMotor = hardwareMap.get(DcMotor.class, "right_front_motor");
        leftRearMotor = hardwareMap.get(DcMotor.class, "left_rear_motor");
        rightRearMotor = hardwareMap.get(DcMotor.class, "right_rear_motor");

        // Set motor directions
        leftFrontMotor.setDirection(DcMotor.Direction.FORWARD);
        rightFrontMotor.setDirection(DcMotor.Direction.REVERSE);
        leftRearMotor.setDirection(DcMotor.Direction.FORWARD);
        rightRearMotor.setDirection(DcMotor.Direction.REVERSE);

        // Wait for the start button to be pressed
        waitForStart();

        while (opModeIsActive()) {
            // Read joystick inputs
            double leftPower = -gamepad1.left_stick_y;
            double rightPower = -gamepad1.right_stick_y;

            // Apply power to the motors
            leftFrontMotor.setPower(leftPower);
            rightFrontMotor.setPower(rightPower);
            leftRearMotor.setPower(leftPower);
            rightRearMotor.setPower(rightPower);

            // Stop the motors when joystick inputs are in the neutral position
            if (Math.abs(leftPower) < 0.1 && Math.abs(rightPower) < 0.1) {
                leftFrontMotor.setPower(0);
                rightFrontMotor.setPower(0);
                leftRearMotor.setPower(0);
                rightRearMotor.setPower(0);
            }

            // Continue to update telemetry and perform other tasks
            telemetry.update();
        }
    }
}

© Copyright CircuitBreakers - All Rights Reserved

Made with ‌

Easy Website Builder