Files
2022NoWayHome/src/main/java/frc4388/robot/commands/CommandChooser.java
T

73 lines
1.8 KiB
Java
Raw Normal View History

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;
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;
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;
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.
this.c1 = c1;
this.c2 = c2;
2022-03-18 18:32:33 -06:00
this.b1 = bs1.getAsBoolean();
this.b2 = bs2.getAsBoolean();
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
public void initialize() {
2022-03-18 22:25:39 -06:00
this.chosen = getChosen();
this.chosen.initialize();
}
2022-03-18 18:32:33 -06:00
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
this.chosen.execute();
}
2022-03-18 18:32:33 -06:00
// Called once the command ends or is interrupted.
@Override
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() {
return this.chosen.isFinished();
2022-03-18 18:32:33 -06:00
}
}