Skip to content

Commit 97c947e

Browse files
authored
Merge pull request #11 from IgniteRobotics/feature/climber
Climber setup
2 parents 93e84dc + a9ea5b9 commit 97c947e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.configs.Slot0Configs;
7+
import com.ctre.phoenix6.configs.SoftwareLimitSwitchConfigs;
8+
import com.ctre.phoenix6.controls.DutyCycleOut;
9+
import edu.wpi.first.units.measure.Angle;
10+
import edu.wpi.first.units.measure.Current;
11+
12+
final class ClimberConstants {
13+
14+
private ClimberConstants() {}
15+
16+
// TODO: Change to actual port
17+
protected static final int CLIMB_MOTOR_ID = 10;
18+
19+
// TODO: Tune motor
20+
protected static final Angle ALLOWABLE_CLIMB_ERROR = Rotations.of(0.1);
21+
protected static final Angle CLIMB_FORWARD_LIMIT = Rotations.of(100);
22+
protected static final Angle CLIMB_REVERSE_LIMIT = Rotations.of(0);
23+
protected static final double CLIMB_KS = 0;
24+
protected static final double CLIMB_KP = 0;
25+
protected static final double CLIMB_KD = 0;
26+
27+
protected static Slot0Configs createClimbMotorSlot0Configs() {
28+
Slot0Configs slot = new Slot0Configs();
29+
slot.kS = CLIMB_KS;
30+
slot.kP = CLIMB_KP;
31+
slot.kD = CLIMB_KD;
32+
return slot;
33+
}
34+
35+
protected static SoftwareLimitSwitchConfigs createClimbSoftwareLimitSwitchConfigs() {
36+
SoftwareLimitSwitchConfigs configs = new SoftwareLimitSwitchConfigs();
37+
configs.ForwardSoftLimitEnable = false;
38+
configs.ReverseSoftLimitEnable = false;
39+
configs.ForwardSoftLimitThreshold = CLIMB_FORWARD_LIMIT.in(Rotations);
40+
configs.ReverseSoftLimitThreshold = CLIMB_REVERSE_LIMIT.in(Rotations);
41+
return configs;
42+
}
43+
44+
protected static final DutyCycleOut SAFE_HOMING_EFFORT = new DutyCycleOut(-0.2);
45+
protected static final Current SAFE_STATOR_LIMIT = Amp.of(0.8);
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package frc.robot.subsystems.climber;
2+
3+
import frc.robot.preferences.DoublePreference;
4+
5+
final class ClimberPreferences {
6+
7+
protected static DoublePreference pullPosition =
8+
new DoublePreference("Climber/Pull Down (Rotations)", 3); // in rotations
9+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)