/*      Class PropIntDeriv
*
*       This class contains the constructor to create an instance of
*       a proportional plus integral plus Derivative (PID) controller and
*       the methods needed to use this controller in control loops in the
*       time domain, Laplace transform s domain or the z-transform z domain.
*
*       This class is a subclass of the superclass BlackBox.
*
*       Author:  Michael Thomas Flanagan.
*
*       Created: August 2002
*       Updated: 17 April 2003, 3 May 2005, 2 July 2006, 27 February 2008, 6 April 2008, 7 November 2009
*                24 May 2010, 13-15 April 2012
*
*       DOCUMENTATION:
*       See Michael T Flanagan's JAVA library on-line web page:
*       http://www.ee.ucl.ac.uk/~mflanaga/java/PropIntDeriv.html
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2002 - 2012  Michael Thomas Flanagan
*
*   PERMISSION TO COPY:
*   Permission to use, copy and modify this software and its documentation for
*   NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
*   to the author, Michael Thomas Flanagan at www.ee.ac.uk/~mflanaga, appears in all copies.
*
*   Dr Michael Thomas Flanagan makes no representations about the suitability
*   or fitness of the software for any or for a particular purpose.
*   Michael Thomas Flanagan shall not be liable for any damages suffered
*   as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/

package flanagan.control;

import flanagan.complex.Complex;
import flanagan.complex.ComplexPoly;
import flanagan.plot.Plot;
import flanagan.plot.PlotGraph;


public class PropIntDeriv extends BlackBox{
    private double kp = 1.0D;                       //  proportional gain
    private double ti = Double.POSITIVE_INFINITY;   //  integral time constant
    private double ki = 0.0D;                       //  integral gain
    private double td = 0.0D;                       //  derivative time constant
    private double kd = 0.0D;                       //  derivative gain

    // Constructor - unit proportional gain, zero integral gain, zero derivative gain
    public PropIntDeriv(){
        super("PropIntDeriv");
        super.setSnumer(new ComplexPoly(0.0D, 1.0D, 0.0D));
        super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        super.setZtransformMethod(1);
        super.addDeadTimeExtras();
    }
    
    // Constructor - uproportional gain kp, integral gai ki, derivative gain kd
    public PropIntDeriv(double kp, double ki, double kd){
        super("PropIntDeriv");
        this.kp=kp;
        this.ki=ki;
        this.ti=this.kp/ki;
        this.kd=kd;
        this.td=this.kp/kd;
        super.setSnumer(new ComplexPoly(kp, ki, kd));
        super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        super.setZtransformMethod(1);
        super.addDeadTimeExtras();
    }

    // Set the proportional gain
    public void setKp(double kp){
        this.kp=kp;
        if(super.sNumerDeg!=2){
            super.setSnumer(new ComplexPoly(this.ki, this.kp, this.kd));
            super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(1, new Complex(kp, 0.0));
            this.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

    // Set the integral gain
    public void setKi(double ki){
        this.ki=ki;
        this.ti=this.kp/ki;
        if(super.sNumerDeg!=2 || this.sDenomDeg!=1){
            super.setSnumer(new ComplexPoly(this.ki, this.kp, this.kd));
            super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(0, new Complex(ki, 0.0));
            this.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

    // Set the integral time constant
    public void setTi(double ti){
        this.ti=ti;
        this.ki=this.kp/ti;
        if(super.sNumerDeg!=2 || this.sDenomDeg!=1){
            super.setSnumer(new ComplexPoly(this.ki, this.kp, this.kd));
            super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(0, new Complex(ki, 0.0));
            this.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

    // Set the derivative gain
    public void setKd(double kd){
        this.kd=kd;
        this.td=kd/this.kp;
        if(super.sNumerDeg!=2 || this.sDenomDeg!=1){
            super.setSnumer(new ComplexPoly(this.ki, this.kp, this.kd));
            super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(2, new Complex(kd, 0.0));
            this.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

    // Set the derivative time constant
    public void setTd(double td){
        this.td=td;
        this.kd=this.kp*td;
        if(super.sNumerDeg!=2 || this.sDenomDeg!=1){
            super.setSnumer(new ComplexPoly(this.ki, this.kp, this.kd));
            super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(2, new Complex(kd, 0.0));
            this.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

    // Get the proprtional gain
    public double getKp(){
        return this.kp;
    }

    // Get the integral gain
    public double getKi(){
        return this.ki;
    }

    // Get the integral time constant
    public double getTi(){
        return this.ti;
    }

    // Get the derivative gain
    public double getKd(){
        return this.kd;
    }

    // Get the derivative time constant
    public double getTd(){
        return this.td;
    }

    // Perform z transform using an already set delta T
    public void zTransform(){
        if(super.deltaT==0.0D)System.out.println("z-transform attempted in PropIntDeriv with a zero sampling period");
        super.deadTimeWarning("zTransform");
        if(super.ztransMethod==0){
            this.mapstozAdHoc();
        }
        else{
            double kit = this.ki*super.deltaT;
            double kdt = this.kd/super.deltaT;
            Complex[] coef = Complex.oneDarray(3);
            coef[0].reset(0.0D,0.0D);
            coef[1].reset(-1.0D,0.0D);
            coef[2].reset(1.0D,0.0D);
            super.zDenom.resetPoly(coef);
            switch(this.integMethod){
                // Trapezium rule
                case 0: coef[0].reset(kdt,0.0D);
                        coef[1].reset(kit/2.0D-2.0D*kdt-this.kp,0.0D);
                        coef[2].reset(this.kp+kit/2.0D+kdt,0.0);
                        super.zNumer.resetPoly(coef);
                        break;
                // Backward rectangular rule
                case 1: coef[0].reset(kdt,0.0D);
                        coef[1].reset(-2.0D*kdt-this.kp,0.0D);
                        coef[2].reset(this.kp+kit+kdt,0.0);
                        super.zNumer.resetPoly(coef);
                        break;
                // Foreward tectangular rule
                case 2: coef[0].reset(kdt,0.0D);
                        coef[1].reset(kit-2.0D*kdt-this.kp,0.0D);
                        coef[2].reset(this.kp+kdt,0.0);
                        super.zNumer.resetPoly(coef);
                        break;
                default:    System.out.println("Integration method option in PropIntDeriv must be 0,1 or 2");
                        System.out.println("It was set at "+integMethod);
                        System.out.println("z-transform not performed");
            }
        }
        super.zZeros = super.zNumer.roots();
        super.zPoles = super.zDenom.roots();
    }

    // Perform z transform setting delta T
    public void zTransform(double deltaT){
        super.setDeltaT(deltaT);
        this.zTransform();
    }

    // Calculate the pole and the zero in the z-domain for an already set sampling period
    public void calcPolesZerosZ(){
        if(super.deltaT==0.0D)System.out.println("z-pole and z-zero calculation attempted in PropIntDeriv.calcPolesZerosZ( with a zero sampling period");
        this.zTransform();
        super.zPoles[0].reset(0.0D, 0.0D);
        super.zPoles[1].reset(1.0D, 0.0D);
        super.zZeros = super.zNumer.roots();
    }

    // Calculate the pole and the zero in the z-domain setting the  sampling period
    public void calcPolesZerosZ(double deltaT){
        this.deltaT = deltaT;
        this.calcPolesZerosZ();
    }

    // Plots the time course for a step input
    public void stepInput(double stepMag, double finalTime){

        // Calculate time course outputs
        int n = 50;                             // number of points on plot
        double incrT = finalTime/(double)(n-1); // plotting increment
        double cdata[][] = new double [2][n];   // plotting array
        double sum = 0.0D;                      // integration sum

        cdata[0][0]=0.0D;
        for(int i=1; i 

Ads help maintain this website.