import com.ctre.phoenix6.configs.CurrentLimitsConfigs;
import com.ctre.phoenix6.controls.DutyCycleOut;
+import com.ctre.phoenix6.controls.VoltageOut;
import com.ctre.phoenix6.hardware.TalonFX;
import org.littletonrobotics.junction.Logger;
import frc.robot.constants.IdConstants;
public class Spindexer extends SubsystemBase implements SpindexerIO {
- private TalonFX motor = new TalonFX(IdConstants.SPINDEXER_ID, Constants.CANIVORE_SUB);
+ private TalonFX motorOne = new TalonFX(IdConstants.SPINDEXER_ONE_ID, Constants.CANIVORE_SUB);
+ private TalonFX motorTwo = new TalonFX(IdConstants.SPINDEXER_TWO_ID, Constants.CANIVORE_SUB);
private double power = 0.0;
public int ballCount = 0;
limitConfig.StatorCurrentLimitEnable = true;
limitConfig.SupplyCurrentLowerLimit = SpindexerConstants.currentLimit;
limitConfig.SupplyCurrentLowerTime = 1.5;
- motor.getConfigurator().apply(limitConfig);
+ motorOne.getConfigurator().apply(limitConfig);
+ motorTwo.getConfigurator().apply(limitConfig);
if (!Constants.DISABLE_SMART_DASHBOARD) {
SmartDashboard.putData("Spindexer Run Forward", new InstantCommand(() -> maxSpindexer()));
SmartDashboard.putData("Spindexer Run Reverse", new InstantCommand(() -> reverseSpindexer()));
SmartDashboard.putData("Spindexer Stop", new InstantCommand(() -> stopSpindexer()));
}
-
- resetPID.setTolerance(0.05);
}
public enum SpindexerState {
MAX,
REVERSE,
STOPPED,
- RESET,
CUSTOM,
}
updateInputs();
Logger.processInputs("Spindexer", inputs);
- if (resetPos == null) {
- motor.setPosition(0.1 * gearRatio);
- resetPos = (motor.getPosition().getValueAsDouble() / gearRatio) % 1.0;
- resetPID.reset();
- }
-
if (state == SpindexerState.MAX) {
- motor.setControl(new DutyCycleOut(SpindexerConstants.spindexerMaxPower).withEnableFOC(true));
+ setMotorVoltages(SpindexerConstants.spindexerForwardVoltage);
reversing = false;
} else if (state == SpindexerState.REVERSE) {
- motor.setControl(new DutyCycleOut(SpindexerConstants.spindexerReversePower).withEnableFOC(true));
+ setMotorVoltages(SpindexerConstants.spindexerReverseVoltage);
reversing = true;
} else if (state == SpindexerState.STOPPED) {
- motor.setControl(new DutyCycleOut(0.0).withEnableFOC(true));
+ setMotorVoltages(0.0);
reversing = false;
- } else if (state == SpindexerState.RESET && resetPos != null) {
- motor.setControl(new DutyCycleOut(resetPID.calculate((motor.getPosition().getValueAsDouble() / gearRatio) % 1.0, resetPos)).withEnableFOC(true));
} else {
- motor.setControl(new DutyCycleOut(power).withEnableFOC(true));
+ setMotorVoltages(power);
reversing = false;
}
- // scale threshold based on power
- double velocityThreshold = SpindexerConstants.spindexerVelocityWithBall * power;
if (!Constants.DISABLE_SMART_DASHBOARD) {
- SmartDashboard.putNumber("Spindexer Ball Count", ballCount);
-
SmartDashboard.putBoolean("Spindexer Running", state == SpindexerState.MAX || state == SpindexerState.CUSTOM);
SmartDashboard.putBoolean("Spindexer Has Ball", ballCount > 0);
}
- boolean isSpindexerSlow = inputs.spindexerVelocity < velocityThreshold;
- if (wasSpindexerSlow && !isSpindexerSlow && power > 0.1) {
- ballCount++;
- }
- wasSpindexerSlow = isSpindexerSlow;
-
if (!Constants.DISABLE_SMART_DASHBOARD) {
SmartDashboard.putBoolean("Spindexer Reversing", state == SpindexerState.REVERSE);
}
}
+ public void setMotorVoltages(double voltage) {
+ motorOne.setControl(new VoltageOut(voltage).withEnableFOC(true));
+ motorTwo.setControl(new VoltageOut(voltage).withEnableFOC(true));
+ }
+
public void maxSpindexer() {
state = SpindexerState.MAX;
}
state = SpindexerState.CUSTOM;
}
- public void resetSpindexer() {
- state = SpindexerState.RESET;
- }
-
- public void resetResetAngle() {
- resetPos = null;
- }
-
public double getStatorCurrent() {
- return inputs.spindexerCurrent;
+ return inputs.spindexerOneCurrent + inputs.spindexerTwoCurrent;
}
public void setNewCurrentLimit(double newCurrentLimit) {
limitConfig.StatorCurrentLimitEnable = true;
limitConfig.SupplyCurrentLowerLimit = newCurrentLimit;
limitConfig.SupplyCurrentLowerTime = 1.5;
- motor.getConfigurator().apply(limitConfig);
+ motorOne.getConfigurator().apply(limitConfig);
+ motorTwo.getConfigurator().apply(limitConfig);
}
@Override
public void updateInputs() {
- inputs.spindexerVelocity = motor.getVelocity().getValueAsDouble(); //SpindexerConstants.gearRatio;
- inputs.spindexerCurrent = motor.getStatorCurrent().getValueAsDouble();
+ inputs.spindexerOneVelocity = motorOne.getVelocity().getValueAsDouble();
+ inputs.spindexerOneCurrent = motorOne.getStatorCurrent().getValueAsDouble();
+ inputs.spindexerTwoVelocity = motorTwo.getVelocity().getValueAsDouble();
+ inputs.spindexerTwoCurrent = motorTwo.getStatorCurrent().getValueAsDouble();
}
-
- private Double resetPos;
- private PIDController resetPID = new PIDController(4.0, 0.0, 0);
-
- private final double gearRatio = 27.0 / 1.0; //spindexer spins once for every 27 motor spins
-
-
-
}