Ground roller intakes frequently operate without optical beam-break sensors due to dust, debris, or space constraints. By monitoring stator current, the subsystem can detect when a foam ball, plastic note, or rubber disc physically engages the rollers, automatically stopping the motor or transitioning to a game-piece holding state.
Subsystem Implementation
Section titled “Subsystem Implementation”package frc.robot.subsystems;
import com.ctre.phoenix6.configs.TalonFXConfiguration;import com.ctre.phoenix6.controls.VoltageOut;import com.ctre.phoenix6.hardware.TalonFX;import com.ctre.phoenix6.signals.NeutralModeValue;
import edu.wpi.first.units.measure.Current;import edu.wpi.first.units.measure.Voltage;import static edu.wpi.first.units.Units.*;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;import edu.wpi.first.wpilibj2.command.Command;import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class IntakeSubsystem extends SubsystemBase { private final TalonFX m_rollerMotor = new TalonFX(17, "rio"); private final VoltageOut m_voltage = new VoltageOut(0);
// Current spike threshold: free spinning is ~6A; game-piece contact spikes to > 25A private static final double CURRENT_SPIKE_THRESHOLD_AMPS = 25.0; private int m_spikeDebounceCycles = 0;
public IntakeSubsystem() { configureHardware(); setDefaultCommand(stopCommand()); }
private void configureHardware() { TalonFXConfiguration cfg = new TalonFXConfiguration();
cfg.MotorOutput.NeutralMode = NeutralModeValue.Brake;
// Current limits to prevent stalling damage cfg.CurrentLimits.StatorCurrentLimitEnable = true; cfg.CurrentLimits.StatorCurrentLimit = 35.0; // 35A max torque cfg.CurrentLimits.SupplyCurrentLimitEnable = true; cfg.CurrentLimits.SupplyCurrentLimit = 30.0;
m_rollerMotor.getConfigurator().apply(cfg); }
@Override public void periodic() { double statorCurrent = m_rollerMotor.getStatorCurrent().getValueAsDouble(); SmartDashboard.putNumber("Intake/StatorCurrent_A", statorCurrent); SmartDashboard.putBoolean("Intake/GamePieceDetected", hasGamePiece());
// Debounce: must exceed threshold for 3 consecutive cycles (60ms) to avoid false positives on motor spinup if (statorCurrent > CURRENT_SPIKE_THRESHOLD_AMPS) { m_spikeDebounceCycles++; } else { m_spikeDebounceCycles = 0; } }
public boolean hasGamePiece() { return m_spikeDebounceCycles >= 3; }
/** * Runs intake forward at commanded voltage until a game piece is acquired. */ public Command intakeUntilAcquiredCommand(Voltage appliedVoltage) { return run(() -> m_rollerMotor.setControl(m_voltage.withOutput(appliedVoltage.in(Volts)))) .until(this::hasGamePiece) .andThen(stopCommand()) .withName("IntakeUntilAcquired"); }
public Command ejectCommand(Voltage reverseVoltage) { return run(() -> m_rollerMotor.setControl(m_voltage.withOutput(-Math.abs(reverseVoltage.in(Volts))))) .withName("Eject"); }
public Command stopCommand() { return run(() -> m_rollerMotor.setControl(m_voltage.withOutput(0))) .withName("StopIntake"); }}Spike Debouncing is Essential
When any electric motor starts from rest, inrush current briefly spikes for 20 to 40 ms before back-EMF develops. Requiring 3 consecutive robot cycles (60ms) above the threshold prevents false-positive stops during initial motor spinup.