-
Notifications
You must be signed in to change notification settings - Fork 35
/
SuperstructureCommands.java
475 lines (396 loc) · 17.1 KB
/
SuperstructureCommands.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.team254.frc2019.statemachines;
import com.team254.frc2019.Constants;
import com.team254.frc2019.GamePiece;
import com.team254.frc2019.states.SuperstructureGoal;
import com.team254.frc2019.states.SuperstructureState;
import com.team254.frc2019.subsystems.EndEffector;
import com.team254.frc2019.subsystems.Superstructure;
import com.team254.lib.geometry.Rotation2d;
import com.team254.lib.geometry.Translation2d;
import com.team254.lib.util.Util;
/**
* Commands for the Superstructure to go to predetermined states or vision based
* states
*/
public class SuperstructureCommands {
private static boolean mCargoShipPosition = false;
private static boolean mMiddlePosition = false;
private static boolean mHighPosition = false;
private static boolean mLowPosition = false;
private SuperstructureCommands() {}
private static void observeDisk() {
EndEffector.getInstance().updateObservedGamePiece(GamePiece.DISK);
}
private static void observeBall() {
EndEffector.getInstance().updateObservedGamePiece(GamePiece.BALL);
}
public static void setTurretPosition(double position) {
Superstructure ss = Superstructure.getInstance();
SuperstructureGoal lastCommand = ss.getGoal();
if (lastCommand == null) {
return;
}
SuperstructureState newCommand = new SuperstructureState(
position,
lastCommand.state.elevator,
lastCommand.state.shoulder,
lastCommand.state.wrist);
selectPositionByGamepiece(newCommand, newCommand);
}
public static void setTurretManualHeading(Rotation2d manualHeading) {
setTurretPosition(Util.toTurretSafeAngleDegrees(manualHeading));
}
public static void jogTurret(double deltaP) {
Superstructure ss = Superstructure.getInstance();
ss.jogTurret(deltaP);
}
public static double goToAutoScore() {
return goToAutoScore(-1.0);
}
public static double goToAutoScore(double offsetOverride) {
Superstructure ss = Superstructure.getInstance();
EndEffector end_effector = EndEffector.getInstance();
if (!ss.isOnTarget()) {
return Double.NaN;
}
SuperstructureGoal lastCommand = ss.getGoal();
if (lastCommand == null) {
return Double.NaN;
}
Translation2d end_effector_pos = lastCommand.state.getPlanarWristJointLocation();
double offset = offsetOverride;
double allowedHeightLoss = Double.NaN;
if (end_effector.getObservedGamePiece() == GamePiece.BALL) {
if (SuperstructureCommands.isInCargoShipPosition()) {
offset = 0.0;
} else if (SuperstructureCommands.isInLowPosition()) {
// Low ball suffers from limelight tilt set high to avoid colliding with rocket.
offset = 6.0;
} else if (SuperstructureCommands.isInMiddlePosition()) {
offset = 2.0;
} else if (SuperstructureCommands.isInHighPosition()) {
offset = 2.0;
allowedHeightLoss = 2.0;
}
} else if (end_effector.getObservedGamePiece() == GamePiece.DISK) {
if (SuperstructureCommands.isInLowPosition()) {
offset = 1.0;
} else if (SuperstructureCommands.isInHighPosition()) {
offset = -1.0;
allowedHeightLoss = 4.0;
}
}
double desired_z;
if (ss.getElevatorControlMode() == Superstructure.ElevatorControlModes.PRISMATIC_WRIST) {
desired_z = ss.getHeightForWristLevel();
} else {
desired_z = end_effector_pos.y();
}
double range = ss.getCorrectedRangeToTarget();
double desired_x =
range - Constants.kTurretToArmOffset - Math.cos(Math.toRadians(lastCommand.state.wrist)) * Constants.kWristToTremorsEnd;
if (ss.getElevatorControlMode() != Superstructure.ElevatorControlModes.PRISMATIC_WRIST
&& end_effector_pos.x() >= desired_x - offset) {
// Already past position, just shoot.
} else {
setEndEffectorPos(desired_x - offset, desired_z, allowedHeightLoss);
}
// Return the remaining distance.
return desired_x - offset - ss.getCurrentState().getPlanarWristJointLocation().x();
}
public static boolean setEndEffectorPos(double x, double z, double allowedHeightLoss) {
boolean reachable = true;
Superstructure ss = Superstructure.getInstance();
SuperstructureGoal lastCommand = ss.getGoal();
if (lastCommand == null) {
return false;
}
if (x < 0 || x > Constants.kArmLength) {
reachable = false;
}
x = Util.limit(x, 0, Constants.kArmLength);
double shoulder_angle_rads = Math.acos(x / Constants.kArmLength);
if (Double.isNaN(shoulder_angle_rads)) {
System.out.println("Unreachable jog!");
return false;
}
if (Math.abs(lastCommand.state.shoulder) > 1.0) {
shoulder_angle_rads *= Math.signum(lastCommand.state.shoulder);
}
double elevator_height_diff = Math.sin(shoulder_angle_rads) * Constants.kArmLength;
double elevator_height = z - elevator_height_diff;
if (Double.isNaN(elevator_height)) {
System.out.println("Unreachable jog elevator height!");
return false;
}
if (elevator_height < 0 || elevator_height > Constants.kElevatorConstants.kMaxUnitsLimit) {
reachable = false;
elevator_height =
Util.limit(elevator_height, 0, Constants.kElevatorConstants.kMaxUnitsLimit);
if (Double.isNaN(allowedHeightLoss)) {
// Maintain z, adjust x to make this work.
shoulder_angle_rads = Math.asin((z - elevator_height) / Constants.kArmLength);
} else {
double adjusted_z = z - Math.signum(z - elevator_height_diff) * allowedHeightLoss;
double adjusted_angle_rads =
Math.asin((adjusted_z - elevator_height) / Constants.kArmLength);
// Take the shoulder angle with a larger magnitude in the case that we don't need to
// lose all the allowed height loss.
if (Math.abs(adjusted_angle_rads) > Math.abs(shoulder_angle_rads)) {
shoulder_angle_rads = adjusted_angle_rads;
}
}
if (Double.isNaN(shoulder_angle_rads)) {
System.out.println("Unreachable jog after correction!");
return false;
}
}
SuperstructureState newCommand = new SuperstructureState(
lastCommand.state.turret,
elevator_height,
Math.toDegrees(shoulder_angle_rads),
lastCommand.state.wrist);
ss.setHeightForWristLevel(z);
ss.setGoal(new SuperstructureGoal(newCommand),
Superstructure.ElevatorControlModes.PRISMATIC_WRIST);
return reachable;
}
public static void jogElevator(double delta) {
Superstructure ss = Superstructure.getInstance();
ss.jogElevator(delta);
}
// Both of these are in in.
public static void jogEndEffector(double jogX, double jogZ) {
Superstructure ss = Superstructure.getInstance();
SuperstructureGoal lastCommand = ss.getGoal();
if (lastCommand == null) {
return;
}
if (Util.epsilonEquals(jogX, 0.0)) {
double elevator_height = lastCommand.state.elevator + jogZ;
elevator_height =
Util.limit(elevator_height, 0.0, Constants.kElevatorConstants.kMaxUnitsLimit);
SuperstructureState newCommand = new SuperstructureState(
lastCommand.state.turret,
elevator_height,
lastCommand.state.shoulder,
lastCommand.state.wrist);
sendCommandToSuperstructure(newCommand);
return;
}
// Find current end effector positions.
Translation2d end_effector_pos = lastCommand.state.getPlanarWristJointLocation();
double jogged_x = end_effector_pos.x() + jogX;
double jogged_z = end_effector_pos.y() + jogZ;
if (jogged_x > Constants.kArmLength) {
// Constrain to max
jogged_x = Util.limit(jogged_x, 0, Constants.kArmLength);
}
jogged_z = Util.limit(jogged_z, 0, Constants.kElevatorConstants.kMaxUnitsLimit);
setEndEffectorPos(jogged_x, jogged_z, Double.NaN);
}
public static SuperstructureState onlyArms(SuperstructureState setpoint) {
Superstructure ss = Superstructure.getInstance();
SuperstructureGoal desiredState = ss.getGoal();
double turret = 0.0;
if (desiredState != null) {
turret = desiredState.state.turret;
}
return new SuperstructureState(
turret,
setpoint.elevator,
setpoint.shoulder,
setpoint.wrist);
}
public static void goToPickupDiskFromWallFrontFar() {
var intake = IntakeDiskWallFar;
observeDisk();
selectPositionByGamepiece(
onlyArms(intake),
onlyArms(intake));
}
public static void goToPickupDiskFromWallFront() {
var intake = IntakeDiskWall;
observeDisk();
selectPositionByGamepiece(
onlyArms(intake),
onlyArms(intake));
}
public static void goToPickupBallFromGroundFront() {
var intake = IntakeBallGroundFront;
observeBall();
sendCommandToSuperstructure(intake);
}
public static void goToPickupBallFromGroundClosest() {
observeBall();
var curTurret = Superstructure.getInstance().getCurrentState().turret;
var command = new SuperstructureState(IntakeBallGroundFront);
command.turret = curTurret > 90f ? 180 : 0;
sendCommandToSuperstructure(command);
}
public static void goToPickupDiskFromWallBack() {
var intake = IntakeDiskWallBack;
observeDisk();
selectPositionByGamepiece(intake, intake);
}
public static void goToPickupBallFromGroundBack() {
var intake = IntakeBallGroundBack;
observeBall();
selectPositionByGamepiece(intake, intake);
}
public static void goToPickupBallFromWall() {
var intake = IntakeBallWall;
observeBall();
selectPositionByGamepiece(intake, intake);
}
public static void goToStowed() {
sendCommandToSuperstructure(tuckedPosition);
}
public static void goToStowedBackwards() {
sendCommandToSuperstructure(tuckedPositionBackwards);
}
public static void goToStowedWithBall() {
selectPositionByGamepiece(
onlyArms(tuckedPosition),
onlyArms(tuckedPosition)
);
}
public static void goToScoreLow() {
selectPositionByGamepiece(
onlyArms(ScoreBallLow),
onlyArms(ScoreDiskLow)
);
mLowPosition = true;
}
public static void goToPreScoreLow() {
selectPositionByGamepiece(
onlyArms(PreScoreBallLow),
onlyArms(PreScoreBallLow)
);
mLowPosition = true;
}
public static void goToScoreMiddle() {
selectPositionByGamepiece(
onlyArms(ScoreBallMiddle),
onlyArms(ScoreDiskMiddle)
);
mMiddlePosition = true;
}
public static void goToScoreHigh() {
selectPositionByGamepiece(
onlyArms(ScoreBallHigh),
onlyArms(ScoreDiskHigh)
);
mHighPosition = true;
}
public static void goToScoreCargo() {
selectPositionByGamepiece(
onlyArms(ScoreBallCargoShip),
onlyArms(ScoreBallCargoShip)
);
mCargoShipPosition = true;
}
public static void goToPrepareForStingerClimb() {
sendCommandToSuperstructure(PrepareForStingerClimb);
}
public static void goToPrepareForVacuumClimb() {
sendCommandToSuperstructure(onlyArms(PrepareForVacuumClimb));
}
public static void goToPrepareForStingerClimbLow() {
sendCommandToSuperstructure(PrepareForStingerClimbLow);
}
public static void goToPreAutoThrust() {
SuperstructureGoal pre_wrist = Superstructure.getInstance().getPreWristLevelGoal();
if (pre_wrist != null) {
sendCommandToSuperstructure(onlyArms(pre_wrist.state));
}
}
public static void goToVacuumClimbing() {
sendCommandToSuperstructure(onlyArms(VacuumClimb));
}
public static void goToVacuumHab3ContactPoint() {
sendCommandToSuperstructure(
onlyArms(VacuumHab3ContactPoint)
);
}
public static boolean isPreparingToClimb() {
return false;
}
private static void selectPositionByGamepiece(
SuperstructureState withBall,
SuperstructureState withDisk) {
selectPositionByGamepieceWithClimb(withBall, withDisk, withDisk);
}
private static void selectPositionByGamepieceWithClimb(
SuperstructureState withBall,
SuperstructureState withDisk,
SuperstructureState whileClimbing) {
switch (EndEffector.getInstance().getObservedGamePiece()) {
case BALL:
sendCommandToSuperstructure(withBall);
break;
case DISK:
sendCommandToSuperstructure(withDisk);
break;
case CLIMB:
sendCommandToSuperstructure(whileClimbing);
break;
}
mCargoShipPosition = false;
mMiddlePosition = false;
mHighPosition = false;
mLowPosition = false;
}
public static boolean isInLowPosition() {
return mLowPosition;
}
public static boolean isInHighPosition() {
return mHighPosition;
}
public static boolean isInMiddlePosition() {
return mMiddlePosition;
}
public static boolean isInCargoShipPosition() {
return mCargoShipPosition;
}
private static void sendCommandToSuperstructure(SuperstructureState position) {
// hmb, hacks ahead
Superstructure ss = Superstructure.getInstance();
//ss.setPlannerEnabled();
ss.setGoal(new SuperstructureGoal(position));
}
private static final double kElevatorStowedLowHeight = 12.7;
public static final SuperstructureState tuckedPosition =
new SuperstructureState(0, kElevatorStowedLowHeight, -90, 0);
public static final SuperstructureState stowedPosition = tuckedPosition;
public static final SuperstructureState tuckedPositionBackwards =
new SuperstructureState(180, kElevatorStowedLowHeight, -90, 0);
private static final SuperstructureState climbPosition =
new SuperstructureState(0, 27.1, -40.4, -45.7);
static SuperstructureState StowedDisk = stowedPosition;
public static final double kLevel = 0.0;
static SuperstructureState IntakeDiskWallFar = new SuperstructureState(0, 1.75, -28.0, kLevel);
static SuperstructureState IntakeDiskWall = new SuperstructureState(0, 10.65, -70.0, kLevel);
static SuperstructureState IntakeDiskWallBack = new SuperstructureState(180, 1.2, -27.8, kLevel);
static SuperstructureState ScoreDiskLow = new SuperstructureState(0, kElevatorStowedLowHeight, -90, kLevel);
static SuperstructureState ScoreDiskMiddle = new SuperstructureState(0, 0, 90, kLevel);
static SuperstructureState ScoreDiskHigh = new SuperstructureState(0, 28.0, 90.0, kLevel);
static SuperstructureState StowedBall = stowedPosition;
static SuperstructureState IntakeBallGroundFront = new SuperstructureState(0, 9.0, -53.5, -53.3);
static SuperstructureState IntakeBallGroundBack = new SuperstructureState(180, 9.0, -53.5, -53.3);
static SuperstructureState IntakeBallWall = stowedPosition;
static SuperstructureState ScoreBallLow = new SuperstructureState(0, 22.2, -90, 0);
static SuperstructureState PreScoreBallLow = new SuperstructureState(0, 14.2, -90, 0);
static SuperstructureState ScoreBallMiddle = new SuperstructureState(0, 8.2, 90.0, 0);
static SuperstructureState ScoreBallHigh = new SuperstructureState(0, Constants.kElevatorConstants.kMaxUnitsLimit, 83, 20.3);
static SuperstructureState ScoreBallCargoShip = new SuperstructureState(0, Constants.kElevatorConstants.kMaxUnitsLimit, -90, 0);
public static SuperstructureGoal PreScoreBallLowGoal = new SuperstructureGoal(PreScoreBallLow);
// Stinger climb for SFR and SVR
static SuperstructureState PrepareForStingerClimb = climbPosition;
static SuperstructureState PrepareForStingerClimbLow =
new SuperstructureState(0, 20, -40.4, -45.7);
// Vacuum climb for champs
static SuperstructureState PrepareForVacuumClimb = new SuperstructureState(0, 31.0, -90, kLevel);
public static SuperstructureState VacuumHab3ContactPoint = new SuperstructureState(0, 29.0, -45, kLevel);
public static SuperstructureState VacuumClimb = new SuperstructureState(0, 4.25, -55, kLevel);
}