22
33import edu .wpi .first .math .controller .ElevatorFeedforward ;
44import edu .wpi .first .math .controller .PIDController ;
5+ import edu .wpi .first .math .numbers .N1 ;
6+ import edu .wpi .first .math .numbers .N2 ;
7+ import edu .wpi .first .math .system .LinearSystem ;
58import edu .wpi .first .math .system .plant .DCMotor ;
9+ import edu .wpi .first .math .system .plant .LinearSystemId ;
610import edu .wpi .first .math .trajectory .TrapezoidProfile ;
711import edu .wpi .first .wpilibj .simulation .ElevatorSim ;
12+ import org .curtinfrc .frc2025 .util .FeedbackAnalysis ;
13+ import org .curtinfrc .frc2025 .util .FeedbackAnalysis .FeedbackGains ;
814
915public class ElevatorIOSim implements ElevatorIO {
1016 private static final double KV = 3.35 ;
1117 private static final double KA = 0.02 ;
1218 private static final double KG = 0.12 ;
1319
20+ private final LinearSystem <N2 , N1 , N2 > elevatorSystem =
21+ LinearSystemId .identifyPositionSystem (KV , KA );
1422 private final ElevatorSim elevatorSim =
15- new ElevatorSim (KV , KA , DCMotor .getKrakenX60Foc (2 ), 0 , 0.62 , true , 0 );
23+ new ElevatorSim (elevatorSystem , DCMotor .getKrakenX60Foc (2 ), 0 , 0.62 , true , 0 );
24+ private final FeedbackGains gains =
25+ FeedbackAnalysis .getPositionGains (elevatorSystem , 12 , 0.005 , 0.1 , 0.02 , 0.02 );
1626 private double volts = 0 ;
1727
1828 private final TrapezoidProfile .Constraints constraints =
1929 new TrapezoidProfile .Constraints (3.53 , 48.74 );
2030 private final TrapezoidProfile profile = new TrapezoidProfile (constraints );
2131
22- private final PIDController pid = new PIDController (0 , 0 , 0 );
32+ private final PIDController pid = new PIDController (gains . kP () , 0 , gains . kD () );
2333 private final ElevatorFeedforward ff = new ElevatorFeedforward (0 , KG , KV , KA );
2434
2535 @ Override
@@ -39,12 +49,11 @@ public void setVoltage(double volts) {
3949
4050 @ Override
4151 public void setPosition (double positionMetres ) {
52+ var currentState =
53+ new TrapezoidProfile .State (
54+ elevatorSim .getPositionMeters (), elevatorSim .getVelocityMetersPerSecond ());
4255 var setpoint =
43- profile .calculate (
44- 0.02 ,
45- new TrapezoidProfile .State (
46- elevatorSim .getPositionMeters (), elevatorSim .getVelocityMetersPerSecond ()),
47- new TrapezoidProfile .State (positionMetres , 0 ));
56+ profile .calculate (0.02 , currentState , new TrapezoidProfile .State (positionMetres , 0 ));
4857 setVoltage (
4958 pid .calculate (elevatorSim .getPositionMeters (), setpoint .position )
5059 + ff .calculateWithVelocities (
0 commit comments