Use SwerveModuleState

This commit is contained in:
Astatin3
2024-06-23 11:16:55 -06:00
parent 44dd9f3a4e
commit 3df6fbf7d9
@@ -154,12 +154,14 @@ public class SwerveModule extends SubsystemBase {
* Get state of swerve module * Get state of swerve module
* @return speed in m/s and angle in degrees * @return speed in m/s and angle in degrees
*/ */
// public SwerveModuleState getState() { public SwerveModuleState getState() {
// return new SwerveModuleState( return new SwerveModuleState(
// Units.inchesToMeters(driveMotor.getSelectedSensorVelocity() * SwerveDriveConstants.Conversions.INCHES_PER_TICK) * SwerveDriveConstants.Conversions.TICK_TIME_TO_SECONDS, Units.inchesToMeters(driveMotor.getVelocity().getValue() *
// getAngle() SwerveDriveConstants.Conversions.INCHES_PER_WHEEL_REV *
// ); SwerveDriveConstants.Conversions.WHEEL_REV_PER_MOTOR_REV),
// } getAngle()
);
}
/** /**
* Returns the current position of the SwerveModule * Returns the current position of the SwerveModule
@@ -174,29 +176,18 @@ public class SwerveModule extends SubsystemBase {
* @param desiredState a SwerveModuleState representing the desired new state of the module * @param desiredState a SwerveModuleState representing the desired new state of the module
// */ // */
public void setDesiredState(SwerveModuleState desiredState) { public void setDesiredState(SwerveModuleState desiredState) {
Rotation2d currentRotation = this.getAngle(); SwerveModuleState state = SwerveModuleState.optimize(desiredState, this.getAngle());
// SwerveModuleState state = SwerveModuleState.optimize(desiredState, currentRotation); Rotation2d rot = state.angle;
double speed = Units.metersToFeet(state.speedMetersPerSecond) / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND;
// // calculate the difference between our current rotational position and our new rotational position rotateToAngle(rot.getRotations());
// Rotation2d rotationDelta = state.angle.minus(currentRotation); driveMotor.set(Math.max(Math.min(speed, 1.), -1.));
// // calculate the new absolute position of the SwerveModule based on the difference in rotation
// double deltaTicks = (rotationDelta.getDegrees() / 360.) * SwerveDriveConstants.Conversions.CANCODER_TICKS_PER_ROTATION;
// // convert the CANCoder from its position reading to ticks
// double currentTicks = encoder.getPosition() / encoder.configGetFeedbackCoefficient();
// angleMotor.set(TalonFXControlMode.Position, currentTicks + deltaTicks);
// double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
// driveMotor.set((feetPerSecond / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
} }
// public void reset(double position) { public void reset() {
// encoder.setPositionToAbsolute(); // encoder.setPosition(0);
// } }
// public double getCurrent() { // public double getCurrent() {
// return angleMotor.getSupplyCurrent() + driveMotor.getSupplyCurrent(); // return angleMotor.getSupplyCurrent() + driveMotor.getSupplyCurrent();