forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShootOnMove.java
More file actions
424 lines (341 loc) · 15.2 KB
/
ShootOnMove.java
File metadata and controls
424 lines (341 loc) · 15.2 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package org.firstinspires.ftc.teamcode;
import com.pedropathing.follower.Follower;
import com.pedropathing.geometry.Pose;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.VoltageSensor;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.teamcode.hardwareClasses.FlywheelASG;
import org.firstinspires.ftc.teamcode.hardwareClasses.Turret;
import org.firstinspires.ftc.teamcode.pedroPathing.constants.Constants;
@Disabled
@TeleOp(name = "SOTM_TeleOp")
public class ShootOnMove extends LinearOpMode {
private Servo hoodServo, armServo, clutchServo;
private Follower follower;
private Turret turret;
private DcMotorEx intakeMotor;
private FlywheelASG flywheel;
private static final boolean AUTO_SHOT_FROM_DISTANCE = true;
private static final boolean ENABLE_SHOT_ON_MOVE_COMP = true;
private static final double VELOCITY_FILTER_ALPHA = 0.25;
private static final double PREDICTED_DISTANCE_ALPHA = 0.45;
private static final double METERS_TO_INCHES = 39.3701;
private static final double TIME_TUNER = 2.5;
private boolean lastX = false;
private boolean intakeToggleOn = false;
private boolean lastB = false;
private boolean lastTriangle = false;
private boolean lastCross = false;
private boolean lastDpadUp = false;
private boolean lastDpadDown = false;
private boolean shootOnTheMove = ENABLE_SHOT_ON_MOVE_COMP;
private final ElapsedTime sequenceTimer = new ElapsedTime();
private final ElapsedTime poseVelocityTimer = new ElapsedTime();
private boolean velocityInitialized = false;
private double lastPoseX = 0.0, lastPoseY = 0.0;
private double fieldVxInPerSec = 0.0, fieldVyInPerSec = 0.0;
private double predictedShotDistance = 0.0;
private double filteredPredictedShotDistance = 0.0;
private boolean predictedDistanceInitialized = false;
private double radialVelocityToGoal = 0.0;
private enum FeedState {IDLE, WAIT_BEFORE_INTAKE, RUN_INTAKE, DONE}
private FeedState feedState = FeedState.IDLE;
private double hoodAngleDeg = 50.0;
private double targetVelocityRad = 250.0;
private static final double TARGET_X = 5;
private static final double TARGET_Y = 139;
private static final double TURRET_CENTER_OFFSET_IN = 1.5;
private static final double TURRET_MIN_DEG = -160.0;
private static final double TURRET_MAX_DEG = 160.0;
private static final double TURRET_OFFSET_DEG = 180.0;
@Override
public void runOpMode() {
intakeMotor = hardwareMap.get(DcMotorEx.class, "intake_motor");
intakeMotor.setDirection(DcMotorSimple.Direction.REVERSE);
hoodServo = hardwareMap.get(Servo.class, "hoodServo");
armServo = hardwareMap.get(Servo.class, "armServo");
clutchServo = hardwareMap.get(Servo.class, "clutchServo");
VoltageSensor battery = hardwareMap.voltageSensor.iterator().next();
flywheel = new FlywheelASG(hardwareMap, battery);
follower = Constants.createFollower(hardwareMap);
follower.setStartingPose(new Pose(72, 72, Math.toRadians(0)));
follower.updatePose();
follower.setMaxPower(1);
follower.startTeleOpDrive();
turret = new Turret();
turret.init(hardwareMap, "turretMotor", DcMotorSimple.Direction.REVERSE);
// Initialization
clutchOut();
setHoodAngle(hoodAngleDeg);
armBlock();
intakeMotor.setPower(0.0);
telemetry.addLine("Initialized");
telemetry.addData("Auto Shot From Distance", AUTO_SHOT_FROM_DISTANCE);
telemetry.addData("Shot On Move Compensation", ENABLE_SHOT_ON_MOVE_COMP);
telemetry.update();
while (opModeInInit()) {
turret.update();
}
waitForStart();
if (isStopRequested()) return;
flywheel.setTargetVelocity(targetVelocityRad);
while (opModeIsActive()) {
follower.update();
Pose pose = follower.getPose();
updateEstimatedFieldVelocity(pose);
double robotHeadingDeg = Math.toDegrees(pose.getHeading());
drive(robotHeadingDeg);
trackGoalFromOdometry(pose);
turret.update();
if (AUTO_SHOT_FROM_DISTANCE) {
double lookupDistance = predictedDistanceInitialized
? filteredPredictedShotDistance
: Math.hypot(TARGET_X - pose.getX(), TARGET_Y - pose.getY());
updateShotFromDistance(lookupDistance);
} else {
handleFlywheelAdjustment();
handleHoodAdjustment();
}
handleXToggle();
handleBSequence();
updateFeedSequence();
flywheel.setTargetVelocity(targetVelocityRad);
flywheel.update();
telemetry.addData("Pose", pose.toString());
telemetry.update();
}
flywheel.stop();
intakeMotor.setPower(0.0);
}
private void drive(double robotHeadingDeg) {
double trigger = Range.clip(1 - gamepad1.right_trigger, 0.2, 1);
if (!(gamepad1.left_trigger > 0.5)) {
follower.setTeleOpDrive(gamepad1.left_stick_y * trigger,
gamepad1.left_stick_x * trigger,
-gamepad1.right_stick_x * trigger,
false);
} else {
follower.setTeleOpDrive(-gamepad1.left_stick_y * trigger,
-gamepad1.left_stick_x * trigger,
-gamepad1.right_stick_x * trigger,
true);
}
}
private void handleXToggle() {
boolean xPressed = gamepad1.x;
if (xPressed && !lastX) {
intakeToggleOn = !intakeToggleOn;
if (feedState == FeedState.IDLE || feedState == FeedState.DONE) {
intakeMotor.setPower(intakeToggleOn ? 1.0 : 0.0);
if (intakeToggleOn) { clutchOut(); armBlock(); }
}
}
lastX = xPressed;
}
private void handleBSequence() {
boolean bPressed = gamepad1.b;
if (bPressed && !lastB) {
intakeMotor.setPower(0.0);
armShoot();
clutchIn();
sequenceTimer.reset();
feedState = FeedState.WAIT_BEFORE_INTAKE;
}
lastB = bPressed;
}
private void updateFeedSequence() {
switch (feedState) {
case IDLE: break;
case WAIT_BEFORE_INTAKE:
if (sequenceTimer.seconds() >= 0.1) { intakeMotor.setPower(1.0); sequenceTimer.reset(); feedState = FeedState.RUN_INTAKE; }
break;
case RUN_INTAKE:
if (sequenceTimer.seconds() >= 0.9) { intakeMotor.setPower(0.0); feedState = FeedState.DONE; }
break;
case DONE: break;
}
}
private void trackGoalFromOdometry(Pose pose) {
double robotX = pose.getX();
double robotY = pose.getY();
double robotHeadingRad = pose.getHeading();
double robotHeadingDeg = Math.toDegrees(robotHeadingRad);
double turretX = robotX - TURRET_CENTER_OFFSET_IN * Math.cos(robotHeadingRad);
double turretY = robotY - TURRET_CENTER_OFFSET_IN * Math.sin(robotHeadingRad);
double actualDx = TARGET_X - turretX;
double actualDy = TARGET_Y - turretY;
double actualDistance = Math.hypot(actualDx, actualDy);
double shotTimeSec = estimateShotTimeSec(actualDistance);
double ux = 0.0, uy = 0.0;
if (actualDistance > 1e-6) { ux = actualDx / actualDistance; uy = actualDy / actualDistance; }
radialVelocityToGoal = fieldVxInPerSec * ux + fieldVyInPerSec * uy;
predictedShotDistance = Math.max(0.0, actualDistance - radialVelocityToGoal * shotTimeSec);
if (!predictedDistanceInitialized) {
filteredPredictedShotDistance = predictedShotDistance;
predictedDistanceInitialized = true;
} else {
filteredPredictedShotDistance = PREDICTED_DISTANCE_ALPHA * predictedShotDistance
+ (1.0 - PREDICTED_DISTANCE_ALPHA) * filteredPredictedShotDistance;
}
double compensatedTargetX = TARGET_X;
double compensatedTargetY = TARGET_Y;
double MIN_SPEED = 0.5;
double FULL_COMP_SPEED = 15.0;
double speed = Math.sqrt(fieldVxInPerSec * fieldVxInPerSec + fieldVyInPerSec * fieldVyInPerSec);
double compFactor = (speed- MIN_SPEED) / (FULL_COMP_SPEED - MIN_SPEED);
compFactor = Math.max(0.0, Math.min(1.0, compFactor));
if (shootOnTheMove) {
compensatedTargetX = TARGET_X - fieldVxInPerSec * shotTimeSec * compFactor;
compensatedTargetY = TARGET_Y - fieldVyInPerSec * shotTimeSec * compFactor;//
}
double dx = compensatedTargetX - turretX;
double dy = compensatedTargetY - turretY;
double angleToTargetFieldDeg = Math.toDegrees(Math.atan2(dy, dx));
double angleToTargetRobotDeg = normalize180(angleToTargetFieldDeg - robotHeadingDeg);
double desiredTurretDeg = normalize180(angleToTargetRobotDeg + TURRET_OFFSET_DEG);
double safeTurretDeg = wrapIntoTurretWindow(desiredTurretDeg, turret.getCurrentAngle(), TURRET_MIN_DEG, TURRET_MAX_DEG);
turret.setAngle(safeTurretDeg);
telemetry.addData("Shoot on the move: ", shootOnTheMove);
telemetry.addData("Velocity: ", speed);
telemetry.addData("Acceleration ", follower.getAcceleration().getMagnitude());
telemetry.addData("Actual Dist To Goal", actualDistance);
telemetry.addData("Shot Time (s)", shotTimeSec);
}
private void initVelocityEstimator(Pose pose) {
lastPoseX = pose.getX();
lastPoseY = pose.getY();
fieldVxInPerSec = 0.0;
fieldVyInPerSec = 0.0;
velocityInitialized = true;
poseVelocityTimer.reset();
}
private void updateEstimatedFieldVelocity(Pose pose) {
double x = pose.getX();
double y = pose.getY();
if (!velocityInitialized) { initVelocityEstimator(pose); return; }
double dt = poseVelocityTimer.seconds();
poseVelocityTimer.reset();
if (dt <= 1e-4) { lastPoseX = x; lastPoseY = y; return; }
double rawVx = (x - lastPoseX) / dt;
double rawVy = (y - lastPoseY) / dt;
fieldVxInPerSec = VELOCITY_FILTER_ALPHA * rawVx + (1 - VELOCITY_FILTER_ALPHA) * fieldVxInPerSec;
fieldVyInPerSec = VELOCITY_FILTER_ALPHA * rawVy + (1 - VELOCITY_FILTER_ALPHA) * fieldVyInPerSec;
lastPoseX = x;
lastPoseY = y;
}
private double estimateShotTimeSec(double distanceInches) {
double distanceMeters = distanceInches/METERS_TO_INCHES;
double time = ShootingCalc.get2DTimeNeeded(distanceMeters, Math.toRadians(hoodAngleDeg), 0.984-0.3);
return time * TIME_TUNER;
}
private double normalize180(double a) { return ((a + 180) % 360 + 360) % 360 - 180; }
private double wrapIntoTurretWindow(double desiredDeg, double referenceDeg, double minDeg, double maxDeg) {
double best = Double.NaN;
for (int k = -2; k <= 2; k++) {
double candidate = desiredDeg + 360.0 * k;
if (candidate >= minDeg && candidate <= maxDeg) {
if (Double.isNaN(best) || Math.abs(candidate - referenceDeg) < Math.abs(best - referenceDeg)) {
best = candidate;
}
}
}
if (Double.isNaN(best)) {
best = Range.clip(desiredDeg, minDeg, maxDeg);
}
return best;
}
private void handleFlywheelAdjustment() {
boolean trianglePressed = gamepad1.triangle;
boolean crossPressed = gamepad1.cross;
if (trianglePressed && !lastTriangle) {
targetVelocityRad += 5.0;
}
if (crossPressed && !lastCross) {
targetVelocityRad -= 5.0;
targetVelocityRad = Math.max(0.0, targetVelocityRad);
}
lastTriangle = trianglePressed;
lastCross = crossPressed;
}
private void handleHoodAdjustment() {
boolean dpadUpPressed = gamepad1.dpad_up;
boolean dpadDownPressed = gamepad1.dpad_down;
if (dpadUpPressed && !lastDpadUp) {
hoodAngleDeg += 1.0;
setHoodAngle(hoodAngleDeg);
}
if (dpadDownPressed && !lastDpadDown) {
hoodAngleDeg -= 1.0;
setHoodAngle(hoodAngleDeg);
}
lastDpadUp = dpadUpPressed;
lastDpadDown = dpadDownPressed;
}
private void updateShotFromDistance(double distance) {
// [distance inches, hood angle deg, flywheel velocity rad/s]
double[][] shotTable = {
{47.0, 30.0, 275.0},
{52.0, 30.0, 275.0},
{59.0, 33.0, 280.0},
{64.5, 36.0, 285.0},
{70.0, 36.0, 300.0},
{76.5, 36.0, 320.0},
{81.0, 38.0, 320.0},
{88.0, 38.0, 330.0},
{94.0, 38.0, 340.0},
{102.0, 40.0, 340.0},
{115.0, 44.0, 375.0},
{119.7, 44.0, 380.0},
{126.0, 44.0, 385.0}
};
// Clamp below first point
if (distance <= shotTable[0][0]) {
hoodAngleDeg = shotTable[0][1];
targetVelocityRad = shotTable[0][2];
setHoodAngle(hoodAngleDeg);
return;
}
// Clamp above last point
int last = shotTable.length - 1;
if (distance >= shotTable[last][0]) {
hoodAngleDeg = shotTable[last][1];
targetVelocityRad = shotTable[last][2];
setHoodAngle(hoodAngleDeg);
return;
}
// Interpolate between surrounding points
for (int i = 0; i < shotTable.length - 1; i++) {
double d1 = shotTable[i][0];
double a1 = shotTable[i][1];
double v1 = shotTable[i][2];
double d2 = shotTable[i + 1][0];
double a2 = shotTable[i + 1][1];
double v2 = shotTable[i + 1][2];
if (distance >= d1 && distance <= d2) {
double t = (distance - d1) / (d2 - d1);
hoodAngleDeg = a1 + t * (a2 - a1);
targetVelocityRad = (v1 + t * (v2 - v1)) + 10;
setHoodAngle(hoodAngleDeg);
return;
}
}
}
public void clutchIn() { clutchServo.setPosition(0.48); }
public void clutchOut() { clutchServo.setPosition(0.52); }
public void armBlock() { armServo.setPosition(0.39); }
public void armShoot() { armServo.setPosition(0.54); }
public void setHoodAngle(double angleDeg) {
final double MIN_ANGLE = 30.0, MAX_ANGLE = 60.0;
final double MIN_POS = 0.395, MAX_POS = 0.9;
hoodAngleDeg = Math.max(MIN_ANGLE, Math.min(MAX_ANGLE, angleDeg));
double t = (hoodAngleDeg - MIN_ANGLE)/(MAX_ANGLE-MIN_ANGLE);
double pos = MIN_POS + t*(MAX_POS-MIN_POS);
hoodServo.setPosition(Math.max(MIN_POS, Math.min(MAX_POS, pos)));
}
}