-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMU.java
184 lines (158 loc) · 6.8 KB
/
IMU.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.YawPitchRollAngles;
import com.qualcomm.robotcore.hardware.ImuOrientationOnRobot;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
@TeleOp(name="Yale Holo", group="WeRobot")
public class YaleHolo extends LinearOpMode {
float rotate_angle = 0;
double reset_angle = 0;
private DcMotor front_left_wheel = null;
private DcMotor back_left_wheel = null;
private DcMotor back_right_wheel = null;
private DcMotor front_right_wheel = null;
IMU imu;
@Override
public void runOpMode() {
front_left_wheel = hardwareMap.dcMotor.get("flm");
back_left_wheel = hardwareMap.dcMotor.get("blm");
back_right_wheel = hardwareMap.dcMotor.get("brm");
front_right_wheel = hardwareMap.dcMotor.get("frm");
front_left_wheel.setDirection(DcMotor.Direction.REVERSE);
back_left_wheel.setDirection(DcMotor.Direction.REVERSE);
front_right_wheel.setDirection(DcMotor.Direction.FORWARD);
back_right_wheel.setDirection(DcMotor.Direction.FORWARD);
front_left_wheel.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
back_left_wheel.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
front_right_wheel.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
back_right_wheel.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
imu = hardwareMap.get(IMU.class, "imu");
imu.initialize(
new IMU.Parameters(
new RevHubOrientationOnRobot(
RevHubOrientationOnRobot.LogoFacingDirection.UP,
RevHubOrientationOnRobot.UsbFacingDirection.FORWARD
)
)
);
imu.resetYaw();
while(!opModeIsActive()){}
while(opModeIsActive()){
drive();
resetAngle();
//driveSimple();
telemetry.update();
}
}
public void driveSimple(){
double power = .5;
if(gamepad1.dpad_up){ //Forward
front_left_wheel.setPower(-power);
back_left_wheel.setPower(-power);
back_right_wheel.setPower(-power);
front_right_wheel.setPower(-power);
}
else if(gamepad1.dpad_left){ //Left
front_left_wheel.setPower(power);
back_left_wheel.setPower(-power);
back_right_wheel.setPower(power);
front_right_wheel.setPower(-power);
}
else if(gamepad1.dpad_down){ //Back
front_left_wheel.setPower(power);
back_left_wheel.setPower(power);
back_right_wheel.setPower(power);
front_right_wheel.setPower(power);
}
else if(gamepad1.dpad_right){ //Right
front_left_wheel.setPower(-power);
back_left_wheel.setPower(power);
back_right_wheel.setPower(-power);
front_right_wheel.setPower(power);
}
else if(Math.abs(gamepad1.right_stick_x) > 0){ //Rotation
front_left_wheel.setPower(-gamepad1.right_stick_x);
back_left_wheel.setPower(-gamepad1.right_stick_x);
back_right_wheel.setPower(gamepad1.right_stick_x);
front_right_wheel.setPower(gamepad1.right_stick_x);
}
else{
front_left_wheel.setPower(0);
back_left_wheel.setPower(0);
back_right_wheel.setPower(0);
front_right_wheel.setPower(0);
}
}
public void drive() {
double Protate = gamepad1.right_stick_x/4;
double stick_x = gamepad1.left_stick_x * Math.sqrt(Math.pow(1-Math.abs(Protate), 2)/2); //Accounts for Protate when limiting magnitude to be less than 1
double stick_y = gamepad1.left_stick_y * Math.sqrt(Math.pow(1-Math.abs(Protate), 2)/2);
double theta = 0;
double Px = 0;
double Py = 0;
double gyroAngle = getHeading() * Math.PI / 180; //Converts gyroAngle into radians
if (gyroAngle <= 0) {
gyroAngle = gyroAngle + (Math.PI / 2);
} else if (0 < gyroAngle && gyroAngle < Math.PI / 2) {
gyroAngle = gyroAngle + (Math.PI / 2);
} else if (Math.PI / 2 <= gyroAngle) {
gyroAngle = gyroAngle - (3 * Math.PI / 2);
}
gyroAngle = -1 * gyroAngle;
if(gamepad1.right_bumper){ //Disables gyro, sets to -Math.PI/2 so front is defined correctly.
gyroAngle = -Math.PI/2;
}
//Linear directions in case you want to do straight lines.
if(gamepad1.dpad_right){
stick_x = 0.5;
}
else if(gamepad1.dpad_left){
stick_x = -0.5;
}
if(gamepad1.dpad_up){
stick_y = -0.5;
}
else if(gamepad1.dpad_down){
stick_y = 0.5;
}
//MOVEMENT
theta = Math.atan2(stick_y, stick_x) - gyroAngle - (Math.PI / 2);
Px = Math.sqrt(Math.pow(stick_x, 2) + Math.pow(stick_y, 2)) * (Math.sin(theta + Math.PI / 4));
Py = Math.sqrt(Math.pow(stick_x, 2) + Math.pow(stick_y, 2)) * (Math.sin(theta - Math.PI / 4));
telemetry.addData("Stick_X", stick_x);
telemetry.addData("Stick_Y", stick_y);
telemetry.addData("Magnitude", Math.sqrt(Math.pow(stick_x, 2) + Math.pow(stick_y, 2)));
telemetry.addData("Front Left", Py - Protate);
telemetry.addData("Back Left", Px - Protate);
telemetry.addData("Back Right", Py + Protate);
telemetry.addData("Front Right", Px + Protate);
front_left_wheel.setPower(Py - Protate);
back_left_wheel.setPower(Px - Protate);
back_right_wheel.setPower(Py + Protate);
front_right_wheel.setPower(Px + Protate);
}
public void resetAngle(){
if(gamepad1.a){
reset_angle = getHeading() + reset_angle;
}
}
public double getHeading(){
double heading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES);
if(heading < -180) {
heading = heading + 360;
}
else if(heading > 180){
heading = heading - 360;
}
heading = heading - reset_angle;
return heading;
}
}