/*      Class PropInt
*
*       This class contains the constructor to create an instance of
*       a proportional plus integral(PI) 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 April 2012
*
*       DOCUMENTATION:
*       See Michael T Flanagan's JAVA library on-line web page:
*       http://www.ee.ucl.ac.uk/~mflanaga/java/PropInt.html
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2002 - 2010  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 PropInt 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

    // Constructor - unit proportional gain, zero integral gain
    public PropInt(){
        super("PropInt");
        super.setSnumer(new ComplexPoly(0.0D, 1.0D));
        super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        super.setZtransformMethod(1);
        super.addDeadTimeExtras();
    }
    
    // Constructor - kp proportional gain, ki integral gain
    public PropInt(double kp, double ki){
        super("PropInt");
        this.kp = kp;
        this.ki = ki;
        this.ti=this.kp/ki;
        super.setSnumer(new ComplexPoly(new Complex(ki, 0.0), new Complex(kp, 0.0)));
        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!=1){
            super.setSnumer(new ComplexPoly(new Complex(ki, 0.0), new Complex(kp, 0.0)));
        super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(1, new Complex(kp, 0.0));
            super.calcPolesZerosS();
        }
        super.addDeadTimeExtras();
    }

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


    // Set the integral time constant
    public void setTi(double ti){
        this.ti=ti;
        this.ki=this.kp/ti;
        if(super.sNumerDeg!=1){
            super.setSnumer(new ComplexPoly(new Complex(ki, 0.0), new Complex(kp, 0.0)));
        super.setSdenom(new ComplexPoly(0.0D, 1.0D));
        }
        else{
            super.sNumer.resetCoeff(0, new Complex(ki, 0.0));
            super.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;
    }


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

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

    // 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.