Skip to content

Commit b5e156d

Browse files
committed
Turned Intake Methods to Command Factories
1 parent 5f81efc commit b5e156d

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
package frc.robot.subsystems.intake;
22

3+
import com.ctre.phoenix6.configs.Slot0Configs;
4+
35
public class IntakeConstants {
46

7+
private IntakeConstants(){}
8+
59
//TODO: Change to actual ports
6-
public static final int kIntakeRollerMotor = 2;
7-
public static final int kIntakeExtensionMotor = 3;
10+
public static final int ROLLER_MOTOR_ID = 2;
11+
public static final int EXTENSION_MOTOR_ID = 3;
12+
13+
//TODO: Tune
14+
public static final double ALLOWABLE_EXTENSION_ERROR = 0.1;
15+
public static final double EXTENSION_kP = 0;
16+
public static final double EXTENSION_kD = 0;
17+
public static Slot0Configs createExtensionMotorSlot0Configs(){
18+
Slot0Configs slot = new Slot0Configs();
19+
slot.kP = EXTENSION_kP;
20+
slot.kD = EXTENSION_kD;
21+
return slot;
22+
}
23+
824

925
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package frc.robot.subsystems.intake;
22

3+
import static edu.wpi.first.units.Units.MetersPerSecond;
4+
import static edu.wpi.first.units.Units.RotationsPerSecond;
5+
36
import frc.robot.preferences.DoublePreference;
47

58
public class IntakePreferences {
6-
public static DoublePreference rollerIntakeSpeed = new DoublePreference("Intake/Roller Intake Speed", 0.5);
9+
10+
private IntakePreferences(){};
11+
12+
public static DoublePreference rollerIntakeSpeed = new DoublePreference("Intake/Roller Intake Speed", 0.1);
713
public static DoublePreference intakeCollectPosition = new DoublePreference("Intake/Stowed Intake Position", 3);
814
}

src/main/java/frc/robot/subsystems/intake/IntakeSubsystem.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
11
package frc.robot.subsystems.intake;
22

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;
37
import com.ctre.phoenix6.hardware.TalonFX;
48

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;
819

920
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");
1230
}
1331

14-
public void spinRoller(){
15-
m_intakeRollerMotor.set(IntakePreferences.rollerIntakeSpeed.getValue());
32+
public Command stopRollerCommand(){
33+
return runOnce(() -> rollerMotor.stopMotor()).withName("Stop Intake Roller");
1634
}
1735

18-
public void stopRoller(){
19-
m_intakeRollerMotor.stopMotor();
36+
private boolean atExtensionSetpoint(){
37+
return Math.abs(extensionMotor.getPosition().getValueAsDouble() - currentPositionTarget) < IntakeConstants.ALLOWABLE_EXTENSION_ERROR;
2038
}
2139

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()));
2443
}
2544

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");
2947
}
3048

31-
public void stow(){
32-
stopRoller();
33-
setIntakePosition(0);
49+
public Command stowCommand(){
50+
return stopRollerCommand().andThen(setIntakePositionCommand(0.0)).withName("Stow Intake");
3451
}
3552

3653

0 commit comments

Comments
 (0)