Class SparkSignals
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
FieldsModifier and TypeFieldDescriptionstatic booleanSticky indicator that is set whenever a Spark-related error is detected. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidifOk(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 voidifOk(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 doubleifOkOrDefault(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 doubleifOkOrDefault(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 voidtryUntilOk(int maxAttempts, Supplier<com.revrobotics.REVLibError> command) Repeatedly executes a Spark command until it succeeds or the maximum number of attempts is reached.
-
Field Details
-
sparkStickyFault
public static boolean sparkStickyFaultSticky indicator that is set whenever a Spark-related error is detected.Once set, this flag remains
trueuntil 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 valuesupplier- supplier that retrieves the value from the Sparkconsumer- 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 valuessuppliers- array of suppliers retrieving Spark valuesconsumer- 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 valuesupplier- supplier that retrieves the valuedefaultValue- 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 valuessuppliers- array of suppliers retrieving Spark valuestransformer- function that computes a result from the retrieved valuesdefaultValue- value to return if any supplier reports an error- Returns:
- transformed value if all inputs are valid, otherwise
defaultValue
-
tryUntilOk
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 upcommand- supplier that executes the Spark command and returns aREVLibError
-