Package edu.wpi.first.wpilibj.command
Class WhileCommand
- java.lang.Object
-
- edu.wpi.first.wpilibj.SendableBase
-
- edu.wpi.first.wpilibj.command.Command
-
- edu.wpi.first.wpilibj.command.WhileCommand
-
- All Implemented Interfaces:
edu.wpi.first.wpilibj.Sendable
,java.lang.AutoCloseable
public class WhileCommand extends edu.wpi.first.wpilibj.command.Command
A command which loops 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, and transferred over to this command at construction. This allows CommandGroups and canceling to work properly. If this command is interrupted (cancelled or booted by another command), then the body command will be cancelled. If the body command is cancelled, then it will start again on the next iteration. Because HyperLib is currently in pre-release, these semantics may change based on what we consider "reasonable defaults".
-
-
Constructor Summary
Constructors Constructor Description WhileCommand(java.lang.String name, java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command body)
Construct a new WhileCommand from a condition and a body, with the given name.WhileCommand(java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command body)
Construct a new WhileCommand from a condition and a body.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
execute()
If the loop has already ended, do nothing.protected void
initialize()
Reset the state as if this command has not yet been run.protected void
interrupted()
Cancel the body command.protected boolean
isFinished()
Check if the loop has finished.-
Methods inherited from class edu.wpi.first.wpilibj.command.Command
cancel, clearRequirements, doesRequire, end, getGroup, initSendable, isCanceled, isCompleted, isInterruptible, isRunning, isTimedOut, requires, setInterruptible, setRunWhenDisabled, setTimeout, start, timeSinceInitialized, toString, willRunWhenDisabled
-
Methods inherited from class edu.wpi.first.wpilibj.SendableBase
addChild, close, free, getName, getSubsystem, setName, setName, setName, setSubsystem
-
-
-
-
Constructor Detail
-
WhileCommand
public WhileCommand(java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command body)
Construct a new WhileCommand from a condition and a body. After construction, the body command's requirements are moved to this command. That means you should not re-use the body command after passing it to this method. Instead, create a new one.- Parameters:
condition
- The condition to checkbody
- The command to run as the body
-
WhileCommand
public WhileCommand(java.lang.String name, java.util.function.BooleanSupplier condition, edu.wpi.first.wpilibj.command.Command body)
Construct a new WhileCommand from a condition and a body, with the given name. After construction, the body command's requirements are moved to this command. That means you should not re-use the body command after passing it to this method. Instead, create a new one.- Parameters:
name
- The name of the commandcondition
- The condition to checkbody
- The command to run as the body
-
-
Method Detail
-
initialize
protected void initialize()
Reset the state as if this command has not yet been run. We don't call checkCondition() here, because the condition is already checked in execute(), and it makes sense semantically to check the condition once per iteration. This might matter if the condition passed in has side effects.- Overrides:
initialize
in classedu.wpi.first.wpilibj.command.Command
-
execute
protected void execute()
If the loop has already ended, do nothing. Otherwise, check if the body command is running. If it is not, check the condition and start it if necessary.- Overrides:
execute
in classedu.wpi.first.wpilibj.command.Command
-
interrupted
protected void interrupted()
Cancel the body command.- Overrides:
interrupted
in classedu.wpi.first.wpilibj.command.Command
-
isFinished
protected boolean isFinished()
Check if the loop has finished. This is only true if the body command has ended and the condition returned false on most recent check.- Specified by:
isFinished
in classedu.wpi.first.wpilibj.command.Command
-
-