System Controlling: November 2012
Processing math: 100%

Sunday, November 18, 2012

Model predictive control of brushed DC motor

Motor model

The state space model of a brushed DC motor as follow:
A=[RaLaKeLa0KmJbJ0010]B=[1La00]C=[010]D=[0]
As you can see, I assumed that the torque is zero. 
Where the states are:
˙x(t)=[didtdωdtdθdt]

Controller design


I already introduced the MPC controller, please read that first before you continue to read this blog. 
The first step is to determine the Toeplitz matrix:
[xk+1|kxk+Nc|kxk+Nc+1|kxk+N|k]=[ΦΦNcΦNc+1ΦN]xk+[ΓNc1i=0ΦiΓNci=0ΦiΓNi=0ΦiΓ]uk1+[Γ0Nci=0ΦiΓΦΓ+ΓN1i=0ΦiΓNNci=0ΦiΓ][Δuk+1|kΔuk+Nc1|k] 
I wrote the following Matlab function to build up the  $\Phi^*$, $\Gamma^*$, $G_y$:
function [FiFi,FiGam,Gy] = getFiFi_FiGa(Fi,Ga,C,N,Nc,n)
    
    FiFi=[]; %ennek a merete N*n x n kell legyen
    
    for i=1:N,    
        FiFi=[FiFi; C*Fi^i]; 
    end
    % FiGam szamitasa
    FiGam=[];
    for i=1:N,
        S=0;
        for j=0:i-1,       
            S=S+C*Fi^j*Ga;    
        end
        FiGam=[FiGam; S];
    end
    
    ng=size(FiGam,1);
    mg=size(FiGam,2);
    % Gy szamitasa
    Gy=zeros(ng,mg*Nc);
    % Gy szamitasa
    for j=1:mg:Nc*mg   % oszlopok         
        Gy(j:end, j:j+mg-1)=FiGam(1:end-j+1,:);
    end

