Class SparkSignals

java.lang.Object
frc.robot.util.SparkSignals

@NullMarked public class SparkSignals extends Object
Utility methods for safely interacting with REV Spark motor controllers in the presence of transient or persistent communication errors.

REV API calls report errors via REVLibError rather than exceptions. These helpers centralize common error-handling patterns and ensure that invalid or stale values are not inadvertently consumed by higher-level logic.

All utility methods update a shared sparkStickyFault flag whenever a non-kOk error is detected. This flag may be monitored elsewhere in the robot code to trigger diagnostics, alerts, or safe fallback behavior.

This class is purely static and is not intended to be instantiated.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static boolean
    Sticky indicator that is set whenever a Spark-related error is detected.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    ifOk(com.revrobotics.spark.SparkBase spark, DoubleSupplier[] suppliers, Consumer<double[]> consumer)
    Retrieves multiple values from a Spark controller and processes them only if all values are valid.
    static void
    ifOk(com.revrobotics.spark.SparkBase spark, DoubleSupplier supplier, DoubleConsumer consumer)
    Retrieves and processes a value from a Spark controller only if the underlying call completed successfully.
    static double
    ifOkOrDefault(com.revrobotics.spark.SparkBase spark, DoubleSupplier[] suppliers, Function<double[],Double> transformer, double defaultValue)
    Retrieves multiple values from a Spark controller and applies a transformation only if all values are valid.
    static double
    ifOkOrDefault(com.revrobotics.spark.SparkBase spark, DoubleSupplier supplier, double defaultValue)
    Retrieves a value from a Spark controller, returning a default if the value is invalid.
    static void
    tryUntilOk(int maxAttempts, Supplier<com.revrobotics.REVLibError> command)
    Repeatedly executes a Spark command until it succeeds or the maximum number of attempts is reached.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • sparkStickyFault

      public static boolean sparkStickyFault
      Sticky indicator that is set whenever a Spark-related error is detected.

      Once set, this flag remains true until explicitly cleared by user code. It can be used to surface non-fatal communication issues that may otherwise go unnoticed during operation.

  • Method Details

    • ifOk

      public static void ifOk(com.revrobotics.spark.SparkBase spark, DoubleSupplier supplier, DoubleConsumer consumer)
      Retrieves and processes a value from a Spark controller only if the underlying call completed successfully.

      If the Spark reports an error, the value is discarded and the consumer is not invoked.

      Parameters:
      spark - the Spark device associated with the value
      supplier - supplier that retrieves the value from the Spark
      consumer - consumer that processes the value if it is valid
    • ifOk

      public static void ifOk(com.revrobotics.spark.SparkBase spark, DoubleSupplier[] suppliers, Consumer<double[]> consumer)
      Retrieves multiple values from a Spark controller and processes them only if all values are valid.

      If any supplier produces an error, processing is aborted and the consumer is not invoked.

      Parameters:
      spark - the Spark device associated with the values
      suppliers - array of suppliers retrieving Spark values
      consumer - consumer that processes the values if all are valid
    • ifOkOrDefault

      public static double ifOkOrDefault(com.revrobotics.spark.SparkBase spark, DoubleSupplier supplier, double defaultValue)
      Retrieves a value from a Spark controller, returning a default if the value is invalid.

      If an error occurs, the default value is returned and the sticky fault flag is set.

      Parameters:
      spark - the Spark device associated with the value
      supplier - supplier that retrieves the value
      defaultValue - value to return if an error is detected
      Returns:
      the retrieved value if valid, otherwise defaultValue
    • ifOkOrDefault

      public static double ifOkOrDefault(com.revrobotics.spark.SparkBase spark, DoubleSupplier[] suppliers, Function<double[],Double> transformer, double defaultValue)
      Retrieves multiple values from a Spark controller and applies a transformation only if all values are valid.

      If any value is invalid, the transformation is skipped and a default value is returned.

      Parameters:
      spark - the Spark device associated with the values
      suppliers - array of suppliers retrieving Spark values
      transformer - function that computes a result from the retrieved values
      defaultValue - value to return if any supplier reports an error
      Returns:
      transformed value if all inputs are valid, otherwise defaultValue
    • tryUntilOk

      public static void tryUntilOk(int maxAttempts, Supplier<com.revrobotics.REVLibError> command)
      Repeatedly executes a Spark command until it succeeds or the maximum number of attempts is reached.

      This is commonly used for configuration calls that may fail during initialization due to transient CAN issues.

      Parameters:
      maxAttempts - maximum number of attempts before giving up
      command - supplier that executes the Spark command and returns a REVLibError