Skip to content

Commit 595acba

Browse files
constants, sparkmax
1 parent 641d411 commit 595acba

4 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/main/java/frc/robot/subsystems/elevator/Elevator.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111

12-
public class Elevator extends SubsystemBase{
12+
public class Elevator extends SubsystemBase {
1313

1414

1515
private final ElevatorIO io;
@@ -18,7 +18,28 @@ public class Elevator extends SubsystemBase{
1818

1919

2020
public Elevator(ElevatorIO io) {
21+
2122
this.io = io;
23+
24+
SysId =
25+
new SysIdRoutine(
26+
new SysIdRoutine.Config(
27+
Volts.per(Second).of(ElevatorConstants.SYSID_RAMP_RATE),
28+
Volts.of(ElevatorConstants.SYSID_STEP_VOLTAGE),
29+
Seconds.of(ElevatorConstants.SYSID_TIME),
30+
(state) -> Logger.recordOutput("Elevator/SysIdTestState", state.toString())),
31+
new SysIdRoutine.Mechanism(
32+
v -> io.setVoltage(v.in(Volts)),
33+
(sysidLog) -> {
34+
sysidLog
35+
.motor("Elevator")
36+
.voltage(m_appliedVoltage.mut_replace(inputs.appliedVoltage, Volts))
37+
.linearPosition(m_position.mut_replace(inputs.positionMeters, Meters))
38+
.linearVelocity(
39+
m_velocity.mut_replace(inputs.velocityMetersPerSec, MetersPerSecond));
40+
},
41+
this));
42+
}
2243
}
2344

2445

@@ -27,6 +48,15 @@ public void periodic() {
2748
io.updateInputs(ElevatorInputs);
2849
}
2950

51+
// Sets the PID setpoint
52+
public void setPID(double setpoint) {
53+
if (setpoint > ElevatorConstants.ELEVATOR_MAX_HEIGHT)
54+
setpoint = ElevatorConstants.ELEVATOR_MAX_HEIGHT;
55+
else if (setpoint < ElevatorConstants.ELEVATOR_MIN_HEIGHT)
56+
setpoint = ElevatorConstants.ELEVATOR_MIN_HEIGHT;
57+
io.setSetpoint(setpoint);
58+
}
59+
3060
public Command runVoltageCommand(Supplier<Voltage> voltage) {
3161
return run(() -> io.setVoltage(voltage.get())).withName("Voltage");
3262
}
@@ -38,4 +68,3 @@ public Command runRPMCommand(Supplier<AngularVelocity> rpm) {
3868

3969

4070

41-
}

src/main/java/frc/robot/subsystems/elevator/ElevatorConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public class ElevatorConstants {
1111
public static final int LIMIT_SWITCH_CHANNEL = 0;
1212
public static double POSITION_CONVERSION_FACTOR = 0.0;
1313
public static final boolean MOTOR_DEFAULT_IDLE_MODE = true;
14+
// MAX AND MIN HEIGHTS
15+
public static final double ELEVATOR_MAX_HEIGHT = 1.0; // meters
16+
public static final double ELEVATOR_MIN_HEIGHT = 0.005;
1417

18+
// MAX velocity and acceleration
19+
public static final double MAX_ACCELERATION = 4.0; // m/s^2
20+
public static final double MAX_VELOCITY = 2.0;
1521

1622
}

src/main/java/frc/robot/subsystems/elevator/ElevatorIO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ default double getVelocity(){
4141
return 0.0;
4242
}
4343

44+
default double getAppliedVoltage(){
45+
return 0.0;
46+
}
47+
4448
default void setRPM(double rpm){}
4549

4650
// Checking if elevator goes above limit height

src/main/java/frc/robot/subsystems/elevator/ElevatorIOSparkMax

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public class ElevatorIOSparkMax {
3939
public ElevatorIOSparkMax() {
4040
leftMotor = new SparkMax(ElevatorConstants.LEFT_MOTOR_ID, MotorType.kBrushless);
4141
rightMotor = new SparkMax(ElevatorConstants.RIGHT_MOTOR_ID, MotorType.kBrushless);
42-
42+
// encoders have the same height, since elevator moves up and down
4343
leftEncoder = leftMotor.getEncoder();
4444
leftEncoder.setPosition(0);
4545

46+
47+
// Configs were borrowed from previous code :)
4648
SparkMaxConfig leftConfig = new SparkMaxConfig();
4749
leftConfig
4850
.smartCurrentLimit(Constants.NEO_CURRENT_LIMIT)
@@ -57,12 +59,6 @@ public class ElevatorIOSparkMax {
5759
SparkMaxConfig rightConfig = new SparkMaxConfig();
5860
rightConfig.apply(leftConfig);
5961
rightConfig.follow(leftMotor, true);
60-
61-
// reset safe kResetSafeParameters switches the motor to default paramaters, then adds the
62-
// changes from the config object
63-
// persist paramaters saves these changes to the motor memory so it doesn't get cooked during
64-
// brownouts
65-
// only use persist in the BEGINNING, not later
6662
leftMotor.configure(leftConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
6763
rightMotor.configure(
6864
rightConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
@@ -74,11 +70,64 @@ public class ElevatorIOSparkMax {
7470
}
7571

7672
private void updateMotorConfig(SparkMaxConfig config) {
77-
// DO NOT RESET paramaters becasue we only want to change some paramaters, not all
78-
// DO NOT PERSIST because this is a temporary change that we don't want to save to memory
7973
leftMotor.configure(config, ResetMode.kNoResetSafeParameters, PersistMode.kNoPersistParameters);
8074
rightMotor.configure(
8175
config, ResetMode.kNoResetSafeParameters, PersistMode.kNoPersistParameters);
8276
}
77+
78+
@Override
79+
public void updateInputs(ElevatorIOInputs inputs) {
80+
inputs.setpointMeters = getSetpoint();
81+
inputs.positionMeters = getPosition();
82+
inputs.velocityMetersPerSec = getVelocity();
83+
inputs.appliedVoltage = getAppliedVoltage();
84+
inputs.isLimitSwitchPressed = isLimitSwitchPressed();
85+
86+
}
87+
88+
@Override
89+
public double getSetpoint() {
90+
return pidController.getSetpoint().position;
91+
}
92+
93+
@Override
94+
public double getPosition {
95+
return leftEncoder.getPosition();
96+
}
97+
98+
@Override
99+
public double getVelocity {
100+
return leftEncoder.getVelocity();
101+
}
102+
103+
@Override
104+
public double getAppliedVoltage {
105+
return leftMotor.getAppliedOutput() * leftMotor.getBusVoltage();
106+
}
107+
108+
@Override
109+
public boolean isLimitSwitchPressed() {
110+
return !limitSwitch.get();
111+
}
112+
113+
114+
@Override
115+
public void setSetpoint(double setpoint) {
116+
pidController.setGoal(MathUtil.clamp(setpoint, ElevatorConstants.MIN_HEIGHT, ElevatorConstants.MAX_HEIGHT));
117+
}
118+
119+
@Override
120+
public void goToSetpoint() {
121+
double pidOutput = pid.Controller.calculate(getPosition());
122+
123+
double ffOutput = feedforward.calculate(pidController.getSetpoint().velocity, pidController.getSetpoint().acceleration);
124+
125+
double acceleration = pidController.getSetpoint().acceleration;
126+
127+
setVoltage(math.util.clamp(pidOutput, -ElevatorConstants.MAX_VOLTAGE, ElevatorConstants.MAX_VOLTAGE));
128+
}
129+
130+
131+
83132
}
84133
}

0 commit comments

Comments
 (0)