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

146 lines
4.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
import static edu.wpi.first.units.Units.RotationsPerSecond;
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.wpilibj2.command.SubsystemBase;
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();
Supplier<Pose2d> m_swervePoseSupplier;
public Intake(
2026-02-14 15:57:33 -07:00
IntakeIO io
2026-02-07 14:51:05 -07:00
// Supplier<Pose2d> swervePoseSupplier
2026-01-27 18:16:23 -08:00
) {
this.io = io;
2026-02-07 14:51:05 -07:00
// this.m_swervePoseSupplier = swervePoseSupplier;
2026-01-27 18:16:23 -08:00
}
public enum IntakeMode {
2026-02-09 17:18:54 -08:00
Extended,
Retracted,
2026-03-25 11:39:55 -06:00
Extending,
Retracting,
2026-03-05 23:12:50 -07:00
Idle,
2026-03-25 11:15:46 -06:00
Bouncing
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
this.state.currentBounceTime = Utils.getSystemTimeSeconds() + IntakeConstants.BOUNCE_HALF_PERIOD;
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(){
io.setRollerOutput(state, 0);
}
2026-03-19 14:21:46 -06:00
public double getRollerTarget() {
return state.rollerTargetOutput;
}
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;
// }
@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-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
io.updateInputs(state);
2026-03-25 11:15:46 -06:00
// getCurrentTime
2026-02-21 12:54:16 -08:00
switch (mode) {
case Extended:
io.setArmAngle(state, Rotations.of(IntakeConstants.ARM_LIMIT_EXTENDED.get()));
io.setRollerOutput(state, IntakeConstants.ROLLER_PERCENT_OUTPUT.get());
break;
case Retracted:
io.setArmAngle(state, Rotations.of(IntakeConstants.ARM_LIMIT_RETRACTED.get()));
io.setRollerOutput(state, 0);
break;
2026-03-25 11:39:55 -06:00
case Extending:
io.setArmAngle(state, Rotations.of(IntakeConstants.ARM_EXTEND_PERCENT_OUTPUT.get()));
io.setRollerOutput(state, IntakeConstants.ROLLER_PERCENT_OUTPUT.get());
break;
case Retracting:
io.setArmAngle(state, Rotations.of(IntakeConstants.ARM_RETRACT_PERCENT_OUTPUT.get()));
io.setRollerOutput(state, 0);
break;
2026-03-25 11:15:46 -06:00
case Bouncing:
2026-02-21 12:54:16 -08:00
io.setRollerOutput(state, 0);
2026-03-25 11:15:46 -06:00
if(
state.armMotorCurrent.in(Amps) < IntakeConstants.INTAKE_BOUNCE_CURRENT_LIMIT.get() &&
state.armMotorVelocity.in(RotationsPerSecond) < IntakeConstants.INTAKE_BOUNCE_VELOCITY_LIMIT.get()
) {
this.state.currentBounceTime = Utils.getSystemTimeSeconds() + IntakeConstants.BOUNCE_HALF_PERIOD;
}
// 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.BOUNCE_HALF_PERIOD) * IntakeConstants.INTAKE_BOUNCE_OUTPUT;
// Clamp the output of the motor to some value
percentOutput = Math.max(Math.min(percentOutput, IntakeConstants.INTAKE_BOUNCE_MAX_OUTPUT), -IntakeConstants.INTAKE_BOUNCE_MAX_OUTPUT);
io.armOutput(percentOutput);
2026-02-21 12:54:16 -08:00
break;
case Idle:
2026-03-25 11:15:46 -06:00
// io.setArmAngle(state, Rotations.of(IntakeConstants.ARM_LIMIT_RETRACTED.get()));
2026-03-05 23:12:50 -07:00
io.setRollerOutput(state, 0);
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
}
}