Files
2026KPopRobotHunters/src/main/java/frc4388/robot/subsystems/intake/Intake.java
T

216 lines
7.7 KiB
Java
Raw Normal View History

2026-01-27 18:16:23 -08:00
package frc4388.robot.subsystems.intake;
2026-03-25 11:15:46 -06:00
import static edu.wpi.first.units.Units.Amps;
2026-02-09 17:18:54 -08:00
import static edu.wpi.first.units.Units.Rotations;
2026-03-25 11:15:46 -06:00
2026-01-27 18:16:23 -08:00
import java.util.function.Supplier;
import org.littletonrobotics.junction.Logger;
2026-03-25 11:15:46 -06:00
import com.ctre.phoenix6.Utils;
2026-01-27 18:16:23 -08:00
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
2026-01-27 18:16:23 -08:00
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc4388.robot.subsystems.swerve.SwerveDrive;
2026-01-27 18:16:23 -08:00
public class Intake extends SubsystemBase {
2026-02-09 17:18:54 -08:00
public IntakeIO io;
2026-01-27 18:16:23 -08:00
IntakeStateAutoLogged state = new IntakeStateAutoLogged();
SwerveDrive m_SwerveDrive;
2026-01-27 18:16:23 -08:00
Supplier<Pose2d> m_swervePoseSupplier;
public Intake(
IntakeIO io,
SwerveDrive m_SwerveDrive
2026-02-07 14:51:05 -07:00
// Supplier<Pose2d> swervePoseSupplier
2026-01-27 18:16:23 -08:00
) {
this.io = io;
this.m_SwerveDrive = m_SwerveDrive;
2026-02-07 14:51:05 -07:00
// this.m_swervePoseSupplier = swervePoseSupplier;
2026-01-27 18:16:23 -08:00
}
public enum IntakeMode {
2026-03-30 16:15:43 -06:00
ExtendingIdle,
ExtendingRolling,
2026-04-09 19:11:53 -06:00
EncoderFix,
2026-03-25 11:39:55 -06:00
Retracting,
2026-04-07 20:37:26 -06:00
ArmIdleRollingNot,
2026-03-30 16:15:43 -06:00
2026-03-05 23:12:50 -07:00
Idle,
2026-03-31 18:53:16 -06:00
RectractTorque,
2026-04-09 07:18:28 -06:00
RectractAuto,
2026-04-03 22:08:13 -06:00
Bouncing,
2026-04-04 20:41:00 -06:00
ExpelBalls,
LabubuGrowl
2026-01-27 18:16:23 -08:00
}
private boolean overCompressed = false;
2026-01-27 18:16:23 -08:00
2026-02-21 12:54:16 -08:00
private IntakeMode mode = IntakeMode.Idle;
2026-02-10 17:33:39 -08:00
2026-01-27 18:16:23 -08:00
public void setMode(IntakeMode mode) {
2026-02-10 17:33:39 -08:00
this.mode = mode;
2026-03-25 11:15:46 -06:00
switch (mode) {
case Bouncing:
// When bounce is enabled: set the bounce timer
2026-03-28 13:30:33 -06:00
this.state.currentBounceTime = Utils.getSystemTimeSeconds() + IntakeConstants.INTAKE_BOUNCE_HALF_PERIOD.get();
2026-03-25 11:15:46 -06:00
break;
default:
break;
}
2026-01-27 18:16:23 -08:00
}
2026-02-10 18:42:47 -08:00
public IntakeMode getMode() {
return mode;
}
2026-02-25 15:33:01 -08:00
public void rollerStop(){
2026-04-04 13:02:50 -06:00
this.mode = IntakeMode.Idle;
// io.setRollerOutput(state, 0);
2026-02-25 15:33:01 -08:00
}
2026-03-19 14:21:46 -06:00
public double getRollerTarget() {
return state.rollerTargetOutput;
}
2026-04-04 13:03:19 -06:00
public boolean intakeAtReference() {
2026-04-09 09:28:35 -06:00
return state.extendedSoftLimit;
2026-04-04 13:03:19 -06:00
}
2026-03-19 14:21:46 -06:00
public double getRollerSpeed() {
return state.rollerOutput;
}
2026-01-27 18:16:23 -08:00
// public enum FieldZone {
// // The robot should aim at the hub
// InShootZone,
// // The robot should aim towards the wall
// AimAtWall,
// }
// // Calculate what should be done based off of the position of the robot
// // TODO: Implement field zones
// public FieldZone getTarget(Pose2d position) {
// return FieldZone.InShootZone;
// }
2026-03-31 18:53:16 -06:00
2026-01-27 18:16:23 -08:00
@Override
public void periodic() {
// FaultReporter.register(this); // TODO Implement fault reporter
2026-02-14 10:55:51 -08:00
// System.out.println(m_armLimitSwitch.get());
2026-04-04 20:41:00 -06:00
// ChassisSpeeds chassisSpeeds = m_SwerveDrive.chassisSpeeds;
2026-01-27 18:16:23 -08:00
2026-04-04 20:41:00 -06:00
// double ChassisOverallSpeed = Math.hypot(chassisSpeeds.vxMetersPerSecond, chassisSpeeds.vyMetersPerSecond);
2026-01-27 18:16:23 -08:00
Logger.processInputs("Intake", state);
2026-02-20 20:59:06 -08:00
Logger.recordOutput("Intake/IntakeState", this.mode);
2026-01-27 18:16:23 -08:00
2026-03-31 18:53:16 -06:00
2026-01-27 18:16:23 -08:00
io.updateInputs(state);
2026-04-04 20:41:00 -06:00
// overCompressed =
// Math.abs(state.armMotorCurrent.in(Amps)) > IntakeConstants.INTAKE_BOUNCE_CURRENT_LIMIT.get();
// // Math.abs(state.armMotorVelocity.in(RotationsPerSecond)) < IntakeConstants.INTAKE_BOUNCE_VELOCITY_LIMIT.get();
2026-04-04 20:41:00 -06:00
// Logger.recordOutput("overCompressed", overCompressed);
2026-03-25 11:15:46 -06:00
// getCurrentTime
2026-02-21 12:54:16 -08:00
switch (mode) {
2026-03-30 16:15:43 -06:00
case ExtendingIdle:
2026-04-17 17:27:23 -06:00
// io.armOutput(IntakeConstants.ARM_EXTEND_PERCENT_OUTPUT.get());
2026-03-30 16:15:43 -06:00
io.setRollerOutput(state, 0);
break;
2026-04-09 19:11:53 -06:00
2026-03-30 16:15:43 -06:00
case ExtendingRolling:
2026-04-17 17:27:23 -06:00
// io.armOutput(IntakeConstants.ARM_EXTEND_PERCENT_OUTPUT.get());
2026-04-06 22:44:17 -06:00
io.setRollerOutput(state, IntakeConstants.ROLLER_PERCENT_OUTPUT.get()); //getTargetRollerSpeed(ChassisOverallSpeed));
2026-03-25 11:39:55 -06:00
break;
2026-03-30 16:15:43 -06:00
2026-04-09 19:11:53 -06:00
case EncoderFix:
2026-04-17 17:27:23 -06:00
// io.armFix(IntakeConstants.FIX_ARM_PERCENT_OUTPUT.get());
2026-04-09 19:11:53 -06:00
io.setRollerOutput(state, 0);
break;
2026-03-25 11:39:55 -06:00
case Retracting:
2026-04-17 17:27:23 -06:00
// io.armOutput(IntakeConstants.ARM_RETRACT_PERCENT_OUTPUT.get());
2026-03-30 16:15:43 -06:00
2026-04-03 22:08:13 -06:00
// if(state.intakeEncoder.in(Rotations) > IntakeConstants.ARM_REVERSE_ROLLER_RANGE.get()) {
io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
// } else {
// io.setRollerOutput(state, 0);
// }
2026-03-25 11:39:55 -06:00
break;
2026-04-07 20:37:26 -06:00
case ArmIdleRollingNot:
2026-04-17 17:27:23 -06:00
// io.armOutput(0);
2026-04-07 20:37:26 -06:00
io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
break;
2026-03-25 11:15:46 -06:00
case Bouncing:
2026-03-30 19:52:57 -06:00
// io.setRollerOutput(state, 0);
2026-03-25 11:15:46 -06:00
2026-04-17 17:27:23 -06:00
// if(
// state.armMotorCurrent.in(Amps) > IntakeConstants.INTAKE_BOUNCE_CURRENT_LIMIT.get()
// // Math.abs(state.armMotorVelocity.in(RotationsPerSecond)) < IntakeConstants.INTAKE_BOUNCE_VELOCITY_LIMIT.get()
// ) {
// this.state.currentBounceTime = Utils.getSystemTimeSeconds() + IntakeConstants.INTAKE_BOUNCE_HALF_PERIOD.get();
// }
// // Get the time delta from the last bounce time update
// double currentTime = Utils.getSystemTimeSeconds() - state.currentBounceTime;
// // Get the percentage through the bounce period (0 output means one half period has passed)
// double percentOutput = (currentTime / IntakeConstants.INTAKE_BOUNCE_HALF_PERIOD.get()) * IntakeConstants.INTAKE_BOUNCE_OUTPUT.get();
// // Clamp the output of the motor to some value
// percentOutput = -Math.max(Math.min(percentOutput, IntakeConstants.INTAKE_BOUNCE_MAX_OUTPUT.get()), -IntakeConstants.INTAKE_BOUNCE_MAX_OUTPUT.get());
// io.armOutput(percentOutput);
// if(percentOutput < 0) {
// io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
// } else {
// io.setRollerOutput(state, 0);
// }
// break;
// case RectractTorque:
// io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
// if (!overCompressed){
// io.armOutput(IntakeConstants.ARM_SQUEEZE_PERCENT_OUTPUT.get());
// } else {
// io.armOutput(IntakeConstants.ARM_REDUCED_SQUEEZE_PERCENT_OUTPUT.get());
// }
2026-04-03 22:08:13 -06:00
// if(state.intakeEncoder.in(Rotations) > IntakeConstants.ARM_REVERSE_ROLLER_RANGE.get()) {
// io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
// } else {
// io.setRollerOutput(state, 0);
// }
2026-02-21 12:54:16 -08:00
break;
2026-04-09 07:18:28 -06:00
case RectractAuto:
io.setRollerOutput(state, IntakeConstants.ROLLER_RETRACT_PERCENT_OUTPUT.get());
2026-04-17 17:27:23 -06:00
// io.armOutput(IntakeConstants.ARM_AUTO_OUTPUT.get());
2026-04-09 07:18:28 -06:00
break;
2026-02-21 12:54:16 -08:00
case Idle:
2026-04-17 17:27:23 -06:00
// io.armOutput(0);
2026-03-30 16:15:43 -06:00
io.setRollerOutput(state, 0);
2026-03-05 23:12:50 -07:00
break;
2026-04-03 22:08:13 -06:00
case ExpelBalls:
2026-04-17 17:27:23 -06:00
// io.armOutput(0);
2026-04-04 13:02:50 -06:00
io.setRollerOutput(state, IntakeConstants.ROLLER_EJECT_PERCENT_OUTPUT.get());
2026-04-03 22:08:13 -06:00
break;
2026-04-04 20:41:00 -06:00
case LabubuGrowl:
2026-04-17 17:27:23 -06:00
// io.armOutput(IntakeConstants.ARM_EXTEND_PERCENT_OUTPUT.get());
2026-04-04 20:41:00 -06:00
io.setRollerOutput(state, IntakeConstants.ROLLER_LABUBU_GROWL_PERCENT_OUTPUT.get());
break;
2026-02-21 12:54:16 -08:00
}
2026-02-17 18:53:26 -08:00
// if (state.retractedLimit){
// this.mode = IntakeMode.Retracted;
// }
2026-02-10 17:33:39 -08:00
2026-01-27 18:16:23 -08:00
}
}