Skip to content

Commit

Permalink
Merge pull request #58 from ajs256/add-more-alerts
Browse files Browse the repository at this point in the history
Add alerts to PWMDutyCycleEncoder and SparkMax
  • Loading branch information
thenetworkgrinch authored Mar 24, 2024
2 parents 9a2eb4e + 20a8c99 commit 8c3470f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions swervelib/encoders/PWMDutyCycleEncoderSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class PWMDutyCycleEncoderSwerve extends SwerveAbsoluteEncoder
* An {@link Alert} for if the encoder cannot report accurate velocities.
*/
private Alert inaccurateVelocities;
/**
* An {@link Alert} for if the encoder is disconnected.
*/
private Alert disconnected;

/**
* Constructor for the PWM duty cycle encoder.
Expand All @@ -39,6 +43,10 @@ public PWMDutyCycleEncoderSwerve(int pin)
"Encoders",
"The PWM Duty Cycle encoder may not report accurate velocities!",
Alert.AlertType.WARNING_TRACE);
inaccurateVelocities = new Alert(
"Encoders",
"The swerve encoder on port " + pin + "is disconnected!",
Alert.AlertType.ERROR_TRACE);

}

Expand All @@ -61,6 +69,7 @@ public void configure(boolean inverted)
@Override
public double getAbsolutePosition()
{
disconnected.set(!encoder.isConnected());
return (isInverted ? -1.0 : 1.0) * encoder.getAbsolutePosition() * 360;
}

Expand All @@ -83,6 +92,7 @@ public Object getAbsoluteEncoder()
@Override
public double getVelocity()
{
disconnected.set(!encoder.isConnected());
inaccurateVelocities.set(true);
return encoder.get();
}
Expand Down
10 changes: 9 additions & 1 deletion swervelib/motors/SparkMaxSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class SparkMaxSwerve extends SwerveMotor
*/
private Supplier<Double> position;

/**
* An {@link Alert} for if there is an error configuring the motor.
*/
private Alert failureConfiguringAlert;

/**
* Initialize the swerve motor.
*
Expand All @@ -74,6 +79,9 @@ public SparkMaxSwerve(CANSparkMax motor, boolean isDriveMotor)
position = encoder::getPosition;
// Spin off configurations in a different thread.
// configureSparkMax(() -> motor.setCANTimeout(0)); // Commented out because it prevents feedback.
failureConfiguringAlert = new Alert("Motors",
"Failure configuring motor " + motor.getDeviceId(),
Alert.AlertType.WARNING_TRACE);
}

/**
Expand Down Expand Up @@ -101,7 +109,7 @@ private void configureSparkMax(Supplier<REVLibError> config)
return;
}
}
DriverStation.reportWarning("Failure configuring motor " + motor.getDeviceId(), true);
failureConfiguringAlert.set(true);
}

/**
Expand Down

0 comments on commit 8c3470f

Please sign in to comment.