|
| 1 | +package org.team1540.robot2024.subsystems.indexer; |
| 2 | + |
| 3 | +import edu.wpi.first.math.MathUtil; |
| 4 | +import edu.wpi.first.math.controller.PIDController; |
| 5 | +import edu.wpi.first.math.system.plant.DCMotor; |
| 6 | +import edu.wpi.first.wpilibj.simulation.DCMotorSim; |
| 7 | +import edu.wpi.first.wpilibj.simulation.SimDeviceSim; |
| 8 | +import org.team1540.robot2024.Constants; |
| 9 | + |
| 10 | +import static org.team1540.robot2024.Constants.Indexer.*; |
| 11 | + |
| 12 | +public class IndexerIOSim implements IndexerIO { |
| 13 | + |
| 14 | + |
| 15 | + private final DCMotorSim intakeSim = new DCMotorSim(DCMotor.getNEO(1), INTAKE_GEAR_RATIO,0.025); |
| 16 | + private final DCMotorSim feederSim = new DCMotorSim(DCMotor.getNEO(1), FEEDER_GEAR_RATIO,0.025); |
| 17 | + private final SimDeviceSim beamBreakSim = new SimDeviceSim("Indexer Beam Break"); |
| 18 | + private final PIDController feederSimPID = new PIDController(FEEDER_KP, FEEDER_KI,FEEDER_KD); |
| 19 | + |
| 20 | + @Override |
| 21 | + public void updateInputs(IndexerIOInputs inputs) { |
| 22 | + intakeSim.update(Constants.LOOP_PERIOD_SECS); |
| 23 | + feederSim.update(Constants.LOOP_PERIOD_SECS); |
| 24 | + inputs.intakeCurrentAmps = intakeSim.getCurrentDrawAmps(); |
| 25 | +// inputs.intakeVoltage = intakeSim.getBusVoltage() * intakeSim.getAppliedOutput(); |
| 26 | + inputs.intakeVelocityRPM = intakeSim.getAngularVelocityRPM(); |
| 27 | + inputs.feederCurrentAmps = feederSim.getCurrentDrawAmps(); |
| 28 | +// inputs.feederVoltage = feederSim.getBusVoltage() * feederSim.getAppliedOutput(); |
| 29 | + inputs.feederVelocityRPM = feederSim.getAngularVelocityRPM(); |
| 30 | + inputs.noteInIntake = beamBreakSim.getBoolean("Indexer Beam Break").get(); |
| 31 | + |
| 32 | + // this is a very funny line of code, and absolutely does not belong here, but I don't know how to do this otherwise |
| 33 | + feederSim.setState(feederSim.getAngularPositionRad(), feederSimPID.calculate(feederSim.getAngularVelocityRadPerSec())); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void setIntakeVoltage(double volts) { |
| 39 | + intakeSim.setInputVoltage(MathUtil.clamp(volts, -12, 12)); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void setFeederVelocity(double velocity) { |
| 44 | + feederSimPID.setSetpoint(velocity); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void setFeederVoltage(double volts) { |
| 49 | + feederSim.setInputVoltage(MathUtil.clamp(volts, -12, 12)); |
| 50 | + } |
| 51 | +} |
0 commit comments