Class CommandBuilder


  • public class CommandBuilder
    extends java.lang.Object
    A builder class to create commands in autonomous. Currently, it wraps a CommandGroup, but the implementation may change in future versions. To build a command, add child commands using sequential(Command) and parallel(Command), then get a command using build().
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandBuilder()
      Construct a new CommandBuilder
      CommandBuilder​(java.lang.String name)
      Construct a new CommandBuilder with a given name
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      edu.wpi.first.wpilibj.command.Command build()
      Build a command.
      CommandBuilder forLoop​(int count, edu.wpi.first.wpilibj.command.Command body)
      Run a command a given number of times.
      CommandBuilder ifThen​(java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command ifTrue)
      Run a command if and only if a condition is met at runtime.
      CommandBuilder ifThenElse​(java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command ifTrue, edu.wpi.first.wpilibj.command.Command ifFalse)
      Run one of two commands, depending on if a condition is true.
      CommandBuilder parallel​(edu.wpi.first.wpilibj.command.Command command)
      Add a command in parallel.
      CommandBuilder parallel​(edu.wpi.first.wpilibj.command.Command command, double timeout)
      Add a command in parallel.
      CommandBuilder parallel​(java.util.function.Consumer<CommandBuilder> command)
      Add a sequence of commands in parallel.
      CommandBuilder release​(edu.wpi.first.wpilibj.command.Subsystem subsystem)
      End any command currently running on the subsystem.
      CommandBuilder releaseAll()
      End all commands currently running in parallel.
      CommandBuilder sequential​(edu.wpi.first.wpilibj.command.Command command)
      Add a command in sequence.
      CommandBuilder sequential​(edu.wpi.first.wpilibj.command.Command command, double timeout)
      Add a command in sequence.
      CommandBuilder waitForChildren()
      Wait for all running parallel commands to finish before executing the next command.
      CommandBuilder waitForCondition​(java.util.function.BooleanSupplier condition)
      Wait until an arbitrary condition is met.
      CommandBuilder waitForCondition​(java.util.function.BooleanSupplier condition, double timeout)
      Wait until an arbitrary condition is met, or until a timeout expires.
      CommandBuilder waitForDuration​(double seconds)
      Wait for a given duration.
      CommandBuilder waitForMatchTime​(double seconds)
      Wait until a given match time.
      CommandBuilder whileLoop​(java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command body)
      Loop repeatedly while checking a condition.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CommandBuilder

        public CommandBuilder()
        Construct a new CommandBuilder
      • CommandBuilder

        public CommandBuilder​(java.lang.String name)
        Construct a new CommandBuilder with a given name
        Parameters:
        name - The name of the command to build
    • Method Detail

      • sequential

        public CommandBuilder sequential​(edu.wpi.first.wpilibj.command.Command command)
        Add a command in sequence. Execution of the next command will not happen until this one finishes.
        Parameters:
        command - The command to add in sequence
        Returns:
        This CommandBuilder object
      • sequential

        public CommandBuilder sequential​(edu.wpi.first.wpilibj.command.Command command,
                                         double timeout)
        Add a command in sequence. Execution of the next command will not happen until this one finishes, or until the timeout expires
        Parameters:
        command - The command to add in sequence
        timeout - The timeout, in seconds. if negative, no timeout is used.
        Returns:
        This CommandBuilder object
      • parallel

        public CommandBuilder parallel​(edu.wpi.first.wpilibj.command.Command command)
        Add a command in parallel. Execution of the next command will begin immediately, while this one continues to execute in parallel. Note that the command created will not end until all parallel commands finish.
        Parameters:
        command - The command to add in parallel
        Returns:
        This CommandBuilder object
      • parallel

        public CommandBuilder parallel​(edu.wpi.first.wpilibj.command.Command command,
                                       double timeout)
        Add a command in parallel. Execution of the next command will begin immediately, while this one continues to execute in parallel. The command will be killed if the timeout expires. Note that the command created will not end until all parallel commands finish.
        Parameters:
        command - The command to add in parallel
        timeout - The timeout, in seconds. if negative, no timeout is used.
        Returns:
        This CommandBuilder object
      • parallel

        public CommandBuilder parallel​(java.util.function.Consumer<CommandBuilder> command)
        Add a sequence of commands in parallel. Execution of the next command will begin immediately, while this one continues to execute in parallel. The command will be killed if the timeout expires.
        Parameters:
        command - A function taking a CommandBuilder specifying the sequence of commands
        Returns:
        {CommandBuilder}
      • waitForDuration

        public CommandBuilder waitForDuration​(double seconds)
        Wait for a given duration.
        Parameters:
        seconds - The time to wait, in seconds
        Returns:
        This CommandBuilder object
      • waitForMatchTime

        public CommandBuilder waitForMatchTime​(double seconds)
        Wait until a given match time.
        Parameters:
        seconds - The match time to wait for, in seconds from the start
        Returns:
        This CommandBuilder object
      • waitForCondition

        public CommandBuilder waitForCondition​(java.util.function.BooleanSupplier condition)
        Wait until an arbitrary condition is met.
        Parameters:
        condition - The condition to wait for.
        Returns:
        This CommandBuilder object
      • waitForCondition

        public CommandBuilder waitForCondition​(java.util.function.BooleanSupplier condition,
                                               double timeout)
        Wait until an arbitrary condition is met, or until a timeout expires.
        Parameters:
        condition - The condition to wait for
        timeout - The timeout in seconds
        Returns:
        This CommandBuilder object
      • ifThen

        public CommandBuilder ifThen​(java.util.function.BooleanSupplier condition,
                                     edu.wpi.first.wpilibj.command.Command ifTrue)
        Run a command if and only if a condition is met at runtime.
        Parameters:
        condition - The condition to check
        ifTrue - The command to run if the condition is true
        Returns:
        This CommandBuilder object
      • ifThenElse

        public CommandBuilder ifThenElse​(java.util.function.BooleanSupplier condition,
                                         edu.wpi.first.wpilibj.command.Command ifTrue,
                                         edu.wpi.first.wpilibj.command.Command ifFalse)
        Run one of two commands, depending on if a condition is true.
        Parameters:
        condition - The condition to check
        ifTrue - The command to run if the condition is true
        ifFalse - The command to run if the condition is false
        Returns:
        This CommandBuilder object
      • whileLoop

        public CommandBuilder whileLoop​(java.util.function.BooleanSupplier condition,
                                        edu.wpi.first.wpilibj.command.Command body)
        Loop repeatedly while checking a condition. Given a command as a body and a condition to check, this command will repeatedly check the condition, and execute the command if it is met. The requirements of the body command are removed when this method is called. This means you cannot re-use the command object you pass to this method. This allows CommandGroups and cancelling to work properly. Because HyperLib is currently in pre-release, these semantics may change based on what we consider "reasonable defaults".
        Parameters:
        condition - The condition to check
        body - The command to run as the loop body
        Returns:
        This CommandBuilder object
        See Also:
        WhileCommand
      • forLoop

        public CommandBuilder forLoop​(int count,
                                      edu.wpi.first.wpilibj.command.Command body)
        Run a command a given number of times. This is effectively a for loop, although the command cannot make use of the value of the counter. This uses whileLoop(BooleanSupplier, Command) under the hood, so the semantics are the same as that.
        Parameters:
        count - The number of times to run the command.
        body - The body of the command to execute.
        Returns:
        This CommandBuilder object
      • release

        public CommandBuilder release​(edu.wpi.first.wpilibj.command.Subsystem subsystem)
        End any command currently running on the subsystem. This is accomplished by running a command which ends instantly, which requires the given subsystem. Afterwards, the default command will run, if there is one.
        Parameters:
        subsystem - The subsystem to release
        Returns:
        This CommandBuilder object
      • releaseAll

        public CommandBuilder releaseAll()
        End all commands currently running in parallel. This is accomplished by starting an command which requires all the subsystems used so far, which ends instantly. This will have the effect of starting the default commands for all these subsystems, if they have any.
        Returns:
        This CommandBuilder object
      • waitForChildren

        public CommandBuilder waitForChildren()
        Wait for all running parallel commands to finish before executing the next command.
        Returns:
        This CommandBuilder object
      • build

        public edu.wpi.first.wpilibj.command.Command build()
        Build a command. Note that for simplicity, calling this method multiple times will return the same Command object. This may change in the future, to match the behavior of other objects following the builder pattern.
        Returns:
        A command created from this builder