-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShooterSubsystem.java
More file actions
660 lines (580 loc) · 25 KB
/
ShooterSubsystem.java
File metadata and controls
660 lines (580 loc) · 25 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot.subsystems.shooter;
import java.util.List;
import java.util.Map.Entry;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
import org.apache.commons.math3.util.Precision;
import org.lasarobotics.hardware.revrobotics.Spark;
import org.lasarobotics.hardware.revrobotics.Spark.FeedbackSensor;
import org.lasarobotics.hardware.revrobotics.Spark.MotorKind;
import org.lasarobotics.hardware.revrobotics.SparkPIDConfig;
import org.lasarobotics.led.LEDStrip;
import org.lasarobotics.led.LEDStrip.Pattern;
import org.lasarobotics.led.LEDSubsystem;
import org.lasarobotics.utils.GlobalConstants;
import org.littletonrobotics.junction.Logger;
import com.revrobotics.CANSparkBase.ControlType;
import com.revrobotics.CANSparkBase.IdleMode;
import com.revrobotics.SparkPIDController.ArbFFUnits;
import edu.wpi.first.apriltag.AprilTag;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.filter.Debouncer;
import edu.wpi.first.math.filter.Debouncer.DebounceType;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.units.Angle;
import edu.wpi.first.units.Current;
import edu.wpi.first.units.Dimensionless;
import edu.wpi.first.units.Distance;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import edu.wpi.first.units.Units;
import edu.wpi.first.units.Velocity;
import edu.wpi.first.units.Voltage;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
public class ShooterSubsystem extends SubsystemBase implements AutoCloseable {
public static class Hardware {
private Spark topFlywheelMotor;
private Spark bottomFlywheelMotor;
private Spark angleMotor;
private Spark indexerMotor;
private LEDStrip ledStrip;
public Hardware(Spark lSlaveMotor,
Spark rMasterMotor,
Spark angleMotor,
Spark indexerMotor,
LEDStrip ledStrip) {
this.topFlywheelMotor = lSlaveMotor;
this.bottomFlywheelMotor = rMasterMotor;
this.angleMotor = angleMotor;
this.indexerMotor = indexerMotor;
this.ledStrip = ledStrip;
}
}
/** Shooter state */
public static class State {
public final Measure<Velocity<Distance>> speed;
public final Measure<Angle> angle;
public static final State AMP_PREP_STATE = new State(ZERO_FLYWHEEL_SPEED, Units.Degrees.of(55.0));
public static final State AMP_SCORE_STATE = new State(Units.MetersPerSecond.of(+3.1), Units.Degrees.of(55.0));
public static final State SPEAKER_PREP_STATE = new State(ZERO_FLYWHEEL_SPEED, Units.Degrees.of(55.0));
public static final State SPEAKER_SCORE_STATE = new State(Units.MetersPerSecond.of(+15.0), Units.Degrees.of(55.0));
public static final State SOURCE_PREP_STATE = new State(ZERO_FLYWHEEL_SPEED, Units.Degrees.of(55.0));
public static final State SOURCE_INTAKE_STATE = new State(Units.MetersPerSecond.of(-10.0), Units.Degrees.of(55.0));
public static final State PASSING_STATE = new State(Units.MetersPerSecond.of(+15.0), Units.Degrees.of(45.0));
public static final State PODIUM_SCORE_STATE = new State(Units.MetersPerSecond.of(+16.25786), Units.Degrees.of(36));
public static final State TEST_STOP_STATE = new State(ZERO_FLYWHEEL_SPEED, Units.Degrees.of(30.0));
public State(Measure<Velocity<Distance>> speed, Measure<Angle> angle) {
this.speed = speed;
this.angle = angle;
}
}
private static final SplineInterpolator SPLINE_INTERPOLATOR = new SplineInterpolator();
public static final Measure<Velocity<Distance>> ZERO_FLYWHEEL_SPEED = Units.MetersPerSecond.of(0.0);
private static final Measure<Voltage> ANGLE_FF = Units.Volts.of(0.1);
private static final Measure<Current> FLYWHEEL_CURRENT_LIMIT = Units.Amps.of(80.0);
private static final Measure<Current> ANGLE_MOTOR_CURRENT_LIMIT = Units.Amps.of(50.0);
private static final Measure<Voltage> ANGLE_MOTOR_FF = Units.Volts.of(0.1);
private static final Measure<Dimensionless> INDEXER_SPEED = Units.Percent.of(100.0);
private static final Measure<Dimensionless> INDEXER_SLOW_SPEED = Units.Percent.of(4.0);
private static final String MECHANISM_2D_LOG_ENTRY = "/Mechanism2d";
private static final String SHOOTER_STATE_FLYWHEEL_SPEED = "/CurrentState/FlywheelSpeed";
private static final String SHOOTER_STATE_ANGLE_DEGREES = "/CurrentState/Angle";
private static final String SHOOTER_DESIRED_STATE_ANGLE = "/DesiredState/Angle";
private static final String SHOOTER_DESIRED_STATE_SPEED = "/DesiredState/FlywheelSpeed";
private static final String SHOOTER_TARGET_DISTANCE = "/TargetDistance";
private static final String SHOOTER_NOTE_INSIDE_INDICATOR = "Note";
private final Measure<Distance> MIN_SHOOTING_DISTANCE = Units.Meters.of(0.0);
private final Measure<Distance> MAX_SHOOTING_DISTANCE;
private final Measure<Velocity<Distance>> MAX_FLYWHEEL_SPEED;
private final Measure<Velocity<Distance>> SPINUP_SPEED = Units.MetersPerSecond.of(10.0);
private final Measure<Current> NOTE_SHOT_CURRENT_THRESHOLD = Units.Amps.of(10.0);
private final Measure<Time> NOTE_SHOT_TIME_THRESHOLD = Units.Seconds.of(0.1);
private final Measure<Time> READY_TIME_THRESHOLD = Units.Seconds.of(GlobalConstants.ROBOT_LOOP_PERIOD * 2);
private final Debouncer NOTE_SHOT_DETECTOR = new Debouncer(NOTE_SHOT_TIME_THRESHOLD.in(Units.Seconds), DebounceType.kRising);
private final Debouncer READY_DEBOUNCER = new Debouncer(READY_TIME_THRESHOLD.in(Units.Seconds), DebounceType.kBoth);
private Spark m_topFlywheelMotor;
private Spark m_bottomFlywheelMotor;
private Spark m_angleMotor;
private Spark m_indexerMotor;
private LEDStrip m_ledStrip;
private TrapezoidProfile.Constraints m_angleConstraint;
private Supplier<Pose2d> m_poseSupplier;
private Supplier<AprilTag> m_targetSupplier;
private SparkPIDConfig m_flywheelConfig;
private SparkPIDConfig m_angleConfig;
private State m_desiredShooterState;
private PolynomialSplineFunction m_shooterAngleCurve;
private PolynomialSplineFunction m_shooterFlywheelCurve;
private Mechanism2d m_mechanism2d;
private MechanismLigament2d m_simShooterJoint;
private TrapezoidProfile m_simShooterAngleMotionProfile;
private TrapezoidProfile.State m_simShooterAngleState;
private Measure<Distance> m_targetDistance;
/**
* Create an instance of ShooterSubsystem
* <p>
* NOTE: ONLY ONE INSTANCE SHOULD EXIST AT ANY TIME!
* <p>
* @param shooterHardware Hardware devices required by shooter
* @param flywheelConfig Flywheel PID config
* @param angleConfig Angle adjust PID config
* @param angleFF Angle adjust feed forward gains
* @param angleConstraint Angle adjust motion constraint
* @param topFlywheelDiameter Top flywheel diameter
* @param bottomFlywheelDiameter Bottom flywheel diameter
* @param shooterMap Shooter lookup table
* @param poseSupplier Robot pose supplier
* @param targetSupplier Speaker target supplier
*/
public ShooterSubsystem(Hardware shooterHardware, SparkPIDConfig flywheelConfig, SparkPIDConfig angleConfig,
TrapezoidProfile.Constraints angleConstraint, Measure<Distance> topFlywheelDiameter, Measure<Distance> bottomFlywheelDiameter,
List<Entry<Measure<Distance>, State>> shooterMap,
Supplier<Pose2d> poseSupplier, Supplier<AprilTag> targetSupplier) {
setSubsystem(getClass().getSimpleName());
MAX_FLYWHEEL_SPEED = Units.MetersPerSecond.of((shooterHardware.topFlywheelMotor.getKind().getMaxRPM() / 60) * (topFlywheelDiameter.in(Units.Meters) * Math.PI));
this.m_topFlywheelMotor = shooterHardware.topFlywheelMotor;
this.m_bottomFlywheelMotor = shooterHardware.bottomFlywheelMotor;
this.m_angleMotor = shooterHardware.angleMotor;
this.m_indexerMotor = shooterHardware.indexerMotor;
this.m_ledStrip = shooterHardware.ledStrip;
this.m_flywheelConfig = flywheelConfig;
this.m_angleConfig = angleConfig;
this.m_angleConstraint = angleConstraint;
this.m_poseSupplier = poseSupplier;
this.m_targetSupplier = targetSupplier;
// Initialize PID
m_topFlywheelMotor.initializeSparkPID(m_flywheelConfig, FeedbackSensor.NEO_ENCODER);
m_bottomFlywheelMotor.initializeSparkPID(m_flywheelConfig, FeedbackSensor.NEO_ENCODER);
m_angleMotor.initializeSparkPID(m_angleConfig, FeedbackSensor.THROUGH_BORE_ENCODER, true, true);
// Set flywheel conversion factor
var topFlywheelConversionFactor = topFlywheelDiameter.in(Units.Meters) * Math.PI;
var bottomFlywheelConversionFactor = bottomFlywheelDiameter.in(Units.Meters) * Math.PI;
m_topFlywheelMotor.setPositionConversionFactor(FeedbackSensor.NEO_ENCODER, topFlywheelConversionFactor);
m_topFlywheelMotor.setVelocityConversionFactor(FeedbackSensor.NEO_ENCODER, topFlywheelConversionFactor / 60);
m_bottomFlywheelMotor.setPositionConversionFactor(FeedbackSensor.NEO_ENCODER, bottomFlywheelConversionFactor);
m_bottomFlywheelMotor.setVelocityConversionFactor(FeedbackSensor.NEO_ENCODER, bottomFlywheelConversionFactor / 60);
// Set angle adjust conversion factor
var angleConversionFactor = Math.PI * 2;
m_angleMotor.setPositionConversionFactor(FeedbackSensor.THROUGH_BORE_ENCODER, angleConversionFactor);
m_angleMotor.setVelocityConversionFactor(FeedbackSensor.THROUGH_BORE_ENCODER, angleConversionFactor / 60);
// Set idle mode
m_topFlywheelMotor.setIdleMode(IdleMode.kCoast);
m_bottomFlywheelMotor.setIdleMode(IdleMode.kCoast);
m_angleMotor.setIdleMode(IdleMode.kBrake);
m_indexerMotor.setIdleMode(IdleMode.kBrake);
// Set current limits
m_topFlywheelMotor.setSmartCurrentLimit(FLYWHEEL_CURRENT_LIMIT);
m_bottomFlywheelMotor.setSmartCurrentLimit(FLYWHEEL_CURRENT_LIMIT);
m_angleMotor.setSmartCurrentLimit(ANGLE_MOTOR_CURRENT_LIMIT);
// Disable indexer hard limits
m_indexerMotor.disableForwardLimitSwitch();
m_indexerMotor.disableReverseLimitSwitch();
// Initialize shooter state
m_desiredShooterState = getCurrentState();
// Register LED strip with LED subsystem
LEDSubsystem.getInstance().add(m_ledStrip);
// Set LED strip to team color
m_ledStrip.set(Pattern.TEAM_COLOR_SOLID);
// Set maximum shooting distance
MAX_SHOOTING_DISTANCE = shooterMap.get(shooterMap.size() - 1).getKey();
// Initialize shooter curves
initializeShooterCurves(shooterMap);
// Set default command to track speaker angle
setDefaultCommand(run(() -> {
var state = getAutomaticState();
state = new State(ZERO_FLYWHEEL_SPEED, state.angle);
setState(state, true);
}));
// Initialize sim variables
m_mechanism2d = new Mechanism2d(1.0, 1.0);
m_simShooterJoint = m_mechanism2d.getRoot("shooter", 0.5, 0.33).append(new MechanismLigament2d("shooter", 0.4, 1.0));
m_simShooterAngleMotionProfile = new TrapezoidProfile(m_angleConstraint);
m_simShooterAngleState = new TrapezoidProfile.State(m_angleConfig.getLowerLimit(), 0.0);
// Register LED strip with LED subsystem
LEDSubsystem.getInstance().add(m_ledStrip);
// Set LEDs to team color
m_ledStrip.set(Pattern.TEAM_COLOR_BREATHE);
}
/**
* Initialize hardware devices for shooter subsystem
* @return Hardware object containing all necessary devices for this subsystem
*/
public static Hardware initializeHardware() {
Hardware shooterHardware = new Hardware(
new Spark(Constants.ShooterHardware.TOP_FLYWHEEL_MOTOR_ID, MotorKind.NEO_VORTEX),
new Spark(Constants.ShooterHardware.BOTTOM_FLYWHEEL_MOTOR_ID, MotorKind.NEO_VORTEX),
new Spark(Constants.ShooterHardware.ANGLE_MOTOR_ID, MotorKind.NEO),
new Spark(Constants.ShooterHardware.INDEXER_MOTOR_ID, MotorKind.NEO),
new LEDStrip(LEDStrip.initializeHardware(Constants.ShooterHardware.LED_STRIP_ID))
);
return shooterHardware;
}
/**
* Initialize spline functions for shooter
* @param shooterMap List of distance and shooter state pairs
*/
private void initializeShooterCurves(List<Entry<Measure<Distance>, State>> shooterMap) {
double[] distances = new double[shooterMap.size()];
double[] flywheelSpeeds = new double[shooterMap.size()];
double[] angles = new double[shooterMap.size()];
for (int i = 0; i < shooterMap.size(); i++) {
distances[i] = shooterMap.get(i).getKey().in(Units.Meters);
flywheelSpeeds[i] = shooterMap.get(i).getValue().speed.in(Units.MetersPerSecond);
angles[i] = shooterMap.get(i).getValue().angle.in(Units.Radians);
}
m_shooterFlywheelCurve = SPLINE_INTERPOLATOR.interpolate(distances, flywheelSpeeds);
m_shooterAngleCurve = SPLINE_INTERPOLATOR.interpolate(distances, angles);
}
/**
* Set shooter to desired state
* @param state Desired shooter state
*/
private void setState(State state, boolean continuous) {
// Normalize state to valid range
m_desiredShooterState = normalizeState(state);
// Set flywheel speed, coast to a stop if speed not desired
if (state.speed.isNear(ZERO_FLYWHEEL_SPEED, 0.01)) {
m_topFlywheelMotor.stopMotor();
m_bottomFlywheelMotor.stopMotor();
} else {
m_topFlywheelMotor.set(m_desiredShooterState.speed.in(Units.MetersPerSecond), ControlType.kVelocity);
m_bottomFlywheelMotor.set(m_desiredShooterState.speed.in(Units.MetersPerSecond), ControlType.kVelocity);
}
// Set angle
if (continuous) m_angleMotor.set(m_desiredShooterState.angle.in(Units.Radians), ControlType.kPosition, ANGLE_FF.in(Units.Volts), ArbFFUnits.kVoltage);
else m_angleMotor.smoothMotion(m_desiredShooterState.angle.in(Units.Radians), m_angleConstraint, motionState -> ANGLE_FF.in(Units.Volts));
}
/**
* Normalize shooter state to be within valid values
* @param state Desired state
* @return Valid shooter state
*/
private State normalizeState(State state) {
Measure<Velocity<Distance>> clampedSpeed = Units.MetersPerSecond.of(MathUtil.clamp(
state.speed.in(Units.MetersPerSecond),
-MAX_FLYWHEEL_SPEED.in(Units.MetersPerSecond),
+MAX_FLYWHEEL_SPEED.in(Units.MetersPerSecond)
));
Measure<Angle> clampedAngle = Units.Radians.of(MathUtil.clamp(
state.angle.in(Units.Radians),
m_angleConfig.getLowerLimit(),
m_angleConfig.getUpperLimit()
));
return new State(clampedSpeed, clampedAngle);
}
/**
* Reset shooter state
*/
private void resetState() {
setState(new State(ZERO_FLYWHEEL_SPEED, m_desiredShooterState.angle), false);
}
/**
* Get current shooter state
* @return Current shooter state
*/
private State getCurrentState() {
return new State(
Units.MetersPerSecond.of(m_topFlywheelMotor.getInputs().encoderVelocity),
Units.Radians.of(m_angleMotor.getInputs().absoluteEncoderPosition)
);
}
/**
* Get shooter state based on distance to target
* @return Shooter state for current target distance
*/
private State getAutomaticState() {
var targetDistance = getTargetDistance();
var flywheelSpeed = m_shooterFlywheelCurve.value(targetDistance.in(Units.Meters));
var angle = m_shooterAngleCurve.value(targetDistance.in(Units.Meters));
return new State(Units.MetersPerSecond.of(flywheelSpeed), Units.Radians.of(angle));
}
/**
* Get distance to target, clamped to maximum shooting distance
* @return Distance to target
*/
private Measure<Distance> getTargetDistance() {
return m_targetDistance;
}
/**
* Check if shooter has reached desired state and is ready
* @return True if ready
*/
private boolean isReady() {
return READY_DEBOUNCER.calculate(
Precision.equals(
m_angleMotor.getInputs().absoluteEncoderPosition,
m_desiredShooterState.angle.in(Units.Radians),
m_angleConfig.getTolerance()
) &&
Precision.equals(
m_topFlywheelMotor.getInputs().encoderVelocity,
m_desiredShooterState.speed.in(Units.MetersPerSecond),
m_flywheelConfig.getTolerance()
) &&
Precision.equals(
m_bottomFlywheelMotor.getInputs().encoderVelocity,
m_desiredShooterState.speed.in(Units.MetersPerSecond),
m_flywheelConfig.getTolerance()
));
}
/**
* Feed game piece to flywheels
* @param slow True to run slowly
*/
private void feedStart(boolean slow) {
m_indexerMotor.set(slow ? +INDEXER_SLOW_SPEED.in(Units.Percent) : +INDEXER_SPEED.in(Units.Percent));
}
/**
* Reverse indexer
* @param slow True to run slowly
*/
private void feedReverse(boolean slow) {
m_indexerMotor.set(slow ? -INDEXER_SLOW_SPEED.in(Units.Percent) : -INDEXER_SPEED.in(Units.Percent));
}
/**
* Stop feeding
*/
private void feedStop() {
m_indexerMotor.stopMotor();
}
@Override
public void periodic() {
// This method will be called once per scheduler run
// Put note indicator on SmartDashboard
SmartDashboard.putBoolean(SHOOTER_NOTE_INSIDE_INDICATOR, isObjectPresent());
// Update target distance
m_targetDistance = Units.Meters.of(MathUtil.clamp(
m_poseSupplier.get().getTranslation().getDistance(m_targetSupplier.get().pose.toPose2d().getTranslation()),
MIN_SHOOTING_DISTANCE.in(Units.Meters),
MAX_SHOOTING_DISTANCE.in(Units.Meters)
));
// Log outputs
var currentState = getCurrentState();
Logger.recordOutput(getName() + MECHANISM_2D_LOG_ENTRY, m_mechanism2d);
Logger.recordOutput(getName() + SHOOTER_STATE_FLYWHEEL_SPEED, currentState.speed.in(Units.MetersPerSecond));
Logger.recordOutput(getName() + SHOOTER_STATE_ANGLE_DEGREES, currentState.angle.in(Units.Degrees));
Logger.recordOutput(getName() + SHOOTER_DESIRED_STATE_ANGLE, m_desiredShooterState.angle.in(Units.Degrees));
Logger.recordOutput(getName() + SHOOTER_DESIRED_STATE_SPEED, m_desiredShooterState.speed.in(Units.MetersPerSecond));
Logger.recordOutput(getName() + SHOOTER_TARGET_DISTANCE, getTargetDistance());
}
@Override
public void simulationPeriodic() {
// This method will be called once per scheduler run in simulation
m_topFlywheelMotor.getInputs().encoderVelocity = m_desiredShooterState.speed.in(Units.MetersPerSecond);
m_bottomFlywheelMotor.getInputs().encoderVelocity = m_desiredShooterState.speed.in(Units.MetersPerSecond);
m_angleMotor.getInputs().absoluteEncoderPosition = m_simShooterAngleState.position;
m_angleMotor.getInputs().absoluteEncoderVelocity = m_simShooterAngleState.velocity;
m_simShooterAngleState = m_simShooterAngleMotionProfile.calculate(
GlobalConstants.ROBOT_LOOP_PERIOD,
m_simShooterAngleState,
new TrapezoidProfile.State(m_desiredShooterState.angle.in(Units.Radians), 0.0)
);
m_simShooterJoint.setAngle(Rotation2d.fromRadians(Math.PI - m_simShooterAngleState.position));
}
/**
* Intake a game piece from the ground intake to be later fed to the shooter
* @return Command to intake to the shooter
*/
public Command intakeCommand() {
return runEnd(
() -> getDefaultCommand().execute(),
() -> {
m_indexerMotor.disableForwardLimitSwitch();
feedStop();
}
).beforeStarting(() -> {
m_indexerMotor.enableForwardLimitSwitch();
feedStart(true);
}).until(() -> isObjectPresent());
}
/**
* Intake a game piece from the source via the shooter
* @return Command that intakes via the shooter
*/
public Command sourceIntakeCommand() {
return startEnd(
() -> {
m_indexerMotor.enableReverseLimitSwitch();
feedReverse(true);
setState(State.SOURCE_INTAKE_STATE, false);
},
() -> {
m_indexerMotor.disableReverseLimitSwitch();
feedStop();
resetState();
}
).until(() -> isObjectPresent());
}
/**
* Intake a game piece from the ground intake to be later fed to the shooter with no limit switches
* @return Command to feed through the shooter
*/
public Command feedCommand() {
return startEnd(() -> feedStart(false), () -> feedStop());
}
public Command feedThroughCommand(BooleanSupplier isAimed) {
return startEnd(() -> {
feedStart(false);
setState(new State(Units.MetersPerSecond.of(2.0), m_desiredShooterState.angle), false);
},
() -> {
feedStop();
resetState();
});
}
/**
* Reverse note from shooter into intake
* @return Command to outtake note in shooter
*/
public Command outtakeCommand() {
return startEnd(
() -> feedReverse(false),
() -> feedStop()
);
}
/**
* Shoot by manually setting shooter state
* @param stateSupplier Desired shooter state supplier
* @return Command to control shooter manually
*/
public Command shootManualCommand(Supplier<State> stateSupplier) {
return runEnd(
() -> {
if (isReady()) feedStart(false);
else feedStop();
},
() -> {
feedStop();
resetState();
}
).beforeStarting(() -> setState(stateSupplier.get(), false), this);
}
/**
* Shoot by manually setting shooter state
* @param state Desired shooter state
* @return Command to control shooter manually
*/
public Command shootManualCommand(State state) {
return shootManualCommand(() -> state);
}
/**
* Shoot automatically based on current location
* @param isAimed Is robot aimed at target
* @param override Shoot even if target tag is not visible and not in range
* @return Command to automatically shoot note
*/
public Command shootCommand(BooleanSupplier isAimed, BooleanSupplier override) {
return runEnd(
() -> {
setState(getAutomaticState(), true);
if ((RobotBase.isSimulation() | isReady() && isAimed.getAsBoolean()) || override.getAsBoolean())
feedStart(false);
else feedStop();
},
() -> {
feedStop();
resetState();
}
);
}
/**
* Shoot automatically based on current location, checking if target tag is visible and robot is in range
* @param isAimed Is robot aimed at target
* @return Command to automatically shoot note
*/
public Command shootCommand(BooleanSupplier isAimed) {
return shootCommand(isAimed, () -> false);
}
/**
* Shoot note into speaker from subwoofer
* @return Command to shoot note when parked against the subwoofer
*/
public Command shootSpeakerCommand() {
return shootManualCommand(State.SPEAKER_SCORE_STATE);
}
/**
* Pass note from opponent side to your side
* @return Command to pass note
*/
public Command passCommand() {
return shootManualCommand(State.PASSING_STATE);
}
/**
* Shoot note into speaker from podium
* @return Command to shoot note into speaker when near podium
*/
public Command shootPodiumCommand() {
return shootManualCommand(State.PODIUM_SCORE_STATE);
}
/**
* Move shooter up and down
* @return Command to move the shooter between upper and lower limits
*/
public Command shootPartyMode() {
final State TOP = new State(ZERO_FLYWHEEL_SPEED, Units.Radians.of(m_angleConfig.getUpperLimit()));
final State BOTTOM = new State(ZERO_FLYWHEEL_SPEED, Units.Radians.of(m_angleConfig.getLowerLimit()));
return Commands.sequence(
run(() -> setState(TOP, false)).until(() -> isReady()),
run(() -> setState(BOTTOM, false)).until(() -> isReady())
).repeatedly();
}
/**
* Move shooter to amp position
* @return Command that prepares shooter for scoring in the amp
*/
public Command prepareForAmpCommand() {
return startEnd(
() -> setState(State.AMP_PREP_STATE, false),
() -> resetState()
).until(() -> isReady());
}
/**
* Score note in amp
* @return Command that shoots note into amp
*/
public Command scoreAmpCommand() {
return shootManualCommand(State.AMP_SCORE_STATE);
}
/**
* Spin up flywheel
* @return Command that spins up flywheel
*/
public Command spinupCommand() {
return run(() -> setState(new State(SPINUP_SPEED, m_desiredShooterState.angle), false));
}
/**
* Whether a game piece is in the indexer
* @return The value of the indexer motor's forward limit switch
*/
public boolean isObjectPresent() {
return m_indexerMotor.getInputs().forwardLimitSwitch;
}
/**
* Whether a note has been shot
* @return If the current of the top flywheel motor is greater than the threshold for a specified time
*/
public boolean hasBeenShot() {
return NOTE_SHOT_DETECTOR.calculate(m_topFlywheelMotor.getOutputCurrent().compareTo(NOTE_SHOT_CURRENT_THRESHOLD) > 0);
}
@Override
public void close() {
m_topFlywheelMotor.close();
m_bottomFlywheelMotor.close();
m_angleMotor.close();
m_indexerMotor.close();
}
}