1
1
package frc .robot .subsystems .intake .pivot ;
2
2
3
+ import edu .wpi .first .math .controller .ArmFeedforward ;
3
4
import edu .wpi .first .math .util .Units ;
4
5
import edu .wpi .first .wpilibj2 .command .SubsystemBase ;
5
6
13
14
import com .revrobotics .spark .SparkBase .ControlType ;
14
15
import com .revrobotics .spark .SparkBase .PersistMode ;
15
16
import com .revrobotics .spark .SparkBase .ResetMode ;
17
+ import com .revrobotics .spark .SparkClosedLoopController .ArbFFUnits ;
18
+ import com .revrobotics .spark .ClosedLoopSlot ;
16
19
import com .revrobotics .spark .SparkClosedLoopController ;
17
20
18
21
import static frc .robot .Constants .IntakeConstants .PIVOT_CONVERSION_FACTOR ;
22
+ import static frc .robot .Constants .IntakeConstants .PIVOT_D ;
23
+ import static frc .robot .Constants .IntakeConstants .PIVOT_I ;
19
24
import static frc .robot .Constants .IntakeConstants .PIVOT_ID ;
25
+ import static frc .robot .Constants .IntakeConstants .PIVOT_KG ;
26
+ import static frc .robot .Constants .IntakeConstants .PIVOT_KS ;
27
+ import static frc .robot .Constants .IntakeConstants .PIVOT_KV ;
28
+ import static frc .robot .Constants .IntakeConstants .PIVOT_P ;
20
29
import static frc .robot .Constants .IntakeConstants .PIVOT_TOLERANCE ;
21
30
22
31
@@ -31,11 +40,10 @@ public class PivotSubsystem extends SubsystemBase{
31
40
private SparkMaxConfig sparkMaxConfig ;
32
41
private SoftLimitConfig softLimitConfig ;
33
42
34
- private double p = 1 ;
35
- private double i = 0 ;
36
- private double d = 0 ;
43
+ private ArmFeedforward armFeedforward ;
37
44
38
45
private PivotState targetState ;
46
+ private double gravityFF ;
39
47
40
48
public PivotSubsystem (){
41
49
@@ -53,7 +61,9 @@ public PivotSubsystem(){
53
61
.reverseSoftLimit (0 );
54
62
55
63
closedLoopConfig = new ClosedLoopConfig ();
56
- closedLoopConfig .pid (p , i , d );
64
+ closedLoopConfig .pid (PIVOT_P , PIVOT_I , PIVOT_D , ClosedLoopSlot .kSlot0 );
65
+
66
+ armFeedforward = new ArmFeedforward (PIVOT_KS , PIVOT_KG , PIVOT_KV );
57
67
58
68
sparkMaxConfig = new SparkMaxConfig ();
59
69
sparkMaxConfig .apply (closedLoopConfig )
@@ -65,6 +75,7 @@ public PivotSubsystem(){
65
75
pivotPID = pivotMotor .getClosedLoopController ();
66
76
67
77
targetState = PivotState .ZERO ;
78
+ gravityFF = 0 ;
68
79
69
80
}
70
81
@@ -77,7 +88,8 @@ public PivotState getTargetState() {
77
88
}
78
89
79
90
public void setState (PivotState targetState ) {
80
- pivotPID .setReference (targetState .getTargetAngle (), ControlType .kPosition );
91
+ gravityFF = armFeedforward .calculate (targetState .getTargetAngle (), 2 );
92
+ pivotPID .setReference (targetState .getTargetAngle (), ControlType .kPosition , ClosedLoopSlot .kSlot0 , gravityFF , ArbFFUnits .kVoltage );
81
93
// System.out.println("state set to : " + targetState.getTargetAngle());
82
94
this .targetState = targetState ;
83
95
}
@@ -89,5 +101,9 @@ public void setSpeed(double speed) {
89
101
public boolean atState (PivotState state ) {
90
102
return Math .abs (getCurrentAngle () - state .getTargetAngle ()) < PIVOT_TOLERANCE ;
91
103
}
104
+
105
+ public double getPositionError () {
106
+ return Math .abs (getCurrentAngle () - targetState .getTargetAngle ());
107
+ }
92
108
}
93
109
0 commit comments