Grey0730 / 23891-ftc

0 stars 0 forks source link

TeleOp in RR #2

Open Grey0730 opened 3 months ago

Grey0730 commented 3 months ago

package org.firstinspires.ftc.teamcode;

import com.acmerobotics.roadrunner.drive.MecanumDrive; import com.acmerobotics.roadrunner.geometry.Pose2d; import com.acmerobotics.roadrunner.trajectory.Trajectory; import com.acmerobotics.roadrunner.trajectory.constraints.TrajectoryConstraint; import com.qualcomm.robotcore.eventloop.opmode.TeleOp; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; import com.qualcomm.robotcore.util.ElapsedTime;

@TeleOp(name = "Mecanum Drive Road Runner") public class MecanumDriveRR extends MecanumDrive { private DcMotor frontLeft, frontRight, backLeft, backRight;

@Override
public void initialize(HardwareMap hardwareMap) {
    frontLeft = hardwareMap.get(DcMotor.class, "front_left");
    frontRight = hardwareMap.get(DcMotor.class, "front_right");
    backLeft = hardwareMap.get(DcMotor.class, "back_left");
    backRight = hardwareMap.get(DcMotor.class, "back_right");

    frontLeft.setDirection(DcMotor.Direction.FORWARD);
    frontRight.setDirection(DcMotor.Direction.REVERSE);
    backLeft.setDirection(DcMotor.Direction.FORWARD);
    backRight.setDirection(DcMotor.Direction.REVERSE);

    frontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
    frontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
    backLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
    backRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);

    setPoseEstimate(new Pose2d());
}

@Override
public void update() {
    double drive = -gamepad1.left_stick_y;
    double strafe = gamepad1.left_stick_x;
    double rotate = gamepad1.right_stick_x;

    setDrivePower(new Pose2d(drive + strafe + rotate, drive - strafe - rotate, drive - strafe + rotate, drive + strafe - rotate));
}

@Override
public void setDrivePower(Pose2d power) {
    frontLeft.setPower(power.getX());
    frontRight.setPower(power.getY());
    backLeft.setPower(power.getZ());
    backRight.setPower(power.getHeading());
}

}

Grey0730 commented 3 months ago

Explanation:

1.  Dependencies: Ensure you have Road Runner dependencies configured in your build.gradle file.
2.  Initialization: Set up the hardware and initialize the mecanum drive. Adjust motor directions as needed.
3.  Update Loop: Capture joystick inputs and convert them into a Pose2d object to represent the drivetrain’s power. Road Runner’s Pose2d class is used for easier representation of motion in 2D space.
4.  Set Drive Power: Assign the calculated power to each motor. Road Runner uses the Pose2d object to define the power for the drivetrain.
  1. Make sure you have the Road Runner library added to your project. You can add this through your build.gradle file.