Files
2025RidgeScape/src/main/java/frc4388/robot/subsystems/Elevator.java
T

152 lines
5.2 KiB
Java
Raw Normal View History

2025-01-17 19:53:14 -07: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.subsystems;
import com.ctre.phoenix6.configs.Slot0Configs;
2025-02-17 15:45:59 -07:00
import com.ctre.phoenix6.controls.PositionDutyCycle;
2025-01-17 19:53:14 -07:00
import com.ctre.phoenix6.controls.PositionVoltage;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.signals.NeutralModeValue;
2025-02-17 15:45:59 -07:00
import edu.wpi.first.math.geometry.Translation2d;
2025-02-17 11:48:43 -07:00
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.wpilibj.DigitalInput;
2025-02-17 11:48:43 -07:00
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2025-01-17 19:53:14 -07:00
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc4388.robot.Constants.ElevatorConstants;
public class Elevator extends SubsystemBase {
/** Creates a new Elevator. */
private TalonFX elevatorMotor;
private TalonFX endefectorMotor;
2025-01-17 19:53:14 -07:00
2025-02-17 11:48:43 -07:00
private DigitalInput basinBeamBreak;
private DigitalInput endefectorLimitSwitch;
2025-01-27 19:35:08 -07:00
public enum CoordinationState {
2025-02-17 11:48:43 -07:00
Waiting, // for coral into the though
WatingBeamTriped, //once the beam break trips
Ready, // Has coral in endefector
2025-02-01 15:52:06 -07:00
PrimedThree, // Arm and elevator Waiting to score in the level 3 position
ScoringThree, // Arm and elevator in the level three position
2025-02-01 15:52:06 -07:00
PrimedFour, // Arm and elevator Waiting to score in the level 4 position
ScoringFour, // Arm and elevator in the level four position
BallRemoverL2Primed, // Arm and elevator ready to remove the ball in the level 2 reef.
BallRemoverL2Go, // Arm and elevator removing the ball in the level 2 reef.
BallRemoverL3Primed, // Arm and elevator ready to remove the ball in the level 3 reef.
BallRemoverL3Go, // Arm and elevator removing the ball in the level 3 reef.
}
2025-01-27 19:35:08 -07:00
private CoordinationState currentState;
public Elevator(TalonFX elevatorTalonFX, TalonFX endefectorTalonFX, DigitalInput basinLimitSwitch, DigitalInput endefectorLimitSwitch) {
2025-01-17 19:53:14 -07:00
elevatorMotor = elevatorTalonFX;
endefectorMotor = endefectorTalonFX;
2025-02-17 11:48:43 -07:00
this.basinBeamBreak = basinLimitSwitch;
this.endefectorLimitSwitch = endefectorLimitSwitch;
2025-01-17 19:53:14 -07:00
2025-01-17 20:09:14 -07:00
elevatorMotor.setNeutralMode(NeutralModeValue.Brake);
endefectorMotor.setNeutralMode(NeutralModeValue.Brake);
2025-01-17 20:09:14 -07:00
elevatorMotor.getConfigurator().apply(ElevatorConstants.ELEVATOR_PID);
endefectorMotor.getConfigurator().apply(ElevatorConstants.ENDEFECTOR_PID);
2025-01-27 19:35:08 -07:00
currentState = CoordinationState.Ready;
2025-01-17 19:53:14 -07:00
}
//PID methods
private void PIDPosition(TalonFX motor, double position) {
2025-02-17 15:45:59 -07:00
var request = new PositionDutyCycle(position);
motor.setControl(request);
2025-01-17 19:53:14 -07:00
}
public void elevatorStop() {
elevatorMotor.set(0);
2025-01-17 19:53:14 -07:00
}
public void endefectorStop() {
endefectorMotor.set(0);
}
2025-01-17 19:53:14 -07:00
2025-01-27 19:35:08 -07:00
public void transitionState(CoordinationState state) {
currentState = state;
switch (currentState) {
case Waiting: {
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_ELEVATOR);
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFECTOR);
break;
}
2025-02-17 11:48:43 -07:00
case WatingBeamTriped: {
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_BEAM_BREAK_ELEVATOR);
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFECTOR);
2025-02-17 15:45:59 -07:00
armShuffle();
2025-02-17 11:48:43 -07:00
break;
}
2025-01-27 19:35:08 -07:00
case Ready: {
PIDPosition(elevatorMotor, ElevatorConstants.GROUND_POSITION_ELEVATOR);
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFECTOR);
break;
}
case ScoringThree: {
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR);
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_TOP_ENDEFECTOR);
break;
}
case ScoringFour: {
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR);
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_TOP_ENDEFECTOR);
break;
}
}
2025-01-17 19:53:14 -07:00
}
2025-02-17 15:45:59 -07:00
// public void driveElevatorStick(Translation2d stick) {
// if (stick.getNorm() > 0.05) {
// elevatorMotor.set(stick.getY());
// }
// }
2025-01-27 19:35:08 -07:00
private void periodicWaiting() {
2025-02-17 11:48:43 -07:00
if (basinBeamBreak.get()) transitionState(CoordinationState.WatingBeamTriped);
}
private void periodicWaitingTripped() {
if (basinBeamBreak.get()) transitionState(CoordinationState.Ready);
2025-01-17 19:53:14 -07:00
}
private void periodicScoring() {
2025-01-27 19:35:08 -07:00
if (!endefectorLimitSwitch.get()) transitionState(CoordinationState.Waiting);
}
2025-01-17 19:53:14 -07:00
@Override
public void periodic() {
// This method will be called once per scheduler run
2025-02-17 11:48:43 -07:00
SmartDashboard.putNumber("Basin", basinBeamBreak.get() ? 1 : 0);
2025-02-17 15:45:59 -07:00
SmartDashboard.putNumber("endefector", endefectorLimitSwitch.get() ? 1 : 0);
// if (currentState == CoordinationState.Waiting) {
// periodicWaiting();
// } else if (currentState == CoordinationState.WatingBeamTriped) {
// periodicWaitingTripped();
// }
2025-02-15 15:42:17 -07:00
// } else if (currentState == CoordinationState.ScoringThree || currentState == CoordinationState.ScoringFour) {
// periodicScoring();
// }
2025-01-17 19:53:14 -07:00
}
2025-02-17 15:45:59 -07:00
public void armShuffle(){
if(!basinBeamBreak.get()){
//shuffle the coral with the arm until coral hits beam break
}
}
2025-01-17 19:53:14 -07:00
}