forked from brandon-gong/ftc-mecanum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMecanumDrive.java
98 lines (85 loc) · 3.74 KB
/
MecanumDrive.java
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
87
88
89
90
91
92
93
94
95
96
97
98
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
/**
* This is an example minimal implementation of the mecanum drivetrain
* for demonstration purposes. Not tested and not guaranteed to be bug free.
*
* @author Brandon Gong
*/
@TeleOp(name="Mecanum Drive Example", group="Iterative Opmode")
public class MecanumDrive extends OpMode {
/*
* The mecanum drivetrain involves four separate motors that spin in
* different directions and different speeds to produce the desired
* movement at the desired speed.
*/
// declare and initialize four DcMotors.
private DcMotor front_left = null;
private DcMotor front_right = null;
private DcMotor back_left = null;
private DcMotor back_right = null;
@Override
public void init() {
// Name strings must match up with the config on the Robot Controller
// app.
front_left = hardwareMap.get(DcMotor.class, "front_left");
front_right = hardwareMap.get(DcMotor.class, "front_right");
back_left = hardwareMap.get(DcMotor.class, "back_left");
back_right = hardwareMap.get(DcMotor.class, "back_right");
}
@Override
public void loop() {
// Mecanum drive is controlled with three axes: drive (front-and-back),
// strafe (left-and-right), and twist (rotating the whole chassis).
double drive = gamepad1.left_stick_y;
double strafe = gamepad1.left_stick_x;
double twist = gamepad1.right_stick_x;
/*
* If we had a gyro and wanted to do field-oriented control, here
* is where we would implement it.
*
* The idea is fairly simple; we have a robot-oriented Cartesian (x,y)
* coordinate (strafe, drive), and we just rotate it by the gyro
* reading minus the offset that we read in the init() method.
* Some rough pseudocode demonstrating:
*
* if Field Oriented Control:
* get gyro heading
* subtract initial offset from heading
* convert heading to radians (if necessary)
* new strafe = strafe * cos(heading) - drive * sin(heading)
* new drive = strafe * sin(heading) + drive * cos(heading)
*
* If you want more understanding on where these rotation formulas come
* from, refer to
* https://en.wikipedia.org/wiki/Rotation_(mathematics)#Two_dimensions
*/
// You may need to multiply some of these by -1 to invert direction of
// the motor. This is not an issue with the calculations themselves.
double[] speeds = {
(drive + strafe + twist),
(drive - strafe - twist),
(drive - strafe + twist),
(drive + strafe - twist)
};
// Because we are adding vectors and motors only take values between
// [-1,1] we may need to normalize them.
// Loop through all values in the speeds[] array and find the greatest
// *magnitude*. Not the greatest velocity.
double max = Math.abs(speeds[0]);
for(int i = 0; i < speeds.length; i++) {
if ( max < Math.abs(speeds[i]) ) max = Math.abs(speeds[i]);
}
// If and only if the maximum is outside of the range we want it to be,
// normalize all the other speeds based on the given speed value.
if (max > 1) {
for (int i = 0; i < speeds.length; i++) speeds[i] /= max;
}
// apply the calculated values to the motors.
front_left.setPower(speeds[0]);
front_right.setPower(speeds[1]);
back_left.setPower(speeds[2]);
back_right.setPower(speeds[3]);
}
}