Skip to content

Commit f165840

Browse files
committed
feat: create elevator tunable for cruise velocity and max acceleration
Signed-off-by: Dasun Abeykoon <[email protected]>
1 parent 803705f commit f165840

File tree

5 files changed

+81
-28
lines changed

5 files changed

+81
-28
lines changed

src/main/java/org/frc6423/robot/subsystems/superstructure/elevator/Elevator.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2727
import java.util.function.DoubleSupplier;
2828
import org.frc6423.lib.utilities.NtUtils;
29-
import org.frc6423.robot.Constants;
29+
import org.frc6423.robot.Constants.Flags;
3030
import org.frc6423.robot.Robot;
3131

3232
/** Elevator Subsystem */
@@ -95,27 +95,35 @@ public static enum ElevatorExtension {
9595
private boolean isZeroed = false;
9696

9797
private static final String gainTopic = "Tunables/elevator";
98-
private static final DoubleEntry gainKg = NtUtils.createDoubleEntry(gainTopic + "/kG", 0.0);
99-
private static final DoubleEntry gainKs = NtUtils.createDoubleEntry(gainTopic + "/kS", 0.0);
100-
private static final DoubleEntry gainKv = NtUtils.createDoubleEntry(gainTopic + "/kV", 0.0);
101-
private static final DoubleEntry gainKa = NtUtils.createDoubleEntry(gainTopic + "/kA", 0.0);
102-
private static final DoubleEntry gainKp = NtUtils.createDoubleEntry(gainTopic + "/kP", 0.0);
103-
private static final DoubleEntry gainKd = NtUtils.createDoubleEntry(gainTopic + "/kD", 0.0);
98+
private static final DoubleEntry tunableKg = NtUtils.createDoubleEntry(gainTopic + "/kG", 0.0);
99+
private static final DoubleEntry tunableKs = NtUtils.createDoubleEntry(gainTopic + "/kS", 0.0);
100+
private static final DoubleEntry tunableKv = NtUtils.createDoubleEntry(gainTopic + "/kV", 0.0);
101+
private static final DoubleEntry tunableKa = NtUtils.createDoubleEntry(gainTopic + "/kA", 0.0);
102+
private static final DoubleEntry tunableKp = NtUtils.createDoubleEntry(gainTopic + "/kP", 0.0);
103+
private static final DoubleEntry tunableKd = NtUtils.createDoubleEntry(gainTopic + "/kD", 0.0);
104+
private static final DoubleEntry tunableMaxVel =
105+
NtUtils.createDoubleEntry(gainTopic + "/maxVel", 0.0);
106+
private static final DoubleEntry tunableMaxAccel =
107+
NtUtils.createDoubleEntry(gainTopic + "/maxAccel", 0.0);
104108

105109
static {
106-
if (!Constants.Flags.TUNE_MODE) {
107-
gainKg.unpublish();
108-
gainKs.unpublish();
109-
gainKv.unpublish();
110-
gainKa.unpublish();
111-
gainKp.unpublish();
112-
gainKd.unpublish();
113-
gainKg.close();
114-
gainKs.close();
115-
gainKv.close();
116-
gainKa.close();
117-
gainKp.close();
118-
gainKd.close();
110+
if (!Flags.TUNE_MODE) {
111+
tunableKg.unpublish();
112+
tunableKs.unpublish();
113+
tunableKv.unpublish();
114+
tunableKa.unpublish();
115+
tunableKp.unpublish();
116+
tunableKd.unpublish();
117+
tunableMaxVel.unpublish();
118+
tunableMaxAccel.unpublish();
119+
tunableKg.close();
120+
tunableKs.close();
121+
tunableKv.close();
122+
tunableKa.close();
123+
tunableKp.close();
124+
tunableKd.close();
125+
tunableMaxVel.close();
126+
tunableMaxAccel.close();
119127
}
120128
}
121129

@@ -149,9 +157,16 @@ public void periodic() {
149157

150158
filteredCurrent = currentFilter.calculate(hardware.getParentStatorCurrentAmps());
151159

152-
if (Constants.Flags.TUNE_MODE) {
160+
if (Flags.TUNE_MODE) {
153161
hardware.setGains(
154-
gainKg.get(), gainKs.get(), gainKv.get(), gainKa.get(), gainKp.get(), gainKd.get());
162+
tunableKg.get(),
163+
tunableKs.get(),
164+
tunableKv.get(),
165+
tunableKa.get(),
166+
tunableKp.get(),
167+
tunableKd.get(),
168+
tunableMaxVel.get(),
169+
tunableMaxAccel.get());
155170
}
156171
}
157172

@@ -215,6 +230,7 @@ public Command runCurrentHoming() {
215230
if (!interupted) {
216231
hardware.resetEncoders(0.0);
217232
isZeroed = true;
233+
System.out.println("Elevator Zeroed");
218234
}
219235
});
220236
}

