1
1
package org .codeorange .frc2024 .utility .simulation ;
2
2
3
+ import edu .wpi .first .math .MathUtil ;
3
4
import edu .wpi .first .math .Matrix ;
5
+ import edu .wpi .first .math .controller .ArmFeedforward ;
6
+ import edu .wpi .first .math .controller .SimpleMotorFeedforward ;
4
7
import edu .wpi .first .math .numbers .N1 ;
5
8
import edu .wpi .first .math .numbers .N2 ;
6
9
import edu .wpi .first .math .system .LinearSystem ;
7
10
import edu .wpi .first .math .system .plant .DCMotor ;
11
+ import edu .wpi .first .math .util .Units ;
8
12
import edu .wpi .first .wpilibj .simulation .DCMotorSim ;
13
+ import org .codeorange .frc2024 .utility .wpimodified .PIDController ;
9
14
10
15
/**
11
- * DCMotorSim but stores input voltage because i am lazy
16
+ * DCMotorSim but stores input voltage and has built in gain calculation because i am lazy
12
17
*/
13
18
public class BetterDCMotorSim extends DCMotorSim {
14
19
private double inputVoltage = 0 ;
20
+ private PIDController pidController = new PIDController (0 , 0 , 0 );
21
+ private SimpleMotorFeedforward feedforward = new SimpleMotorFeedforward (0 , 0 , 0 );
22
+
23
+ public void setPID (PIDController controller ) {
24
+ pidController = controller ;
25
+ }
26
+
27
+ public void setFeedforward (SimpleMotorFeedforward ff ) {
28
+ feedforward = ff ;
29
+ }
30
+
31
+ public void setGains (PIDController controller , SimpleMotorFeedforward ff ) {
32
+ setPID (controller );
33
+ setFeedforward (ff );
34
+ }
15
35
16
36
public BetterDCMotorSim (LinearSystem <N2 , N1 , N2 > plant , DCMotor gearbox , double gearing ) {
17
37
super (plant , gearbox , gearing );
@@ -29,13 +49,41 @@ public BetterDCMotorSim(DCMotor gearbox, double gearing, double jKgMetersSquared
29
49
super (gearbox , gearing , jKgMetersSquared , measurementStdDevs );
30
50
}
31
51
52
+ public BetterDCMotorSim (DCMotor gearbox , double gearing , double jKgMetersSquared , PIDController pidController , SimpleMotorFeedforward feedforward ) {
53
+ this (gearbox , gearing , jKgMetersSquared );
54
+ this .pidController = pidController ;
55
+ this .feedforward = feedforward ;
56
+ }
57
+
32
58
@ Override
33
59
public void setInputVoltage (double volts ) {
34
- inputVoltage = volts ;
35
- setInput (volts );
60
+ inputVoltage = MathUtil . clamp ( volts , - 12 , 12 ) ;
61
+ setInput (inputVoltage );
36
62
}
37
63
38
64
public double getInputVoltage () {
39
65
return inputVoltage ;
40
66
}
67
+
68
+ public void setPosition (double setpoint ) {
69
+ setPosition (setpoint , 0 );
70
+ }
71
+
72
+ public void setPosition (double setpoint , double ffVolts ) {
73
+ setInputVoltage (
74
+ pidController .calculate (getAngularPositionRotations (), setpoint )
75
+ + ffVolts
76
+ );
77
+ }
78
+
79
+ public void setVelocity (double setpoint ) {
80
+ setInputVoltage (
81
+ feedforward .calculate (setpoint )
82
+ + pidController .calculate (getAngularVelocityRPS (), setpoint )
83
+ );
84
+ }
85
+
86
+ public double getAngularVelocityRPS () {
87
+ return Units .radiansToRotations (getAngularVelocityRadPerSec ());
88
+ }
41
89
}
0 commit comments