diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java index be5c03a10e..0d2178f6b5 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java @@ -93,6 +93,19 @@ public final class Commands { () -> {}, run, interrupted -> end.run(), () -> false, requirements); } + /** + * Constructs a command that runs an action once, and then runs an action every iteration until + * interrupted. + * + * @param start the action to run on start + * @param run the action to run every iteration + * @param requirements subsystems the action requires + * @return the command + */ + public static Command startRun(Runnable start, Runnable run, Subsystem... requirements) { + return new FunctionalCommand(start, run, interrupted -> {}, () -> false, requirements); + } + /** * Constructs a command that prints a message and finishes. * diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java index ed9454362a..c0e694774c 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java @@ -146,6 +146,18 @@ public interface Subsystem { return Commands.runEnd(run, end, this); } + /** + * Constructs a command that runs an action once and then runs another action every iteration + * until interrupted. Requires this subsystem. + * + * @param start the action to run on start + * @param run the action to run every iteration + * @return the command + */ + default Command startRun(Runnable start, Runnable run) { + return Commands.startRun(start, run, this); + } + /** * Constructs a {@link DeferredCommand} with the provided supplier. This subsystem is added as a * requirement. diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp index 2e698425bb..15458083d6 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp @@ -58,6 +58,14 @@ CommandPtr cmd::RunEnd(std::function run, std::function end, .ToPtr(); } +CommandPtr cmd::StartRun(std::function start, std::function run, + Requirements requirements) { + return FunctionalCommand( + std::move(start), std::move(run), [](bool interrupted) {}, + [] { return false; }, requirements) + .ToPtr(); +} + CommandPtr cmd::Print(std::string_view msg) { return PrintCommand(msg).ToPtr(); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp index a0241bdd02..5800c44069 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp @@ -61,6 +61,11 @@ CommandPtr Subsystem::RunEnd(std::function run, return cmd::RunEnd(std::move(run), std::move(end), {this}); } +CommandPtr Subsystem::StartRun(std::function start, + std::function run) { + return cmd::StartRun(std::move(start), std::move(run), {this}); +} + CommandPtr Subsystem::Defer(wpi::unique_function supplier) { return cmd::Defer(std::move(supplier), {this}); } diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h b/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h index 0124d0ae3f..6cfad01b1f 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Commands.h @@ -86,6 +86,18 @@ CommandPtr StartEnd(std::function start, std::function end, CommandPtr RunEnd(std::function run, std::function end, Requirements requirements = {}); +/** + * Constructs a command that runs an action once, and then runs an action every + * iteration until interrupted. + * + * @param start the action to run on start + * @param run the action to run every iteration + * @param requirements subsystems the action requires + */ +[[nodiscard]] +CommandPtr StartRun(std::function start, std::function run, + Requirements requirements = {}); + /** * Constructs a command that prints a message and finishes. * diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h index b95b837c5f..d62f69ca35 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h @@ -159,6 +159,16 @@ class Subsystem { [[nodiscard]] CommandPtr RunEnd(std::function run, std::function end); + /** + * Constructs a command that runs an action once, and then runs an action + * every iteration until interrupted. Requires this subsystem. + * + * @param start the action to run on start + * @param run the action to run every iteration + */ + [[nodiscard]] + CommandPtr StartRun(std::function start, std::function run); + /** * Constructs a DeferredCommand with the provided supplier. This subsystem is * added as a requirement.