Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
refactor: Rewrite arm feedforward classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed Mar 5, 2024
1 parent 75731e8 commit 1d483a4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 43 deletions.
19 changes: 0 additions & 19 deletions src/main/java/frc/lib/ArmFeedforwardCalculator.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.ejml.data.MatrixType;
import org.ejml.simple.SimpleMatrix;

public class TwoJointedArmFeedforward {
public class DoubleJointedArmFeedforward {

private final double m1;
private final double m2;
Expand All @@ -22,7 +22,7 @@ public class TwoJointedArmFeedforward {
private final SimpleMatrix Kb_MATRIX = new SimpleMatrix(2, 2, MatrixType.DDRM);
private final SimpleMatrix B_MATRIX = new SimpleMatrix(2, 2, MatrixType.DDRM);

public TwoJointedArmFeedforward(JointConstants shoulder, JointConstants elbow) {
public DoubleJointedArmFeedforward(JointConstants shoulder, JointConstants elbow) {
this.m1 = shoulder.massKg();
this.m2 = elbow.massKg();
this.l1 = shoulder.lengthMeters();
Expand Down
68 changes: 57 additions & 11 deletions src/main/java/frc/lib/SingleJointedArmFeedforward.java
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();
}
}
6 changes: 4 additions & 2 deletions src/main/java/frc/robot/arm/ShoulderMotorIOSparkMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import frc.lib.AccelerationCalculator;
import frc.lib.Configurator;
import frc.lib.SingleJointedArmFeedforward;
import frc.lib.SingleJointedArmFeedforward.SingleJointedArmFeedforwardConstants;
import frc.robot.arm.ArmConstants.ShoulderMotorConstants;

/** Shoulder motor using a Spark Max. */
Expand All @@ -30,7 +31,7 @@ public ShoulderMotorIOSparkMax() {

feedback = new PIDController(ShoulderMotorConstants.KP, 0, 0);

feedforward = new SingleJointedArmFeedforward(0, 0, 0);
feedforward = new SingleJointedArmFeedforward(new SingleJointedArmFeedforwardConstants());

accelerationCalculator = new AccelerationCalculator();
}
Expand Down Expand Up @@ -71,7 +72,8 @@ public void setSetpoint(double positionRotations, double velocityRotationsPerSec

double feedforwardVolts =
feedforward.calculate(
Rotation2d.fromRotations(measuredPositionRotations), velocityRotationsPerSecond);
Rotation2d.fromRotations(measuredPositionRotations),
Rotation2d.fromRotations(velocityRotationsPerSecond));

setVoltage(feedbackVolts + feedforwardVolts);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/frc/robot/arm/WristMotorIOSparkMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import frc.lib.AccelerationCalculator;
import frc.lib.Configurator;
import frc.lib.SingleJointedArmFeedforward;
import frc.lib.SingleJointedArmFeedforward.SingleJointedArmFeedforwardConstants;
import frc.robot.arm.ArmConstants.WristMotorConstants;

public class WristMotorIOSparkMax implements WristMotorIO {
Expand All @@ -28,7 +29,7 @@ public WristMotorIOSparkMax() {

feedback = new PIDController(WristMotorConstants.KP, 0, 0);

feedforward = new SingleJointedArmFeedforward(0, 0, 0);
feedforward = new SingleJointedArmFeedforward(new SingleJointedArmFeedforwardConstants());

accelerationCalculator = new AccelerationCalculator();
}
Expand Down Expand Up @@ -71,7 +72,8 @@ public void setSetpoint(double positionRotations, double velocityRotationsPerSec

double feedforwardVolts =
feedforward.calculate(
Rotation2d.fromRotations(measuredPositionRotations), velocityRotationsPerSecond);
Rotation2d.fromRotations(measuredPositionRotations),
Rotation2d.fromRotations(velocityRotationsPerSecond));

sparkMax.setVoltage(feedbackVolts + feedforwardVolts);
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/frc/robot/intake/PivotMotorIOTalonSRX.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import frc.lib.ArmFeedforwardCalculator;
import frc.lib.Configurator;
import frc.lib.SingleJointedArmFeedforward;
import frc.lib.SingleJointedArmFeedforward.SingleJointedArmFeedforwardConstants;
import frc.robot.intake.IntakeConstants.PivotMotorConstants;

/** Pivot motor using a Talon SRX. */
Expand All @@ -25,11 +25,10 @@ public PivotMotorIOTalonSRX() {

feedback = new PIDController(PivotMotorConstants.KP, 0, 0);

// TODO
double kg =
ArmFeedforwardCalculator.calculateArmGravityCompensation(Rotation2d.fromDegrees(26), 1.8);
SingleJointedArmFeedforwardConstants constants =
new SingleJointedArmFeedforwardConstants().withKg(Rotation2d.fromDegrees((26)), 1.8);

feedforward = new SingleJointedArmFeedforward(0, kg, 0);
feedforward = new SingleJointedArmFeedforward(constants);
}

@Override
Expand Down Expand Up @@ -82,8 +81,7 @@ public void setSetpoint(double positionRotations, double velocityRotationsPerSec
double feedbackVolts = feedback.calculate(measuredPositionRotations, positionRotations);

double feedforwardVolts =
feedforward.calculate(
Rotation2d.fromRotations(measuredPositionRotations), velocityRotationsPerSecond);
feedforward.calculate(Rotation2d.fromRotations(measuredPositionRotations));

double percent = (feedbackVolts + feedforwardVolts) / talonSRX.getBusVoltage();

Expand Down

0 comments on commit 1d483a4

Please sign in to comment.