Class Command

java.lang.Object
edu.wpi.first.wpilibj.command.Command
Direct Known Subclasses:
CommandGroup, CurveStraightDrive, DistanceDrive, PIDDistanceDrive, TimeDrive

public abstract class Command
extends java.lang.Object
The Command class is at the very core of the entire command framework. Every command can be started with a call to start(). Once a command is started it will call initialize(), and then will repeatedly call execute() until the isFinished() returns true. Once it does, end() will be called.

However, if at any point while it is running cancel() is called, then the command will be stopped and interrupted() will be called.

If a command uses a Subsystem, then it should specify that it does so by calling the requires(...) method in its constructor. Note that a Command may have multiple requirements, and requires(...) should be called for each one.

If a command is running and a new command with shared requirements is started, then one of two things will happen. If the active command is interruptible, then cancel() will be called and the command will be removed to make way for the new one. If the active command is not interruptible, the other one will not even be started, and the active one will continue functioning.

See Also:
Subsystem, CommandGroup, IllegalUseOfCommandException
  • Constructor Summary

    Constructors 
    Constructor Description
    Command()
    Creates a new command.
    Command​(double timeout)
    Creates a new command with the given timeout and a default name.
    Command​(double timeout, Subsystem subsystem)
    Creates a new command with the given timeout and a default name.
    Command​(Subsystem subsystem)
    Creates a new command with the given timeout and a default name.
    Command​(java.lang.String name)
    Creates a new command with the given name.
    Command​(java.lang.String name, double timeout)
    Creates a new command with the given name and timeout.
    Command​(java.lang.String name, double timeout, Subsystem subsystem)
    Creates a new command with the given name and timeout.
    Command​(java.lang.String name, Subsystem subsystem)
    Creates a new command with the given name.
  • Method Summary

    Modifier and Type Method Description
    void cancel()
    This will cancel the current command.
    boolean doesRequire​(Subsystem system)
    Checks if the command requires the given Subsystem.
    boolean isCanceled()
    Returns whether or not this has been canceled.
    boolean isCompleted()
    Whether or not this command has completed running.
    boolean isInterruptible()
    Returns whether or not this command can be interrupted.
    boolean isRunning()
    Returns whether or not the command is running.
    void setRunWhenDisabled​(boolean run)
    Sets whether or not this Command should run when the robot is disabled.
    void start()
    Starts up the command.
    double timeSinceInitialized()
    Returns the time since this command was initialized (in seconds).
    boolean willRunWhenDisabled()
    Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Command

      public Command()
      Creates a new command. The name of this command will be set to its class name.
    • Command

      public Command​(java.lang.String name)
      Creates a new command with the given name.
      Parameters:
      name - the name for this command
      Throws:
      java.lang.IllegalArgumentException - if name is null
    • Command

      public Command​(double timeout)
      Creates a new command with the given timeout and a default name. The default name is the name of the class.
      Parameters:
      timeout - the time (in seconds) before this command "times out"
      Throws:
      java.lang.IllegalArgumentException - if given a negative timeout
      See Also:
      isTimedOut()
    • Command

      public Command​(Subsystem subsystem)
      Creates a new command with the given timeout and a default name. The default name is the name of the class.
      Parameters:
      subsystem - the subsystem that this command requires
      Throws:
      java.lang.IllegalArgumentException - if given a negative timeout
      See Also:
      isTimedOut()
    • Command

      public Command​(java.lang.String name, Subsystem subsystem)
      Creates a new command with the given name.
      Parameters:
      name - the name for this command
      subsystem - the subsystem that this command requires
      Throws:
      java.lang.IllegalArgumentException - if name is null
    • Command

      public Command​(double timeout, Subsystem subsystem)
      Creates a new command with the given timeout and a default name. The default name is the name of the class.
      Parameters:
      timeout - the time (in seconds) before this command "times out"
      subsystem - the subsystem that this command requires
      Throws:
      java.lang.IllegalArgumentException - if given a negative timeout
      See Also:
      isTimedOut()
    • Command

      public Command​(java.lang.String name, double timeout)
      Creates a new command with the given name and timeout.
      Parameters:
      name - the name of the command
      timeout - the time (in seconds) before this command "times out"
      Throws:
      java.lang.IllegalArgumentException - if given a negative timeout or name was null.
      See Also:
      isTimedOut()
    • Command

      public Command​(java.lang.String name, double timeout, Subsystem subsystem)
      Creates a new command with the given name and timeout.
      Parameters:
      name - the name of the command
      timeout - the time (in seconds) before this command "times out"
      subsystem - the subsystem that this command requires
      Throws:
      java.lang.IllegalArgumentException - if given a negative timeout
      java.lang.IllegalArgumentException - if given a negative timeout or name was null.
      See Also:
      isTimedOut()
  • Method Details

    • timeSinceInitialized

      public final double timeSinceInitialized()
      Returns the time since this command was initialized (in seconds). This function will work even if there is no specified timeout.
      Returns:
      the time since this command was initialized (in seconds).
    • start

      public void start() throws java.lang.Exception
      Starts up the command. Gets the command ready to start.

      Note that the command will eventually start, however it will not necessarily do so immediately, and may in fact be canceled before initialize is even called.

      Throws:
      java.lang.Exception
      IllegalUseOfCommandException - if the command is a part of a CommandGroup
    • isRunning

      public boolean isRunning()
      Returns whether or not the command is running. This may return true even if the command has just been canceled, as it may not have yet called interrupted().
      Returns:
      whether or not the command is running
    • cancel

      public void cancel() throws java.lang.Exception
      This will cancel the current command.

      This will cancel the current command eventually. It can be called multiple times. And it can be called when the command is not running. If the command is running though, then the command will be marked as canceled and eventually removed.

      A command can not be canceled if it is a part of a command group, you must cancel the command group instead.

      Throws:
      java.lang.Exception
      IllegalUseOfCommandException - if this command is a part of a command group
    • isCanceled

      public boolean isCanceled()
      Returns whether or not this has been canceled.
      Returns:
      whether or not this has been canceled
    • isCompleted

      public boolean isCompleted()
      Whether or not this command has completed running.
      Returns:
      whether or not this command has completed running.
    • isInterruptible

      public boolean isInterruptible()
      Returns whether or not this command can be interrupted.
      Returns:
      whether or not this command can be interrupted
    • doesRequire

      public boolean doesRequire​(Subsystem system)
      Checks if the command requires the given Subsystem.
      Parameters:
      system - the system
      Returns:
      whether or not the subsystem is required, or false if given null
    • setRunWhenDisabled

      public void setRunWhenDisabled​(boolean run)
      Sets whether or not this Command should run when the robot is disabled.

      By default a command will not run when the robot is disabled, and will in fact be canceled.

      Parameters:
      run - whether or not this command should run when the robot is disabled
    • willRunWhenDisabled

      public boolean willRunWhenDisabled()
      Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself.
      Returns:
      True if this command will run when the robot is disabled.