2026-01-27 18:16:23 -08:00
|
|
|
package frc4388.robot.subsystems.intake;
|
|
|
|
|
|
2026-03-25 11:39:55 -06:00
|
|
|
import static edu.wpi.first.units.Units.Amps;
|
2026-02-27 20:19:01 -08:00
|
|
|
import static edu.wpi.first.units.Units.Rotations;
|
2026-03-28 09:59:30 -06:00
|
|
|
import static edu.wpi.first.units.Units.RotationsPerSecond;
|
2026-02-16 15:57:24 -07:00
|
|
|
|
2026-01-27 18:16:23 -08:00
|
|
|
import com.ctre.phoenix6.hardware.TalonFX;
|
2026-03-28 15:56:54 -06:00
|
|
|
import com.revrobotics.PersistMode;
|
|
|
|
|
import com.revrobotics.ResetMode;
|
2026-03-25 11:39:55 -06:00
|
|
|
import com.revrobotics.spark.SparkMax;
|
2026-01-27 18:16:23 -08:00
|
|
|
|
2026-02-27 20:19:01 -08:00
|
|
|
import edu.wpi.first.units.measure.Angle;
|
2026-03-28 13:30:33 -06:00
|
|
|
import frc4388.utility.compute.JankCoder;
|
2026-01-27 18:16:23 -08:00
|
|
|
|
|
|
|
|
public class IntakeReal implements IntakeIO {
|
|
|
|
|
|
2026-03-28 09:59:30 -06:00
|
|
|
SparkMax m_armMotor;
|
2026-04-01 17:58:15 -06:00
|
|
|
TalonFX m_rollerMotor;
|
2026-03-28 13:30:33 -06:00
|
|
|
JankCoder m_encoder;
|
2026-02-09 16:03:48 -08:00
|
|
|
|
2026-01-27 18:16:23 -08:00
|
|
|
public IntakeReal(
|
2026-03-28 09:59:30 -06:00
|
|
|
SparkMax armMotor,
|
2026-04-01 17:58:15 -06:00
|
|
|
TalonFX rollerMotor,
|
2026-03-28 13:30:33 -06:00
|
|
|
JankCoder jankCoder
|
2026-01-27 18:16:23 -08:00
|
|
|
) {
|
|
|
|
|
// m_angleMotor = angleMotor;
|
|
|
|
|
// m_pitchMotor = pitchMotor;
|
|
|
|
|
m_armMotor = armMotor;
|
|
|
|
|
m_rollerMotor = rollerMotor;
|
2026-03-28 13:30:33 -06:00
|
|
|
m_encoder = jankCoder;
|
2026-03-28 15:56:54 -06:00
|
|
|
|
|
|
|
|
m_armMotor.configure(IntakeConstants.ARM_MOTOR_CONFIG, ResetMode.kNoResetSafeParameters, PersistMode.kPersistParameters);
|
2026-04-01 17:58:15 -06:00
|
|
|
m_rollerMotor.getConfigurator().apply(IntakeConstants.ROLLER_MOTOR_CONFIG);
|
2026-01-27 18:16:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2026-02-17 18:53:26 -08:00
|
|
|
public void setRollerOutput(IntakeState state, double rollerOutput) {
|
|
|
|
|
state.rollerTargetOutput = rollerOutput;
|
2026-02-27 20:19:01 -08:00
|
|
|
|
2026-02-17 18:53:26 -08:00
|
|
|
m_rollerMotor.set(rollerOutput);
|
2026-01-27 18:16:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setArmAngle(IntakeState state, Angle angle) {
|
2026-02-16 15:57:24 -07:00
|
|
|
|
2026-01-27 18:16:23 -08:00
|
|
|
state.armTargetAngle = angle;
|
|
|
|
|
// Assume that the angle is always accurate, because I think we will use a shaft encoder
|
|
|
|
|
// Assume that 0 degrees = forwards. Might need an offset here
|
2026-02-09 16:03:48 -08:00
|
|
|
|
2026-02-10 18:42:47 -08:00
|
|
|
// angle = clampAng(angle, IntakeConstants.ARM_LIMIT_RETRACTED, IntakeConstants.ARM_LIMIT_EXTENDED);
|
2026-02-09 16:03:48 -08:00
|
|
|
|
2026-01-27 18:16:23 -08:00
|
|
|
// (REAL_ROT) * (MOTOR_ROT / REAL_ROT) = MOTOR_ROT
|
2026-02-10 17:33:39 -08:00
|
|
|
Angle motorAngle = angle.times(IntakeConstants.ARM_MOTOR_GEAR_RATIO);
|
2026-02-09 16:03:48 -08:00
|
|
|
|
|
|
|
|
// PositionDutyCycle posRequest = new PositionDutyCycle(motorTargetAngle);
|
2026-03-28 09:59:30 -06:00
|
|
|
// m_armMotor.setControl(
|
|
|
|
|
// armPosition
|
|
|
|
|
// .withPosition(motorAngle)
|
|
|
|
|
// .withLimitReverseMotion(!m_armLimitSwitch.get())
|
|
|
|
|
// );
|
2026-02-27 20:19:01 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 10:55:51 -08:00
|
|
|
@Override
|
|
|
|
|
public void stopArm(){
|
|
|
|
|
m_armMotor.set(0);
|
2026-02-18 16:36:02 -08:00
|
|
|
// m_rollerMotor.set(0);
|
2026-02-14 10:55:51 -08:00
|
|
|
}
|
2026-02-16 16:19:51 -07:00
|
|
|
|
2026-03-28 13:30:33 -06:00
|
|
|
private boolean retractedLimit() {
|
2026-03-28 15:56:54 -06:00
|
|
|
return m_encoder.get() <= IntakeConstants.ARM_LIMIT_RETRACTED.get();
|
2026-03-28 13:30:33 -06:00
|
|
|
}
|
|
|
|
|
private boolean extendedLimit() {
|
2026-03-28 15:56:54 -06:00
|
|
|
return m_encoder.get() >= IntakeConstants.ARM_LIMIT_EXTENDED.get();
|
2026-03-28 13:30:33 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 15:00:40 -07:00
|
|
|
@Override
|
2026-02-16 15:57:24 -07:00
|
|
|
public void armOutput(double percentOutput){
|
2026-03-28 13:30:33 -06:00
|
|
|
|
2026-03-28 15:56:54 -06:00
|
|
|
// if(retractedLimit()) {
|
|
|
|
|
// percentOutput = Math.max(percentOutput, 0);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (extendedLimit()) {
|
2026-03-28 13:30:33 -06:00
|
|
|
percentOutput = Math.min(percentOutput, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 09:59:30 -06:00
|
|
|
m_armMotor.set(percentOutput);
|
2026-03-28 13:30:33 -06:00
|
|
|
|
2026-02-14 15:00:40 -07:00
|
|
|
}
|
2026-01-27 18:16:23 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateInputs(IntakeState state) {
|
2026-03-28 15:56:54 -06:00
|
|
|
m_encoder.update();
|
|
|
|
|
|
|
|
|
|
|
2026-03-28 09:59:30 -06:00
|
|
|
state.armAngle = Rotations.of(m_armMotor.getEncoder().getPosition()).div(IntakeConstants.ARM_MOTOR_GEAR_RATIO);
|
|
|
|
|
state.armMotorVelocity = RotationsPerSecond.of(m_armMotor.getEncoder().getVelocity()).div(IntakeConstants.ARM_MOTOR_GEAR_RATIO);
|
|
|
|
|
// state.armMotorAcceleration = RotationsPerSecondPerSecond.of(m_armMotor.getEncoder().ge);
|
|
|
|
|
state.armMotorCurrent = Amps.of(m_armMotor.getOutputCurrent());
|
|
|
|
|
|
2026-02-17 18:53:26 -08:00
|
|
|
state.rollerOutput = m_rollerMotor.get();
|
2026-04-02 18:27:51 -06:00
|
|
|
state.rollerMotorCurrent = m_rollerMotor.getStatorCurrent().getValue();
|
2026-02-17 18:53:26 -08:00
|
|
|
|
2026-03-28 15:56:54 -06:00
|
|
|
state.retractedSoftLimit = retractedLimit();
|
|
|
|
|
state.extendedSoftLimit = extendedLimit();
|
2026-03-28 13:30:33 -06:00
|
|
|
|
2026-03-28 15:56:54 -06:00
|
|
|
state.intakeEncoder = m_encoder.getRotations();
|
2026-03-30 18:57:14 -06:00
|
|
|
state.encoderConnected = m_encoder.isConnected();
|
|
|
|
|
|
2026-03-28 15:56:54 -06:00
|
|
|
state.retractedLimitSwitch = m_armMotor.getReverseLimitSwitch().isPressed();
|
2026-03-28 13:30:33 -06:00
|
|
|
|
2026-03-28 15:56:54 -06:00
|
|
|
if(state.retractedLimitSwitch) {
|
|
|
|
|
m_encoder.resetRotations();
|
|
|
|
|
}
|
2026-02-09 17:18:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateGains() {
|
2026-03-30 18:57:14 -06:00
|
|
|
m_encoder.loadRotations();
|
2026-02-09 16:03:48 -08:00
|
|
|
|
2026-03-28 09:59:30 -06:00
|
|
|
// IntakeConstants.ARM_PID.kP = IntakeConstants.arm_kP.get();
|
|
|
|
|
// IntakeConstants.ARM_PID.kI = IntakeConstants.arm_kI.get();
|
|
|
|
|
// IntakeConstants.ARM_PID.kD = IntakeConstants.arm_kD.get();
|
|
|
|
|
// m_armMotor.getConfigurator().apply(IntakeConstants.ARM_PID);
|
2026-02-09 16:03:48 -08:00
|
|
|
|
2026-01-27 18:16:23 -08:00
|
|
|
}
|
2026-01-27 16:46:12 -07:00
|
|
|
}
|