|
1 | 1 | package frc.robot.subsystems.intake; |
2 | 2 |
|
| 3 | +import static edu.wpi.first.units.Units.Rotations; |
| 4 | +import static edu.wpi.first.units.Units.RotationsPerSecond; |
| 5 | + |
| 6 | +import com.ctre.phoenix6.controls.PositionTorqueCurrentFOC; |
3 | 7 | import com.ctre.phoenix6.hardware.TalonFX; |
4 | 8 |
|
5 | | -public class IntakeSubsystem { |
6 | | - private final TalonFX m_intakeRollerMotor; |
7 | | - private final TalonFX m_intakeExtensionMotor; |
| 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 | +import edu.wpi.first.wpilibj2.command.WaitUntilCommand; |
| 13 | + |
| 14 | +public class IntakeSubsystem extends SubsystemBase{ |
| 15 | + private final TalonFX rollerMotor; |
| 16 | + private final TalonFX extensionMotor; |
| 17 | + |
| 18 | + private double currentPositionTarget; |
8 | 19 |
|
9 | 20 | public IntakeSubsystem(){ |
10 | | - m_intakeRollerMotor = new TalonFX(IntakeConstants.kIntakeRollerMotor); |
11 | | - m_intakeExtensionMotor = new TalonFX(IntakeConstants.kIntakeExtensionMotor); |
| 21 | + rollerMotor = new TalonFX(IntakeConstants.ROLLER_MOTOR_ID); |
| 22 | + extensionMotor = new TalonFX(IntakeConstants.EXTENSION_MOTOR_ID); |
| 23 | + extensionMotor.getConfigurator().apply(IntakeConstants.createExtensionMotorSlot0Configs()); |
| 24 | + extensionMotor.setPosition(0); |
| 25 | + currentPositionTarget = 0; |
| 26 | + } |
| 27 | + |
| 28 | + public Command spinRollerCommand(){ |
| 29 | + return runOnce(() -> rollerMotor.set(IntakePreferences.rollerIntakeSpeed.getValue())).withName("Spin Intake Roller"); |
12 | 30 | } |
13 | 31 |
|
14 | | - public void spinRoller(){ |
15 | | - m_intakeRollerMotor.set(IntakePreferences.rollerIntakeSpeed.getValue()); |
| 32 | + public Command stopRollerCommand(){ |
| 33 | + return runOnce(() -> rollerMotor.stopMotor()).withName("Stop Intake Roller"); |
16 | 34 | } |
17 | 35 |
|
18 | | - public void stopRoller(){ |
19 | | - m_intakeRollerMotor.stopMotor(); |
| 36 | + private boolean atExtensionSetpoint(){ |
| 37 | + return Math.abs(extensionMotor.getPosition().getValueAsDouble() - currentPositionTarget) < IntakeConstants.ALLOWABLE_EXTENSION_ERROR; |
20 | 38 | } |
21 | 39 |
|
22 | | - public void setIntakePosition(double position){ |
23 | | - m_intakeExtensionMotor.setPosition(position); |
| 40 | + public Command setIntakePositionCommand(double position){ |
| 41 | + currentPositionTarget = position; |
| 42 | + return runOnce(() -> extensionMotor.setControl(new PositionTorqueCurrentFOC(position))).andThen(Commands.waitUntil(() -> atExtensionSetpoint())); |
24 | 43 | } |
25 | 44 |
|
26 | | - public void collect(){ |
27 | | - spinRoller(); |
28 | | - setIntakePosition(IntakePreferences.intakeCollectPosition.getValue()); |
| 45 | + public Command collectCommand(){ |
| 46 | + return setIntakePositionCommand(IntakePreferences.intakeCollectPosition.getValue()).andThen(spinRollerCommand()).withName("Activate Intake Collection"); |
29 | 47 | } |
30 | 48 |
|
31 | | - public void stow(){ |
32 | | - stopRoller(); |
33 | | - setIntakePosition(0); |
| 49 | + public Command stowCommand(){ |
| 50 | + return stopRollerCommand().andThen(setIntakePositionCommand(0.0)).withName("Stow Intake"); |
34 | 51 | } |
35 | 52 |
|
36 | 53 |
|
|
0 commit comments