With the new Command Based Framework, I am confused as to how to make a command finish its action, even while interrupted by another command.
I guess you could reschedule your command again in the end method.
@Override
public void end(boolean interrupted) {
if (interrupted) {
CommandScheduler.getInstance().schedule(this);
}
}
That would restart it, not finish it.
Well that would interrupt the new command. To my knowledge, there’s no way to stop that new command from interrupting besides from making sure that new command is never scheduled in the first place (disable its scheduling from triggers/buttons/other logic when desired command is running). When the command is restarted, it will initialize again. So maybe have some field in your command to remember its state and not do all of the initialize logic like:
public class ExampleCommand extends CommandBase {
private DriveTrain driveTrain;
private boolean running = false;
private Timer timer;
private Integer somethingThatChangesWhenExecuting;
private boolean finishEarly = false;
/**
* Creates a new ExampleCommand.
*/
public ExampleCommand(DriveTrain driveTrain) {
this.driveTrain = driveTrain;
addRequirements(driveTrain);
timer = new Timer();
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
if (!running) {
running = true;
finishEarly = false;
timer.start();
somethingThatChangesWhenExecuting = 0;
}
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
somethingThatChangesWhenExecuting += 1;
driveTrain.drive(0, somethingThatChangesWhenExecuting);
if (driveTrain.someCondition()) {
finishEarly = true;
}
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
if (!finishEarly && running) {
CommandScheduler.getInstance().schedule(this);
}
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
// End command after 10 seconds
return !(running = !timer.hasPeriodPassed(10.0));
}
}
Also maybe you would want this type of logic in another abstract class that extends CommandBase to avoid unnecessary clutter in a command’s class.
Edit: look at other person’s solution
Specifically look for the search term interruptible in the documentation links above to see how you may achieve what you’re looking for.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.