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

356 lines
13 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;
2025-02-18 19:39:01 -07:00
import java.time.Instant;
2025-02-27 19:47:21 -07:00
import org.opencv.ml.RTrees;
2025-01-17 19:53:14 -07:00
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;
2025-02-26 16:05:18 -07:00
import frc4388.robot.Constants.LEDConstants;
2025-02-28 11:46:48 -07:00
import frc4388.robot.Constants.AutoConstants;
2025-02-26 16:05:18 -07:00
import frc4388.robot.subsystems.LED;
import frc4388.utility.LEDPatterns;
import frc4388.utility.Status;
import frc4388.utility.Subsystem;
2025-02-26 16:05:18 -07:00
import frc4388.utility.TimesNegativeOne;
import frc4388.utility.Status.ReportLevel;
2025-01-17 19:53:14 -07:00
public class Elevator extends Subsystem {
2025-01-17 19:53:14 -07:00
/** Creates a new Elevator. */
private TalonFX elevatorMotor;
2025-02-25 19:47:57 -07:00
private TalonFX endeffectorMotor;
2025-02-26 16:05:18 -07:00
private LED led;
2025-01-17 19:53:14 -07:00
2025-02-18 19:39:01 -07:00
private long wait = 0;
private long maxWait = 1000;
2025-02-21 20:22:07 -07:00
private double elevatorRefrence = 0;
2025-02-25 19:47:57 -07:00
private double endeffectorRefrence = 0;
2025-02-21 20:22:07 -07:00
private boolean elevatorManualStop = true;
private boolean endefectorManualStop = true;
2025-02-21 20:22:07 -07:00
2025-02-27 19:47:21 -07:00
private boolean disableAutoIntake = false;
2025-02-17 11:48:43 -07:00
private DigitalInput basinBeamBreak;
2025-02-25 19:47:57 -07:00
private DigitalInput endeffectorLimitSwitch;
2025-03-03 17:49:06 -07:00
private DigitalInput intakeIR;
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
2025-02-25 19:47:57 -07:00
WatingBeamTripped, //once the beam break trips
2025-02-17 11:48:43 -07:00
Ready, // Has coral in endefector
2025-02-18 19:39:01 -07:00
Hovering, // Has coral in endefector
2025-02-21 20:22:07 -07:00
L2Score,
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;
2025-03-03 17:49:06 -07:00
// public Elevator(TalonFX elevatorTalonFX, TalonFX endeffectorTalonFX, DigitalInput basinLimitSwitch, DigitalInput endeffectorLimitSwitch, LED led) {
public Elevator(TalonFX elevatorTalonFX, TalonFX endeffectorTalonFX, DigitalInput basinLimitSwitch, DigitalInput endeffectorLimitSwitch, DigitalInput intakeDigitalInput, LED led) {
2025-01-17 19:53:14 -07:00
elevatorMotor = elevatorTalonFX;
2025-02-25 19:47:57 -07:00
endeffectorMotor = endeffectorTalonFX;
2025-02-26 16:05:18 -07:00
this.led = led;
2025-02-17 11:48:43 -07:00
this.basinBeamBreak = basinLimitSwitch;
2025-02-25 19:47:57 -07:00
this.endeffectorLimitSwitch = endeffectorLimitSwitch;
2025-03-03 17:49:06 -07:00
this.intakeIR = intakeDigitalInput;
2025-01-17 19:53:14 -07:00
2025-01-17 20:09:14 -07:00
elevatorMotor.setNeutralMode(NeutralModeValue.Brake);
2025-02-25 19:47:57 -07:00
endeffectorMotor.setNeutralMode(NeutralModeValue.Brake);
2025-01-17 20:09:14 -07:00
elevatorMotor.getConfigurator().apply(ElevatorConstants.ELEVATOR_PID);
2025-02-25 19:47:57 -07:00
endeffectorMotor.getConfigurator().apply(ElevatorConstants.ENDEFFECTOR_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-21 20:22:07 -07:00
if (motor == elevatorMotor) elevatorRefrence = position;
2025-02-25 19:47:57 -07:00
else endeffectorRefrence = position;
2025-02-21 20:22:07 -07:00
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
}
2025-02-25 19:47:57 -07:00
public void endeffectorStop() {
endeffectorMotor.set(0);
}
2025-01-17 19:53:14 -07:00
2025-02-26 16:05:18 -07:00
2025-01-27 19:35:08 -07:00
public void transitionState(CoordinationState state) {
currentState = state;
switch (currentState) {
case Waiting: {
2025-02-18 19:39:01 -07:00
wait = System.currentTimeMillis() + maxWait;
2025-01-27 19:35:08 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_ELEVATOR);
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(LEDConstants.WAITING_PATTERN);
2025-01-27 19:35:08 -07:00
break;
}
2025-02-25 19:47:57 -07:00
case WatingBeamTripped: {
2025-02-17 11:48:43 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_BEAM_BREAK_ELEVATOR);
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(LEDConstants.DOWN_PATTERN);
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);
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(LEDConstants.DOWN_PATTERN);
2025-01-27 19:35:08 -07:00
break;
}
2025-02-18 19:39:01 -07:00
case Hovering: {
2025-03-03 16:32:27 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.HOVERING_POSITION_ELEVATOR);
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(LEDConstants.READY_PATTERN);
2025-02-18 19:39:01 -07:00
break;
}
2025-02-21 20:22:07 -07:00
case L2Score: {
2025-03-03 19:43:36 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.L2_SCORE_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.L2_SCORE_ENDEFFECTOR + AutoConstants.ARM_OFFSET_TRIM.get());
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-21 20:22:07 -07:00
break;
}
2025-02-18 19:39:01 -07:00
case PrimedFour: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_TOP_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-01-27 19:35:08 -07:00
break;
}
case ScoringFour: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.SCORING_FOUR_ENDEFFECTOR + AutoConstants.ARM_OFFSET_TRIM.get());
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-01-27 19:35:08 -07:00
break;
}
2025-02-19 21:28:28 -07:00
case PrimedThree: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.SCORING_THREE_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.PRIMED_THREE_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-19 21:28:28 -07:00
break;
}
case ScoringThree: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.SCORING_THREE_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFFECTOR + AutoConstants.ARM_OFFSET_TRIM.get());
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-19 21:28:28 -07:00
break;
}
2025-02-21 16:54:18 -07:00
case BallRemoverL2Primed: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_MIDDLE_ENDEFFECTOR);
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-21 16:54:18 -07:00
break;
}
case BallRemoverL2Go: {
2025-02-21 20:22:07 -07:00
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.DEALGAE_L2_ENDEFFECTOR + AutoConstants.ARM_OFFSET_TRIM.get());
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-21 20:22:07 -07:00
break;
}
case BallRemoverL3Primed: {
PIDPosition(elevatorMotor, ElevatorConstants.DEALGAE_L3_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.COMPLETLY_MIDDLE_ENDEFFECTOR);
2025-02-21 20:22:07 -07:00
break;
}
case BallRemoverL3Go: {
PIDPosition(elevatorMotor, ElevatorConstants.DEALGAE_L3_ELEVATOR + AutoConstants.ELEVATOR_OFFSET_TRIM.get());
2025-02-25 19:47:57 -07:00
PIDPosition(endeffectorMotor, ElevatorConstants.DEALGAE_L2_ENDEFFECTOR + AutoConstants.ARM_OFFSET_TRIM.get());
2025-02-26 16:05:18 -07:00
led.setMode(TimesNegativeOne.isRed ? LEDConstants.RED_PATTERN : LEDConstants.BLUE_PATTERN);
2025-02-21 16:54:18 -07:00
break;
}
2025-02-19 21:28:28 -07:00
default: {
assert false;
}
2025-01-27 19:35:08 -07:00
}
2025-01-17 19:53:14 -07:00
}
2025-02-27 19:47:21 -07:00
public void togggleAutoIntaking() {
disableAutoIntake = !disableAutoIntake;
}
2025-02-25 19:47:57 -07:00
public boolean elevatorAtReference() {
2025-02-21 20:22:07 -07:00
// double elevatorRefrence = elevatorMotor.getClosedLoopReference().getValueAsDouble();
2025-02-18 19:39:01 -07:00
double elevatorPosition = elevatorMotor.getPosition().getValueAsDouble();
2025-02-25 16:59:19 -07:00
double diffrence = elevatorRefrence - elevatorPosition;
2025-02-18 19:39:01 -07:00
2025-02-25 11:33:46 -07:00
boolean headedUp = diffrence < 0;
boolean forwardLimit = elevatorMotor.getForwardLimit().asSupplier().get().value == 0;
boolean reverseLimit = elevatorMotor.getReverseLimit().asSupplier().get().value == 0;
2025-02-18 19:39:01 -07:00
2025-02-25 16:59:19 -07:00
return (Math.abs(diffrence) <= 0.5 || (reverseLimit && headedUp) || (forwardLimit && !headedUp));
2025-02-25 11:33:46 -07:00
}
2025-02-18 19:39:01 -07:00
2025-02-25 19:47:57 -07:00
public boolean endeffectorAtReference() {
2025-02-21 20:22:07 -07:00
// double elevatorRefrence = endefectorMotor.getClosedLoopReference().getValueAsDouble();
2025-02-25 19:47:57 -07:00
double endeffectorPosition = endeffectorMotor.getPosition().getValueAsDouble();
2025-02-26 17:14:17 -07:00
double diffrence = endeffectorRefrence - endeffectorPosition;
2025-02-25 11:33:46 -07:00
boolean headedUp = diffrence < 0;
2025-02-26 17:14:17 -07:00
boolean forwardLimit = endeffectorMotor.getForwardLimit().asSupplier().get().value == 0;
boolean reverseLimit = endeffectorMotor.getReverseLimit().asSupplier().get().value == 0;
2025-02-18 19:39:01 -07:00
2025-02-25 16:59:19 -07:00
return (Math.abs(diffrence) <= 0.5 || (reverseLimit && headedUp) || (forwardLimit && !headedUp));
2025-02-18 19:39:01 -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-18 19:39:01 -07:00
if (!basinBeamBreak.get())
2025-02-19 21:28:28 -07:00
transitionState(CoordinationState.Ready);
2025-03-03 16:32:27 -07:00
// if(!endeffectorLimitSwitch.get())
// transitionState(CoordinationState.Hovering);
2025-02-17 11:48:43 -07:00
}
2025-02-21 20:22:07 -07:00
// private void periodicWaitingTripped() {
// if (!basinBeamBreak.get() && System.currentTimeMillis() > wait)
// transitionState(CoordinationState.Ready);
// }
2025-01-17 19:53:14 -07:00
2025-02-18 19:39:01 -07:00
private void periodicReady() {
2025-02-27 19:45:18 -07:00
if (elevatorAtReference() && !endeffectorLimitSwitch.get())
2025-02-21 20:22:07 -07:00
transitionState(CoordinationState.Hovering);
2025-02-27 19:45:18 -07:00
if(elevatorAtReference() && endeffectorLimitSwitch.get())
2025-03-03 16:32:27 -07:00
transitionState(CoordinationState.Hovering);
2025-02-18 19:39:01 -07:00
}
private void periodicScoring() {
2025-02-27 19:45:18 -07:00
if (!endeffectorLimitSwitch.get())
transitionState(CoordinationState.Waiting);
2025-02-25 19:47:57 -07:00
}
public void manualElevatorVel(double velocity) {
if (Math.abs(velocity) > 0.1) {
elevatorMotor.set(velocity);
elevatorManualStop = false;
return;
}
if (!elevatorManualStop) {
elevatorManualStop = true;
elevatorMotor.set(0);
}
2025-02-25 19:47:57 -07:00
}
public void manualEndeffectorVel(double velocity) {
if (Math.abs(velocity) > 0.1) {
endeffectorMotor.set(velocity);
endefectorManualStop = false;
return;
}
if (!endefectorManualStop) {
endefectorManualStop = true;
endeffectorMotor.set(0);
}
}
2025-01-17 19:53:14 -07:00
@Override
public void periodic() {
2025-02-18 19:39:01 -07:00
2025-01-17 19:53:14 -07:00
// 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-25 19:47:57 -07:00
SmartDashboard.putNumber("endefector", endeffectorLimitSwitch.get() ? 1 : 0);
2025-03-03 17:49:06 -07:00
SmartDashboard.putNumber("intake", intakeIR.get() ? 1 : 0);
2025-02-21 20:22:07 -07:00
SmartDashboard.putString("State", currentState.toString());
2025-02-27 19:47:21 -07:00
if (disableAutoIntake) return;
2025-02-17 15:45:59 -07:00
2025-02-18 19:39:01 -07:00
if (currentState == CoordinationState.Waiting) {
2025-02-25 11:33:46 -07:00
periodicWaiting();
2025-02-25 19:47:57 -07:00
} else if (currentState == CoordinationState.WatingBeamTripped) {
2025-02-19 21:28:28 -07:00
// periodicWaitingTripped();
2025-02-18 19:39:01 -07:00
} else if (currentState == CoordinationState.Ready) {
2025-02-21 20:22:07 -07:00
periodicReady();
2025-02-18 19:39:01 -07:00
}
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
2025-03-03 17:49:06 -07:00
public boolean isL4Primed() {
2025-02-26 15:40:29 -07:00
return currentState == CoordinationState.PrimedFour;
}
2025-03-03 17:49:06 -07:00
public boolean isL3Primed() {
2025-02-26 15:40:29 -07:00
return currentState == CoordinationState.PrimedThree;
}
2025-03-03 17:49:06 -07:00
public boolean hasCoral() {
2025-02-28 18:46:30 -07:00
return elevatorAtReference() && currentState == CoordinationState.Hovering && !endeffectorLimitSwitch.get();
}
2025-03-03 17:49:06 -07:00
public boolean readyToMove() {
// return !intakeIR.get() || hasCoral();
return hasCoral();
}
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
}
}
@Override
public String getSubsystemName() {
return "Elevator";
}
@Override
public void queryStatus() {}
@Override
public Status diagnosticStatus() {
Status status = new Status();
status.addReport(ReportLevel.INFO, "Elevator Mode: " + currentState.name());
status.diagnoseHardwareCTRE("Elevator Motor", elevatorMotor);
status.diagnoseHardwareCTRE("Endeffector Motor", endeffectorMotor);
return status;
}
2025-01-17 19:53:14 -07:00
}