|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.wpi.first.wpilibj.command.Command
public abstract class Command
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.
Subsystem
,
CommandGroup
,
IllegalUseOfCommandException
Constructor Summary | |
---|---|
Command()
Creates a new command. |
|
Command(double timeout)
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. |
Method Summary | |
---|---|
void |
cancel()
This will cancel the current command. |
boolean |
doesRequire(Subsystem system)
Checks if the command requires the given Subsystem . |
protected abstract void |
end()
Called when the command ended peacefully. |
protected abstract void |
execute()
The execute method is called repeatedly until this Command either finishes or is canceled. |
CommandGroup |
getGroup()
Returns the CommandGroup that this command is a part of. |
java.lang.String |
getName()
Returns the name of this command. |
java.lang.String |
getSmartDashboardType()
|
ITable |
getTable()
|
protected abstract void |
initialize()
The initialize method is called the first time this Command is run after being started. |
void |
initTable(ITable table)
Initializes a table for this sendable object. |
protected abstract void |
interrupted()
Called when the command ends because somebody called cancel()
or another command shared the same requirements as this one, and booted
it out. |
boolean |
isCanceled()
Returns whether or not this has been canceled. |
protected abstract boolean |
isFinished()
Returns whether this command is finished. |
boolean |
isInterruptible()
Returns whether or not this command can be interrupted. |
boolean |
isRunning()
Returns whether or not the command is running. |
protected boolean |
isTimedOut()
Returns whether or not the timeSinceInitialized()
method returns a number which is greater than or equal to the timeout for the command. |
protected void |
requires(Subsystem subsystem)
This method specifies that the given Subsystem is used by this command. |
protected void |
setInterruptible(boolean interruptible)
Sets whether or not this command can be interrupted. |
void |
setRunWhenDisabled(boolean run)
Sets whether or not this Command should run when the robot is disabled. |
protected void |
setTimeout(double seconds)
Sets the timeout of this command. |
void |
start()
Starts up the command. |
double |
timeSinceInitialized()
Returns the time since this command was initialized (in seconds). |
java.lang.String |
toString()
The string representation for a Command is by default its name. |
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 |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Command()
public Command(java.lang.String name)
name
- the name for this command
java.lang.IllegalArgumentException
- if name is nullpublic Command(double timeout)
timeout
- the time (in seconds) before this command "times out"
java.lang.IllegalArgumentException
- if given a negative timeoutisTimedOut()
public Command(java.lang.String name, double timeout)
name
- the name of the commandtimeout
- the time (in seconds) before this command "times out"
java.lang.IllegalArgumentException
- if given a negative timeout or name was null.isTimedOut()
Method Detail |
---|
public java.lang.String getName()
getName
in interface NamedSendable
protected final void setTimeout(double seconds)
seconds
- the timeout (in seconds)
java.lang.IllegalArgumentException
- if seconds is negativeisTimedOut()
public final double timeSinceInitialized()
protected void requires(Subsystem subsystem)
Subsystem
is used by this command.
This method is crucial to the functioning of the Command System in general.
Note that the recommended way to call this method is in the constructor.
subsystem
- the Subsystem
required
java.lang.IllegalArgumentException
- if subsystem is null
IllegalUseOfCommandException
- if this command has started before or if it has been given to a CommandGroup
Subsystem
protected abstract void initialize()
protected abstract void execute()
protected abstract boolean isFinished()
end()
will be called.
It may be useful for a team to reference the isTimedOut()
method
for time-sensitive commands.
isTimedOut()
protected abstract void end()
protected abstract void interrupted()
cancel()
or another command shared the same requirements as this one, and booted
it out.
This is where you may want to wrap up loose ends, like shutting off a motor that was being used in the command.
Generally, it is useful to simply call the end()
method
within this method
protected boolean isTimedOut()
timeSinceInitialized()
method returns a number which is greater than or equal to the timeout for the command.
If there is no timeout, this will always return false.
public void 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.
IllegalUseOfCommandException
- if the command is a part of a CommandGrouppublic boolean isRunning()
interrupted()
.
public void cancel()
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.
IllegalUseOfCommandException
- if this command is a part of a command grouppublic boolean isCanceled()
public boolean isInterruptible()
protected void setInterruptible(boolean interruptible)
interruptible
- whether or not this command can be interruptedpublic boolean doesRequire(Subsystem system)
Subsystem
.
system
- the system
public CommandGroup getGroup()
CommandGroup
that this command is a part of.
Will return null if this Command
is not in a group.
CommandGroup
that this command is a part of (or null if not in group)public void setRunWhenDisabled(boolean run)
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.
run
- whether or not this command should run when the robot is disabledpublic boolean willRunWhenDisabled()
Command
will run when the robot is disabled, or if it will cancel itself.
Command
will run when the robot is disabled, or if it will cancel itselfpublic java.lang.String toString()
Command
is by default its name.
toString
in class java.lang.Object
public java.lang.String getSmartDashboardType()
getSmartDashboardType
in interface Sendable
public void initTable(ITable table)
Sendable
initTable
in interface Sendable
table
- The table to put the values in.public ITable getTable()
getTable
in interface Sendable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |