Package frc.lib.util

Class SparkUtil

java.lang.Object
frc.lib.util.SparkUtil

public class SparkUtil extends Object
Utility class for safely interacting with REVLib Spark devices.

Provides methods to check for errors before processing data and to retry configuration commands until successful.

  • Method Details

    • ifOk

      public static boolean ifOk(SparkBase spark, DoubleSupplier supplier, DoubleConsumer consumer)
      Processes a double value from a Spark only if the last operation was successful.
      Parameters:
      spark - The Spark device to check for errors.
      supplier - A function to supply the value.
      consumer - A function to consume the value if the device is error-free.
      Returns:
      True if the operation was successful, false otherwise.
    • ifOk

      public static boolean ifOk(SparkBase spark, DoubleSupplier[] suppliers, Consumer<double[]> consumer)
      Processes multiple double values from a Spark only if all operations were successful.
      Parameters:
      spark - The Spark device to check for errors.
      suppliers - An array of functions to supply the values.
      consumer - A function to consume the values if the device is error-free.
      Returns:
      True if all operations were successful, false otherwise.
    • ifOk

      public static boolean ifOk(SparkBase spark, BooleanSupplier supplier, BooleanConsumer consumer)
      Processes a boolean value from a Spark only if the last operation was successful.
      Parameters:
      spark - The Spark device to check for errors.
      supplier - A function to supply the value.
      consumer - A function to consume the value if the device is error-free.
      Returns:
      True if the operation was successful, false otherwise.
    • ifOk

      public static boolean ifOk(SparkBase spark, BooleanSupplier[] suppliers, Consumer<boolean[]> consumer)
      Processes multiple boolean values from a Spark only if all operations were successful.
      Parameters:
      spark - The Spark device to check for errors.
      suppliers - An array of functions to supply the values.
      consumer - A function to consume the values if the device is error-free.
      Returns:
      True if all operations were successful, false otherwise.
    • tryUntilOk

      public static boolean tryUntilOk(int maxAttempts, Supplier<REVLibError> command)
      Attempts to run a configuration command until no error is produced or max attempts are reached.
      Parameters:
      maxAttempts - The maximum number of times to retry the command.
      command - A function to execute and check for errors.
      Returns:
      True if the command succeeded within the attempt limit, false otherwise.
    • createBaseConfig

      public static SparkMaxConfig createBaseConfig(Current currentLimit, boolean inverted)
      Creates a base SparkMaxConfig with common settings for our robot, including brake mode, current limit, and voltage compensation. This serves as a starting point for configuring Spark Max motors.
      Parameters:
      currentLimit - The current limit for the Spark Max motor.
      inverted - Whether the motor output should be inverted.
      Returns:
      A configured SparkMaxConfig instance.