Files
PBSwissMX/matlab/StateSpaceControlDesign.m
2019-02-11 16:55:58 +01:00

265 lines
7.9 KiB
Matlab

function [ssc]=StateSpaceControlDesign(mot,mode)
% !!! first it need to run: [mot1,mot2]=identifyFxFyStage() to build a motor object !!!
%
% builds a state space controller designed for the plant.
% shows step answers of open and closed loop, also for the observer controller and the final discrete observer
%
% the matchich simulink model is: 'observer'
%References:
%http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=ControlStateSpace
%space state controller:
% web(fullfile(docroot, 'simulink/examples.html'))
% web(fullfile(docroot, 'simulink/examples/inverted-pendulum-with-animation.html'))
% web(fullfile(docroot, 'simulink/examples/double-spring-mass-system.html'))
%
% https://www.youtube.com/watch?v=Lax3etc837U
%plant and model
% ss_plt :real plant (model of real plant)
% ss_c1 :current, mechanic, no resonance
% ss_d1 :simpl. current, mechanic, no resonance
% ss_1 :no current, mechanic, no resonance
% ss_0 :no current, simpl. mechanic, no resonance
%prefilt:prefilter mode
%0 no filter
%1 inverse resonance filter
%2 manual setup filter
%verb: mode(bits) to plot/simulate
% 0: 1: poles of model and placed poles of controller
% 1: 2: bode plots of open loop
% 2: 4: step answer on open loop
% 3: 8: step answer on closed loop with space state controller
% 4: 16: step answer on closed loop with observer controller
% 5: 32: step answer on closed loop with disctrete observer controller
% 6: 64: plot all closed loop bode and pole-zero diagrams of desPos->actPos
% 7:128: bode plot of filt_pos_err
%use_lqr: use lqr instead of pole placement
verb=1;
use_lqr=0;
filt_pos_err=Prefilt(mot,2);
%locate poles: 2500rad/s = 397Hz, 6300rad/s = 1027Hz
switch mode
case -1 %TESTING
ss_plt=mot.ss_plt; %ss_plt ss_c1 ss_d1 ss_1 ss_0
ss_mdl=mot.ss_d1; %ss_plt ss_c1 ss_d1 ss_1 ss_0
if mot.id==1
pl=[-2200 -2100 -2000]; % stable with scaling of .05 .. 1.0;
else
pl=[-2500 -900 -800];
end
plObs=2*pl;
case 0
ss_plt=mot.ss_plt; %ss_plt ss_c1 ss_d1 ss_1 ss_0
ss_mdl=mot.ss_c1; %ss_plt ss_c1 ss_d1 ss_1 ss_0
if mot.id==1
pl=[-3300 -3200 -2900 -2800];
else
pl=[-3300 -3200 -2700 -2600];
end
plObs=2*pl;
case 1
ss_plt=mot.ss_plt; %ss_plt ss_c1 ss_d1 ss_1 ss_0
ss_mdl=mot.ss_d1; %ss_plt ss_c1 ss_d1 ss_1 ss_0
if mot.id==1
pl=[-2200 -2100 -2000]; % stable with scaling of .05 .. 1.0;
else
pl=[-2500 -900 -800];
end
plObs=2*pl;
case 2
ss_plt=mot.ss_plt; %ss_plt ss_c1 ss_d1 ss_1 ss_0
ss_mdl=mot.ss_c1; %ss_plt ss_c1 ss_d1 ss_1 ss_0
use_lqr=1
end
[Am,Bm,Cm,Dm]=ssdata(ss_mdl);
if bitand(verb,1) && use_lqr==0
format compact
format shortG
disp(pole(ss_mdl)) %==eig(Am)
%damp(ss_mdl) %further informations
disp(pl)
disp(plObs)
format short
end
if bitand(verb,2)
figure();h=bodeplot(ss_plt,ss_mdl);
setoptions(h,'IOGrouping','all')
end
xp0 = zeros(1,length(ss_plt.A));
xm0 = zeros(1,length(Am));
if bitand(verb,4)
% step answer on open loop:
t = 0:1E-4:.5;
u = ones(size(t));
[yp,t,x] = lsim(ss_plt,u,t,xp0);
[ym,t,x] = lsim(ss_mdl,u,t,xm0);
figure();plot(t,yp,t,ym,'--');title('step on open loop (plant and model)');
legend('plt.iqMeas','plt.iqVolts','plt.actPos','mdl.iqMeas','mdl.iqVolts','mdl.actPos')
end
%w0=abs(poles);
%ang=angle(-poles);
%-------------------
%p=w0.*exp(j.*ang)
% *** space state controller ***
%
%place poles for the controller feedback
if use_lqr %use the lqr controller
Q=eye(length(Am));
R=1;
[K,P,E]=lqr(Am,Bm,Q,R,0);
else
K = place(Am,Bm,pl);
%K = acker(Am,Bm,pl);
end %if lqr
V=-1./(Cm*(Am-Bm*K)^-1*Bm); %(from Lineare Regelsysteme2 (Glattfelder) page:173 )
if length(V)>1
V=V(3); % only the position scaling needed
end
ss_cl = ss(Am-Bm*K,Bm*V,Cm,0,'Name','space state controller','InputName',ss_mdl.InputName,'OutputName',ss_mdl.OutputName);
if bitand(verb,8)
% step answer on closed loop with space state controller:
t = 0:1E-4:.5;
[y,t,x]=lsim(ss_cl,V*u,t,xm0);
figure();plot(t,y);title('step on closed loop');
end
% *** observer controller ***
%
%observer poles
if use_lqr %use the lqr controller
Q=eye(length(Am')); % ??????????????? CHANGES NEEDED ????????????
R=eye(size(Cm,1));
[L,P,E]=lqr(Am',Cm',Q,R,0);
else
L=place(Am',Cm',plObs)';
%L=acker(A',C',plObs)';
end
At = [ Am-Bm*K Bm*K
zeros(size(Am)) Am-L*Cm ];
Bt = [ Bm*V
zeros(size(Bm)) ];
Ct = [ Cm zeros(size(Cm)) ];
Dt=0;
ss_t = ss(At,Bt,Ct,Dt,'Name','observer controller','InputName',{'desPos'},'OutputName',ss_mdl.OutputName);
if bitand(verb,16)
% step answer on closed loop with observer controller:
figure();lsim(ss_t,ones(size(t)),t,[xm0 xm0]);title('step on closed loop with observer');
end
% *** disctrete observer controller ***
%
Ts=1/5000; % 5kHz
ss_tz = c2d(ss_t,Ts);
[Atz,Btz,Ctz,Dtz]=ssdata(ss_tz );
ss_tz.Name='discrete obsvr ctrl';
if bitand(verb,32)
% step answer on closed loop with disctrete observer controller:
t = 0:Ts:.05;
figure();lsim(ss_tz ,ones(size(t)),t,[xm0 xm0]);title('step on closed loop with observer discrete');
end
if bitand(verb,64)
%plot all bode diagrams of desPos->actPos
figure();
idx=length(ss_cl.OutputName);
h=bodeplot(ss_cl(idx),ss_t(idx),ss_tz(idx));
setoptions(h,'FreqUnits','Hz','Grid','on');legend('location','sw');
figure();
h=pzplot(ss_cl(idx),ss_t(idx));
setoptions(h,'FreqUnits','Hz','Grid','on');legend('location','sw');
figure();
h=pzplot(ss_tz(idx));
setoptions(h,'FreqUnits','Hz','Grid','on');legend('location','sw');
end
%calculate matrices for the simulink system
Ao=Am-L*Cm;
Bo=[Bm L];
Co=K;
Do=zeros(size(Co,1),size(Bo,2));
ss_o = ss(Ao,Bo,Co,Do,'Name','observer controller','InputName',[{'desPos'}; ss_mdl.OutputName ],'OutputName',{'k*xt'});
%discrete plant
%ss_pltz = c2d(ss_plt,Ts);
%[Apz,Bpz,Cpz,Dpz]=ssdata(ss_pltz);
%discrete observer controller
ss_oz = c2d(ss_o,Ts);
%discrete prefilter
filt_pos_err_z=c2d(filt_pos_err,Ts);
if bitand(verb,128)
h=bodeplot(filt_pos_err,filt_pos_err_z);
setoptions(h,'FreqUnits','Hz','Grid','on');legend('location','sw');
end
%state space controller
ssc=struct();
for k=["Ts","ss_plt","ss_o","ss_oz","filt_pos_err","filt_pos_err_z","V"]
ssc=setfield(ssc,k,eval(k));
end
save(sprintf('/tmp/ssc%d.mat',mot.id),'-struct','ssc');
end
function pf=Prefilt(mot,mode)
switch mode
case 0 %no filter
pf=tf(1,1);
case 1 %inverse resonance
if mot.id==1
den=mot.mdl.num2;%num=1;
num=mot.mdl.den2;%den=[1 0 0];
pf=tf(num,den);
else
den=conv(conv(conv(mot.mdl.num2,mot.mdl.num3),mot.mdl.num4),mot.mdl.num5);%num=1;
num=conv(conv(conv(mot.mdl.den2,mot.mdl.den3),mot.mdl.den4),mot.mdl.den5);%den=[1 0 0];
pf=tf(num,den);
end
case 2
if mot.id==1
f=200;w0=f*2*pi; num1=[1 300 w0^2]; den1=[1 200 w0^2];
numV=num1;
denV=den1;
pf=tf(numV,denV);
else
%Lag
f=[100 200]; w=f*2*pi; T=1./w;
tf1=tf([T(1) 1],[T(2) 1]);
%bo = bodeoptions;
%bo.FreqUnits = 'Hz'; bo.MagUnits='abs'; bo.Grid='on';
%bode(tf1,bo)
%k=1.2; aa=2; f=[40 60];w=f*2*pi; tf([1 33 w0^2]; den3=[1 20 w0^2];
%f=277;w0=f*2*pi; num1=[1 20 w0^2]; den1=[1 500 w0^2];
%f=138;w0=f*2*pi; num2=[1 300 w0^2]; den2=[1 100 w0^2];
%f=60;w0=f*2*pi; num3=[1 33 w0^2]; den3=[1 20 w0^2];
%numV=conv(conv(num1,num2),num3);
%denV=conv(conv(den1,den2),den3) ;
%pf=tf(numV,denV);
pf=tf1;
end
end
%controlSystemDesigner('bode',1,pf); % <<<<<<<<< This opens a transferfunction that can be edited
end