[commands] Add RepeatCommand (#4009)

Co-authored-by: Starlight220 <53231611+Starlight220@users.noreply.github.com>
This commit is contained in:
Excalibur FRC | 6738
2022-04-08 08:02:08 +03:00
committed by GitHub
parent 88222daa3d
commit 1b26e2d5da
8 changed files with 359 additions and 0 deletions

View File

@@ -257,6 +257,22 @@ public interface Command {
return new PerpetualCommand(this);
}
/**
* Decorates this command to run repeatedly, restarting it when it ends, until this command is
* interrupted. The decorated command can still be canceled.
*
* <p>Note: This decorator works by composing this command within a CommandGroup. The command
* cannot be used independently after being decorated, or be re-decorated with a different
* decorator, unless it is manually cleared from the list of grouped commands with {@link
* CommandGroupBase#clearGroupedCommand(Command)}. The decorated command can, however, be further
* decorated without issue.
*
* @return the decorated command
*/
default RepeatCommand repeat() {
return new RepeatCommand(this);
}
/**
* Decorates this command to run "by proxy" by wrapping it in a {@link ProxyScheduleCommand}. This
* is useful for "forking off" from command groups when the user does not wish to extend the

View File

@@ -0,0 +1,70 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.wpilibj2.command.CommandGroupBase.registerGroupedCommands;
import static edu.wpi.first.wpilibj2.command.CommandGroupBase.requireUngrouped;
/**
* A command that runs another command repeatedly, restarting it when it ends, until this command is
* interrupted. While this class does not extend {@link CommandGroupBase}, it is still considered a
* CommandGroup, as it allows one to compose another command within it; the command instances that
* are passed to it cannot be added to any other groups, or scheduled individually.
*
* <p>As a rule, CommandGroups require the union of the requirements of their component commands.
*
* <p>This class is provided by the NewCommands VendorDep
*/
public class RepeatCommand extends CommandBase {
protected final Command m_command;
/**
* Creates a new RepeatCommand. Will run another command repeatedly, restarting it whenever it
* ends, until this command is interrupted.
*
* @param command the command to run repeatedly
*/
public RepeatCommand(Command command) {
requireUngrouped(command);
registerGroupedCommands(command);
m_command = command;
m_requirements.addAll(command.getRequirements());
}
@Override
public void initialize() {
m_command.initialize();
}
@Override
public void execute() {
m_command.execute();
if (m_command.isFinished()) {
// restart command
m_command.end(false);
m_command.initialize();
}
}
@Override
public boolean isFinished() {
return false;
}
@Override
public void end(boolean interrupted) {
m_command.end(interrupted);
}
@Override
public boolean runsWhenDisabled() {
return m_command.runsWhenDisabled();
}
@Override
public RepeatCommand repeat() {
return this;
}
}