2026-01-10 16:52:43 -07:00
|
|
|
package frc4388.robot.subsystems.shooter;
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
import static edu.wpi.first.units.Units.Amps;
|
2026-02-23 16:58:14 -07:00
|
|
|
import static edu.wpi.first.units.Units.Rotation;
|
2026-02-09 17:18:54 -08:00
|
|
|
import static edu.wpi.first.units.Units.RotationsPerSecond;
|
|
|
|
|
|
2026-02-10 18:42:47 -08:00
|
|
|
import org.littletonrobotics.junction.AutoLogOutput;
|
2026-01-10 16:52:43 -07:00
|
|
|
import org.littletonrobotics.junction.Logger;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.math.geometry.Pose2d;
|
2026-02-24 13:50:30 -07:00
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
2026-02-11 15:18:12 -07:00
|
|
|
import edu.wpi.first.math.kinematics.ChassisSpeeds;
|
2026-02-24 10:20:21 -07:00
|
|
|
import edu.wpi.first.units.measure.AngularVelocity;
|
2026-02-24 13:50:30 -07:00
|
|
|
import edu.wpi.first.units.measure.Current;
|
2026-01-10 16:52:43 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
2026-02-11 15:18:12 -07:00
|
|
|
import frc4388.robot.constants.Constants;
|
2026-02-10 18:42:47 -08:00
|
|
|
import frc4388.robot.subsystems.LED;
|
|
|
|
|
import frc4388.robot.subsystems.intake.Intake;
|
|
|
|
|
import frc4388.robot.subsystems.swerve.SwerveDrive;
|
2026-02-11 15:18:12 -07:00
|
|
|
import frc4388.utility.compute.FieldPositions;
|
2026-01-10 16:52:43 -07:00
|
|
|
|
|
|
|
|
public class Shooter extends SubsystemBase {
|
2026-02-09 17:18:54 -08:00
|
|
|
public ShooterIO io;
|
2026-01-10 16:52:43 -07:00
|
|
|
ShooterStateAutoLogged state = new ShooterStateAutoLogged();
|
|
|
|
|
|
2026-02-10 18:42:47 -08:00
|
|
|
SwerveDrive m_SwerveDrive;
|
|
|
|
|
Intake m_Intake;
|
|
|
|
|
LED m_robotLED;
|
|
|
|
|
|
|
|
|
|
|
2026-02-07 14:51:05 -07:00
|
|
|
// Supplier<Pose2d> m_swervePoseSupplier;
|
2026-02-27 20:02:12 -07:00
|
|
|
public boolean badShooterVelocity;
|
2026-01-10 16:52:43 -07:00
|
|
|
|
2026-01-29 18:07:19 -07:00
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
public Shooter(
|
2026-02-10 18:42:47 -08:00
|
|
|
ShooterIO io,
|
|
|
|
|
SwerveDrive swerveDrive,
|
|
|
|
|
Intake intake,
|
|
|
|
|
LED robotLED
|
2026-01-10 16:52:43 -07:00
|
|
|
) {
|
|
|
|
|
this.io = io;
|
2026-02-10 18:42:47 -08:00
|
|
|
this.m_SwerveDrive = swerveDrive;
|
|
|
|
|
this.m_Intake = intake;
|
|
|
|
|
this.m_robotLED = robotLED;
|
2026-02-07 14:51:05 -07:00
|
|
|
// this.m_swervePoseSupplier = swervePoseSupplier;
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum FieldZone {
|
|
|
|
|
// The robot should aim at the hub
|
|
|
|
|
InShootZone,
|
|
|
|
|
// The robot should aim towards the wall
|
|
|
|
|
AimAtWall,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 18:07:19 -07:00
|
|
|
|
|
|
|
|
public enum ShooterMode {
|
2026-02-10 18:42:47 -08:00
|
|
|
Shooting,
|
2026-02-24 10:20:21 -07:00
|
|
|
Feeding,
|
|
|
|
|
Idle
|
2026-01-29 18:07:19 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
private ShooterMode mode = ShooterMode.Idle;
|
2026-02-21 12:54:16 -08:00
|
|
|
private boolean shooterButtonReady = false;
|
2026-02-10 18:42:47 -08:00
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
public void spinUpShooting() {
|
2026-02-24 10:20:21 -07:00
|
|
|
this.mode = ShooterMode.Shooting;
|
2026-02-23 16:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
public void spinUpFeeding() {
|
2026-02-24 10:20:21 -07:00
|
|
|
this.mode = ShooterMode.Feeding;
|
2026-02-10 18:42:47 -08:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
public void spinUpIdle() {
|
2026-02-24 10:20:21 -07:00
|
|
|
this.mode = ShooterMode.Idle;
|
2026-01-29 18:07:19 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 16:58:14 -07:00
|
|
|
|
2026-02-27 20:02:12 -07:00
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
public void allowShooting() {
|
2026-02-21 12:54:16 -08:00
|
|
|
shooterButtonReady = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
public void denyShooting() {
|
2026-02-21 12:54:16 -08:00
|
|
|
shooterButtonReady = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
|
2026-02-10 18:42:47 -08:00
|
|
|
@AutoLogOutput
|
|
|
|
|
public ShooterMode getMode() {
|
|
|
|
|
return mode;
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-27 20:02:12 -07:00
|
|
|
@AutoLogOutput
|
|
|
|
|
public boolean isShooterUpToSpeed() {
|
|
|
|
|
return !badShooterVelocity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-01-29 18:07:19 -07:00
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
|
|
|
|
// FaultReporter.register(this); // TODO Implement fault reporter
|
|
|
|
|
|
|
|
|
|
Logger.processInputs("Shooter", state);
|
2026-02-10 18:42:47 -08:00
|
|
|
io.updateInputs(state);
|
2026-01-10 16:52:43 -07:00
|
|
|
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
// Get robot positon and speeds
|
|
|
|
|
ChassisSpeeds chassisSpeeds = m_SwerveDrive.chassisSpeeds;
|
|
|
|
|
double XYSpeed = Math.sqrt(Math.pow(chassisSpeeds.vxMetersPerSecond,2) + Math.pow(chassisSpeeds.vyMetersPerSecond,2));
|
|
|
|
|
double AngSpeed = Math.abs(chassisSpeeds.omegaRadiansPerSecond * (180/Math.PI));
|
2026-02-11 15:18:12 -07:00
|
|
|
|
2026-02-17 16:09:58 -07:00
|
|
|
Pose2d robotPose2d = m_SwerveDrive.getPose2d();
|
2026-02-11 15:18:12 -07:00
|
|
|
|
2026-02-14 11:50:09 -08:00
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
// Calculate aim lead
|
|
|
|
|
// Get the current speed of the robot
|
|
|
|
|
Translation2d robotSpeed = new Translation2d(
|
|
|
|
|
chassisSpeeds.vxMetersPerSecond,
|
|
|
|
|
chassisSpeeds.vyMetersPerSecond
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Calculate a point to aim ahead of the actual position.
|
|
|
|
|
Translation2d fieldPosLead = robotSpeed.times(ShooterConstants.AIM_LEAD_TIME.get()).plus(robotPose2d.getTranslation());
|
|
|
|
|
|
|
|
|
|
// Get the robot's aim distance to hub
|
|
|
|
|
double distanceToHub = (fieldPosLead.minus(FieldPositions.HUB_POSITION).getNorm());
|
|
|
|
|
|
|
|
|
|
//Center of hub to cameras in inches
|
|
|
|
|
Logger.recordOutput("Hub Dist", distanceToHub);
|
2026-02-23 17:33:25 -07:00
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
boolean driverError =
|
|
|
|
|
// XYSpeed <= ShooterConstants.ROBOT_SPEED_TOLERANCE.get() |
|
|
|
|
|
// AngSpeed <= ShooterConstants.ROBOT_ANG_SPEED_TOLERANCE.get() |
|
|
|
|
|
distanceToHub <= ShooterConstants.ROBOT_MIN_HUB.get() |
|
|
|
|
|
distanceToHub >= ShooterConstants.ROBOT_MAX_HUB.get();
|
2026-02-14 10:55:51 -08:00
|
|
|
|
2026-02-21 15:55:29 -07:00
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
double shooterSpeed = Math.abs(state.motor1Velocity.in(RotationsPerSecond) + state.motor2Velocity.in(RotationsPerSecond)) / 2;
|
|
|
|
|
double shooterSpeedTarget = Math.abs(state.motor1TargetVelocity.in(RotationsPerSecond) + state.motor2TargetVelocity.in(RotationsPerSecond)) / 2;
|
2026-02-14 12:49:33 -08:00
|
|
|
|
2026-02-27 20:02:12 -07:00
|
|
|
badShooterVelocity = Math.abs(shooterSpeed - shooterSpeedTarget) > ShooterConstants.SHOOTER_SPEED_TOLERANCE.get();
|
|
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
|
2026-02-10 17:33:39 -08:00
|
|
|
switch (mode) {
|
2026-02-10 18:42:47 -08:00
|
|
|
case Shooting:
|
2026-02-23 16:58:14 -07:00
|
|
|
io.setShooterVelocity(state, ShooterConstants.getTargetShooterSpeed(distanceToHub));
|
|
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
int bitmask = (
|
|
|
|
|
(shooterButtonReady ? 1 : 0) +
|
|
|
|
|
(badShooterVelocity ? 2 : 0) +
|
|
|
|
|
(driverError ? 4 : 0)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
switch (bitmask) {
|
|
|
|
|
case 0b000: // No errors but button is not pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.OPREADY);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b001: // No errors and shoot button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_FORWARD_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.OPREADY);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b010: // Bad shooter velocity, button is not pressed
|
|
|
|
|
case 0b011: // Bad shooter velocty, button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b100: // Driver error, button is not pressed
|
|
|
|
|
case 0b101: // Driver error, button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.OPREADY_BADPHYS);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b110: // Driver error, bad shooter vel, button is not pressed
|
|
|
|
|
case 0b111: // Driver error, bad shooter vel, button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL_BADPHYS);
|
|
|
|
|
break;
|
2026-02-21 12:54:16 -08:00
|
|
|
}
|
2026-02-10 17:33:39 -08:00
|
|
|
break;
|
2026-02-24 10:20:21 -07:00
|
|
|
case Feeding:
|
|
|
|
|
io.setShooterVelocity(state, RotationsPerSecond.of(ShooterConstants.SHOOTER_FEED_VELOCITY.get()));
|
2026-02-23 16:58:14 -07:00
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
int bitmask2 = (
|
|
|
|
|
(shooterButtonReady ? 1 : 0) +
|
|
|
|
|
(badShooterVelocity ? 2 : 0)
|
|
|
|
|
);
|
2026-02-23 16:58:14 -07:00
|
|
|
|
2026-02-24 10:20:21 -07:00
|
|
|
switch (bitmask2) {
|
|
|
|
|
case 0b000: // No errors but button is not pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.OPREADY_FEED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b001: // No errors and shoot button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_FORWARD_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.OPREADY_FEED);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0b010: // Bad shooter velocity, button is not pressed
|
|
|
|
|
case 0b011: // Bad shooter velocty, button is pressed
|
|
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// case 0b100: // Driver error, button is not pressed
|
|
|
|
|
// case 0b101: // Driver error, button is pressed
|
|
|
|
|
// m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL);
|
|
|
|
|
// io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
|
|
// case 0b110: // Driver error, bad shooter vel, button is not pressed
|
|
|
|
|
// case 0b111: // Driver error, bad shooter vel, button is pressed
|
|
|
|
|
// m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL_BADPHYS);
|
|
|
|
|
// io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
|
|
|
|
// break;
|
2026-02-23 16:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2026-02-24 10:20:21 -07:00
|
|
|
case Idle:
|
2026-02-24 13:50:30 -07:00
|
|
|
|
|
|
|
|
io.setShooterCurrentLimitSpeed(
|
|
|
|
|
state,
|
2026-02-25 17:34:24 -07:00
|
|
|
ShooterConstants.SHOOTER_IDLE_PERCENT_OUTPUT.get()
|
|
|
|
|
// Amps.of(ShooterConstants.SHOOTER_IDLE_MAX_CURRENT.get()),
|
|
|
|
|
// RotationsPerSecond.of(ShooterConstants.INDEXER_REVERSE_OUTPUT.get())
|
2026-02-24 13:50:30 -07:00
|
|
|
);
|
2026-02-16 18:21:53 -07:00
|
|
|
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
|
2026-02-24 10:20:21 -07:00
|
|
|
m_robotLED.setMode(Constants.LEDConstants.DEFAULT_PATTERN);
|
2026-02-10 17:33:39 -08:00
|
|
|
break;
|
2026-02-24 10:20:21 -07:00
|
|
|
}
|
2026-02-10 17:33:39 -08:00
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
2026-02-14 12:49:33 -08:00
|
|
|
}
|