src/main/java/org/frc6423/robot/subsystems/superstructure/elevator/ElevatorIO.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,18 @@ public interface ElevatorIO extends AutoCloseable {
7777
* @param kA output per unit of target acceleration (output/(mps/s))
7878
* @param kP output per unit of error in position (meters)
7979
* @param kD output per unit of error in velocity (mps)
80+
* @param maxVel max velocity of motion profile
81+
* @param maxAccel max acceleration of motion profile
8082
*/
81-
public void setGains(double kG, double kS, double kV, double kA, double kP, double kD);
83+
public void setGains(
84+
double kG,
85+
double kS,
86+
double kV,
87+
double kA,
88+
double kP,
89+
double kD,
90+
double maxVel,
91+
double maxAccel);
8292

8393
/**
8494
* Set parent/child motors voltage setpoint

src/main/java/org/frc6423/robot/subsystems/superstructure/elevator/ElevatorIONone.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public double getChildTempCelsius() {
5757
public void resetEncoders(double poseMeters) {}
5858

5959
@Override
60-
public void setGains(double kG, double kS, double kV, double kA, double kP, double kD) {}
60+
public void setGains(
61+
double kG,
62+
double kS,
63+
double kV,
64+
double kA,
65+
double kP,
66+
double kD,
67+
double maxVel,
68+
double maxAccel) {}
6169

6270
@Override
6371
public void setVolts(double volts) {}

src/main/java/org/frc6423/robot/subsystems/superstructure/elevator/ElevatorIOReal.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,23 @@ public void resetEncoders(double poseMeters) {
143143
}
144144

145145
@Override
146-
public void setGains(double kG, double kS, double kV, double kA, double kP, double kD) {
146+
public void setGains(
147+
double kG,
148+
double kS,
149+
double kV,
150+
double kA,
151+
double kP,
152+
double kD,
153+
double maxVel,
154+
double maxAccel) {
147155
conf.Slot0.kG = kG;
148156
conf.Slot0.kS = kS;
149157
conf.Slot0.kV = kV;
150158
conf.Slot0.kA = kA;
151159
conf.Slot0.kP = kP;
152160
conf.Slot0.kD = kD;
161+
conf.MotionMagic.MotionMagicCruiseVelocity = maxVel;
162+
conf.MotionMagic.MotionMagicAcceleration = maxAccel;
153163

154164
// I think the configurator will only apply if the config is different so this should be fine
155165
tryUntilOk(() -> parent.getConfigurator().apply(conf), 10, PARENT_MOTOR_ID);

src/main/java/org/frc6423/robot/subsystems/superstructure/elevator/ElevatorIOSim.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class ElevatorIOSim implements ElevatorIO {
3535

3636
private double setpointPose;
3737

38-
private final ElevatorFeedforward feedforward = new ElevatorFeedforward(0.0, 0.1265, 0.4, 0.0);
38+
private final ElevatorFeedforward feedforward = new ElevatorFeedforward(0.0, 0.127, 2.65, 0.0);
3939
private final ProfiledPIDController feedback =
40-
new ProfiledPIDController(15.0, 0.0, 0.0, new TrapezoidProfile.Constraints(2.25, 10.0));
40+
new ProfiledPIDController(15.0, 0.0, 0.0, new TrapezoidProfile.Constraints(2.25, 4.5));
4141

4242
public ElevatorIOSim() {}
4343

@@ -105,13 +105,22 @@ public double getChildTempCelsius() {
105105
public void resetEncoders(double poseMeters) {}
106106

107107
@Override
108-
public void setGains(double kG, double kS, double kV, double kA, double kP, double kD) {
108+
public void setGains(
109+
double kG,
110+
double kS,
111+
double kV,
112+
double kA,
113+
double kP,
114+
double kD,
115+
double maxVel,
116+
double maxAccel) {
109117
feedforward.setKg(kG);
110118
feedforward.setKs(kS);
111119
feedforward.setKv(kV);
112120
feedforward.setKa(kA);
113121
feedback.setP(kP);
114122
feedback.setD(kD);
123+
feedback.setConstraints(new TrapezoidProfile.Constraints(maxVel, maxAccel));
115124
}
116125

117126
@Override

0 commit comments

Comments
 (0)