Skip to content

Commit

Permalink
Updated RealSwerveModule and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTinker64 committed Sep 12, 2023
1 parent 79343b9 commit 55b0096
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
57 changes: 25 additions & 32 deletions src/main/java/frc/robot/subsystems/RealSwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,27 @@ public RealSwerveModule(CANSparkMax driveMotor, CANSparkMax turningMotor,
resetEncoders();
}

public double getDrivePosition() {
return m_driveEncoder.getPosition();
public SwerveModuleState getState() {
return new SwerveModuleState(getDriveVelocity(), new Rotation2d(getTurningPosition()));
}

public double getTurningPosition() {
return m_turningEncoder.getPosition();
public SwerveModulePosition getPosition() {
return new SwerveModulePosition(getDrivePosition(), new Rotation2d(getTurningPosition()));
}

public double getDriveVelocity() {
return m_driveEncoder.getVelocity();
public void setDesiredState(SwerveModuleState state) {
m_driveMotor.set(state.speedMetersPerSecond / DriveConstants.kPhysicalMaxSpeedMetersPerSecond);
state = SwerveModuleState.optimize(state, getState().angle);
double desiredTurnSpeed = turningPidController.calculate(getTurningPosition(), state.angle.getRadians());
m_turningMotor.set(desiredTurnSpeed);

desiredState = state;
SmartDashboard.putString("Swerve[" + m_absoluteEncoder.getChannel() + "] state", state.toString());
}

public double getTurningVelocity() {
return m_turningEncoder.getVelocity();
public void resetEncoders() {
m_driveEncoder.setPosition(0);
m_turningEncoder.setPosition(getAbsoluteEncoderRad());
}

public double getAbsoluteEncoderRad() {
Expand All @@ -96,38 +103,24 @@ public double getAbsoluteEncoderRad() {
return angle * (kAbsoluteEncoderReversed ? -1.0 : 1.0); // Look up ternary or conditional operators in java
}

public void resetEncoders() {
m_driveEncoder.setPosition(0);
m_turningEncoder.setPosition(getAbsoluteEncoderRad());
public SwerveModuleState getDesiredState() {
return desiredState;
}

public SwerveModuleState getState() {
return new SwerveModuleState(getDriveVelocity(), new Rotation2d(getTurningPosition()));
public double getDrivePosition() {
return m_driveEncoder.getPosition();
}

public SwerveModulePosition getPosition() {
return new SwerveModulePosition(getDrivePosition(), new Rotation2d(getTurningPosition()));
public double getTurningPosition() {
return m_turningEncoder.getPosition();
}

public void setDesiredState(SwerveModuleState state) {
if (Math.abs(state.speedMetersPerSecond) < 0.001) {
m_driveMotor.set(0);
} else {
m_driveMotor.set(state.speedMetersPerSecond / DriveConstants.kPhysicalMaxSpeedMetersPerSecond);
}
state = SwerveModuleState.optimize(state, getState().angle);
double desiredTurnSpeed = turningPidController.calculate(getTurningPosition(), state.angle.getRadians());
if (Math.abs(desiredTurnSpeed) < 0.001) {
m_turningMotor.set(0);
} else {
m_turningMotor.set(desiredTurnSpeed);
}

SmartDashboard.putString("Swerve[" + m_absoluteEncoder.getChannel() + "] state", state.toString());
public double getDriveVelocity() {
return m_driveEncoder.getVelocity();
}

public SwerveModuleState getDesiredState() {
return desiredState;
public double getTurningVelocity() {
return m_turningEncoder.getVelocity();
}

public void stopMotors() {
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/SwerveModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void setDesiredStateTemplate(double speed, double angle) {
ArgumentCaptor<Double> argumentCaptor = ArgumentCaptor.forClass(Double.class);
verify(mockTurningMotor).set(argumentCaptor.capture());
double turningMotorSpeed = argumentCaptor.getValue();
System.out.println("Angle: " + angle + ", Speed: " + turningMotorSpeed);
if (angle > 0 && angle <= 90 || angle > 180 && angle <= 270) {
if (turningMotorSpeed <= 0.0 || turningMotorSpeed > 1.0) {
throw new AssertionError("Value out of range!");
Expand All @@ -92,7 +93,7 @@ void setDesiredStateTemplate(double speed, double angle) {
throw new AssertionError("Value out of range!");
}
} else {
if (turningMotorSpeed != 0.0) {
if (turningMotorSpeed < -0.001 || turningMotorSpeed > 0.001) {
throw new AssertionError("Value out of range!");
}
}
Expand All @@ -114,10 +115,8 @@ void testStraigtBackwardFullThrottle() {
}

@Test
void testEveryTenthAngleInPlace() {
for (double i = 0.0; i <= 360; i += 10) {
setDesiredStateTemplate(0.0, i);
}
void test180InPlace() {
setDesiredStateTemplate(0.0, 180);
}

@Test
Expand Down

0 comments on commit 55b0096

Please sign in to comment.