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(swerve): Implement simulated swerve.
- Loading branch information
1 parent
e5b8ecd
commit 0d39dab
Showing
17 changed files
with
557 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package frc.robot.swerve; | ||
|
||
/** Azimuth encoder hardware interface. */ | ||
public interface AzimuthEncoderIO { | ||
|
||
/** Values for the azimuth encoder hardware interface. */ | ||
public static class AzimuthEncoderIOValues { | ||
/** Position of the azimuth encoder in rotations. */ | ||
public double angleRotations = 0.0; | ||
} | ||
|
||
/** | ||
* Updates the azimuth encoder's values. | ||
* | ||
* @param values | ||
*/ | ||
public void update(AzimuthEncoderIOValues values); | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package frc.robot.swerve; | ||
|
||
/** Simulated azimuth encoder. */ | ||
public class AzimuthEncoderIOSim implements AzimuthEncoderIO { | ||
|
||
@Override | ||
public void update(AzimuthEncoderIOValues values) { | ||
values.angleRotations = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package frc.robot.swerve; | ||
|
||
/** Drive motor hardware interface. */ | ||
public interface DriveMotorIO { | ||
|
||
/** Values for the drive motor hardware interface. */ | ||
public static class DriveMotorIOValues { | ||
/** Position of the drive motor in meters. */ | ||
public double positionMeters = 0.0; | ||
|
||
/** Velocity of the drive motor in meters per second. */ | ||
public double velocityMetersPerSecond = 0.0; | ||
} | ||
|
||
/** | ||
* Updates the drive motor's values. | ||
* | ||
* @param values | ||
*/ | ||
public void update(DriveMotorIOValues values); | ||
|
||
/** | ||
* Sets the drive motor's position. | ||
* | ||
* @param positionMeters the drive motor's position. | ||
*/ | ||
public void setPosition(double positionMeters); | ||
|
||
/** | ||
* Sets the drive motor's setpoint. | ||
* | ||
* @param velocityMetersPerSecond the drive motor's setpoint. | ||
*/ | ||
public void setSetpoint(double velocityMetersPerSecond); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package frc.robot.swerve; | ||
|
||
import frc.robot.RobotConstants; | ||
|
||
/** Simulated drive motor. */ | ||
public class DriveMotorIOSim implements DriveMotorIO { | ||
|
||
/** Represents the position of the drive motor. */ | ||
private double positionMeters; | ||
|
||
/** Represents the velocity of the drive motor. */ | ||
private double velocityMetersPerSecond; | ||
|
||
@Override | ||
public void update(DriveMotorIOValues values) { | ||
this.positionMeters += velocityMetersPerSecond * RobotConstants.TICK_PERIOD; | ||
|
||
values.positionMeters = positionMeters; | ||
values.velocityMetersPerSecond = velocityMetersPerSecond; | ||
} | ||
|
||
@Override | ||
public void setPosition(double positionMeters) { | ||
this.positionMeters = positionMeters; | ||
} | ||
|
||
@Override | ||
public void setSetpoint(double velocityMetersPerSecond) { | ||
this.velocityMetersPerSecond = velocityMetersPerSecond; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package frc.robot.swerve; | ||
|
||
/** Steer motor hardware interface. */ | ||
public interface SteerMotorIO { | ||
|
||
/** Values for the steer motor hardware interface. */ | ||
public static class SteerMotorIOValues { | ||
/** Position of the steer motor in rotations. */ | ||
public double angleRotations = 0.0; | ||
/** Velocity of the steer motor in rotations per second. */ | ||
public double omegaRotationsPerSecond = 0.0; | ||
} | ||
|
||
/** | ||
* Updates the steer motor's values. | ||
* | ||
* @param values | ||
*/ | ||
public void update(SteerMotorIOValues values); | ||
|
||
/** | ||
* Sets the steer motor's position. | ||
* | ||
* @param angleRotations the steer motor's position. | ||
*/ | ||
public void setPosition(double angleRotations); | ||
|
||
/** | ||
* Sets the steer motor's setpoint. | ||
* | ||
* @param angleRotations the steer motor's setpoint. | ||
*/ | ||
public void setSetpoint(double angleRotations); | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package frc.robot.swerve; | ||
|
||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.system.plant.DCMotor; | ||
import edu.wpi.first.wpilibj.simulation.FlywheelSim; | ||
import frc.robot.RobotConstants; | ||
import frc.robot.swerve.SwerveConstants.MK4iConstants; | ||
|
||
/** Simulated steer motor. */ | ||
public class SteerMotorIOSim implements SteerMotorIO { | ||
|
||
/** Represents the motor used to steer the wheel. */ | ||
private final DCMotor motorSim = DCMotor.getFalcon500(1); // TODO | ||
|
||
/** Represents the wheel steered by the motor. */ | ||
private final FlywheelSim wheelSim = new FlywheelSim(motorSim, MK4iConstants.STEER_GEARING, MK4iConstants.STEER_MOI); | ||
|
||
/** Represents the angle of the steer motor. */ | ||
private double angleRotations; | ||
|
||
/** Feedback controller for the angle. */ | ||
private final PIDController angleController = new PIDController(1.0, 0, 0); | ||
|
||
public SteerMotorIOSim() { | ||
angleRotations = 0.0; | ||
|
||
angleController.enableContinuousInput(0, 1); | ||
} | ||
|
||
@Override | ||
public void update(SteerMotorIOValues values) { | ||
double voltage = angleController.calculate(angleRotations); | ||
|
||
wheelSim.setInputVoltage(voltage); | ||
wheelSim.update(RobotConstants.TICK_PERIOD); | ||
|
||
double omegaRotationsPerSecond = wheelSim.getAngularVelocityRPM() / 60.0; | ||
angleRotations += omegaRotationsPerSecond; | ||
|
||
values.angleRotations = angleRotations; | ||
values.omegaRotationsPerSecond = omegaRotationsPerSecond; | ||
} | ||
|
||
@Override | ||
public void setPosition(double angleRotations) { | ||
this.angleRotations = 0.0; | ||
} | ||
|
||
@Override | ||
public void setSetpoint(double angleRotations) { | ||
angleController.setSetpoint(angleRotations); | ||
} | ||
|
||
|
||
|
||
} |
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
Oops, something went wrong.