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

@@ -48,19 +48,21 @@ ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
return ParallelRaceGroup(std::move(temp));
}
SequentialCommandGroup Command::BeforeStarting(std::function<void()> toRun) && {
SequentialCommandGroup Command::BeforeStarting(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) && {
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::make_unique<InstantCommand>(
std::move(toRun), std::initializer_list<Subsystem*>{}));
std::move(toRun), requirements));
temp.emplace_back(std::move(*this).TransferOwnership());
return SequentialCommandGroup(std::move(temp));
}
SequentialCommandGroup Command::AndThen(std::function<void()> toRun) && {
SequentialCommandGroup Command::AndThen(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) && {
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::move(*this).TransferOwnership());
temp.emplace_back(std::make_unique<InstantCommand>(
std::move(toRun), std::initializer_list<Subsystem*>{}));
std::move(toRun), requirements));
return SequentialCommandGroup(std::move(temp));
}

View File

@@ -12,11 +12,14 @@ using namespace frc2;
FunctionalCommand::FunctionalCommand(std::function<void()> onInit,
std::function<void()> onExecute,
std::function<void(bool)> onEnd,
std::function<bool()> isFinished)
std::function<bool()> isFinished,
std::initializer_list<Subsystem*> 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(); }

View File

@@ -16,8 +16,9 @@ Button Button::WhenPressed(Command* command, bool interruptible) {
return *this;
}
Button Button::WhenPressed(std::function<void()> toRun) {
WhenActive(std::move(toRun));
Button Button::WhenPressed(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun) {
WhileActiveContinous(std::move(toRun));
Button Button::WhileHeld(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun) {
WhenInactive(std::move(toRun));
Button Button::WhenReleased(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) {
WhenInactive(std::move(toRun), requirements);
return *this;
}

View File

@@ -28,8 +28,9 @@ Trigger Trigger::WhenActive(Command* command, bool interruptible) {
return *this;
}
Trigger Trigger::WhenActive(std::function<void()> toRun) {
return WhenActive(InstantCommand(std::move(toRun), {}));
Trigger Trigger::WhenActive(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun) {
return WhileActiveContinous(InstantCommand(std::move(toRun), {}));
Trigger Trigger::WhileActiveContinous(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun) {
return WhenInactive(InstantCommand(std::move(toRun), {}));
Trigger Trigger::WhenInactive(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) {
return WhenInactive(InstantCommand(std::move(toRun), requirements));
}
Trigger Trigger::ToggleWhenActive(Command* command, bool interruptible) {

View File

@@ -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<void()> toRun) &&;
SequentialCommandGroup BeforeStarting(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun) &&;
SequentialCommandGroup AndThen(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements = {}) &&;
/**
* Decorates this command to run perpetually, ignoring its ordinary end

View File

@@ -35,7 +35,8 @@ class FunctionalCommand : public CommandHelper<CommandBase, FunctionalCommand> {
FunctionalCommand(std::function<void()> onInit,
std::function<void()> onExecute,
std::function<void(bool)> onEnd,
std::function<bool()> isFinished);
std::function<bool()> isFinished,
std::initializer_list<Subsystem*> requirements = {});
FunctionalCommand(FunctionalCommand&& other) = default;

View File

@@ -29,7 +29,7 @@ class InstantCommand : public CommandHelper<CommandBase, InstantCommand> {
* @param requirements the subsystems required by this command
*/
InstantCommand(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
InstantCommand(InstantCommand&& other) = default;

View File

@@ -37,7 +37,7 @@ class NotifierCommand : public CommandHelper<CommandBase, NotifierCommand> {
* @param requirements the subsystems required by this command
*/
NotifierCommand(std::function<void()> toRun, units::second_t period,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
NotifierCommand(NotifierCommand&& other);

View File

@@ -40,7 +40,7 @@ class PIDCommand : public CommandHelper<CommandBase, PIDCommand> {
std::function<double()> measurementSource,
std::function<double()> setpointSource,
std::function<void(double)> useOutput,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
/**
* Creates a new PIDCommand, which controls the given output with a

View File

@@ -44,7 +44,7 @@ class ProfiledPIDCommand
std::function<units::meter_t()> measurementSource,
std::function<State()> goalSource,
std::function<void(double, State)> useOutput,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
/**
* Creates a new PIDCommand, which controls the given output with a

View File

@@ -91,7 +91,7 @@ class RamseteCommand : public CommandHelper<CommandBase, RamseteCommand> {
frc2::PIDController leftController,
frc2::PIDController rightController,
std::function<void(units::volt_t, units::volt_t)> output,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
/**
* Constructs a new RamseteCommand that, when executed, will follow the

View File

@@ -30,7 +30,7 @@ class RunCommand : public CommandHelper<CommandBase, RunCommand> {
* @param requirements the subsystems to require
*/
RunCommand(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
RunCommand(RunCommand&& other) = default;

View File

@@ -32,7 +32,7 @@ class StartEndCommand : public CommandHelper<CommandBase, StartEndCommand> {
* @param requirements the subsystems required by this command
*/
StartEndCommand(std::function<void()> onInit, std::function<void()> onEnd,
std::initializer_list<Subsystem*> requirements);
std::initializer_list<Subsystem*> requirements = {});
StartEndCommand(StartEndCommand&& other) = default;

View File

@@ -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<void()> toRun);
Button WhenPressed(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun);
Button WhileHeld(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun);
Button WhenReleased(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements = {});
/**
* Binds a command to start when the button is pressed, and be cancelled when

View File

@@ -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<void()> toRun);
Trigger WhenActive(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun);
Trigger WhileActiveContinous(std::function<void()> toRun,
std::initializer_list<Subsystem*> 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<void()> toRun);
Trigger WhenInactive(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements = {});
/**
* Binds a command to start when the trigger becomes active, and be cancelled