@@ -0,0 +1,2797 @@
+/*%0A* Permission is hereby granted, free of charge, to any person obtaining a copy%0A* of this software and associated documentation files (the %22Software%22), to deal%0A* in the Software without restriction, including without limitation the rights%0A* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A* copies of the Software, and to permit persons to whom the Software is%0A* furnished to do so, subject to the following conditions:%0A%0A* The above copyright notice and this permission notice shall be included in%0A* all copies or substantial portions of the Software.%0A%0A* THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A* THE SOFTWARE.%0A*/%0A%0Apackage pid;%0A%0A// This code implements a PID Controller%0A// =====================================%0A %0A%0Apublic class PIDController %0A%7B%0A    // Kp -  proportional gain%0A%09double Kp;%0A    // Ki -  Integral gain%0A%09double Ki;%0A    // Kd -  derivative gain%0A%09double Kd;%0A    // dt -  loop interval time%0A%09double dt;%0A    // max - maximum value of manipulated variable%0A%09double max;%0A    // min - minimum value of manipulated variable%0A%09double min;%0A%09// pre_error - pre error%0A%09double pre_error;%0A%09// integral - integral%0A%09double integral;%0A%09%0A%09public PIDController(double dt, double max, double min, double Kp, double Kd, double Ki )%0A%09%7B%0A%09%09this.dt = dt;%0A%09%09this.max = max;%0A%09%09this.min = min;%0A%09%09this.Kp = Kp;%0A%09%09this.Kd = Kd;%0A%09%09this.Ki = Ki;%0A%09%09this.pre_error = 0.0;%0A%09%09this.integral = 0.0;%0A%09%7D%0A%09%0A%09public double compute(double setpoint, double pv)%0A%09%7B%0A%09%09// Calculate error%0A%09    double error = setpoint - pv;%0A%0A%09    // Proportional term%0A%09    double Pout = Kp * error;%0A%0A%09    // Integral term%0A%09    integral += error * dt;%0A%09    double Iout = Ki * integral;%0A%0A%09    // Derivative term%0A%09    double derivative = (error - pre_error) / dt;%0A%09    double Dout = Kd * derivative;%0A%0A%09    // Calculate total output%0A%09    double output = Pout + Iout + Dout;%0A%0A%09    // Restrict to max/min%0A%09    if( output %3E max )%0A%09        output = max;%0A%09    else if( output %3C min )%0A%09        output = min;%0A%0A%09    // Save error to previous error%0A%09    pre_error = error;%0A%0A%09    return output;%0A%09%7D%0A%09%0A%09public static void main(String%5B%5D args) %0A%09%7B%0A%09   PIDController pid = new PIDController(0.1, 100, -100, 0.1, 0.01, 0.5);%0A%0A%09    double val = 20;%0A%09    for (int i = 0; i %3C 1000; i++) %0A%09    %7B%0A%09        double inc = pid.compute(0, val);%0A%09        System.out.println(%22val: %22 + val + %22 inc: %22 + inc);%0A%09        val += inc;%0A%09    %7D%0A%09%7D%0A%7D%0A

Create PIDController.java