Advertisement
crackanddie

oskar_pid

Feb 23rd, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package frc.robot.Maths.Common;
  2.  
  3. public class PID {
  4.     private boolean isFirstCall = true;
  5.     private double kP, kI, kD, lowerLimit, upperLimit;
  6.  
  7.     public PID(double kP, double kI, double kD, double lowerLimit, double upperLimit) {
  8.         this.kP = kP;
  9.         this.kI = kI;
  10.         this.kD = kD;
  11.         this.lowerLimit = lowerLimit;
  12.         this.upperLimit = upperLimit;
  13.     }
  14.  
  15.     private double errorP;
  16.     private double errorI;
  17.     private double errorD;
  18.  
  19.     private double output;
  20.  
  21.     public void calculate(double input, double setPoint) {
  22.         if (isFirstCall) {
  23.             errorP = 0;
  24.             errorI = 0;
  25.             errorD = 0;
  26.             isFirstCall = false;
  27.         } else {
  28.             errorP = (setPoint - input)*kP;
  29.             errorI += errorP*kI;
  30.             errorD += -errorI*kD;
  31.         }
  32.         output = Limits.getLimitedValue((errorP + errorI + errorD), lowerLimit, upperLimit) / 100;
  33.     }
  34.  
  35.     public double getOutput() {
  36.         return output;
  37.     }
  38.     public void reset() {
  39.         this.isFirstCall = true;
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement