|
1 | 1 | package frc.robot.subsystems.Arm; |
2 | 2 |
|
3 | | -public class ArmIOSim implements ArmIO {} |
| 3 | +import edu.wpi.first.math.MathUtil; |
| 4 | +import edu.wpi.first.math.controller.ArmFeedforward; |
| 5 | +import edu.wpi.first.math.controller.ProfiledPIDController; |
| 6 | +import edu.wpi.first.math.system.plant.DCMotor; |
| 7 | +import edu.wpi.first.math.trajectory.TrapezoidProfile; |
| 8 | +import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim; |
| 9 | + |
| 10 | +public class ArmIOSim implements ArmIO { |
| 11 | + private final DCMotor m_armGearbox = DCMotor.getNEO(1); |
| 12 | + private final ProfiledPIDController m_controller; |
| 13 | + |
| 14 | + private SingleJointedArmSim sim = |
| 15 | + new SingleJointedArmSim( |
| 16 | + m_armGearbox, |
| 17 | + CoralPivotConstants.CoralPivotSimConstants.kArmReduction, |
| 18 | + SingleJointedArmSim.estimateMOI( |
| 19 | + CoralPivotConstants.CoralPivotSimConstants.kArmLength, |
| 20 | + CoralPivotConstants.CoralPivotSimConstants.kArmMass), |
| 21 | + CoralPivotConstants.CoralPivotSimConstants.kArmLength, |
| 22 | + CoralPivotConstants.CoralPivotSimConstants.kMinAngleRads, |
| 23 | + CoralPivotConstants.CoralPivotSimConstants.kMaxAngleRads, |
| 24 | + true, |
| 25 | + 0.1); |
| 26 | + public ArmIOSim() { |
| 27 | + m_controller = |
| 28 | + new ProfiledPIDController( |
| 29 | + CoralPivotConstants.CoralPivotSimConstants.kPivotSimPID[0], |
| 30 | + CoralPivotConstants.CoralPivotSimConstants.kPivotSimPID[1], |
| 31 | + CoralPivotConstants.CoralPivotSimConstants.kPivotSimPID[2], |
| 32 | + new TrapezoidProfile.Constraints(2.85, 15)); |
| 33 | + |
| 34 | + |
| 35 | + m_controller.setTolerance(0.1, 0.05); |
| 36 | + } |
| 37 | + @Override |
| 38 | + public void updateInputs(ArmIOInputs inputs){ |
| 39 | + sim.update(0.02); |
| 40 | + inputs.angVelocityRadsPerSec = sim.getVelocityRadPerSec(); |
| 41 | + inputs.angleRads = sim.getAngleRads(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setVoltage(double motorVolts){ |
| 46 | + sim.setInputVoltage(motorVolts); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void setPosition(double position){ |
| 51 | + double PIDOutput = m_controller.calculate(sim.getAngleRads(), position); |
| 52 | + sim.setInputVoltage(MathUtil.clamp(PIDOutput, -12, 12)); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void stop() { |
| 58 | + sim.setInputVoltage(0); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +} |
0 commit comments