|
| 1 | +package frc.robot.subsystems.climber; |
| 2 | + |
| 3 | +import static edu.wpi.first.units.Units.Amp; |
| 4 | +import static edu.wpi.first.units.Units.Rotations; |
| 5 | + |
| 6 | +import com.ctre.phoenix6.controls.PositionTorqueCurrentFOC; |
| 7 | +import com.ctre.phoenix6.hardware.TalonFX; |
| 8 | +import edu.wpi.first.units.measure.Angle; |
| 9 | +import edu.wpi.first.wpilibj2.command.Command; |
| 10 | +import edu.wpi.first.wpilibj2.command.Commands; |
| 11 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 12 | + |
| 13 | +public class ClimberSubsystem extends SubsystemBase { |
| 14 | + private final TalonFX climbMotor; |
| 15 | + |
| 16 | + private Angle climbTarget; // Rotations |
| 17 | + private PositionTorqueCurrentFOC climbControl; |
| 18 | + |
| 19 | + public ClimberSubsystem() { |
| 20 | + climbMotor = new TalonFX(ClimberConstants.CLIMB_MOTOR_ID); |
| 21 | + |
| 22 | + climbMotor.getConfigurator().apply(ClimberConstants.createClimbMotorSlot0Configs()); |
| 23 | + climbMotor.getConfigurator().apply(ClimberConstants.createClimbSoftwareLimitSwitchConfigs()); |
| 24 | + climbMotor.setPosition(0); |
| 25 | + climbTarget = Rotations.of(0); |
| 26 | + climbControl = new PositionTorqueCurrentFOC(0); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void periodic() { |
| 31 | + climbMotor.setControl(climbControl.withPosition(climbTarget)); |
| 32 | + } |
| 33 | + |
| 34 | + private boolean atClimbSetpoint() { |
| 35 | + return Math.abs(climbMotor.getPosition().getValueAsDouble() - climbTarget.in(Rotations)) |
| 36 | + < ClimberConstants.ALLOWABLE_CLIMB_ERROR.in(Rotations); |
| 37 | + } |
| 38 | + |
| 39 | + public Command climbToPosition(Angle position) { |
| 40 | + return runOnce(() -> climbTarget = position) |
| 41 | + .andThen(Commands.waitUntil(() -> atClimbSetpoint())); |
| 42 | + } |
| 43 | + |
| 44 | + public Command resetClimb() { |
| 45 | + return climbToPosition(Rotations.of(0)); |
| 46 | + } |
| 47 | + |
| 48 | + public Command climbToPreference() { |
| 49 | + return climbToPosition(Rotations.of(ClimberPreferences.pullPosition.getValue())); |
| 50 | + } |
| 51 | + |
| 52 | + public Command homeClimber() { |
| 53 | + return runEnd( |
| 54 | + () -> climbMotor.set(ClimberConstants.SAFE_HOMING_EFFORT.Output), |
| 55 | + () -> climbMotor.setPosition(0)) |
| 56 | + .until( |
| 57 | + () -> { |
| 58 | + return climbMotor.getStatorCurrent().getValueAsDouble() |
| 59 | + > ClimberConstants.SAFE_STATOR_LIMIT.in(Amp); |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
0 commit comments