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.
feat(arm): Implement simulated elbow motor.
- Loading branch information
1 parent
6eaf20a
commit 2ccd924
Showing
9 changed files
with
125 additions
and
33 deletions.
There are no files selected for viewing
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
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
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,23 +1,84 @@ | ||
package frc.robot.arm; | ||
|
||
import edu.wpi.first.math.controller.ArmFeedforward; | ||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.system.plant.DCMotor; | ||
import edu.wpi.first.math.util.Units; | ||
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim; | ||
import frc.lib.ArmFeedforwardCalculator; | ||
import frc.robot.RobotConstants; | ||
import frc.robot.arm.ArmConstants.ElbowMotorConstants; | ||
|
||
/** Simulated elbow motor. */ | ||
public class ElbowMotorIOSim implements ElbowMotorIO { | ||
|
||
private final DCMotor motor = DCMotor.getNEO(1); | ||
|
||
private final SingleJointedArmSim singleJointedArmSim; | ||
|
||
private final PIDController feedback; | ||
|
||
private final ArmFeedforward feedforward; | ||
|
||
/** Creates a new simulated elbow motor. */ | ||
public ElbowMotorIOSim() { | ||
singleJointedArmSim = | ||
new SingleJointedArmSim( | ||
motor, | ||
ElbowMotorConstants.GEARING, | ||
ElbowMotorConstants.MOI, | ||
ElbowMotorConstants.ELBOW_TO_WRIST_DISTANCE, | ||
ElbowMotorConstants.MINIMUM_ANGLE.getRadians(), | ||
ElbowMotorConstants.MAXIMUM_ANGLE.getRadians(), | ||
true, | ||
0.0); | ||
|
||
feedback = new PIDController(ElbowMotorConstants.KP, 0, 0); | ||
|
||
feedforward = | ||
new ArmFeedforward( | ||
0, | ||
ArmFeedforwardCalculator.calculateArmGravityCompensation( | ||
Rotation2d.fromDegrees(-54.873534), 0.152426), | ||
0); | ||
} | ||
|
||
@Override | ||
public void configure() {} | ||
|
||
@Override | ||
public void update(ElbowMotorIOValues values) {} | ||
public void update(ElbowMotorIOValues values) { | ||
singleJointedArmSim.update(RobotConstants.PERIODIC_DURATION); | ||
|
||
values.positionRotations = Units.radiansToRotations(singleJointedArmSim.getAngleRads()); | ||
values.currentAmps = singleJointedArmSim.getCurrentDrawAmps(); | ||
} | ||
|
||
@Override | ||
public void setPosition(double positionRotations) {} | ||
public void setPosition(double positionRotations) { | ||
singleJointedArmSim.setState(Units.rotationsToRadians(positionRotations), 0); | ||
} | ||
|
||
@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, velocityRotationsPerSecond); | ||
|
||
setVoltage(feedbackVolts + feedforwardVolts); | ||
} | ||
|
||
@Override | ||
public void setVoltage(double volts) {} | ||
public void setVoltage(double volts) { | ||
singleJointedArmSim.setInputVoltage(volts); | ||
} | ||
|
||
@Override | ||
public void stop() {} | ||
public void stop() { | ||
setVoltage(0.0); | ||
} | ||
} |
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