Files
2026KPopRobotHunters/src/main/java/frc4388/robot/subsystems/shooter/Shooter.java
T

188 lines
6.7 KiB
Java
Raw Normal View History

package frc4388.robot.subsystems.shooter;
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;
import org.littletonrobotics.junction.Logger;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
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;
import frc4388.utility.compute.FieldPositions;
public class Shooter extends SubsystemBase {
2026-02-09 17:18:54 -08:00
public ShooterIO io;
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-01-29 18:07:19 -07:00
public Shooter(
2026-02-10 18:42:47 -08:00
ShooterIO io,
SwerveDrive swerveDrive,
Intake intake,
LED robotLED
) {
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;
}
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
// Shooter is actively shooting
Shooting,
// Shooter is going to fire soon
Ready,
// Not ready to shoot
NotReady,
2026-01-29 18:07:19 -07:00
}
2026-02-10 18:42:47 -08:00
private ShooterMode mode = ShooterMode.NotReady;
2026-02-21 12:54:16 -08:00
private boolean shooterButtonReady = false;
2026-02-10 18:42:47 -08:00
public void setShooterReady() {
2026-02-14 14:03:32 -08:00
if(this.mode == ShooterMode.NotReady) {
this.mode = ShooterMode.Ready;
}
2026-02-10 18:42:47 -08:00
}
public void setShooterNotReady() {
this.mode = ShooterMode.NotReady;
2026-01-29 18:07:19 -07:00
}
2026-02-21 12:54:16 -08:00
public void setShooterShoot() {
shooterButtonReady = true;
}
public void setShooterNOTShoot() {
shooterButtonReady = false;
}
2026-02-10 18:42:47 -08:00
@AutoLogOutput
public ShooterMode getMode() {
return mode;
}
2026-01-29 18:07:19 -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-02-17 16:09:58 -07:00
ChassisSpeeds speed = m_SwerveDrive.chassisSpeeds;
double XYSpeed = Math.sqrt(Math.pow(speed.vxMetersPerSecond,2) + Math.pow(speed.vyMetersPerSecond,2));
double AngSpeed = Math.abs(speed.omegaRadiansPerSecond * (180/Math.PI));
2026-02-17 16:09:58 -07:00
Pose2d robotPose2d = m_SwerveDrive.getPose2d();
2026-02-19 18:55:00 -08:00
//
double distanceToHub = (robotPose2d.getTranslation().minus(FieldPositions.HUB_POSITION).getNorm());
//Center of hub to cameras in inches
2026-02-17 16:09:58 -07:00
Logger.recordOutput("Hub Dist", distanceToHub);
2026-02-14 11:50:09 -08:00
2026-02-17 16:09:58 -07:00
if(this.mode != ShooterMode.NotReady) {
// TODO: get if the robot is within the angle of the hub
2026-02-21 12:54:16 -08:00
boolean driverError =
2026-02-14 14:03:32 -08:00
// XYSpeed <= ShooterConstants.ROBOT_SPEED_TOLERANCE.get() |
// AngSpeed <= ShooterConstants.ROBOT_ANG_SPEED_TOLERANCE.get() |
2026-02-21 12:54:16 -08:00
distanceToHub <= ShooterConstants.ROBOT_MIN_HUB.get() |
distanceToHub >= ShooterConstants.ROBOT_MAX_HUB.get();
2026-02-14 14:03:32 -08: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 14:03:32 -08:00
boolean badShooterVelocity = Math.abs(shooterSpeed - shooterSpeedTarget) > ShooterConstants.SHOOTER_SPEED_TOLERANCE.get();
// boolean intakeBad = m_Intake.getMode() == IntxakeMode.Extended;
2026-02-10 18:42:47 -08:00
2026-02-14 14:03:32 -08:00
int bitmask = (driverError ? 1 : 0) + (badShooterVelocity ? 2 : 0);// + (intakeBad ? 4 : 0);
switch (bitmask) {
case 0b000: // No Errors
m_robotLED.setMode(Constants.LEDConstants.OPREADY);
break;
case 0b001: // No op err, yes driver err
m_robotLED.setMode(Constants.LEDConstants.OPREADY_BADPHYS);
break;
case 0b010: // Bad flywheel, no driver err
m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL);
break;
case 0b011: // Bad flywheel, yes driver err
m_robotLED.setMode(Constants.LEDConstants.BAD_FLYWEEL_BADPHYS);
break;
2026-02-23 15:49:03 -07:00
// case 0b100: // Bad intake, no driver err
// m_robotLED.setMode(Constants.LEDConstants.INTAKE_OUT);
// break;
// case 0b101: // Bad intake, yes driver err
// m_robotLED.setMode(Constants.LEDConstants.INTAKE_OUT_BADPHYS);
// break;
// case 0b110: // Bad intake and shooter (intake is more important), no driver err
// m_robotLED.setMode(Constants.LEDConstants.INTAKE_OUT);
// break;
// case 0b111: // Bad intake and shooter (intake is more important), yes driver err
// m_robotLED.setMode(Constants.LEDConstants.INTAKE_OUT_BADPHYS);
// break;
2026-02-14 14:03:32 -08:00
}
2026-02-14 10:55:51 -08:00
// // We set the shooter mode to ready if there are no errors
2026-02-14 14:03:32 -08:00
mode = (
bitmask == 0 ?
ShooterMode.Shooting :
ShooterMode.Ready
);
2026-02-14 10:55:51 -08:00
2026-02-21 15:55:29 -07:00
} else {
m_robotLED.setMode(Constants.LEDConstants.DEFAULT_PATTERN);
}
2026-02-21 12:54:16 -08:00
2026-02-14 12:49:33 -08:00
2026-02-10 17:33:39 -08:00
switch (mode) {
2026-02-10 18:42:47 -08:00
case Shooting:
2026-02-21 12:54:16 -08:00
if(shooterButtonReady) {
io.setShooterVelocity(state, ShooterConstants.getTargetShooterSpeed(distanceToHub));
io.setIndexerOutput(state, ShooterConstants.INDEXER_FORWARD_OUTPUT.get());
} else {
io.setShooterVelocity(state, ShooterConstants.getTargetShooterSpeed(distanceToHub));
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
}
2026-02-10 17:33:39 -08:00
break;
2026-02-10 18:42:47 -08:00
case Ready:
2026-02-21 12:54:16 -08:00
io.setShooterVelocity(state, ShooterConstants.getTargetShooterSpeed(distanceToHub));
2026-02-16 18:21:53 -07:00
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
2026-02-10 17:33:39 -08:00
break;
2026-02-10 18:42:47 -08:00
case NotReady:
io.setShooterVelocity(state, RotationsPerSecond.of(ShooterConstants.SHOOTER_RESTING_VELOCITY.get()));
2026-02-16 18:21:53 -07:00
io.setIndexerOutput(state, ShooterConstants.INDEXER_REVERSE_OUTPUT.get());
2026-02-10 17:33:39 -08:00
break;
}
}
2026-02-14 12:49:33 -08:00
}