|
| 1 | +package frc.robot.lib; |
| 2 | + |
| 3 | +import com.revrobotics.CANSparkMax; |
| 4 | +import com.revrobotics.RelativeEncoder; |
| 5 | +import com.revrobotics.SparkMaxPIDController; |
| 6 | +import com.revrobotics.CANSparkMax.ControlType; |
| 7 | + |
| 8 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 9 | + |
| 10 | +public class SparkVelocityPIDController { |
| 11 | + |
| 12 | + private final CANSparkMax spark; |
| 13 | + private final SparkMaxPIDController pidController; |
| 14 | + private final RelativeEncoder encoder; |
| 15 | + private final String name; |
| 16 | + private double targetSpeed, tolerance; |
| 17 | + private double currentP, currentI, currentD, kS, kV; |
| 18 | + |
| 19 | + public SparkVelocityPIDController(String name, CANSparkMax spark, double defaultP, double defaultI, double defaultD, double kS, double kV, double targetSpeed, double tolerance) { |
| 20 | + this.spark = spark; |
| 21 | + this.pidController = spark.getPIDController(); |
| 22 | + this.encoder = spark.getEncoder(); |
| 23 | + this.name = name; |
| 24 | + this.targetSpeed = targetSpeed; |
| 25 | + this.tolerance = tolerance; |
| 26 | + pidController.setP(this.currentP = defaultP); |
| 27 | + pidController.setI(this.currentI = defaultI); |
| 28 | + pidController.setD(this.currentD = defaultD); |
| 29 | + this.kS = kS; |
| 30 | + this.kV = kV; |
| 31 | + |
| 32 | + SmartDashboard.putNumber(name + ": P", currentP); |
| 33 | + SmartDashboard.putNumber(name + ": I", currentI); |
| 34 | + SmartDashboard.putNumber(name + ": D", currentD); |
| 35 | + SmartDashboard.putNumber(name + ": Target Speed", targetSpeed); |
| 36 | + SmartDashboard.putNumber(name + ": Tolerance", tolerance); |
| 37 | + |
| 38 | + pidController.setReference(targetSpeed, ControlType.kVelocity, 0, calculateFF(targetSpeed)); |
| 39 | + } |
| 40 | + |
| 41 | + public void periodic() { |
| 42 | + double p = SmartDashboard.getNumber(name + ": P", currentP); |
| 43 | + double i = SmartDashboard.getNumber(name + ": I", currentI); |
| 44 | + double d = SmartDashboard.getNumber(name + ": D", currentD); |
| 45 | + |
| 46 | + if(p != currentP) { |
| 47 | + pidController.setP(p); |
| 48 | + currentP = p; |
| 49 | + } |
| 50 | + if(i != currentI) { |
| 51 | + pidController.setI(i); |
| 52 | + currentI = i; |
| 53 | + } |
| 54 | + if(d != currentD) { |
| 55 | + pidController.setD(d); |
| 56 | + currentD = d; |
| 57 | + } |
| 58 | + |
| 59 | + setTargetSpeed(SmartDashboard.getNumber(name + ": Target Speed", targetSpeed)); |
| 60 | + setTolerance(SmartDashboard.getNumber(name + ": Tolerance", tolerance)); |
| 61 | + SmartDashboard.putNumber(name + ": Current Speed", encoder.getVelocity()); |
| 62 | + } |
| 63 | + |
| 64 | + public RelativeEncoder getEncoder() { |
| 65 | + return encoder; |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isAtTargetSpeed() { |
| 69 | + return encoder.getVelocity() > targetSpeed - tolerance; |
| 70 | + } |
| 71 | + |
| 72 | + public String getName() { |
| 73 | + return name; |
| 74 | + } |
| 75 | + |
| 76 | + public double getTargetSpeed() { |
| 77 | + return targetSpeed; |
| 78 | + } |
| 79 | + |
| 80 | + public void setTargetSpeed(double targetSpeed) { |
| 81 | + if(targetSpeed == this.targetSpeed) return; |
| 82 | + SmartDashboard.putNumber(name + ": Target Speed", targetSpeed); |
| 83 | + this.targetSpeed = targetSpeed; |
| 84 | + pidController.setReference(targetSpeed, ControlType.kVelocity, 0, calculateFF(targetSpeed)); |
| 85 | + } |
| 86 | + |
| 87 | + public double getTolerance() { |
| 88 | + return tolerance; |
| 89 | + } |
| 90 | + |
| 91 | + public void setTolerance(double tolerance) { |
| 92 | + SmartDashboard.putNumber(name + ": Tolerance", tolerance); |
| 93 | + this.tolerance = tolerance; |
| 94 | + } |
| 95 | + |
| 96 | + public double calculateFF(double velocity) { |
| 97 | + return kS * Math.signum(velocity) + kV * velocity; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments