forked from vbatts/ftc-robot-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldRelativeMecanumDrive.java
More file actions
86 lines (67 loc) · 3.53 KB
/
FieldRelativeMecanumDrive.java
File metadata and controls
86 lines (67 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.firstinspires.ftc.teamcode.ftc26358;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.YawPitchRollAngles;
@Autonomous(group="@CoachesInstitute")
public class FieldRelativeMecanumDrive extends OpMode {
DcMotor frontLeftMotor;
DcMotor frontRightMotor;
DcMotor backLeftMotor;
DcMotor backRightMotor;
IMU imu;
@Override
public void init() {
frontLeftMotor= hardwareMap.dcMotor.get("front_left_motor");
frontRightMotor = hardwareMap.dcMotor.get("front_right_motor");
backLeftMotor = hardwareMap.dcMotor.get("back_left_motor");
backRightMotor = hardwareMap.dcMotor.get("back_right_motor");
backLeftMotor.setDirection(DcMotor.Direction.REVERSE);
frontLeftMotor.setDirection(DcMotor.Direction.REVERSE);
frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
imu = hardwareMap.get(IMU.class, "imu");
RevHubOrientationOnRobot.LogoFacingDirection logoDirection = RevHubOrientationOnRobot.LogoFacingDirection.UP;
RevHubOrientationOnRobot.UsbFacingDirection usbDirection = RevHubOrientationOnRobot.UsbFacingDirection.FORWARD;
RevHubOrientationOnRobot orientationOnRobot = new RevHubOrientationOnRobot(logoDirection, usbDirection);
imu.initialize(new IMU.Parameters(orientationOnRobot));
}
// Thanks to FTC16072 for sharing this code!!
public void drive(double forward, double right, double rotate) {
double leftFrontPower = forward + right + rotate;
double rightFrontPower = forward - right - rotate;
double rightRearPower = forward + right - rotate;
double leftRearPower = forward - right + rotate;
double maxPower = 1.0;
double maxSpeed = 1.0; // make this slower for outreaches
maxPower = Math.max(maxPower, Math.abs(leftFrontPower));
maxPower = Math.max(maxPower, Math.abs(rightFrontPower));
maxPower = Math.max(maxPower, Math.abs(rightRearPower));
maxPower = Math.max(maxPower, Math.abs(leftRearPower));
frontLeftMotor.setPower(maxSpeed * (leftFrontPower/maxPower));
frontRightMotor.setPower(maxSpeed * (rightFrontPower/maxPower));
backLeftMotor.setPower(maxSpeed * (leftRearPower/maxPower));
backRightMotor.setPower(maxSpeed * (rightRearPower/maxPower));
}
private void driveFieldRelative(double forward, double right, double rotate){
YawPitchRollAngles orientation = imu.getRobotYawPitchRollAngles();
// convert to polar
double theta = Math.atan2(forward, right);
double r = Math.hypot(right, forward);
// rotate angle
theta = AngleUnit.normalizeRadians(theta - orientation.getYaw(AngleUnit.RADIANS));
// convert back to cartesian
double newForward = r * Math.sin(theta);
double newRight = r * Math.cos(theta);
drive(newForward, newRight, rotate);
}
@Override
public void loop() {
driveFieldRelative(-gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
}
}