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

66 lines
1.9 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;
2022-03-19 11:10:04 -06:00
import java.util.Collections;
import java.util.HashMap;
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;
import edu.wpi.first.wpilibj2.command.Subsystem;
2022-03-18 18:32:33 -06:00
public class CommandChooser extends CommandBase {
2022-03-19 11:10:04 -06:00
private HashMap<Command, BooleanSupplier> commandMap;
2022-03-19 11:10:04 -06:00
// // private Command chosen;
2022-03-18 18:32:33 -06:00
/** Creates a new CommandChooser. */
2022-03-19 11:10:04 -06:00
public CommandChooser(HashMap<Command, BooleanSupplier> commandMap) {
this.commandMap = commandMap;
2022-03-18 18:32:33 -06:00
2022-03-19 11:10:04 -06:00
Set<Subsystem> allReqs = Collections.emptySet();
2022-03-18 18:32:33 -06:00
2022-03-19 11:10:04 -06:00
for(Command command : commandMap.keySet())
allReqs.addAll(command.getRequirements());
addRequirements((Subsystem[]) allReqs.toArray());
}
2022-03-18 18:32:33 -06:00
// Called when the command is initially scheduled.
@Override
public void initialize() {
2022-03-19 11:10:04 -06:00
for(Command command : commandMap.keySet())
if(commandMap.get(command).getAsBoolean()) command.initialize();
}
2022-03-18 18:32:33 -06:00
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
2022-03-19 11:10:04 -06:00
for(Command command : commandMap.keySet())
if(commandMap.get(command).getAsBoolean()) command.execute();
}
2022-03-18 18:32:33 -06:00
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
2022-03-19 11:10:04 -06:00
for(Command command : commandMap.keySet())
if(commandMap.get(command).getAsBoolean()) command.end(interrupted);
}
2022-03-18 18:32:33 -06:00
// Returns true when the command should end.
@Override
public boolean isFinished() {
2022-03-19 11:10:04 -06:00
boolean finished = true;
for(Command command : commandMap.keySet())
if(commandMap.get(command).getAsBoolean()) finished &= command.isFinished();
return finished;
2022-03-18 18:32:33 -06:00
}
}