Skip to content

Commit

Permalink
Abstract button board for sim?
Browse files Browse the repository at this point in the history
  • Loading branch information
nab138 committed Feb 15, 2025
1 parent 6335a92 commit 9844198
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public final class Constants {
public static final Mode simMode = Mode.SIM;
public static final Mode currentMode = RobotBase.isReal() ? Mode.REAL : simMode;
public static final boolean simButtonBoard = true;
public static final boolean visionSim = false;
// If true, drive controls are based off alliance like in real life. If false,
// up is positive y, right is positive x.
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
import frc.robot.subsystems.vision.VisionIO;
import frc.robot.subsystems.vision.VisionIOPhotonVisionSim;
import frc.robot.util.AllianceUtil;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.BBoardIOReal;
import frc.robot.util.bboard.BBoardIOSim;
import frc.robot.util.bboard.ButtonBoard;

import org.ironmaple.simulation.SimulatedArena;
import org.ironmaple.simulation.drivesims.SwerveDriveSimulation;
Expand All @@ -77,7 +79,7 @@
public class RobotContainer {
// Subsystems
public final Drive drive;
public final ButtonBoardUtil buttonBoard;
public final ButtonBoard buttonBoard;
private final Vision vision;
private final Elevator elevator;
private final Shoulder shoulder;
Expand Down Expand Up @@ -120,7 +122,7 @@ public RobotContainer() {
elevator = new Elevator(new ElevatorIOSpark());
shoulder = new Shoulder(new ShoulderIOSpark());
endEffector = new EndEffector(new EndEffectorIOSpark());
buttonBoard = new ButtonBoardUtil();
buttonBoard = new ButtonBoard(new BBoardIOReal());
LEDs = new LEDs(new LEDsIORio());
break;

Expand Down Expand Up @@ -149,7 +151,7 @@ public RobotContainer() {
elevator = new Elevator(new ElevatorIOSim());
shoulder = new Shoulder(new ShoulderIOSim());
endEffector = new EndEffector(new EndEffectorIOSim());
buttonBoard = new ButtonBoardUtil();
buttonBoard = new ButtonBoard(Constants.simButtonBoard ? new BBoardIOSim() : new BBoardIOReal());
LEDs = new LEDs(new LEDsIORio());
break;

Expand All @@ -175,7 +177,7 @@ public RobotContainer() {
});
endEffector = new EndEffector(new EndEffectorIO() {
});
buttonBoard = new ButtonBoardUtil();
buttonBoard = new ButtonBoard(new BBoardIOReal());
LEDs = new LEDs(new LEDsIORio());
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/frc/robot/statemachine/StateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.elevator.Elevator;
import frc.robot.subsystems.shoulder.Shoulder;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class StateMachine extends StateMachineBase {
public StateMachine(CommandXboxController driverController, CommandXboxController operatorController,
ButtonBoardUtil buttonBoard, LoggedDashboardChooser<Command> chooser,
ButtonBoard buttonBoard, LoggedDashboardChooser<Command> chooser,
Drive drive, Elevator elevator, Shoulder shoulder, EndEffector endEffector, LEDs LEDs) {
super();
State disabled = new DisabledState(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import frc.robot.subsystems.LEDs.LEDs;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class GoToReefIntake extends State {
public GoToReefIntake(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive, LEDs LEDs) {
public GoToReefIntake(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive, LEDs LEDs) {
super(stateMachine);

startWhenActive(DriveCommands.goToPoint(drive, buttonBoard::getAlgaeReefTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import frc.robot.statemachine.reusable.StateMachineBase;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class GoToScoreCoral extends State {
public GoToScoreCoral(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive) {
public GoToScoreCoral(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive) {
super(stateMachine);

startWhenActive(DriveCommands.goToPoint(drive, buttonBoard::getCoralReefTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import frc.robot.statemachine.reusable.StateMachineBase;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class GoToScoreNet extends State {
public GoToScoreNet(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive) {
public GoToScoreNet(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive) {
super(stateMachine);

// TODO: change position to net
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.AutoTargetUtils;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class GoToScoreProcessor extends State {
public GoToScoreProcessor(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive) {
public GoToScoreProcessor(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive) {
super(stateMachine);

// TODO: change to processor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import frc.robot.subsystems.LEDs.LEDs;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class GoToStationIntake extends State {
public GoToStationIntake(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive, LEDs LEDs) {
public GoToStationIntake(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive, LEDs LEDs) {
super(stateMachine);

startWhenActive(DriveCommands.goToPoint(drive, buttonBoard::getIntakeStationTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.subsystems.elevator.Elevator;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class IntakeAlgae extends State {
public IntakeAlgae(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive, Elevator elevator,
public IntakeAlgae(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive, Elevator elevator,
EndEffector endEffector, LEDs LEDs) {
super(stateMachine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.subsystems.elevator.Elevator;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class IntakeCoral extends State {
public IntakeCoral(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive, Elevator elevator,
public IntakeCoral(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive, Elevator elevator,
EndEffector endEffector, LEDs LEDs) {
super(stateMachine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import frc.robot.subsystems.LEDs.LEDs;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class ScoreAlgaeNet extends State {
public ScoreAlgaeNet(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive,
public ScoreAlgaeNet(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive,
EndEffector endEffector, LEDs LEDs) {
super(stateMachine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.util.AutoTargetUtils;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class ScoreAlgaeProcessor extends State {
public ScoreAlgaeProcessor(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive,
public ScoreAlgaeProcessor(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive,
EndEffector endEffector, LEDs LEDs) {
super(stateMachine);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import frc.robot.subsystems.drive.DriveCommands;
import frc.robot.subsystems.elevator.Elevator;
import frc.robot.subsystems.shoulder.Shoulder;
import frc.robot.util.ButtonBoardUtil;
import frc.robot.util.bboard.ButtonBoard;

public class ScoreCoral extends State {
public ScoreCoral(StateMachineBase stateMachine, ButtonBoardUtil buttonBoard, Drive drive, EndEffector endEffector,
public ScoreCoral(StateMachineBase stateMachine, ButtonBoard buttonBoard, Drive drive, EndEffector endEffector,
Elevator elevator, Shoulder shoulder, LEDs LEDs) {
super(stateMachine);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/frc/robot/util/bboard/BBoardIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package frc.robot.util.bboard;

import frc.robot.util.bboard.ButtonBoard.ButtonInfo;
import frc.robot.util.bboard.ButtonBoard.SelectButtonInfo;

public interface BBoardIO {
public boolean isPressed(ButtonInfo button);
public boolean isPressed(SelectButtonInfo<?> button);
}
30 changes: 30 additions & 0 deletions src/main/java/frc/robot/util/bboard/BBoardIOReal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package frc.robot.util.bboard;

import edu.wpi.first.wpilibj.GenericHID;
import frc.robot.util.bboard.ButtonBoard.ButtonInfo;
import frc.robot.util.bboard.ButtonBoard.SelectButtonInfo;

public class BBoardIOReal implements BBoardIO {
private static GenericHID padOne = new GenericHID(2);
private static GenericHID padTwo = new GenericHID(3);
private static GenericHID padThree = new GenericHID(4);

@Override
public boolean isPressed(ButtonInfo buttonInfo) {
int board = buttonInfo.board();
int button = buttonInfo.button();
return (board == 0 && padOne.getRawButtonPressed(button))
|| (board == 1 && padTwo.getRawButtonPressed(button))
|| (board == 2 && padThree.getRawButtonPressed(button));
}


@Override
public boolean isPressed(SelectButtonInfo<?> buttonInfo) {
int board = buttonInfo.board();
int button = buttonInfo.button();
return (board == 0 && padOne.getRawButtonPressed(button))
|| (board == 1 && padTwo.getRawButtonPressed(button))
|| (board == 2 && padThree.getRawButtonPressed(button));
}
}
35 changes: 35 additions & 0 deletions src/main/java/frc/robot/util/bboard/BBoardIOSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package frc.robot.util.bboard;

import java.util.ArrayList;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.util.bboard.ButtonBoard.ButtonInfo;
import frc.robot.util.bboard.ButtonBoard.SelectButtonInfo;

public class BBoardIOSim implements BBoardIO{
public BBoardIOSim() {
SmartDashboard.putNumberArray("ButtonBoardSim/Pressed", new double[0]);
}

private ArrayList<ButtonInfo> pressedButtons = new ArrayList<>();

public void periodic(){
double[] pressed = SmartDashboard.getNumberArray("ButtonBoardSim/Pressed", new double[0]);
pressedButtons.clear();
for (int i = 0; i < pressed.length - 1; i += 2) {
pressedButtons.add(new ButtonInfo((int) pressed[i], (int) pressed[i + 1]));
}
SmartDashboard.putNumberArray("ButtonBoardSim/Pressed", new double[0]);
}

@Override
public boolean isPressed(ButtonInfo button) {
return pressedButtons.contains(button);
}

@Override
public boolean isPressed(SelectButtonInfo<?> button) {
return pressedButtons.contains(button.buttonInfo());
}

}
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package frc.robot.util;
package frc.robot.util.bboard;

import java.util.List;
import java.util.function.DoubleSupplier;

import org.littletonrobotics.junction.Logger;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.GenericHID;
import frc.robot.subsystems.drive.Drive;
import frc.robot.util.AutoTargetUtils.IntakeStations.IntakeStation;
import frc.robot.util.AutoTargetUtils;
import frc.robot.util.AutoTargetUtils.IntakeStations;
import frc.robot.util.AutoTargetUtils.Reef;
import frc.robot.util.AutoTargetUtils.Reef.CoralLevel;
import frc.robot.util.AutoTargetUtils.Reef.CoralReefLocation;
import me.nabdev.oxconfig.ConfigurableParameter;

public class ButtonBoardUtil {
private static GenericHID padOne = new GenericHID(2);
private static GenericHID padTwo = new GenericHID(3);
private static GenericHID padThree = new GenericHID(4);
public class ButtonBoard {
private final BBoardIO boardIO;

private static boolean isButtonPressed(int board, int button) {
return (board == 0 && padOne.getRawButtonPressed(button))
|| (board == 1 && padTwo.getRawButtonPressed(button))
|| (board == 2 && padThree.getRawButtonPressed(button));
public ButtonBoard(BBoardIO boardIO) {
this.boardIO = boardIO;
}

public record ButtonInfo(int board, int button) {
public boolean isPressed() {
return isButtonPressed(board, button);
}
};

public record SelectButtonInfo<T extends Enum<?>>(int board, int button, T value) {
public boolean isPressed() {
return isButtonPressed(board, button);
public ButtonInfo buttonInfo() {
return new ButtonInfo(board, button);
}
};

Expand Down Expand Up @@ -97,7 +90,7 @@ public boolean getSelectedAlgaeLocation() {

public void periodic(Drive drive) {
for (SelectButtonInfo<CoralReefLocation> button : reefButtons) {
if (button.isPressed()) {
if (boardIO.isPressed(button)) {
coralReefLocation = button.value();
coralReefReferencePose = coralReefLocation.pose();
algaeReefTargetPose = Reef.algae(coralReefLocation.algae());
Expand All @@ -108,27 +101,27 @@ public void periodic(Drive drive) {
}
}
for (SelectButtonInfo<CoralLevel> button : levels) {
if (button.isPressed()) {
if (boardIO.isPressed(button)) {
coralReefLevel = button.value();
if (coralReefLocation != null)
coralReefTargetPose = Reef.coral(coralReefLocation, coralReefLevel);
}
}

for (SelectButtonInfo<IntakeStation> button : intakeStationButtons) {
if (button.isPressed()) {
if (boardIO.isPressed(button)) {
intakeStation = button.value();
intakeStationPose = IntakeStations.intakeStation(intakeStation);
intakeStationReferencePose = intakeStation.pose();
}
}
if (algaeModeToggle.isPressed()) {
if (boardIO.isPressed(algaeModeToggle)) {
algaeMode = !algaeMode;
}
if (processor.isPressed()) {
if (boardIO.isPressed(processor)) {
isProcessor = true;
}
if (net.isPressed()) {
if (boardIO.isPressed(net)) {
isProcessor = false;
}

Expand All @@ -148,8 +141,8 @@ public void periodic(Drive drive) {
Logger.recordOutput("ButtonBoard/CoralReefLevel", coralReefLevel);
Logger.recordOutput("ButtonBoard/IntakeStation", intakeStation);
Logger.recordOutput("ButtonBoard/IntakeStationPose", intakeStationPose);
Logger.recordOutput("ButtonBoard/ClimbUp", climbUp.isPressed());
Logger.recordOutput("ButtonBoard/ClimbDown", climbDown.isPressed());
Logger.recordOutput("ButtonBoard/ClimbUp", boardIO.isPressed(climbUp));
Logger.recordOutput("ButtonBoard/ClimbDown", boardIO.isPressed(climbDown));
}

public Pose2d getCoralReefTarget() {
Expand Down Expand Up @@ -197,7 +190,8 @@ public boolean closeToScoringTarget(Drive drive) {
if (coralReefTargetPose == null) {
return false;
}
return AutoTargetUtils.robotDistToPose(drive, AutoTargetUtils.processor()) < processorDistThreshold.get();
return AutoTargetUtils.robotDistToPose(drive, AutoTargetUtils.processor()) < processorDistThreshold
.get();
} else {
// TODO: Net
return false;
Expand Down

0 comments on commit 9844198

Please sign in to comment.