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;
|
|
|
|
|
import static edu.wpi.first.units.Units.RotationsPerSecond;
|
|
|
|
|
|
2026-03-19 18:32:02 -06:00
|
|
|
import org.littletonrobotics.junction.Logger;
|
2026-01-10 16:52:43 -07:00
|
|
|
import com.ctre.phoenix6.controls.VelocityDutyCycle;
|
|
|
|
|
import com.ctre.phoenix6.hardware.TalonFX;
|
|
|
|
|
|
2026-01-27 18:13:51 -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-03-19 14:21:46 -06:00
|
|
|
import edu.wpi.first.wpilibj.Timer;
|
|
|
|
|
import frc4388.robot.constants.Constants;
|
|
|
|
|
import frc4388.robot.subsystems.intake.Intake;
|
|
|
|
|
import frc4388.robot.subsystems.led.LED;
|
2026-01-10 16:52:43 -07:00
|
|
|
|
2026-01-27 19:15:37 -07:00
|
|
|
public class ShooterReal implements ShooterIO {
|
2026-01-27 18:13:51 -07:00
|
|
|
|
|
|
|
|
TalonFX m_shooter1Motor;
|
|
|
|
|
TalonFX m_shooter2Motor;
|
|
|
|
|
TalonFX m_indexerMotor;
|
2026-02-09 17:18:54 -08:00
|
|
|
|
2026-01-27 18:13:51 -07:00
|
|
|
VelocityDutyCycle shooter1Velocity = new VelocityDutyCycle(0);
|
|
|
|
|
VelocityDutyCycle shooter2Velocity = new VelocityDutyCycle(0);
|
2026-02-17 18:53:26 -08:00
|
|
|
// VelocityDutyCycle m_indexerVelocity = new VelocityDutyCycle(0);
|
2026-01-10 16:52:43 -07:00
|
|
|
|
2026-03-19 14:21:46 -06:00
|
|
|
private final Timer m_stallTimerShooter = new Timer();
|
|
|
|
|
private final Timer m_stallTimerIndexer = new Timer();
|
|
|
|
|
private final Timer m_stallTimerRoller = new Timer();
|
|
|
|
|
private boolean m_shooterStalling = false;
|
|
|
|
|
private boolean m_indexerStalling = false;
|
|
|
|
|
private boolean m_rollerStalling = false;
|
2026-03-19 18:32:02 -06:00
|
|
|
public String motorStall = "";
|
|
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
|
|
|
|
|
public ShooterReal(
|
2026-01-27 18:13:51 -07:00
|
|
|
TalonFX shooter1Motor,
|
|
|
|
|
TalonFX shooter2Motor,
|
|
|
|
|
TalonFX indexerMotor
|
2026-01-10 16:52:43 -07:00
|
|
|
) {
|
2026-02-09 16:03:48 -08:00
|
|
|
m_shooter1Motor = shooter1Motor;
|
|
|
|
|
m_shooter2Motor = shooter2Motor;
|
|
|
|
|
m_indexerMotor = indexerMotor;
|
2026-02-09 17:18:54 -08:00
|
|
|
|
2026-01-27 19:15:37 -07:00
|
|
|
m_shooter1Motor.getConfigurator().apply(ShooterConstants.SHOOTER_PID);
|
|
|
|
|
m_shooter2Motor.getConfigurator().apply(ShooterConstants.SHOOTER_PID);
|
|
|
|
|
|
2026-01-27 18:13:51 -07:00
|
|
|
m_shooter1Motor.getConfigurator().apply(ShooterConstants.SHOOTER1_MOTOR_CONFIG);
|
|
|
|
|
m_shooter2Motor.getConfigurator().apply(ShooterConstants.SHOOTER2_MOTOR_CONFIG);
|
|
|
|
|
m_indexerMotor.getConfigurator().apply(ShooterConstants.INDEXER_MOTOR_CONFIG);
|
2026-02-09 17:18:54 -08:00
|
|
|
|
|
|
|
|
shooter1Velocity.Slot = 0;
|
|
|
|
|
shooter2Velocity.Slot = 0;
|
2026-02-17 18:53:26 -08:00
|
|
|
// m_indexerVelocity.Slot = 0;
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 14:21:46 -06:00
|
|
|
@Override
|
2026-03-19 18:32:02 -06:00
|
|
|
public void motorStalled(ShooterState state, Intake m_Intake, LED m_robotLED) {
|
|
|
|
|
if (Math.abs(state.motor1TargetVelocity.in(RotationsPerSecond)) - Math.abs(state.motor1Velocity.in(RotationsPerSecond)) > 40) {
|
|
|
|
|
if (!m_shooterStalling) {
|
|
|
|
|
m_shooterStalling = true;
|
|
|
|
|
m_stallTimerShooter.restart();
|
|
|
|
|
}
|
|
|
|
|
if (m_stallTimerShooter.hasElapsed(5.0)) {
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.MOTOR_STALLED);
|
|
|
|
|
motorStall = "Shooter Stalled";
|
|
|
|
|
System.out.println(Math.abs(state.motor1TargetVelocity.in(RotationsPerSecond)) - Math.abs(state.motor1Velocity.in(RotationsPerSecond)));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_shooterStalling = false;
|
|
|
|
|
m_stallTimerShooter.reset();
|
|
|
|
|
}
|
2026-03-19 14:21:46 -06:00
|
|
|
|
2026-03-19 18:32:02 -06:00
|
|
|
if (Math.abs(state.indexerTargetOutput) - Math.abs(state.indexerOutput) > 0.3) {
|
|
|
|
|
if (!m_indexerStalling) {
|
|
|
|
|
m_indexerStalling = true;
|
|
|
|
|
m_stallTimerIndexer.restart();
|
|
|
|
|
}
|
|
|
|
|
if (m_stallTimerIndexer.hasElapsed(5.0)) {
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.MOTOR_STALLED);
|
|
|
|
|
motorStall = "Indexer Stalled";
|
|
|
|
|
System.out.println(Math.abs(state.indexerTargetOutput) - Math.abs(state.indexerOutput));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_indexerStalling = false;
|
|
|
|
|
m_stallTimerIndexer.reset();
|
|
|
|
|
}
|
2026-03-19 14:21:46 -06:00
|
|
|
|
2026-03-19 18:32:02 -06:00
|
|
|
if (Math.abs(m_Intake.getRollerTarget()) - Math.abs(m_Intake.getRollerSpeed()) > 0.4) {
|
|
|
|
|
if (!m_rollerStalling) {
|
|
|
|
|
m_rollerStalling = true;
|
|
|
|
|
m_stallTimerRoller.restart();
|
|
|
|
|
}
|
|
|
|
|
if (m_stallTimerRoller.hasElapsed(5.0)) {
|
|
|
|
|
m_robotLED.setMode(Constants.LEDConstants.MOTOR_STALLED);
|
|
|
|
|
motorStall = "Roller Stalled";
|
|
|
|
|
System.out.println(Math.abs(m_Intake.getRollerTarget()) - Math.abs(m_Intake.getRollerSpeed()));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_rollerStalling = false;
|
|
|
|
|
m_stallTimerRoller.reset();
|
|
|
|
|
}
|
|
|
|
|
Logger.recordOutput("Stalled Motor: ", motorStall);
|
2026-03-19 14:21:46 -06:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 18:13:51 -07:00
|
|
|
@Override
|
2026-02-09 17:18:54 -08:00
|
|
|
public void setShooterVelocity(ShooterState state, AngularVelocity target) {
|
2026-01-27 19:15:37 -07:00
|
|
|
state.motor1TargetVelocity = target;
|
2026-01-27 18:13:51 -07:00
|
|
|
state.motor2TargetVelocity = target;
|
2026-01-27 19:15:37 -07:00
|
|
|
|
2026-02-09 18:38:55 -08:00
|
|
|
if(target.baseUnitMagnitude() == 0) {
|
|
|
|
|
m_shooter1Motor.set(0);
|
|
|
|
|
m_shooter2Motor.set(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 14:03:32 -08:00
|
|
|
AngularVelocity motorRps = target.times(ShooterConstants.SHOOTERMOTOR_GEAR_RATIO);
|
2026-02-09 17:18:54 -08:00
|
|
|
|
|
|
|
|
m_shooter1Motor.setControl(shooter1Velocity.withVelocity(motorRps));
|
|
|
|
|
m_shooter2Motor.setControl(shooter2Velocity.withVelocity(motorRps));
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 13:50:30 -07:00
|
|
|
@Override
|
|
|
|
|
public void setShooterCurrentLimitSpeed(
|
|
|
|
|
ShooterState state,
|
2026-02-25 17:34:24 -07:00
|
|
|
double percentOutput
|
|
|
|
|
// Current currentLimit,
|
|
|
|
|
// AngularVelocity targetVelocity
|
2026-02-24 13:50:30 -07:00
|
|
|
) {
|
2026-02-25 17:34:24 -07:00
|
|
|
// state.motor1TargetVelocity = targetVelocity;
|
|
|
|
|
// state.motor2TargetVelocity = targetVelocity;
|
2026-02-24 13:50:30 -07:00
|
|
|
|
2026-02-25 17:34:24 -07:00
|
|
|
// double current = Math.abs(state.motor1Current.in(Amps)) + Math.abs(state.motor2Current.in(Amps));
|
|
|
|
|
// double velocity = (Math.abs(state.motor1Velocity.in(RotationsPerSecond)) + Math.abs(state.motor2Velocity.in(RotationsPerSecond))) / 2;
|
2026-02-24 13:50:30 -07:00
|
|
|
|
2026-02-25 17:34:24 -07:00
|
|
|
// if(
|
|
|
|
|
// Math.abs(currentLimit.in(Amps)) > current &&
|
|
|
|
|
// Math.abs(targetVelocity.in(RotationsPerSecond)) > velocity
|
|
|
|
|
// ) {
|
2026-02-24 13:50:30 -07:00
|
|
|
m_shooter1Motor.set(percentOutput);
|
|
|
|
|
m_shooter2Motor.set(percentOutput);
|
2026-02-25 17:34:24 -07:00
|
|
|
// } else {
|
|
|
|
|
// m_shooter1Motor.set(0);
|
|
|
|
|
// m_shooter2Motor.set(0);
|
|
|
|
|
// }
|
2026-02-24 13:50:30 -07:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
@Override
|
2026-02-16 18:21:53 -07:00
|
|
|
public void setIndexerOutput(ShooterState state, double percentOutput) {
|
|
|
|
|
state.indexerTargetOutput = percentOutput;
|
|
|
|
|
m_indexerMotor.set(percentOutput);
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-09 17:18:54 -08:00
|
|
|
|
2026-01-10 16:52:43 -07:00
|
|
|
@Override
|
|
|
|
|
public void updateInputs(ShooterState state) {
|
|
|
|
|
|
2026-02-14 14:03:32 -08:00
|
|
|
state.motor1Velocity = m_shooter1Motor.getVelocity().getValue().div(ShooterConstants.SHOOTERMOTOR_GEAR_RATIO);
|
|
|
|
|
state.motor2Velocity = m_shooter2Motor.getVelocity().getValue().div(ShooterConstants.SHOOTERMOTOR_GEAR_RATIO);
|
2026-02-16 18:21:53 -07:00
|
|
|
state.indexerOutput = m_indexerMotor.get();
|
|
|
|
|
// state.indexerOutput = m_indexerMotor.getVelocity().getValue().div(ShooterConstants.INDEXER_GEAR_RATIO);
|
2026-01-10 16:52:43 -07:00
|
|
|
|
2026-02-09 17:18:54 -08:00
|
|
|
// state.motorLinearVelocity = InchesPerSecond.of(m_shooter1Motor.getVelocity().getValue().in(RotationsPerSecond) * ShooterConstants.FEEDER_INCHES_PER_ROT);
|
2026-01-27 19:15:37 -07:00
|
|
|
|
2026-01-27 18:13:51 -07:00
|
|
|
state.motor1Current = m_shooter1Motor.getStatorCurrent().getValue();
|
2026-01-27 19:15:37 -07:00
|
|
|
state.motor2Current = m_shooter2Motor.getStatorCurrent().getValue();
|
|
|
|
|
state.indexerCurrent = m_indexerMotor.getStatorCurrent().getValue();
|
2026-02-09 17:18:54 -08:00
|
|
|
|
|
|
|
|
}
|
2026-02-09 18:38:55 -08:00
|
|
|
|
2026-02-09 17:18:54 -08:00
|
|
|
@Override
|
|
|
|
|
public void updateGains() {
|
|
|
|
|
// TEMPORARY PIDs
|
2026-02-09 18:38:55 -08:00
|
|
|
ShooterConstants.SHOOTER_PID.kP = ShooterConstants.shooter_kP.get();
|
|
|
|
|
ShooterConstants.SHOOTER_PID.kI = ShooterConstants.shooter_kI.get();
|
|
|
|
|
ShooterConstants.SHOOTER_PID.kD = ShooterConstants.shooter_kD.get();
|
2026-02-09 17:18:54 -08:00
|
|
|
m_shooter1Motor.getConfigurator().apply(ShooterConstants.SHOOTER_PID);
|
|
|
|
|
m_shooter2Motor.getConfigurator().apply(ShooterConstants.SHOOTER_PID);
|
2026-01-10 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|