[commands] RepeatCommand: restart on following iteration (#4706)

This fixes InstantCommand.repeatedly().
This commit is contained in:
Starlight220
2022-11-26 09:50:42 +02:00
committed by GitHub
parent dd1da77d20
commit 58ed112b51
7 changed files with 79 additions and 11 deletions

View File

@@ -19,6 +19,7 @@ import static edu.wpi.first.wpilibj2.command.CommandGroupBase.requireUngrouped;
*/
public class RepeatCommand extends CommandBase {
protected final Command m_command;
private boolean m_ended;
/**
* Creates a new RepeatCommand. Will run another command repeatedly, restarting it whenever it
@@ -35,16 +36,21 @@ public class RepeatCommand extends CommandBase {
@Override
public void initialize() {
m_ended = false;
m_command.initialize();
}
@Override
public void execute() {
if (m_ended) {
m_ended = false;
m_command.initialize();
}
m_command.execute();
if (m_command.isFinished()) {
// restart command
m_command.end(false);
m_command.initialize();
m_ended = true;
}
}
@@ -62,4 +68,9 @@ public class RepeatCommand extends CommandBase {
public boolean runsWhenDisabled() {
return m_command.runsWhenDisabled();
}
@Override
public InterruptionBehavior getInterruptionBehavior() {
return m_command.getInterruptionBehavior();
}
}