This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Rewrite arm feedforward classes.
- Loading branch information
1 parent
75731e8
commit 1d483a4
Showing
6 changed files
with
72 additions
and
43 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,75 @@ | ||
package frc.lib; | ||
|
||
import edu.wpi.first.math.controller.ArmFeedforward; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
|
||
public class SingleJointedArmFeedforward { | ||
|
||
private final ArmFeedforward feedforward; | ||
public static class SingleJointedArmFeedforwardConstants { | ||
public double kS; | ||
public double kG; | ||
public double kV; | ||
public double kA; | ||
|
||
public SingleJointedArmFeedforward(double ks, double kg, double kv) { | ||
feedforward = new ArmFeedforward(ks, kg, kv); | ||
public SingleJointedArmFeedforwardConstants() { | ||
this.kS = 0.0; | ||
this.kG = 0.0; | ||
this.kV = 0.0; | ||
this.kA = 0.0; | ||
} | ||
|
||
public SingleJointedArmFeedforwardConstants withKs(double kS) { | ||
this.kS = kS; | ||
return this; | ||
} | ||
|
||
public SingleJointedArmFeedforwardConstants withKg(double kG) { | ||
this.kG = kG; | ||
return this; | ||
} | ||
|
||
/** | ||
* Calculates the gravity compensation constant for an arm given the voltage applied at an | ||
* equilibrium position. | ||
* | ||
* @param angle the equilibrium position of the arm. | ||
* @param volts the voltage applied to the arm to hold it the equilibrium position. | ||
* @return the gravity compensation constant for the arm. | ||
*/ | ||
public SingleJointedArmFeedforwardConstants withKg(Rotation2d angle, double volts) { | ||
double kG = volts / angle.getCos(); | ||
|
||
return this.withKg(kG); | ||
} | ||
|
||
public SingleJointedArmFeedforwardConstants withKv(double kV) { | ||
this.kV = kV; | ||
return this; | ||
} | ||
|
||
public SingleJointedArmFeedforwardConstants withKa(double kA) { | ||
this.kA = kA; | ||
return this; | ||
} | ||
} | ||
|
||
public SingleJointedArmFeedforward(double ks, double kg, double kv, double ka) { | ||
feedforward = new ArmFeedforward(ks, kg, kv, ka); | ||
private final SingleJointedArmFeedforwardConstants constants; | ||
|
||
public SingleJointedArmFeedforward(SingleJointedArmFeedforwardConstants constants) { | ||
this.constants = constants; | ||
} | ||
|
||
public double calculate(Rotation2d position) { | ||
return feedforward.calculate(position.getRadians(), 0); | ||
return calculate(position, new Rotation2d()); | ||
} | ||
|
||
public double calculate(Rotation2d position, double velocity) { | ||
return feedforward.calculate(position.getRadians(), velocity); | ||
public double calculate(Rotation2d position, Rotation2d velocity) { | ||
return calculate(position, new Rotation2d(), new Rotation2d()); | ||
} | ||
|
||
public double calculate(Rotation2d position, double velocity, double acceleration) { | ||
return feedforward.calculate(position.getRadians(), velocity, acceleration); | ||
public double calculate(Rotation2d position, Rotation2d velocity, Rotation2d acceleration) { | ||
return constants.kS * Math.signum(velocity.getRotations()) | ||
+ constants.kG * position.getCos() | ||
+ constants.kV * velocity.getRotations() | ||
+ constants.kA * acceleration.getRotations(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters