Skip to content

Commit

Permalink
Mecanum drive teleop (#1)
Browse files Browse the repository at this point in the history
It worked at the metting.
  • Loading branch information
penguinencounter authored Jul 2, 2024
1 parent e03a37f commit 85f036a
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.firstinspires.ftc.teamcode;

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

@TeleOp
public class MecanumTeleOp extends LinearOpMode {
@Override
public void runOpMode() {
DcMotor frontLeft = hardwareMap.dcMotor.get("frontLeft");
DcMotor backLeft = hardwareMap.dcMotor.get("backLeft");
DcMotor frontRight = hardwareMap.dcMotor.get("frontRight");
DcMotor backRight = hardwareMap.dcMotor.get("backRight");

frontLeft.setDirection(DcMotorSimple.Direction.REVERSE);
backLeft.setDirection(DcMotorSimple.Direction.REVERSE);

waitForStart();
if (isStopRequested()) return;

while (opModeIsActive()) {
double y = -gamepad1.left_stick_y;
double x = gamepad1.left_stick_x * 1.1;
double rx = gamepad1.right_stick_x;

// Denominator is the largest motor power (absolute value) or 1
// This ensures all the powers maintain the same ratio,
// but only if at least one is out of the range [-1, 1]
double denominator = Math.max(Math.abs(y) + Math.abs(x) + Math.abs(rx), 1);
double frontLeftPower = (y + x + rx) / denominator;
double backLeftPower = (y - x + rx) / denominator;
double frontRightPower = (y - x - rx) / denominator;
double backRightPower = (y + x - rx) / denominator;

frontLeft.setPower(frontLeftPower);
backLeft.setPower(backLeftPower);
frontRight.setPower(frontRightPower);
backRight.setPower(backRightPower);
}
}
}

0 comments on commit 85f036a

Please sign in to comment.