end
To find the optimal control signal we have to solve the following optimization problem $J_k(u,x_k)=\frac{1}{2}\Delta u_k^TH\Delta u_k+f^T\Delta u_k+const$. I already presented the solution of this problem (see Model predictive control). I am going to  implement the calculation of the $\Delta u_k$ optimal control signal in the following example:
for v=1:n-N
    %Error vector
    E=Yref(v:v+N-1)-FiFi*x0-FiGam*u;
    
     deltau=inv(Gy'*Q*Gy+R)*Gy'*Q*E;
     u=u+deltau(1:2);
     for k=1:length(u)
         if(abs(u(k))>lim)
             u(k) = sign(u(k))*lim;
         end
     end
   
    x0 = Fi*x0+Ga*u;
    yt = C*x0;
    
    
    um=[um u];
    ym=[ym;yt];
end;

In the example above I also tried to constrain the control signal by choosing the $lim$ variable to 38. After choosing the values of the noise matrices as follows
  • $Q=eye(N*Nro)*1e4$ where $N$ is the number of the horizon and $Nro$ is the number of the outputs.
  • $R=eye(Nc*Nrin)*1e-5$ where $Nc$ is the number of the control horizon and $Nrin$ is the number of the inputs.
I got these results:
Figure 1. Rotor speed
Figure 2. Control signal
Figure 3. Error between the reference and measured rotor speed
We can reduce the error by increasing or eliminating the constrain of control signal as you can see in $Figure 4$
Figure 4. Error between the reference and measured rotor speed without limiting the control signal
The other solution  of the optimal control signal calculation in Matlab is the built in $quadprog$ function. You can find a simple example below, also you can read the description of this function on the Matlab help page:
H=2*(Gy'*Q*Gy+R);
for v=1:n-N
    %Hiba vektor    
    E=Yref(v:v+N-1)-FiFi*x0-FiGam*u;
    
    f=-2*E'*Q*Gy;        
    
    b = [abs(lim-u(1));
        abs(lim+u(1))];
    A = [1 0 0 0 0 0 0 0;
        -1 0 0 0 0 0 0 0;
        ];
     
    Aeq=[];beq=[];
    
    [deltau,FVAL,EXITFLAG]=quadprog(H,f,A,b,Aeq,beq);
    u=u+deltau(1:2);
    
   
    x0 = Fi*x0+Ga*u;
    yt = C*x0;    
    
    um=[um u0];
    ym=[ym;yt];
    Em = [Em;E];
end;

Appendix

Motor parameters:
    Rm = 0.35;
    Km=0.0296;
    Ke = 0.0296;
    b = 6.7*10e-5;
    J = 2.9*10e-6;
    Fc = 0.02;
    L = 25 *10e-6;
    Ts = 1e-4;

Saturday, November 17, 2012

Model predictive control

Introduction

The Model Predictive Controller (MPC) theory was developed after 1960, being a special case of the optimal control theory. In the MPC theory the dynamic optimization problem will be rewritten as  a static optimization problem. In the MPC theory they are using the varying control horizon theorem. The static optimization problem will be solved at each time step.
The MPC has the following properties:
  • possibility to constrain the control signal
  • adopts the time delays in the system 
  • possibility to compensate the sensors error
  • simple implementations of MIMO and LTI systems
We consider the discrete LTI sytem:
xk+1=Φkxk+Γkukyk=Cxk
The control objective at time $k$ to minimize the following quadratic cost function:
Jk(u,xk)=Nj=1||ek+j|k||2Qj+||Δuk+j1|k||2Rj
where
$e_{k}=y_k-y^{ref}_{k}$ is the error between the reference trajectory and the measured trajectory,
$\Delta u_{k+j|k}=u_{k+j|k}-u_{k+j-1|k}$ is the change of the input signal between two steps.  
In the $k$ step we already know the $u_{k-1}$, hence we can write
uk|k=Δuk|k+uk1uk+1|k=Δuk+1|k+Δuk|k+uk1uk+Nc1|k=Δuk+Nc1|k++Δuk|k+uk1
The states predictions will be as follows:
xk+1|k=Φxk|k+Γ(Δuk|k+uk1)xk+2|k=Φ2xk|k+(Φ+I)ΓΔuk|k+Γuk+1|k+(Φ+I)Γuk1|kxk+Nc|k=ΦNcxk|k+(ΦNc1++Φ+I)ΓΔuk|k++Γuk+Nc1|k+(ΦNc1++Φ+I)Γuk1|k
After some algebraic operations we can write the equations from above in matrix form:
[xk+1|kxk+Nc|kxk+Nc+1|kxk+N|k]=[ΦΦNcΦNc+1ΦN]xk+[ΓNc1i=0ΦiΓNci=0ΦiΓNi=0ΦiΓ]uk1+[Γ0Nci=0ΦiΓΦΓ+ΓN1i=0ΦiΓNNci=0ΦiΓ][Δuk+1|kΔuk+Nc1|k]
yk+j|k=Cxk+j|k

From the $5^{th}$ and $6^{th}$ equation we can write the following:
yk=Φxk+Γuk1+GyΔuk
And the error is
ek=yrefkΦxkΓuk1

The idea behind the MPC is to rewrite the dynamic optimisation problem as a static optimization problem:
Jk(u,xk)=||ykyrefk||2Qj+||Δuk||2Rj
where
ek=[ek+1|kek+N|k],Δuk=[Δuk+1|kΔuk+Nc1|k]
Q=[Q1000Q2000QN],R=[R1000R2000RNc1] 
From the $8-9$ equations we can deduce
Jk(u,xk)=||GyΔukEk||2Qj+||Δuk||2Rj=[ΔuTkGTyETk]Q[ΔukGyEk]+ΔuTkRΔuk=ΔuTk[GTyQGy+R]Δuk2ETkQGyΔuk+ETkQEk=12ΔuTkHΔuk+fTΔuk+const
where
H=2[GTyQGy+R]
f=2GTyQEk
To get the optimal control signal with the inequality conditions, we have to solve the  $min_{\Delta u_k}\left\{J_k(u,x_k)\right\}$. There is a well know solution of this optimal problem called quadratic programming. In Matlab there is the $quadprog$ function to solve the optimization problem. There is also some C/C++ implementations, one that I know is the ACADO Toolkit. One of the disadvantages of the solution is the time consumption, because of this the sampling time will be bigger therefore it could increase the instability of the system.  
Differentiating (12) the decisions $u_k$ and setting the result equal to zero the optimal control sequence is obtained as seen below:
Δuk=[GTyQGy+R]1GTyQEk
Note that the optimal control deviation ($\Delta u_k$ ) gives offset free control in steady state. The expression above is true if we assume that the control signal changes between two steps is zero. 

Stability check

Hence stability can easily be established a posteriori by checking that the eigenvalues of the closed loop system matrix $\Phi+\Gamma K$ are strictly inside the unit circle, where
K=[GTyQGy+R]1GTyQ

$N_c$ is the control horizon
$N$ is the prediction horizon