Search code examples
filterscilabpid-controller

How can I apply a PID controller in a difference equation form on Scilab to a continuous plant


I just calculated the difference form equation for a PID control and I need to evaluate it in scilab, now, the question that I how to add the difference equation to the system to see if the controller is doing what is expected.

I saw in this document how to do it in Xcos but I want to make it by program or console since I programmed an application to do those calculations.

I've been readind that using the function filter I can apply the equation as a z-transform to the signal I want but I don't know if I could use the difference equation. Another doubt that I have is, in case of using filter, how to put the error as an input to filter.


Solution

  • If you want to do everything discrete (system and controller) then the simplest way is to run the discrete time loop yourself. For example if your equations look like:

     - System: x[k]=A*x[k-1]+B*u[k-1], y[k]=C*x[k]
     - Error: e[k]=yd[k]-y[k-1]
     - Integral: i[k]=i[k-1]+Ts*(e[k]+e[k-1])/2
     - Derivative: d[k]=(e[k]-e[k-1])/Ts 
     - Control equation: u[k]=Kp*e[k]+Ki*i[k]+Kd*d[k] 
    

    you just have to write the above in Scilab (I have supposed that your state is a vector but output and control are scalars, yd is the output to track):

    x(:,1)=x0;
    u=zeros(1,N);
    y=zeros(1,N);
    e=zeros(1,N);
    yd=ones(1,N);
    i=0;
    for k=2:N
        x(:,k)=A*x(:,k-1)+B*u(k-1)
        y(k)=C*x(:,k)
        e(k)=yd(k)-y(k)
        i=i+Ts*(e(k)+e(k-1))/2
        d=(e(k)-e(k-1))/Ts
        u(k)=Kp*e(k)+Ki*i+Kd*d 
    end
    

    The above can be simplified if you don't need all the signal history (I already did it above for the integral and derivative parts).

    Another possibility is to use ltitr(see https://help.scilab.org/docs/6.1.1/en_US/ltitr.html) but in that case you have to express your closed loop system in standard (A,B) form.