Add requirements param to more Command APIs (#2059)

Assorted improvements to the ergonomics of declaring requirements in the new
command framework. C++ requirements list parameters have been defaulted
to an empty list, some missing C++ requirements list parameters have been
added, and both C++ and Java have been given requirements list params in
various InstantCommand wrapper methods (#2049), whose value is
forwarded to the command.
This commit is contained in:
Oblarg
2019-11-08 21:30:30 -05:00
committed by Peter Johnson
parent ff39a96cee
commit 00228678d4
18 changed files with 97 additions and 59 deletions

View File

@@ -112,11 +112,12 @@ public interface Command {
* {@link CommandGroupBase#clearGroupedCommand(Command)}. The decorated command can, however, be
* further decorated without issue.
*
* @param toRun the Runnable to run
* @param toRun the Runnable to run
* @param requirements the required subsystems
* @return the decorated command
*/
default SequentialCommandGroup beforeStarting(Runnable toRun) {
return new SequentialCommandGroup(new InstantCommand(toRun), this);
default SequentialCommandGroup beforeStarting(Runnable toRun, Subsystem... requirements) {
return new SequentialCommandGroup(new InstantCommand(toRun, requirements), this);
}
/**
@@ -128,11 +129,12 @@ public interface Command {
* {@link CommandGroupBase#clearGroupedCommand(Command)}. The decorated command can, however, be
* further decorated without issue.
*
* @param toRun the Runnable to run
* @param toRun the Runnable to run
* @param requirements the required subsystems
* @return the decorated command
*/
default SequentialCommandGroup andThen(Runnable toRun) {
return new SequentialCommandGroup(this, new InstantCommand(toRun));
default SequentialCommandGroup andThen(Runnable toRun, Subsystem... requirements) {
return new SequentialCommandGroup(this, new InstantCommand(toRun, requirements));
}
/**
@@ -279,7 +281,7 @@ public interface Command {
* Whether the command requires a given subsystem. Named "hasRequirement" rather than "requires"
* to avoid confusion with
* {@link edu.wpi.first.wpilibj.command.Command#requires(edu.wpi.first.wpilibj.command.Subsystem)}
* - this may be able to be changed in a few years.
* - this may be able to be changed in a few years.
*
* @param requirement the subsystem to inquire about
* @return whether the subsystem is required

View File

@@ -10,6 +10,7 @@ package edu.wpi.first.wpilibj2.command.button;
import java.util.function.BooleanSupplier;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Subsystem;
/**
* This class provides an easy way to link commands to OI inputs.
@@ -66,11 +67,12 @@ public abstract class Button extends Trigger {
/**
* Runs the given runnable whenever the button is newly pressed.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this button, so calls can be chained
*/
public Button whenPressed(final Runnable toRun) {
whenActive(toRun);
public Button whenPressed(final Runnable toRun, Subsystem... requirements) {
whenActive(toRun, requirements);
return this;
}
@@ -106,11 +108,12 @@ public abstract class Button extends Trigger {
/**
* Constantly runs the given runnable while the button is held.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this button, so calls can be chained
*/
public Button whileHeld(final Runnable toRun) {
whileActiveContinuous(toRun);
public Button whileHeld(final Runnable toRun, Subsystem... requirements) {
whileActiveContinuous(toRun, requirements);
return this;
}
@@ -167,11 +170,12 @@ public abstract class Button extends Trigger {
/**
* Runs the given runnable when the button is released.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this button, so calls can be chained
*/
public Button whenReleased(final Runnable toRun) {
whenInactive(toRun);
public Button whenReleased(final Runnable toRun, Subsystem... requirements) {
whenInactive(toRun, requirements);
return this;
}

View File

@@ -12,6 +12,7 @@ import java.util.function.BooleanSupplier;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.Subsystem;
import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
@@ -100,11 +101,12 @@ public class Trigger {
/**
* Runs the given runnable whenever the trigger just becomes active.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this trigger, so calls can be chained
*/
public Trigger whenActive(final Runnable toRun) {
return whenActive(new InstantCommand(toRun));
public Trigger whenActive(final Runnable toRun, Subsystem... requirements) {
return whenActive(new InstantCommand(toRun, requirements));
}
/**
@@ -155,11 +157,12 @@ public class Trigger {
/**
* Constantly runs the given runnable while the button is held.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this trigger, so calls can be chained
*/
public Trigger whileActiveContinuous(final Runnable toRun) {
return whileActiveContinuous(new InstantCommand(toRun));
public Trigger whileActiveContinuous(final Runnable toRun, Subsystem... requirements) {
return whileActiveContinuous(new InstantCommand(toRun, requirements));
}
/**
@@ -243,11 +246,12 @@ public class Trigger {
/**
* Runs the given runnable when the trigger becomes inactive.
*
* @param toRun the runnable to run
* @param toRun the runnable to run
* @param requirements the required subsystems
* @return this trigger, so calls can be chained
*/
public Trigger whenInactive(final Runnable toRun) {
return whenInactive(new InstantCommand(toRun));
public Trigger whenInactive(final Runnable toRun, Subsystem... requirements) {
return whenInactive(new InstantCommand(toRun, requirements));
}
/**