diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java index 69ab0d6630..6cca974f2a 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java @@ -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 diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Button.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Button.java index d4ff65787e..b7f26ae374 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Button.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Button.java @@ -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; } diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Trigger.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Trigger.java index 9a12deff8f..5167e4ae4f 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Trigger.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Trigger.java @@ -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)); } /** diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp index 8a000cd71d..8d92367a8f 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp @@ -48,19 +48,21 @@ ParallelRaceGroup Command::WithInterrupt(std::function condition) && { return ParallelRaceGroup(std::move(temp)); } -SequentialCommandGroup Command::BeforeStarting(std::function toRun) && { +SequentialCommandGroup Command::BeforeStarting(std::function toRun, + std::initializer_list requirements) && { std::vector> temp; temp.emplace_back(std::make_unique( - std::move(toRun), std::initializer_list{})); + std::move(toRun), requirements)); temp.emplace_back(std::move(*this).TransferOwnership()); return SequentialCommandGroup(std::move(temp)); } -SequentialCommandGroup Command::AndThen(std::function toRun) && { +SequentialCommandGroup Command::AndThen(std::function toRun, + std::initializer_list requirements) && { std::vector> temp; temp.emplace_back(std::move(*this).TransferOwnership()); temp.emplace_back(std::make_unique( - std::move(toRun), std::initializer_list{})); + std::move(toRun), requirements)); return SequentialCommandGroup(std::move(temp)); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/FunctionalCommand.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/FunctionalCommand.cpp index 63c3179633..f0e178f888 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/FunctionalCommand.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/FunctionalCommand.cpp @@ -12,11 +12,14 @@ using namespace frc2; FunctionalCommand::FunctionalCommand(std::function onInit, std::function onExecute, std::function onEnd, - std::function isFinished) + std::function isFinished, + std::initializer_list requirements) : m_onInit{std::move(onInit)}, m_onExecute{std::move(onExecute)}, m_onEnd{std::move(onEnd)}, - m_isFinished{std::move(isFinished)} {} + m_isFinished{std::move(isFinished)} { + AddRequirements(requirements); +} void FunctionalCommand::Initialize() { m_onInit(); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Button.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Button.cpp index e519a9fdaf..440675f061 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Button.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Button.cpp @@ -16,8 +16,9 @@ Button Button::WhenPressed(Command* command, bool interruptible) { return *this; } -Button Button::WhenPressed(std::function toRun) { - WhenActive(std::move(toRun)); +Button Button::WhenPressed(std::function toRun, + std::initializer_list requirements) { + WhenActive(std::move(toRun), requirements); return *this; } @@ -26,8 +27,9 @@ Button Button::WhileHeld(Command* command, bool interruptible) { return *this; } -Button Button::WhileHeld(std::function toRun) { - WhileActiveContinous(std::move(toRun)); +Button Button::WhileHeld(std::function toRun, + std::initializer_list requirements) { + WhileActiveContinous(std::move(toRun), requirements); return *this; } @@ -41,8 +43,9 @@ Button Button::WhenReleased(Command* command, bool interruptible) { return *this; } -Button Button::WhenReleased(std::function toRun) { - WhenInactive(std::move(toRun)); +Button Button::WhenReleased(std::function toRun, + std::initializer_list requirements) { + WhenInactive(std::move(toRun), requirements); return *this; } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp index 304bf98a49..aec118324f 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp @@ -28,8 +28,9 @@ Trigger Trigger::WhenActive(Command* command, bool interruptible) { return *this; } -Trigger Trigger::WhenActive(std::function toRun) { - return WhenActive(InstantCommand(std::move(toRun), {})); +Trigger Trigger::WhenActive(std::function toRun, + std::initializer_list requirements) { + return WhenActive(InstantCommand(std::move(toRun), requirements)); } Trigger Trigger::WhileActiveContinous(Command* command, bool interruptible) { @@ -48,8 +49,9 @@ Trigger Trigger::WhileActiveContinous(Command* command, bool interruptible) { return *this; } -Trigger Trigger::WhileActiveContinous(std::function toRun) { - return WhileActiveContinous(InstantCommand(std::move(toRun), {})); +Trigger Trigger::WhileActiveContinous(std::function toRun, + std::initializer_list requirements) { + return WhileActiveContinous(InstantCommand(std::move(toRun), requirements)); } Trigger Trigger::WhileActiveOnce(Command* command, bool interruptible) { @@ -82,8 +84,9 @@ Trigger Trigger::WhenInactive(Command* command, bool interruptible) { return *this; } -Trigger Trigger::WhenInactive(std::function toRun) { - return WhenInactive(InstantCommand(std::move(toRun), {})); +Trigger Trigger::WhenInactive(std::function toRun, + std::initializer_list requirements) { + return WhenInactive(InstantCommand(std::move(toRun), requirements)); } Trigger Trigger::ToggleWhenActive(Command* command, bool interruptible) { diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h index dc26b8a2e1..b0a57a9a8b 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h @@ -127,17 +127,21 @@ class Command : public frc::ErrorBase { * Decorates this command with a runnable to run before this command starts. * * @param toRun the Runnable to run + * @param requirements the required subsystems * @return the decorated command */ - SequentialCommandGroup BeforeStarting(std::function toRun) &&; + SequentialCommandGroup BeforeStarting(std::function toRun, + std::initializer_list requirements = {}) &&; /** * Decorates this command with a runnable to run after the command finishes. * * @param toRun the Runnable to run + * @param requirements the required subsystems * @return the decorated command */ - SequentialCommandGroup AndThen(std::function toRun) &&; + SequentialCommandGroup AndThen(std::function toRun, + std::initializer_list requirements = {}) &&; /** * Decorates this command to run perpetually, ignoring its ordinary end diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/FunctionalCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/FunctionalCommand.h index dbec995ed4..f85f9be463 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/FunctionalCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/FunctionalCommand.h @@ -35,7 +35,8 @@ class FunctionalCommand : public CommandHelper { FunctionalCommand(std::function onInit, std::function onExecute, std::function onEnd, - std::function isFinished); + std::function isFinished, + std::initializer_list requirements = {}); FunctionalCommand(FunctionalCommand&& other) = default; diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/InstantCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/InstantCommand.h index b9d54c7aa9..97b4da2408 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/InstantCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/InstantCommand.h @@ -29,7 +29,7 @@ class InstantCommand : public CommandHelper { * @param requirements the subsystems required by this command */ InstantCommand(std::function toRun, - std::initializer_list requirements); + std::initializer_list requirements = {}); InstantCommand(InstantCommand&& other) = default; diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/NotifierCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/NotifierCommand.h index 9a3ee26634..847c693eff 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/NotifierCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/NotifierCommand.h @@ -37,7 +37,7 @@ class NotifierCommand : public CommandHelper { * @param requirements the subsystems required by this command */ NotifierCommand(std::function toRun, units::second_t period, - std::initializer_list requirements); + std::initializer_list requirements = {}); NotifierCommand(NotifierCommand&& other); diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/PIDCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/PIDCommand.h index 15563710de..3f17b0d15e 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/PIDCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/PIDCommand.h @@ -40,7 +40,7 @@ class PIDCommand : public CommandHelper { std::function measurementSource, std::function setpointSource, std::function useOutput, - std::initializer_list requirements); + std::initializer_list requirements = {}); /** * Creates a new PIDCommand, which controls the given output with a diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/ProfiledPIDCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/ProfiledPIDCommand.h index 0378ab7cbe..bf0c337dab 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/ProfiledPIDCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/ProfiledPIDCommand.h @@ -44,7 +44,7 @@ class ProfiledPIDCommand std::function measurementSource, std::function goalSource, std::function useOutput, - std::initializer_list requirements); + std::initializer_list requirements = {}); /** * Creates a new PIDCommand, which controls the given output with a diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/RamseteCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/RamseteCommand.h index e717250e7c..0478ef2edf 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/RamseteCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/RamseteCommand.h @@ -91,7 +91,7 @@ class RamseteCommand : public CommandHelper { frc2::PIDController leftController, frc2::PIDController rightController, std::function output, - std::initializer_list requirements); + std::initializer_list requirements = {}); /** * Constructs a new RamseteCommand that, when executed, will follow the diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/RunCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/RunCommand.h index 56cf754934..aae7b74cf1 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/RunCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/RunCommand.h @@ -30,7 +30,7 @@ class RunCommand : public CommandHelper { * @param requirements the subsystems to require */ RunCommand(std::function toRun, - std::initializer_list requirements); + std::initializer_list requirements = {}); RunCommand(RunCommand&& other) = default; diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/StartEndCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/StartEndCommand.h index 8c7d64d2a5..32b7e76abc 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/StartEndCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/StartEndCommand.h @@ -32,7 +32,7 @@ class StartEndCommand : public CommandHelper { * @param requirements the subsystems required by this command */ StartEndCommand(std::function onInit, std::function onEnd, - std::initializer_list requirements); + std::initializer_list requirements = {}); StartEndCommand(StartEndCommand&& other) = default; diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/button/Button.h b/wpilibNewCommands/src/main/native/include/frc2/command/button/Button.h index 3e0f7fe33d..6561652011 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/button/Button.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/button/Button.h @@ -65,8 +65,10 @@ class Button : public Trigger { * Binds a runnable to execute when the button is pressed. * * @param toRun the runnable to execute. + * @param requirements the required subsystems. */ - Button WhenPressed(std::function toRun); + Button WhenPressed(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to be started repeatedly while the button is pressed, and @@ -100,8 +102,10 @@ class Button : public Trigger { * Binds a runnable to execute repeatedly while the button is pressed. * * @param toRun the runnable to execute. + * @param requirements the required subsystems. */ - Button WhileHeld(std::function toRun); + Button WhileHeld(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to be started when the button is pressed, and cancelled @@ -163,8 +167,10 @@ class Button : public Trigger { * Binds a runnable to execute when the button is released. * * @param toRun the runnable to execute. + * @param requirements the required subsystems. */ - Button WhenReleased(std::function toRun); + Button WhenReleased(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to start when the button is pressed, and be cancelled when diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h b/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h index 1a65ff64c1..7e32d0dd9a 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h @@ -96,8 +96,10 @@ class Trigger { * Binds a runnable to execute when the trigger becomes active. * * @param toRun the runnable to execute. + * @paaram requirements the required subsystems. */ - Trigger WhenActive(std::function toRun); + Trigger WhenActive(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to be started repeatedly while the trigger is active, and @@ -145,8 +147,10 @@ class Trigger { * Binds a runnable to execute repeatedly while the trigger is active. * * @param toRun the runnable to execute. + * @param requirements the required subsystems. */ - Trigger WhileActiveContinous(std::function toRun); + Trigger WhileActiveContinous(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to be started when the trigger becomes active, and @@ -234,8 +238,10 @@ class Trigger { * Binds a runnable to execute when the trigger becomes inactive. * * @param toRun the runnable to execute. + * @param requirements the required subsystems. */ - Trigger WhenInactive(std::function toRun); + Trigger WhenInactive(std::function toRun, + std::initializer_list requirements = {}); /** * Binds a command to start when the trigger becomes active, and be cancelled