Skip to content

Commit 1a6bc40

Browse files
committed
tuned Kg in terms of giant metal block
1 parent c7417a0 commit 1a6bc40

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

src/main/java/frc/robot/Constants.java

+13
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,25 @@ public static class IntakeConstants {
7070
public static final int PIVOT_ID = 1;
7171
public static final int ROLLER_ID = 0;
7272
public static final int INTAKE_SENSOR_ID = 0;
73+
7374
public static final double PIVOT_CONVERSION_FACTOR = 30. / (2 * Math.PI);
75+
7476
public static final double ZERO_POSITION = 0;
7577
public static final double SOURCE_POSITION = 0; //TODO: change
7678
public static final double OUTTAKE_POSITION = 0; //TODO: change
7779
public static final double VERTICAL_POSITION = Units.degreesToRadians(90); //TODO: change
80+
7881
public static final double PIVOT_TOLERANCE = .05; //TODO: change
7982

83+
public static final double PIVOT_KG = 1.2;
84+
public static final double PIVOT_KS = 0;
85+
public static final double PIVOT_KV = 0;
86+
87+
public static final double PIVOT_P = 2.1;
88+
public static final double PIVOT_I = 0;
89+
public static final double PIVOT_D = 0;
90+
91+
92+
8093
}
8194
}

src/main/java/frc/robot/RobotContainer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private void configureBindings() {
9292
new SetPivotZeroCommand(pivotSubsystem),
9393
new SetPivotVerticalCommand(pivotSubsystem),
9494
() -> (pivotSubsystem.getTargetState() == PivotState.VERTICAL)
95-
)
95+
).withTimeout(3)
9696
);
9797

9898
// aButton.onTrue(

src/main/java/frc/robot/commands/pivot/SetPivotVerticalCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public SetPivotVerticalCommand(PivotSubsystem pivotSubsystem) {
1515
@Override
1616
public void execute() {
1717
this.pivotSubsystem.setState(PivotState.VERTICAL);
18-
System.out.println("setting vertical");
18+
System.out.println("vertical" + pivotSubsystem.getPositionError());
1919
}
2020

2121
@Override

src/main/java/frc/robot/commands/pivot/SetPivotZeroCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public SetPivotZeroCommand(PivotSubsystem pivotSubsystem) {
1515
@Override
1616
public void execute() {
1717
this.pivotSubsystem.setState(PivotState.ZERO);
18-
System.out.println("setting horizaontal");
18+
System.out.println("horizontal" + pivotSubsystem.getPositionError());
1919
}
2020

2121
@Override

src/main/java/frc/robot/subsystems/intake/pivot/PivotSubsystem.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package frc.robot.subsystems.intake.pivot;
22

3+
import edu.wpi.first.math.controller.ArmFeedforward;
34
import edu.wpi.first.math.util.Units;
45
import edu.wpi.first.wpilibj2.command.SubsystemBase;
56

@@ -13,10 +14,18 @@
1314
import com.revrobotics.spark.SparkBase.ControlType;
1415
import com.revrobotics.spark.SparkBase.PersistMode;
1516
import com.revrobotics.spark.SparkBase.ResetMode;
17+
import com.revrobotics.spark.SparkClosedLoopController.ArbFFUnits;
18+
import com.revrobotics.spark.ClosedLoopSlot;
1619
import com.revrobotics.spark.SparkClosedLoopController;
1720

1821
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;
1924
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;
2029
import static frc.robot.Constants.IntakeConstants.PIVOT_TOLERANCE;
2130

2231

@@ -31,11 +40,10 @@ public class PivotSubsystem extends SubsystemBase{
3140
private SparkMaxConfig sparkMaxConfig;
3241
private SoftLimitConfig softLimitConfig;
3342

34-
private double p = 1;
35-
private double i = 0;
36-
private double d = 0;
43+
private ArmFeedforward armFeedforward;
3744

3845
private PivotState targetState;
46+
private double gravityFF;
3947

4048
public PivotSubsystem(){
4149

@@ -53,7 +61,9 @@ public PivotSubsystem(){
5361
.reverseSoftLimit(0);
5462

5563
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);
5767

5868
sparkMaxConfig = new SparkMaxConfig();
5969
sparkMaxConfig.apply(closedLoopConfig)
@@ -65,6 +75,7 @@ public PivotSubsystem(){
6575
pivotPID = pivotMotor.getClosedLoopController();
6676

6777
targetState = PivotState.ZERO;
78+
gravityFF = 0;
6879

6980
}
7081

@@ -77,7 +88,8 @@ public PivotState getTargetState() {
7788
}
7889

7990
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);
8193
// System.out.println("state set to : " + targetState.getTargetAngle());
8294
this.targetState = targetState;
8395
}
@@ -89,5 +101,9 @@ public void setSpeed(double speed) {
89101
public boolean atState(PivotState state) {
90102
return Math.abs(getCurrentAngle() - state.getTargetAngle()) < PIVOT_TOLERANCE;
91103
}
104+
105+
public double getPositionError() {
106+
return Math.abs(getCurrentAngle() - targetState.getTargetAngle());
107+
}
92108
}
93109

0 commit comments

Comments
 (0)