/*      Class PropDeriv
*
*       This class contains the constructor to create an instance of
*       a Proportional plus Derivative(PD) 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, 2 May 2005, 2 July 2006, 6 April 2008, 30 October 2009, 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/PropDeriv.html
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2006 - 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 PropDeriv extends BlackBox{
    private double kp = 1.0D;           //  proportional gain
    private double td = 0.0D;           //  derivative time constant
    private double kd = 0.0D;           //  derivative gain

    // Constructor - unit proportional gain, zero derivative gain
    public PropDeriv(){
        super("PropDeriv");
        super.sNumerDeg = 1;
        super.sDenomDeg = 0;
        super.setSnumer(new ComplexPoly(1.0D, 0.0D));
        super.setSdenom(new ComplexPoly(1.0D));
        super.setZtransformMethod(1);
        super.addDeadTimeExtras();

    }
    
    // Constructor - kp proportional gain, kd derivative gain
    public PropDeriv(double kp, double kd){
        super("PropDeriv");
        super.sNumerDeg = 1;
        super.sDenomDeg = 0;
        this.kp = kp;
        this.kd = kd;
        this.td=kd/this.kp;
        super.setSnumer(new ComplexPoly(new Complex(this.kp, 0.0D), new Complex(this.kd, 0.0D)));
        super.setSdenom(new ComplexPoly(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(this.kp, 0.0D), new Complex(this.kd, 0.0D)));
            super.setSdenom(new ComplexPoly(1.0D));
        }
        else{
            super.sNumer.resetCoeff(0, new Complex(this.kp, 0.0D));
            if(super.sZeros==null)super.sZeros = Complex.oneDarray(1);
            super.sZeros[0].reset(-this.kp/this.kd, 0.0D);
        }
        super.addDeadTimeExtras();
    }

    // Set the derivative gain
    public void setKd(double kd){
        this.kd=kd;
        this.td=kd/this.kp;
        if(super.sNumerDeg!=1){
            super.setSnumer(new ComplexPoly(new Complex(this.kp, 0.0D), new Complex(this.kd, 0.0D)));
            super.setSdenom(new ComplexPoly(1.0D));
        }
        else{
            super.sNumer.resetCoeff(1, new Complex(this.kd, 0.0D));
            if(super.sZeros==null)super.sZeros = Complex.oneDarray(1);
            super.sZeros[0].reset(-this.kp/this.kd, 0.0D);
        }
        super.addDeadTimeExtras();
    }

    // Set the derivative time constant
    public void setTd(double td){
        this.td=td;
        this.kd=this.td*this.kp;
        if(super.sNumerDeg!=1){
            super.setSnumer(new ComplexPoly(new Complex(this.kp, 0.0D), new Complex(this.kd, 0.0D)));
            super.setSdenom(new ComplexPoly(1.0D));
        }
        else{
            if(super.sZeros==null)super.sZeros = Complex.oneDarray(1);
            super.sNumer.resetCoeff(1, new Complex(this.kd, 0.0D));
            super.sZeros[0].reset(-this.kp/this.kd, 0.0D);
        }
        super.addDeadTimeExtras();
    }

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

    // 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 PropDeriv with a zero sampling period");
        super.deadTimeWarning("zTransform");
        if(ztransMethod==0){
            this.mapstozAdHoc();
        }
        else{
            super.zNumerDeg = 1;
            super.zDenomDeg = 1;
            super.zNumer = new ComplexPoly(-this.kd, this.kp*super.deltaT + this.kd);
            super.zDenom = new ComplexPoly(0.0D, super.deltaT);
            super.zZeros = Complex.oneDarray(1);
            super.zZeros[0].reset(this.kd/(this.kp*super.deltaT + this.kd),0.0D);
            super.zPoles = Complex.oneDarray(1);
            super.zPoles[0].reset(0.0D, 0.0D);
        }
    }

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

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

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

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

Ads help maintain this website.