matlab wip

This commit is contained in:
2018-10-11 17:19:48 +02:00
parent f9ddf04fdc
commit cb9310ee5c
8 changed files with 228 additions and 97 deletions

View File

@@ -1,10 +1,36 @@
function [mot1,mot2]=identifyFxFyStage()
%sample idfrd
%f = logspace(-1,1,100);
%[mag,phase] = bode(idtf([1 .2],[1 2 1 1]),f);
%response = mag.*exp(1j*phase*pi/180);
%m = idfrd(response,f,0.08);
%loads recorded data of the current step and bode diagrams of the stages then plots the bode diagrams and identifies
%the current step transfer function
%
%finally it builds a ss-Model of the stage with:
%
% u +-----------+ y
%iqCmd------->|1 1|-------> iqMeas
% | 2|-------> iqVolts
% | 3|-------> actPos
% +-----------+
%
% the returned motor objects mot1 and mot2 contains:
%
% w,mag,phase : (gathered data with Python)
% meas : a MATLAB idfrd model with data w,mag,phase
% mdl : a structure with the python numerators and denominators for the transfer functions
% tfc,tf_mdl : various transfer functions
% ss : the final continous state space model of the plant
%
% The used data files (generated from Python) are:
% (located for now in: /home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/MXTuning/18_10_02/ )
% - curr_step[1|2].mat
% - full_bode_mot[1|2].mat
% - model[1|2].mat
%References:
%create ss from tf MIMO:
%https://ch.mathworks.com/matlabcentral/answers/37152-how-to-convert-tf2ss-for-mimo-system
%http://ch.mathworks.com/help/control/ug/conversion-between-model-types.html#f3-1039600
%https://ch.mathworks.com/help/control/ref/append.html
function obj=loadData(path,motid)
obj=struct();
@@ -12,7 +38,7 @@ function [mot1,mot2]=identifyFxFyStage()
obj.currstep=f;
%prepend sone zeros to stable system identification
obj.currstep=iddata([zeros(10,1); obj.currstep.data(:,2)],[zeros(10,1); obj.currstep.data(:,3)],50E-6);
f=load(strcat(path,sprintf('full_bode_mot%d.mat',motid)));
obj.w=f.frq*2*pi; %convert from Hz to rad/s
if motid==2
@@ -25,7 +51,7 @@ function [mot1,mot2]=identifyFxFyStage()
fMdl=load(strcat(path,sprintf('model%d.mat',motid)));
obj.mdl=fMdl;
end
function tfc=currstep(obj)
@@ -33,9 +59,6 @@ function [mot1,mot2]=identifyFxFyStage()
opt.Display='off';
tfc = tfest(obj.currstep, 2, 0,opt);
s=str2ndOrd(tfc);
%disp(s);
%h = stepplot(tf1);
%l=obj.currstep.OutputData
t=(0:199)*50E-6;
[y,t]=step(tfc,t);
f=figure();
@@ -57,7 +80,7 @@ function [mot1,mot2]=identifyFxFyStage()
k=num(1)/den(3);
w0=sqrt(den(3));
damp=den(2)/2/w0;
s=sprintf('k:%g w0:%g damp:%g\n',k,w0,damp);
s=sprintf('k:%g w0:%g damp:%g',k,w0,damp);
end
@@ -70,7 +93,7 @@ function [mot1,mot2]=identifyFxFyStage()
opt.initializeMethod='iv';
opt.WeightingFilter=[1,5;30,670]*(2*pi); % Hz->rad/s conversion
figure();
figure();
mot.tf2_0 = tfest(mot.meas, 2, 0, opt);disp(str2ndOrd(mot.tf2_0));
mot.tf_mdl=idtf(mot.mdl.num,mot.mdl.den);
%ss([g1 mot.tf_mdl],'minimal') this doesn't work as expected
@@ -81,9 +104,9 @@ function [mot1,mot2]=identifyFxFyStage()
den1=myNorm(mot.mdl.den1);
num2=myNorm(mot.mdl.num2);
den2=myNorm(mot.mdl.den2);
g1=tf(numc,denc); % iqCmd->iqMeas
g1=tf(numc,denc); % iqCmd->iqMeas
s1=ss(g1);
s1.C=[s1.C; 1/s1.B(1) 0]; % add output iqVolts: iqVolts= i_meas*R+i_meas'*L
s1.C=[s1.C; 1E5* 2.4E-3 1E-3*s1.C(2)*8.8]; % add output iqVolts: iqVolts= i_meas*R+i_meas'*L 2.4mH 8.8Ohm (took random scaling values)
%tf(s1) % display all transfer functions
num=conv(num1,num2);%num=1;
den=conv(den1,den2);%den=[1 0 0];
@@ -92,26 +115,20 @@ function [mot1,mot2]=identifyFxFyStage()
s3=append(s1,s2);
s3.A(3,2)=s3.C(1,2)*s3.B(3,2);
mot.ss=ss(s3.A,s3.B(:,1),s3.C,0); % single input, remove input iqMeas
mot.ss.InputName{1}='iqCmd';
mot.ss.OutputName{1}='iqMeas';
mot.ss.OutputName{2}='iqVolts';
mot.ss.OutputName{3}='actPos';
% u +-----------+ y
%iqCmd------->|1 1|-------> iqMeas
% | 2|-------> iqVolts
% | 3|-------> actPos
% +-----------+
mot.ss.OutputName{3}='actPos';
%h=bodeplot(mot.meas,'r',mot.tf4_2,'b',mot.tf6_4,'g');
%h=bodeplot(mot.meas,'r',mot.tf2_0,'b',mot.tf_mdl,'g',mot.w);
tmp=tf(mot.ss);h=bodeplot(mot.meas,'r',tmp(3,1),'g',mot.w);
setoptions(h,'FreqUnits','Hz','Grid','on');
end
function y=myNorm(y)
%normalizes num and den by factor 1000
%y.*10.^(3*(length(y):-1:1))
%y.*10.^(3*(length(y):-1:1))
end
function mot=fxStage()
@@ -128,10 +145,6 @@ function [mot1,mot2]=identifyFxFyStage()
mot.tf13_9 = tfest(mot.meas, 13, 9, opt);
mot.tf_mdl=idtf(mot.mdl.num,mot.mdl.den);
%create ss from tf MIMO:
%https://ch.mathworks.com/matlabcentral/answers/37152-how-to-convert-tf2ss-for-mimo-system
%http://ch.mathworks.com/help/control/ug/conversion-between-model-types.html#f3-1039600
%https://ch.mathworks.com/help/control/ref/append.html
numc=myNorm(mot.mdl.numc);
denc=myNorm(mot.mdl.denc);
num1=myNorm(mot.mdl.num1);
@@ -146,12 +159,15 @@ function [mot1,mot2]=identifyFxFyStage()
den5=myNorm(mot.mdl.den5);
num=myNorm(mot.mdl.num);
den=myNorm(mot.mdl.den);
g1=tf(numc,denc); % iqCmd->iqMeas
g1=tf(numc,denc); % iqCmd->iqMeas
s1=ss(g1);
s1.C=[s1.C; 1/s1.B(1) 0]; % add output iqVolts: iqVolts= i_meas*R+i_meas'*L
s1.C=[s1.C; 1E5* 2.4E-3 1E-3*s1.C(2)*8.8]; % add output iqVolts: iqVolts= i_meas*R+i_meas'*L 2.4mH 8.8Ohm (took random scaling values)
%tf(s1) % display all transfer functions
num=conv(conv(conv(conv(num1,num2),num3),num4),num5);%num=1;
den=conv(conv(conv(conv(den1,den2),den3),den4),den5);%den=[1 0 0];
num=conv(num1,num2);%num=1;
den=conv(den1,den2);%den=[1 0 0];
g2=tf(num,den); %iqMeas->ActPos
s2=ss(g2);
s3=append(s1,s2);
@@ -162,11 +178,11 @@ function [mot1,mot2]=identifyFxFyStage()
s3.A(3,2)=s3.C(1,2)*s3.B(3,2);
mot.ss=ss(s3.A,s3.B(:,1),s3.C,0); % single input, remove input iqMeas
mot.ss.InputName{1}='iqCmd';
mot.ss.OutputName{1}='iqMeas';
mot.ss.OutputName{2}='iqVolts';
mot.ss.OutputName{3}='actPos' ;
mot.ss.OutputName{3}='actPos' ;
% u +-----------+ y
%iqCmd------->|1 1|-------> iqMeas
% | 2|-------> iqVolts