Changes between Version 4 and Version 5 of ControlSystems/SoftwareTeam/Training/GettingStarted/PIDControl


Ignore:
Timestamp:
Nov 12, 2019, 6:54:31 PM (6 years ago)
Author:
dosheroff
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/PIDControl

    v4 v5  
    1212* And much more. If you have an idea for a usage of PID loops, come see a mentor or software student leader!!
    1313
    14 Code examples are coming soon
     14CODE EXAMPLES YAYYYYYYY
     15{{{public class PID {
     16    private double error, errorSum, actualPrev;
     17    private double setpoint, output;
     18    private double kP, kI, kD;
     19    private Double tolerance;
     20
     21    public PID(double kP, double kI, double kD, Double tolerance){
     22        this.kP = kP;
     23        this.kI = kI;
     24        this.kD = kD;
     25        this.tolerance = tolerance == null ? null : Math.abs(tolerance);
     26    }
     27
     28    public PID(double kP, double kI, double kD){
     29        this(kP, kI, kD, null);
     30    }
     31
     32    public void setSetpoint(double setpoint){
     33        if(this.setpoint != setpoint){
     34            this.setpoint = setpoint;
     35            errorSum = 0;
     36        }
     37    }
     38
     39    public void update(double actual){
     40        error = setpoint - actual;
     41        final double actualChange = actual - actualPrev;
     42        actualPrev = actual;
     43
     44        if(tolerance == null || !withinTolerance()){
     45            errorSum += error;
     46            output = kP*error + kI*errorSum - kD*actualChange;
     47        } else {
     48            errorSum = 0;
     49            output = 0;
     50        }
     51
     52    }
     53
     54    public void update(double actual, double feedForward){
     55        update(actual);
     56        output += feedForward;
     57    }
     58
     59    public double getOutput(){
     60        return output;
     61    }
     62
     63    public boolean withinTolerance(){
     64        return Math.abs(error) < tolerance;
     65    }
     66}
     67}}}