From 20a8c990aa40d85c11ebaa29a852b0b18d44db3e Mon Sep 17 00:00:00 2001 From: Alex Sirota Date: Sun, 24 Mar 2024 11:46:53 -0700 Subject: [PATCH] Add alerts to PWMDutyCycleEncoder and SparkMax --- swervelib/encoders/PWMDutyCycleEncoderSwerve.java | 10 ++++++++++ swervelib/motors/SparkMaxSwerve.java | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/swervelib/encoders/PWMDutyCycleEncoderSwerve.java b/swervelib/encoders/PWMDutyCycleEncoderSwerve.java index 24e15d6..9b52c5d 100644 --- a/swervelib/encoders/PWMDutyCycleEncoderSwerve.java +++ b/swervelib/encoders/PWMDutyCycleEncoderSwerve.java @@ -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. @@ -39,6 +43,10 @@ public class PWMDutyCycleEncoderSwerve extends SwerveAbsoluteEncoder "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); } @@ -61,6 +69,7 @@ public class PWMDutyCycleEncoderSwerve extends SwerveAbsoluteEncoder @Override public double getAbsolutePosition() { + disconnected.set(!encoder.isConnected()); return (isInverted ? -1.0 : 1.0) * encoder.getAbsolutePosition() * 360; } @@ -83,6 +92,7 @@ public class PWMDutyCycleEncoderSwerve extends SwerveAbsoluteEncoder @Override public double getVelocity() { + disconnected.set(!encoder.isConnected()); inaccurateVelocities.set(true); return encoder.get(); } diff --git a/swervelib/motors/SparkMaxSwerve.java b/swervelib/motors/SparkMaxSwerve.java index aa19a0c..ced8a6e 100644 --- a/swervelib/motors/SparkMaxSwerve.java +++ b/swervelib/motors/SparkMaxSwerve.java @@ -52,6 +52,11 @@ public class SparkMaxSwerve extends SwerveMotor */ private Supplier position; + /** + * An {@link Alert} for if there is an error configuring the motor. + */ + private Alert failureConfiguringAlert; + /** * Initialize the swerve motor. * @@ -74,6 +79,9 @@ public class SparkMaxSwerve extends SwerveMotor 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); } /** @@ -101,7 +109,7 @@ public class SparkMaxSwerve extends SwerveMotor return; } } - DriverStation.reportWarning("Failure configuring motor " + motor.getDeviceId(), true); + failureConfiguringAlert.set(true); } /**