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

Commit

Permalink
refactor(arm): Use velocity in setting setpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed Feb 10, 2024
1 parent 9fc300f commit d2b4875
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/main/java/frc/robot/arm/Arm.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public void setState(ArmState state) {
* @return the state of the arm.
*/
public ArmState getState() {
shoulderMotor.update(shoulderMotorValues);
elbowMotor.update(elbowMotorValues);

return new ArmState(
Rotation2d.fromRotations(shoulderMotorValues.positionRotations),
Rotation2d.fromRotations(elbowMotorValues.positionRotations),
Expand All @@ -113,7 +116,7 @@ private void setGoal(ArmState goal) {
this.goal = goal;
this.setpoint = this.setpoint.nextSetpoint(goal);

shoulderMotor.setSetpoint(this.setpoint.shoulder().position);
shoulderMotor.setSetpoint(this.setpoint.shoulder().position, this.setpoint.shoulder().velocity);
elbowMotor.setSetpoint(this.setpoint.elbow().position);
// wristMotor.runSetpoint(state.wrist().getRotations());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/arm/ArmMechanism.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Mechanism2d getMechanism() {
public void setState(ArmState state) {
Rotation2d shoulderRotation = Rotation2d.fromRotations(state.shoulder().position);
Rotation2d elbowRotation = Rotation2d.fromRotations(state.elbow().position);
//Rotation2d wristRotation = Rotation2d.fromRotations(state.wrist().position);
// Rotation2d wristRotation = Rotation2d.fromRotations(state.wrist().position);

shoulder.setAngle(shoulderRotation);
elbow.setAngle(elbowRotation.minus(shoulderRotation));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/frc/robot/arm/ShoulderMotorIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static class ShoulderMotorIOValues {
/**
* Runs the shoulder motor's setpoint.
*
* @param positionRotations the shoulder motor's setpoint.
* @param positionRotations the shoulder motor's position setpoint.
* @param velocityRotationsPerSecond the shoulder motor's velocity setpoint.
*/
public void setSetpoint(double positionRotations);
public void setSetpoint(double positionRotations, double velocityRotationsPerSecond);
}
5 changes: 3 additions & 2 deletions src/main/java/frc/robot/arm/ShoulderMotorIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ public void setPosition(double positionRotations) {
}

@Override
public void setSetpoint(double positionRotations) {
public void setSetpoint(double positionRotations, double velocityRotationsPerSecond) {
double measuredPositionRotations = Units.radiansToRotations(singleJointedArmSim.getAngleRads());

double feedbackVolts = feedback.calculate(measuredPositionRotations, positionRotations);

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

singleJointedArmSim.setInputVoltage(feedbackVolts + feedforwardVolts);
}
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/frc/robot/arm/ShoulderMotorIOSparkMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
import frc.lib.ArmFeedforwardCalculator;
import frc.lib.Configurator;
import frc.robot.arm.ArmConstants.ShoulderMotorConstants;
Expand All @@ -18,7 +17,7 @@ public class ShoulderMotorIOSparkMax implements ShoulderMotorIO {
private final CANSparkMax sparkMax;

/** Feedback controller for shoulder position. */
private final ProfiledPIDController feedback;
private final PIDController feedback;

/** Feedforward controller for shoulder position. */
private final ArmFeedforward feedforward;
Expand All @@ -27,13 +26,7 @@ public class ShoulderMotorIOSparkMax implements ShoulderMotorIO {
public ShoulderMotorIOSparkMax() {
sparkMax = new CANSparkMax(ShoulderMotorConstants.CAN.id(), MotorType.kBrushless);

feedback =
new ProfiledPIDController(
ShoulderMotorConstants.KP,
0,
0,
new Constraints(
ShoulderMotorConstants.MAXIMUM_SPEED, ShoulderMotorConstants.MAXIMUM_ACCELERATION));
feedback = new PIDController(ShoulderMotorConstants.KP, 0, 0);

feedforward =
new ArmFeedforward(
Expand Down Expand Up @@ -62,13 +55,13 @@ public void setPosition(double positionRotations) {
}

@Override
public void setSetpoint(double positionRotations) {
public void setSetpoint(double positionRotations, double velocityRotationsPerSecond) {
double measuredPositionRotations = getPositionRotations();

double feedbackVolts = feedback.calculate(measuredPositionRotations, positionRotations);

double feedforwardVolts =
feedforward.calculate(measuredPositionRotations, feedback.getSetpoint().velocity);
feedforward.calculate(measuredPositionRotations, velocityRotationsPerSecond);

sparkMax.setVoltage(feedbackVolts + feedforwardVolts);
}
Expand Down

0 comments on commit d2b4875

Please sign in to comment.