|
| 1 | +// Copyright (c) 2025 FRC 6423 - Ward Melville Iron Patriots |
| 2 | +// https://github.com/wmironpatriots |
| 3 | +// |
| 4 | +// Open Source Software; you can modify and/or share it under the terms of |
| 5 | +// MIT license file in the root directory of this project |
| 6 | + |
| 7 | +package org.frc6423.robot.subsystems.superstructure.elevator; |
| 8 | + |
| 9 | +import static edu.wpi.first.units.Units.Kilograms; |
| 10 | +import static edu.wpi.first.units.Units.Meters; |
| 11 | +import static edu.wpi.first.units.Units.MetersPerSecond; |
| 12 | +import static org.frc6423.robot.subsystems.superstructure.elevator.Elevator.*; |
| 13 | + |
| 14 | +import edu.wpi.first.math.MathUtil; |
| 15 | +import edu.wpi.first.math.controller.ProfiledPIDController; |
| 16 | +import edu.wpi.first.math.system.plant.DCMotor; |
| 17 | +import edu.wpi.first.math.trajectory.TrapezoidProfile; |
| 18 | +import edu.wpi.first.wpilibj.simulation.ElevatorSim; |
| 19 | + |
| 20 | +/** Simulated {@link ElevatorIOReal} */ |
| 21 | +public class ElevatorIOSim implements ElevatorIO { |
| 22 | + private final DCMotor model = DCMotor.getKrakenX60Foc(2); |
| 23 | + private final ElevatorSim sim = |
| 24 | + new ElevatorSim( |
| 25 | + model, |
| 26 | + GEAR_REDUCTION, |
| 27 | + LIFT_MASS.in(Kilograms), |
| 28 | + DRUM_RADIUS.in(Meters), |
| 29 | + 0.0, |
| 30 | + MAX_EXTENSION_HEIGHT.in(Meters), |
| 31 | + true, |
| 32 | + 0.0); |
| 33 | + |
| 34 | + private double appliedVolts; |
| 35 | + |
| 36 | + private final ProfiledPIDController feedback = |
| 37 | + new ProfiledPIDController(0.0, 0.0, 0.0, new TrapezoidProfile.Constraints(0.0, 0.0)); |
| 38 | + |
| 39 | + public ElevatorIOSim() {} |
| 40 | + |
| 41 | + @Override |
| 42 | + public void periodic() { |
| 43 | + sim.setInputVoltage(appliedVolts); |
| 44 | + sim.update(0.02); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public double getParentPoseMeters() { |
| 49 | + return sim.getPositionMeters(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public double getChildPoseMeters() { |
| 54 | + return sim.getPositionMeters(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public double getSetpointPoseMeters() { |
| 59 | + return feedback.getSetpoint().position; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public double getParentStatorCurrentAmps() { |
| 64 | + return sim.getCurrentDrawAmps(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public double getChildStatorCurrentAmps() { |
| 69 | + return sim.getCurrentDrawAmps(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public double getParentTempCelsius() { |
| 74 | + return 0.0; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public double getChildTempCelsius() { |
| 79 | + return 0.0; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void resetEncoders(double poseMeters) {} |
| 84 | + |
| 85 | + @Override |
| 86 | + public void setVolts(double volts) { |
| 87 | + appliedVolts = MathUtil.clamp(volts, -12.0, 12.0); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void setPose(double poseMeters, double accelerationMpsSqrd) { |
| 92 | + // Clamp pose in range |
| 93 | + poseMeters = MathUtil.clamp(poseMeters, 0.0, MAX_EXTENSION_HEIGHT.in(Meters)); |
| 94 | + // Set new accel |
| 95 | + feedback.setConstraints( |
| 96 | + new TrapezoidProfile.Constraints(MAX_VELOCITY.in(MetersPerSecond), accelerationMpsSqrd)); |
| 97 | + var ffOut = feedback.calculate(getParentPoseMeters(), poseMeters); |
| 98 | + |
| 99 | + setVolts(ffOut); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void close() throws Exception {} |
| 104 | +} |
0 commit comments