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;
|
|
|
|
|
import com.ctre.phoenix6.controls.PositionVoltage;
|
|
|
|
|
import com.ctre.phoenix6.hardware.TalonFX;
|
|
|
|
|
import com.ctre.phoenix6.signals.NeutralModeValue;
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
import edu.wpi.first.wpilibj.DigitalInput;
|
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;
|
2025-01-27 17:29:25 -07:00
|
|
|
private TalonFX endefectorMotor;
|
2025-01-17 19:53:14 -07:00
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
private DigitalInput basinLimitSwitch;
|
|
|
|
|
private DigitalInput endefectorLimitSwitch;
|
|
|
|
|
|
|
|
|
|
public enum CordinationState {
|
|
|
|
|
Waiting, // for coral into the though
|
|
|
|
|
Ready, // Has coral in enefector
|
|
|
|
|
ScoringThree, // Arm and elevator in the level three position
|
|
|
|
|
ScoringFour // Arm and elevator in the level four position
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CordinationState currentState;
|
|
|
|
|
|
|
|
|
|
public Elevator(TalonFX elevatorTalonFX, TalonFX endefectorTalonFX, DigitalInput basinLimitSwitch, DigitalInput endefectorLimitSwitch) {
|
2025-01-17 19:53:14 -07:00
|
|
|
elevatorMotor = elevatorTalonFX;
|
2025-01-27 17:29:25 -07:00
|
|
|
endefectorMotor = endefectorTalonFX;
|
|
|
|
|
|
|
|
|
|
this.basinLimitSwitch = basinLimitSwitch;
|
|
|
|
|
this.endefectorLimitSwitch = endefectorLimitSwitch;
|
2025-01-17 19:53:14 -07:00
|
|
|
|
2025-01-17 20:09:14 -07:00
|
|
|
elevatorMotor.setNeutralMode(NeutralModeValue.Brake);
|
2025-01-27 17:29:25 -07:00
|
|
|
endefectorMotor.setNeutralMode(NeutralModeValue.Brake);
|
2025-01-17 20:09:14 -07:00
|
|
|
|
|
|
|
|
elevatorMotor.getConfigurator().apply(ElevatorConstants.ELEVATOR_PID);
|
2025-01-27 17:29:25 -07:00
|
|
|
endefectorMotor.getConfigurator().apply(ElevatorConstants.ENDEFECTOR_PID);
|
|
|
|
|
currentState = CordinationState.Ready;
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//PID methods
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
private void PIDPosition(TalonFX motor, double position) {
|
2025-01-17 19:53:14 -07:00
|
|
|
var request = new PositionVoltage(position);
|
|
|
|
|
elevatorMotor.setControl(request);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
public void elevatorStop() {
|
|
|
|
|
elevatorMotor.set(0);
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
public void endefectorStop() {
|
|
|
|
|
endefectorMotor.set(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transitionWaiting() {
|
|
|
|
|
PIDPosition(elevatorMotor, ElevatorConstants.WAITING_POSITION_ELEVATOR);
|
|
|
|
|
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFECTOR);
|
|
|
|
|
currentState = CordinationState.Waiting;
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
private void periodicWaiting() {
|
|
|
|
|
if (basinLimitSwitch.get()) transitionReady();
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
public void transitionReady() {
|
|
|
|
|
PIDPosition(elevatorMotor, ElevatorConstants.GROUND_POSITION_ELEVATOR);
|
|
|
|
|
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_DOWN_ENDEFECTOR);
|
|
|
|
|
currentState = CordinationState.Ready;
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
public void transitionScoringThree() {
|
|
|
|
|
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR);
|
|
|
|
|
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_TOP_ENDEFECTOR);
|
|
|
|
|
currentState = CordinationState.ScoringThree;
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:29:25 -07:00
|
|
|
private void periodicScoring() {
|
|
|
|
|
if (!endefectorLimitSwitch.get()) transitionWaiting();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transitionScoringFour() {
|
|
|
|
|
PIDPosition(elevatorMotor, ElevatorConstants.MAX_POSITION_ELEVATOR);
|
|
|
|
|
PIDPosition(endefectorMotor, ElevatorConstants.COMPLETLY_TOP_ENDEFECTOR);
|
|
|
|
|
currentState = CordinationState.ScoringFour;
|
|
|
|
|
}
|
2025-01-17 19:53:14 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
|
|
|
|
// This method will be called once per scheduler run
|
2025-01-27 17:29:25 -07:00
|
|
|
if (currentState == CordinationState.Waiting) {
|
|
|
|
|
periodicWaiting();
|
|
|
|
|
} else if (currentState == CordinationState.ScoringThree || currentState == CordinationState.ScoringFour) {
|
|
|
|
|
periodicScoring();
|
|
|
|
|
}
|
2025-01-17 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
}
|