Package lib.util

Class CmdUtil

java.lang.Object
lib.util.CmdUtil

public final class CmdUtil extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    cancelConflicting(edu.wpi.first.wpilibj2.command.Subsystem... requirements)
    Cancels all commands that conflict with the given subsystems
    static edu.wpi.first.wpilibj2.command.Command
    delayed(double delaySeconds, edu.wpi.first.wpilibj2.command.Command command)
     
    static edu.wpi.first.wpilibj2.command.Command
    nonNull(edu.wpi.first.wpilibj2.command.Command cmd)
    Replace nulls with a no-op command instead
    static void
    schedule(edu.wpi.first.wpilibj2.command.Command... commands)
     
    static boolean
    tryCancelConflicting(edu.wpi.first.wpilibj2.command.Subsystem... requirements)
    Attempts to cancel all commands that conflict with the given subsystems.
    static <T> edu.wpi.first.wpilibj2.command.Command
    withState(Supplier<T> getState, Function<Supplier<T>,edu.wpi.first.wpilibj2.command.Command> getCommand, edu.wpi.first.wpilibj2.command.Subsystem... addedRequirements)
    Useful for creating complicated functional commands with multiple lambdas that reference the same state, and where the state needs to be updated every iteration.

    Methods inherited from class java.lang.Object

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

    • schedule

      public static void schedule(edu.wpi.first.wpilibj2.command.Command... commands)
    • cancelConflicting

      public static void cancelConflicting(edu.wpi.first.wpilibj2.command.Subsystem... requirements)
      Cancels all commands that conflict with the given subsystems
    • tryCancelConflicting

      public static boolean tryCancelConflicting(edu.wpi.first.wpilibj2.command.Subsystem... requirements)
      Attempts to cancel all commands that conflict with the given subsystems. Commands will only be canceled if none of the conflicting commands have their interruption behavior set to kCancelIncoming.

      For example:

      
       if (CmdUtil.tryCancelConflicting(Component.elevator)) {
           Component.elevator.setVoltage(2); // not command-based
       }
       
      Returns:
      true if conflicting commands were canceled
    • nonNull

      public static edu.wpi.first.wpilibj2.command.Command nonNull(edu.wpi.first.wpilibj2.command.Command cmd)
      Replace nulls with a no-op command instead
      Parameters:
      cmd - Command or null
      Returns:
      Non-null command
    • delayed

      public static edu.wpi.first.wpilibj2.command.Command delayed(double delaySeconds, edu.wpi.first.wpilibj2.command.Command command)
    • withState

      public static <T> edu.wpi.first.wpilibj2.command.Command withState(Supplier<T> getState, Function<Supplier<T>,edu.wpi.first.wpilibj2.command.Command> getCommand, edu.wpi.first.wpilibj2.command.Subsystem... addedRequirements)
      Useful for creating complicated functional commands with multiple lambdas that reference the same state, and where the state needs to be updated every iteration.

      For example:

      
       return CmdUtil.<Pose2d>withState(
           this::getNextPose,
           state -> new ParallelCommandGroup(
               c_moveTo(() -> state.get().getTranslation())),
               c_rotateTo(() -> state.get().getRotation())
           )
       )
       
      Type Parameters:
      T - The type of the state
      Parameters:
      getState - Supplier of the current state
      getCommand - Lambda that takes in a state supplier for usage in the functional command(s), and returns a command. Use state.get() to retrieve the current state within this lambda. Calling state.get() does not recompute the state - it returns the state that was already computed this iteration
      addedRequirements - Additional requirements to add
      Returns:
      The constructed command