Skip to content

Commit 353d8fb

Browse files
committed
feat: create arm tunables for motion magic control
Signed-off-by: Dasun Abeykoon <[email protected]>
1 parent f165840 commit 353d8fb

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

src/main/java/org/frc6423/robot/subsystems/superstructure/arm/Arm.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import edu.wpi.first.math.MathUtil;
1818
import edu.wpi.first.math.filter.LinearFilter;
1919
import edu.wpi.first.math.geometry.Rotation2d;
20+
import edu.wpi.first.networktables.DoubleEntry;
2021
import edu.wpi.first.units.measure.Angle;
2122
import edu.wpi.first.units.measure.AngularAcceleration;
2223
import edu.wpi.first.units.measure.AngularVelocity;
@@ -25,6 +26,8 @@
2526
import edu.wpi.first.wpilibj2.command.Command;
2627
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2728
import java.util.function.DoubleSupplier;
29+
import org.frc6423.lib.utilities.NtUtils;
30+
import org.frc6423.robot.Constants.Flags;
2831
import org.frc6423.robot.Robot;
2932

3033
/** Arm Subsystem */
@@ -91,6 +94,39 @@ private ArmAngle(Angle angle) {
9194

9295
private boolean isZeroed = false;
9396

97+
private static final String gainTopic = "Tunables/elevator";
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);
108+
109+
static {
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();
127+
}
128+
}
129+
94130
/**
95131
* @return fake {@link Arm} subsystem
96132
*/
@@ -120,6 +156,18 @@ public void periodic() {
120156
hardware.periodic();
121157

122158
filteredCurrent = currentFilter.calculate(hardware.getStatorCurrentAmps());
159+
160+
if (Flags.TUNE_MODE) {
161+
hardware.setGains(
162+
tunableKg.get(),
163+
tunableKs.get(),
164+
tunableKv.get(),
165+
tunableKa.get(),
166+
tunableKp.get(),
167+
tunableKd.get(),
168+
tunableMaxVel.get(),
169+
tunableMaxAccel.get());
170+
}
123171
}
124172

125173
/**
@@ -161,6 +209,7 @@ public Command runCurrentHoming() {
161209
if (!interupted) {
162210
hardware.resetEncoder(0.0);
163211
isZeroed = true;
212+
System.out.println("Arm Homed!");
164213
}
165214
});
166215
}

src/main/java/org/frc6423/robot/subsystems/superstructure/arm/ArmIO.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ public interface ArmIO extends AutoCloseable {
3838
*/
3939
public void resetEncoder(double poseRads);
4040

41+
/**
42+
* Set arm gains for onboard feedforward and feedback control
43+
*
44+
* @param kG output to overcome gravity
45+
* @param kS output to overcome static friction
46+
* @param kV output per unit of target velocity (output/mps)
47+
* @param kA output per unit of target acceleration (output/(mps/s))
48+
* @param kP output per unit of error in position (meters)
49+
* @param kD output per unit of error in velocity (mps)
50+
* @param maxVel max velocity of motion profile
51+
* @param maxAccel max acceleration of motion profile
52+
*/
53+
public void setGains(
54+
double kG,
55+
double kS,
56+
double kV,
57+
double kA,
58+
double kP,
59+
double kD,
60+
double maxVel,
61+
double maxAccel);
62+
4163
/**
4264
* Set pivot motor voltage setpoint
4365
*

src/main/java/org/frc6423/robot/subsystems/superstructure/arm/ArmIONone.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public double getStatorCurrentAmps() {
3131
@Override
3232
public void resetEncoder(double poseRads) {}
3333

34+
@Override
35+
public void setGains(
36+
double kG,
37+
double kS,
38+
double kV,
39+
double kA,
40+
double kP,
41+
double kD,
42+
double maxVel,
43+
double maxAccel) {}
44+
3445
@Override
3546
public void setVolts(double volts) {}
3647

src/main/java/org/frc6423/robot/subsystems/superstructure/arm/ArmIOReal.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static edu.wpi.first.units.Units.Radians;
1010
import static edu.wpi.first.units.Units.RadiansPerSecond;
1111
import static edu.wpi.first.units.Units.RadiansPerSecondPerSecond;
12+
import static org.frc6423.lib.utilities.CtreUtils.tryUntilOk;
1213
import static org.frc6423.robot.subsystems.superstructure.arm.Arm.CANBUS;
1314
import static org.frc6423.robot.subsystems.superstructure.arm.Arm.GEAR_REDUCTION;
1415
import static org.frc6423.robot.subsystems.superstructure.arm.Arm.MAX_ACCELERATION;
@@ -68,7 +69,7 @@ public ArmIOReal() {
6869
conf.MotionMagic.MotionMagicCruiseVelocity = MAX_VELOCITY.in(RadiansPerSecond);
6970
conf.MotionMagic.MotionMagicAcceleration = MAX_ACCELERATION.in(RadiansPerSecondPerSecond);
7071

71-
motor.getConfigurator().apply(conf);
72+
tryUntilOk(() -> motor.getConfigurator().apply(conf), 10, MOTOR_ID);
7273

7374
poseSig = motor.getPosition();
7475
currentSig = motor.getStatorCurrent();
@@ -99,6 +100,30 @@ public void resetEncoder(double poseRads) {
99100
motor.setPosition(poseRads);
100101
}
101102

103+
@Override
104+
public void setGains(
105+
double kG,
106+
double kS,
107+
double kV,
108+
double kA,
109+
double kP,
110+
double kD,
111+
double maxVel,
112+
double maxAccel) {
113+
114+
conf.Slot0.kG = kG;
115+
conf.Slot0.kS = kS;
116+
conf.Slot0.kV = kV;
117+
conf.Slot0.kA = kA;
118+
conf.Slot0.kP = kP;
119+
conf.Slot0.kD = kD;
120+
conf.MotionMagic.MotionMagicCruiseVelocity = maxVel;
121+
conf.MotionMagic.MotionMagicAcceleration = maxAccel;
122+
123+
// I think the configurator will only apply if the config is different so this should be fine
124+
tryUntilOk(() -> motor.getConfigurator().apply(conf), 10, MOTOR_ID);
125+
}
126+
102127
@Override
103128
public void setVolts(double volts) {
104129
motor.setControl(voltReq.withOutput(volts).withEnableFOC(true));

src/main/java/org/frc6423/robot/subsystems/superstructure/arm/ArmIOSim.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public double getStatorCurrentAmps() {
7272
@Override
7373
public void resetEncoder(double poseRads) {}
7474

75+
@Override
76+
public void setGains(
77+
double kG,
78+
double kS,
79+
double kV,
80+
double kA,
81+
double kP,
82+
double kD,
83+
double maxVel,
84+
double maxAccel) {}
85+
7586
@Override
7687
public void setVolts(double volts) {
7788
pivotAppliedVolts = MathUtil.clamp(volts, -12.0, 12.0);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public Command runCurrentHoming() {
230230
if (!interupted) {
231231
hardware.resetEncoders(0.0);
232232
isZeroed = true;
233-
System.out.println("Elevator Zeroed");
233+
System.out.println("Elevator Homed!");
234234
}
235235
});
236236
}

0 commit comments

Comments
 (0)