-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverOriented.java
More file actions
217 lines (179 loc) · 8.58 KB
/
DriverOriented.java
File metadata and controls
217 lines (179 loc) · 8.58 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
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.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
@TeleOp(name = "DriverOrientedDriving_FieldOriented")
public class DriverOriented extends LinearOpMode {
// Drive motors
private DcMotor motorFL, motorFR, motorBL, motorBR;
// Other motors
private DcMotor intake, launcher;
// IMU
private IMU imu;
private double initialHeading = 0.0;
// Launcher toggle state
private double launcherPower = 0.0;
private boolean lastY = false;
// Button edge detectors
private boolean lastB = false;
private boolean lastA = false;
private boolean lastStart = false;
// Auto-fire state machine
private enum AutoFireState { IDLE, SPINUP, KICK, COOLDOWN }
private AutoFireState autoFireState = AutoFireState.IDLE;
private long autoFireStateStartMs = 0L;
// Push-up servo state
private enum PushState { IDLE, EXTENDED, RETRACTING }
private PushState pushState = PushState.IDLE;
private long pushStateStartMs = 0L;
// Constants
private static final long SPINUP_MS = 1000;
private static final long KICK_MS = 500;
private static final long COOLDOWN_MS = 200;
@Override
public void runOpMode() {
// ---------------- Hardware Map ----------------
motorFL = hardwareMap.get(DcMotor.class, "motor3");
motorFR = hardwareMap.get(DcMotor.class, "motor2");
motorBL = hardwareMap.get(DcMotor.class, "motor4");
motorBR = hardwareMap.get(DcMotor.class, "motor1");
intake = hardwareMap.get(DcMotor.class, "intake");
launcher = hardwareMap.get(DcMotor.class, "launcher");
// ---------------- Motor Directions ----------------
motorFL.setDirection(DcMotor.Direction.FORWARD);
motorBL.setDirection(DcMotor.Direction.FORWARD);
motorFR.setDirection(DcMotor.Direction.REVERSE);
motorBR.setDirection(DcMotor.Direction.REVERSE);
intake.setDirection(DcMotor.Direction.FORWARD);
launcher.setDirection(DcMotor.Direction.FORWARD);
launcher.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// ---------------- IMU Initialization ----------------
imu = hardwareMap.get(IMU.class, "imu");
imu.initialize(new IMU.Parameters(
new RevHubOrientationOnRobot(
RevHubOrientationOnRobot.LogoFacingDirection.RIGHT,
RevHubOrientationOnRobot.UsbFacingDirection.UP
)
));
// Capture initial heading at init (field forward reference)
initialHeading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);
boolean brakeMode = true;
boolean lastBumperState = false;
telemetry.addLine("Initialized. Waiting for start...");
telemetry.update();
waitForStart();
while (opModeIsActive()) {
// ------------- Brake / float toggle ----------------
boolean bumper = gamepad1.left_bumper;
if (bumper && !lastBumperState) brakeMode = !brakeMode;
lastBumperState = bumper;
DcMotor.ZeroPowerBehavior behavior = brakeMode ? DcMotor.ZeroPowerBehavior.BRAKE : DcMotor.ZeroPowerBehavior.FLOAT;
motorFL.setZeroPowerBehavior(behavior);
motorFR.setZeroPowerBehavior(behavior);
motorBL.setZeroPowerBehavior(behavior);
motorBR.setZeroPowerBehavior(behavior);
intake.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
// ------------- Start button resets field orientation ----------------
boolean startPressed = gamepad1.start;
if (startPressed && !lastStart) {
initialHeading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);
}
lastStart = startPressed;
// ---------------- Field-Oriented Drive ----------------
double rawYaw = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);
double botHeading = normalizeRadians(rawYaw - initialHeading);
// Left stick for translation, right stick x for rotation
double y = -gamepad1.left_stick_y; // forward/back
double x = gamepad1.left_stick_x; // left/right
double turn = -gamepad1.right_stick_x; // rotation
// Rotate joystick input by robot heading to get field-oriented motion
double cosH = Math.cos(-botHeading);
double sinH = Math.sin(-botHeading);
double rotatedX = x * cosH - y * sinH;
double rotatedY = x * sinH + y * cosH;
// Mecanum wheel power
double fl = rotatedY + rotatedX + turn;
double fr = rotatedY - rotatedX - turn;
double bl = rotatedY - rotatedX + turn;
double br = rotatedY + rotatedX - turn;
// Normalize
double max = Math.max(1.0, Math.max(Math.abs(fl),
Math.max(Math.abs(fr), Math.max(Math.abs(bl), Math.abs(br)))));
motorFL.setPower(fl / max);
motorFR.setPower(fr / max);
motorBL.setPower(bl / max);
motorBR.setPower(br / max);
// ---------------- Intake ----------------
double intakePower = gamepad1.right_trigger;
intake.setPower(intakePower);
// ---------------- Launcher toggle ----------------
if (gamepad1.y && !lastY) {
launcherPower = (launcherPower == 0.0) ? 1.0 : 0.0;
launcher.setPower(launcherPower);
}
lastY = gamepad1.y;
// ---------------- Push-up servo (B) ----------------
if (gamepad1.b && !lastB) {
if (pushState == PushState.IDLE) {
pushState = PushState.EXTENDED;
pushStateStartMs = System.currentTimeMillis();
} else if (pushState == PushState.EXTENDED) {
pushState = PushState.RETRACTING;
pushStateStartMs = System.currentTimeMillis();
}
}
lastB = gamepad1.b;
if (pushState == PushState.EXTENDED && System.currentTimeMillis() - pushStateStartMs > 600) {
pushState = PushState.RETRACTING;
pushStateStartMs = System.currentTimeMillis();
} else if (pushState == PushState.RETRACTING && System.currentTimeMillis() - pushStateStartMs > 250) {
pushState = PushState.IDLE;
}
// ---------------- Auto-fire single-shot (A) ----------------
if (gamepad1.a && !lastA && autoFireState == AutoFireState.IDLE) {
autoFireState = AutoFireState.SPINUP;
autoFireStateStartMs = System.currentTimeMillis();
launcher.setPower(0.3); // single-shot spin
}
lastA = gamepad1.a;
long nowMs = System.currentTimeMillis();
switch (autoFireState) {
case SPINUP:
if (nowMs - autoFireStateStartMs >= SPINUP_MS) {
autoFireState = AutoFireState.KICK;
autoFireStateStartMs = nowMs;
}
break;
case KICK:
if (nowMs - autoFireStateStartMs >= KICK_MS) {
launcher.setPower(0.0);
autoFireState = AutoFireState.COOLDOWN;
autoFireStateStartMs = nowMs;
}
break;
case COOLDOWN:
if (nowMs - autoFireStateStartMs >= COOLDOWN_MS) {
autoFireState = AutoFireState.IDLE;
}
break;
default: break;
}
// ---------------- Telemetry ----------------
telemetry.addData("Mode", brakeMode ? "BRAKE" : "FLOAT");
telemetry.addData("Heading(deg)", Math.toDegrees(botHeading));
telemetry.addData("Launcher", launcherPower);
telemetry.addData("AutoFireState", autoFireState.name());
telemetry.update();
sleep(20);
}
}
// Normalize angle to [-PI, PI]
private static double normalizeRadians(double angle) {
while (angle > Math.PI) angle -= 2.0 * Math.PI;
while (angle <= -Math.PI) angle += 2.0 * Math.PI;
return angle;
}
}