Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public static Command startRun(Runnable start, Runnable run, Subsystem... requir
return new FunctionalCommand(start, run, interrupted -> {}, () -> false, requirements);
}

/**
* Constructs a command that runs an action once, and then runs an action every iteration until
* interrupted, and then runs a third action.
*
* @param start the action to run on start
* @param run the action to run every iteration
* @param end the action to run on interrupt
* @param requirements subsystems the action requires
* @return the command
*/
public static Command startRunEnd(
Runnable start, Runnable run, Runnable end, Subsystem... requirements) {
requireNonNullParam(end, "end", "Command.runEnd");
return new FunctionalCommand(start, run, interrupted -> end.run(), () -> false, requirements);
}

/**
* Constructs a command that prints a message and finishes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ default Command startRun(Runnable start, Runnable run) {
return Commands.startRun(start, run, this);
}

/**
* Constructs a command that runs an action once, and then runs an action every iteration until
* interrupted, and then runs a third action. Requires this subsystem.
*
* @param start the action to run on start
* @param run the action to run every iteration
* @param end the action to run on interrupt
* @return the command
*/
default Command startRunEnd(Runnable start, Runnable run, Runnable end) {
return Commands.startRunEnd(start, run, end, this);
}

/**
* Constructs a {@link DeferredCommand} with the provided supplier. This subsystem is added as a
* requirement.
Expand Down
11 changes: 11 additions & 0 deletions wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ CommandPtr cmd::StartRun(std::function<void()> start, std::function<void()> run,
.ToPtr();
}

CommandPtr cmd::StartRunEnd(std::function<void()> start,
std::function<void()> run,
std::function<void()> end,
Requirements requirements) {
return FunctionalCommand(
std::move(start), std::move(run),
[end = std::move(end)](bool interrupted) { end(); },
[] { return false; }, requirements)
.ToPtr();
}

CommandPtr cmd::Print(std::string_view msg) {
return PrintCommand(msg).ToPtr();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ CommandPtr Subsystem::StartRun(std::function<void()> start,
return cmd::StartRun(std::move(start), std::move(run), {this});
}

CommandPtr Subsystem::StartRunEnd(std::function<void()> start,
std::function<void()> run,
std::function<void()> end) {
return cmd::StartRunEnd(std::move(start), std::move(run), std::move(end),
{this});
}

CommandPtr Subsystem::Defer(wpi::unique_function<CommandPtr()> supplier) {
return cmd::Defer(std::move(supplier), {this});
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ CommandPtr RunEnd(std::function<void()> run, std::function<void()> end,
CommandPtr StartRun(std::function<void()> start, std::function<void()> run,
Requirements requirements = {});

/**
* Constructs a command that runs an action once, and then runs an action every
* iteration until interrupted, and then runs a third action.
*
* @param start the action to run on start
* @param run the action to run every iteration
* @param end the action to run on interrupt
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr StartRunEnd(std::function<void()> start, std::function<void()> run,
std::function<void()> end,
Requirements requirements = {});

/**
* Constructs a command that prints a message and finishes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ class Subsystem {
[[nodiscard]]
CommandPtr StartRun(std::function<void()> start, std::function<void()> run);

/**
* Constructs a command that runs an action once, and then runs an action
* every iteration until interrupted, and then runs a third action. Requires
* this subsystem.
*
* @param start the action to run on start
* @param run the action to run every iteration
* @param end the action to run on interrupt
*/
[[nodiscard]]
CommandPtr StartRunEnd(std::function<void()> start, std::function<void()> run,
std::function<void()> end);

/**
* Constructs a DeferredCommand with the provided supplier. This subsystem is
* added as a requirement.
Expand Down
Loading