2022-03-18 18:32:33 -06:00
|
|
|
// 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 frc4388.robot.commands;
|
|
|
|
|
|
2022-03-18 19:04:43 -06:00
|
|
|
import java.util.Set;
|
2022-03-18 18:32:33 -06:00
|
|
|
import java.util.function.BooleanSupplier;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.wpilibj2.command.Command;
|
|
|
|
|
import edu.wpi.first.wpilibj2.command.CommandBase;
|
2022-03-18 19:44:17 -06:00
|
|
|
import edu.wpi.first.wpilibj2.command.CommandGroupBase;
|
2022-03-18 19:04:43 -06:00
|
|
|
import edu.wpi.first.wpilibj2.command.Subsystem;
|
2022-03-18 18:32:33 -06:00
|
|
|
|
|
|
|
|
public class CommandChooser extends CommandBase {
|
|
|
|
|
|
|
|
|
|
private Command c1;
|
|
|
|
|
private Command c2;
|
|
|
|
|
|
2022-03-18 19:04:43 -06:00
|
|
|
private Boolean b1;
|
|
|
|
|
private Boolean b2;
|
|
|
|
|
|
|
|
|
|
private Command chosen;
|
2022-03-18 18:32:33 -06:00
|
|
|
|
|
|
|
|
/** Creates a new CommandChooser. */
|
|
|
|
|
public CommandChooser(Command c1, Command c2, BooleanSupplier bs1, BooleanSupplier bs2) {
|
|
|
|
|
// Use addRequirements() here to declare subsystem dependencies.
|
|
|
|
|
|
2022-03-18 19:04:43 -06:00
|
|
|
this.c1 = c1;
|
|
|
|
|
this.c2 = c2;
|
2022-03-18 18:32:33 -06:00
|
|
|
|
2022-03-18 19:04:43 -06:00
|
|
|
this.b1 = bs1.getAsBoolean();
|
|
|
|
|
this.b2 = bs2.getAsBoolean();
|
|
|
|
|
|
|
|
|
|
this.chosen = getChosen();
|
|
|
|
|
|
|
|
|
|
Set<Subsystem> allReqs = c1.getRequirements();
|
|
|
|
|
allReqs.addAll(c2.getRequirements());
|
|
|
|
|
addRequirements((Subsystem[]) allReqs.toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Command getChosen() {
|
|
|
|
|
if (this.b1) {
|
|
|
|
|
return this.c1;
|
|
|
|
|
} else {
|
|
|
|
|
return this.c2;
|
|
|
|
|
}
|
2022-03-18 18:32:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the command is initially scheduled.
|
|
|
|
|
@Override
|
2022-03-18 19:04:43 -06:00
|
|
|
public void initialize() {
|
|
|
|
|
this.chosen.initialize();
|
|
|
|
|
}
|
2022-03-18 18:32:33 -06:00
|
|
|
|
|
|
|
|
// Called every time the scheduler runs while the command is scheduled.
|
|
|
|
|
@Override
|
2022-03-18 19:04:43 -06:00
|
|
|
public void execute() {
|
|
|
|
|
this.chosen.execute();
|
|
|
|
|
}
|
2022-03-18 18:32:33 -06:00
|
|
|
|
|
|
|
|
// Called once the command ends or is interrupted.
|
|
|
|
|
@Override
|
2022-03-18 19:04:43 -06:00
|
|
|
public void end(boolean interrupted) {
|
|
|
|
|
this.chosen.end(interrupted);
|
|
|
|
|
}
|
2022-03-18 18:32:33 -06:00
|
|
|
|
|
|
|
|
// Returns true when the command should end.
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
2022-03-18 19:04:43 -06:00
|
|
|
return this.chosen.isFinished();
|
2022-03-18 18:32:33 -06:00
|
|
|
}
|
|
|
|
|
}
|