|
| 1 | +package frc.robot.Subsystems.Intake; |
| 2 | + |
| 3 | +import static edu.wpi.first.units.Units.*; |
| 4 | +import static frc.robot.Subsystems.Intake.IntakeConstants.*; |
| 5 | + |
| 6 | +import com.ctre.phoenix6.hardware.TalonFX; |
| 7 | +import com.ctre.phoenix6.sim.TalonFXSimState; |
| 8 | +import edu.wpi.first.math.controller.PIDController; |
| 9 | +import edu.wpi.first.math.system.plant.DCMotor; |
| 10 | +import edu.wpi.first.math.system.plant.LinearSystemId; |
| 11 | +import edu.wpi.first.wpilibj.simulation.DCMotorSim; |
| 12 | +import frc.robot.GlobalConstants; |
| 13 | +import frc.robot.Subsystems.Intake.IntakeConstants.Sim; |
| 14 | + |
| 15 | +public class IntakeIOSim implements IntakeIO { |
| 16 | + |
| 17 | + private DCMotorSim spinSim; |
| 18 | + private TalonFXSimState spinSimState; |
| 19 | + private TalonFX spinMotor; |
| 20 | + |
| 21 | + private DCMotorSim linearSim; |
| 22 | + private TalonFXSimState linearSimState; |
| 23 | + private TalonFX linearMotor; |
| 24 | + private final PIDController linearController; |
| 25 | + |
| 26 | + public IntakeIOSim() { |
| 27 | + spinSim = new DCMotorSim(LinearSystemId.createDCMotorSystem(DCMotor.getFalcon500(1), Sim.MOTOR_MOI, 1), DCMotor.getFalcon500(1)); |
| 28 | + spinMotor = new TalonFX(Real.SPIN_MOTOR_ID); |
| 29 | + spinSimState = new TalonFXSimState(spinMotor); |
| 30 | + |
| 31 | + linearSim = new DCMotorSim(LinearSystemId.createDCMotorSystem(DCMotor.getFalcon500(1), Sim.MOTOR_MOI, LINEAR_GEARING), DCMotor.getFalcon500(1)); |
| 32 | + linearMotor = new TalonFX(Real.LINEAR_ACTUATOR_ID); |
| 33 | + linearSimState = new TalonFXSimState(linearMotor); |
| 34 | + linearController = new PIDController(Sim.LINEAR_PID.kP(), Sim.LINEAR_PID.kI(), Sim.LINEAR_PID.kD()); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void updateInputs(IntakeIOInputs inputs) { |
| 39 | + spinSim.update(GlobalConstants.SIMULATION_PERIOD); |
| 40 | + spinSimState.setRotorVelocity(spinSim.getAngularVelocityRPM() / 60); |
| 41 | + spinSimState.setRawRotorPosition(spinSim.getAngularPositionRotations()); |
| 42 | + spinSimState.setSupplyVoltage(12.0); |
| 43 | + |
| 44 | + linearSim.update(GlobalConstants.SIMULATION_PERIOD); |
| 45 | + linearSimState.setRotorVelocity((linearSim.getAngularVelocityRPM() / 60) * LINEAR_GEARING); |
| 46 | + linearSimState.setRawRotorPosition(linearSim.getAngularPositionRotations() * LINEAR_GEARING); |
| 47 | + linearSimState.setSupplyVoltage(12.0); |
| 48 | + |
| 49 | + inputs.spinVelocityRPS = spinMotor.getVelocity().getValue().in(RotationsPerSecond); |
| 50 | + inputs.linearPositionMeters = linearSim.getAngularPositionRotations() * LINEAR_METERS_PER_ROTATION; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setSpinVelocity(double rps, double feedforward) { |
| 55 | + spinSim.setInputVoltage(feedforward); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void setLinearPosition(double meters) { |
| 60 | + linearSim.setInputVoltage(linearController.calculate(linearSim.getAngularPositionRotations(), meters / LINEAR_METERS_PER_ROTATION)); |
| 61 | + } //pid only for sim, irl uses talon pid |
| 62 | + |
| 63 | + @Override |
| 64 | + public TalonFX getSpinMotor() { |
| 65 | + return spinMotor; |
| 66 | + } |
| 67 | +} |
0 commit comments