automate make process

This commit is contained in:
2019-02-13 11:03:11 +01:00
parent 5a05b7355e
commit f3c5fafe5f
6 changed files with 101 additions and 28 deletions

1
.gitignore vendored
View File

@@ -29,6 +29,7 @@ python/ShapePathAnalyser/records/
matlab/figures/*.eps
matlab/figures/*-eps-converted-to.pdf
matlab/ssc?.mat
src/usrServo/usrServo.sublime-project
src/usrServo/usrServo.sublime-workspace

View File

@@ -699,13 +699,26 @@ The servo loop code is generated with: \verb|MXTuning.py mode 256|.\\
This python code reads \verb%/tmp/ssc[1|2].mat% and substitutes part of
\verb%usr_code/usrcode_template.[h|c]% to build the servo loop code \verb%usr_code/usrcode.[h|c]%.\\
Finally the real time servo code is compliled for the DeltaTau with:\\
Finally the real time servo code is compliled and installed on the DeltaTau with:\\
\verb|/epics_ioc_modules/ESB_MX/python/usr_code$ make|\\
\verb|/epics_ioc_modules/ESB_MX/python/usr_code$ make install|\\
Following lines in gpasciiCommander will activate the user servo loop code.
\vspace{1pc}
The \verb|make install| process consists of many sub parts:\\
%\begin{itemize}
%\setlength\itemsep{-1em}
%\item asfgd
%\item asfgd
%\item asfgd
%\end{itemize}
- generate observer date in MATLAB with \verb|StateSpaceControlDesign()|\\
- generate code in python with \verb|MXTuning.py|\\
- compile the code with \verb|MXTuning.py| (make all)\\
- upload the module to Deltatau (make install)\\
- activate the user servo loop (make install)\\
Here the full process:
\begin{tcolorbox}[width=15cm,colback=yellow!5!white,colframe=yellow!75!black,colbacktitle=yellow!50,coltitle=black,title=MATLAB]
@@ -734,6 +747,9 @@ scp userservo_util userphase_util usrServoSample/usralgo.ko root@SAR-CPPM-EXPMX1
\end{verbatim}
\end{tcolorbox}
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black,colbacktitle=red!50,coltitle=black,title=TODO]
Here is still work to do...
\end{tcolorbox}
\begin{tcolorbox}[width=15cm,colback=yellow!5!white,colframe=yellow!75!black,colbacktitle=yellow!50,coltitle=black,title=DeltaTau Shell]
\begin{verbatim}

View File

@@ -220,12 +220,23 @@ function [ssc]=StateSpaceControlDesign(mot,mode)
end
mat2py=struct();
[ozA,ozB,ozC,ozD]=ssdata(ss_oz);
[pos_err_num,pos_err_den]=tfdata(filt_pos_err_z);
for k=["Ts","V","MaxDac","ozA","ozB","ozC","ozD","pos_err_num","pos_err_den"]
mat2py=setfield(mat2py,k,eval(k));
end
fn=sprintf('/tmp/ssc%d.mat',mot.id);
%[ozA,ozB,ozC,ozD]=ssdata(ss_oz);
%[pos_err_num,pos_err_den]=tfdata(filt_pos_err_z);
mat2py.Ts=Ts;
mat2py.V=V;
mat2py.MaxDac=MaxDac;
mat2py.ozA=ss_oz.A;
mat2py.ozB=ss_oz.B;
mat2py.ozC=ss_oz.C;
mat2py.ozD=ss_oz.D;
mat2py.ozInpName=ss_oz.InputName
mat2py.ozOutName=ss_oz.OutputName
mat2py.pos_err_num=filt_pos_err_z.Numerator{1};
mat2py.pos_err_den=filt_pos_err_z.Denominator{1};
fn=[pwd '/' sprintf( 'ssc%d.mat',mot.id)];
save(fn,'-struct','mat2py');
disp(['saved ' fn]);
end

View File

@@ -158,7 +158,11 @@ function motCell=identifyFxFyStage(mode)
end
function plotBode(mot)
figure()
try
figure()
catch
return
end
h=bodeplot(mot.meas,'r',mot.ss_plt(3,1),'g',mot.ss_c1(3,1),'b',mot.ss_d1(3,1),'m',mot.ss_1(2,1),'c',mot.ss_0(2,1),'k',mot.w);
setoptions(h,'FreqUnits','Hz','Grid','on');
p=getoptions(h);p.YLim{2}=[-360 90];p.YLimMode='manual';setoptions(h,p);

View File

@@ -287,32 +287,50 @@ class MXTuning(Tuning):
C=mat['ozC']
D=mat['ozD']
V=mat['V']
u=('DesPos','IqMeas','IqVolts','ActPos')
#u=('DesPos','IqMeas','ActVel','ActPos')
u=[]
for i in range(mat['ozInpName'].shape[0]):
s=str(mat['ozInpName'][i][0][0])
s=s[0].capitalize()+s[1:]
u.append(s)
y=('obsvOut',)
progSample='''double usr_servo_ctrl_{motid}(MotorData *Mptr)
{{
pshm->P[2000]=pshm->P[2000]*.9999+abs(Mptr->PosError)*0.0001; //lowpass of Position error
return pshm->ServoCtrl(Mptr);
}}'''.format(motid=motid)
#y=[]
#for i in range(mat['ozOutName'].shape[0]):
# s=str(mat['ozOutName'][i][0][0])
# s=s[0].capitalize()+s[1:]
# u.append(s)
# progSample='''double usr_servo_ctrl_{motid}(MotorData *Mptr)
#{{
# pshm->P[2000]=pshm->P[2000]*.9999+abs(Mptr->PosError)*0.0001; //lowpass of Position error
# return pshm->ServoCtrl(Mptr);
#}}'''.format(motid=motid)
prog='''double obsvr_servo_ctrl_{motid}(MotorData *Mptr)
{{
//x[n+1]=A*x[n]+B*u
//y=C*x[n]+D*x[n]
//u=[{u}].T
//y=[{y}].T
double x[{A.shape[0]}]; //new state
static double _x[{A.shape[0]}]={{0,0,0,0}}; //old state
//double {u}; // input values
double {y},iqCmd; // output values
double maxDac=Mptr->MaxDac;
static double _x[{A.shape[0]}]={{{xInit}}}; //old state
'''.format(motid=motid,A=A,xInit=','.join('0'*A.shape[0]),u=', '.join(u),y=', '.join(y))
s=' //input values\n'
for i in range(len(u)):
s+=' double {u}=Mptr->{u};\n'.format(u=u[i])
prog+=s+'''
prog+=s
prog+='''
double {y},iqCmd; // output values
double maxDac=Mptr->MaxDac; //other stuff
'''.format(u=', '.join(u),y=', '.join(y))
prog +='''
if (Mptr->ClosedLoop)
{
'''
'''
s=' //x[n+1]=A*x[n]+B*u;\n'
for i in range(A.shape[0]):
@@ -332,10 +350,14 @@ class MXTuning(Tuning):
s+=';\n'
prog+=s+'\n'
s=' //update states: oldstate=newstate\n '
s+=' '.join(map(lambda i: '_x[{i:d}]=x[{i:d}];'.format(i=i),range(A.shape[0])))
prog+=s+'\n\n'
prog+=''' iqCmd=DesPos*{V}-{y};
prog+=''' //set output
iqCmd={u}*{V}-{y};
//return iqCmd;
pshm->P[200{motid}]=iqCmd; //lowpass of Position error
pshm->P[200{motid}]=iqCmd;
return pshm->ServoCtrl(Mptr);
}}
else
@@ -343,7 +365,7 @@ class MXTuning(Tuning):
Mptr->Servo.Integrator=0.0;
return 0.0;
}}
}}'''.format(V=V[0,0],y=y[0],motid=motid)
}}'''.format(u=u[0],V=V[0,0],y=y[0],motid=motid)
hdr='''double obsvr_servo_ctrl_{motid}(MotorData *Mptr);
EXPORT_SYMBOL(obsvr_servo_ctrl_{motid});'''.format(motid=motid)
@@ -767,8 +789,10 @@ EXPORT_SYMBOL(obsvr_servo_ctrl_{motid});'''.format(motid=motid)
#after this go to: python/usr_code and call make to build the controller
#to activate the controller checkout: PBTools/pbtools/usr_servo_phase
base=os.path.dirname(__file__)
(hdr1,prog1)=self.usr_servo_gen_code('/tmp/ssc1.mat')
(hdr2,prog2)=self.usr_servo_gen_code('/tmp/ssc2.mat')
#(hdr1,prog1)=self.usr_servo_gen_code('/tmp/ssc1.mat')
#(hdr2,prog2)=self.usr_servo_gen_code('/tmp/ssc2.mat')
(hdr1,prog1)=self.usr_servo_gen_code(os.path.join(base,'../matlab/ssc1.mat'))
(hdr2,prog2)=self.usr_servo_gen_code(os.path.join(base,'../matlab/ssc2.mat'))
fn_ct=os.path.join(base,'usr_code/usrcode_template.c')
fn_ht=os.path.join(base,'usr_code/usrcode_template.h')
fnc=os.path.join(base,'usr_code/usrcode.c')
@@ -868,7 +892,7 @@ Examples:'''+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
#plt.ion()
#args.host='MOTTEST-CPPM-CRM0573'
#args.host=None
args.host=None
if args.host is None:
comm=gt=None
else:

View File

@@ -73,7 +73,11 @@ KBUILD_EXTRA_SYMBOLS := /usr/local/dtlibs/libppmac/Module.symvers
#all::
# cp -f /usr/local/usralgo/usralgomain.c $(PWD)
all:
all: usralgo.ko
usralgo.ko: usrcode.c
$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules
# $(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules V=1
@@ -108,6 +112,19 @@ tags:
Debug: all
../../matlab/ssc%.mat: ../../matlab/StateSpaceControlDesign.m ../../matlab/identifyFxFyStage.m
cd ../../matlab; /afs/psi.ch/sys/psi.x86_64_slp6/Programming/matlab/2018a/bin/matlab -nodisplay -nojvm -nosplash -nodesktop -r \
"clear;clear global;close all;\
mot=identifyFxFyStage(7);\
sscType=0;\
for k=1:2;\
[ssc]=StateSpaceControlDesign(mot{k},sscType);\
end;\
exit(0);"
usrcode.c: ../../matlab/ssc1.mat ../../matlab/ssc2.mat ../MXTuning.py
cd ..;./MXTuning.py -m512
install: all
scp usralgo.ko root@$(PPMAC):/tmp
scp /home/zamofing_t/Documents/prj/SwissFEL/PBTools/pbtools/usr_servo_phase/userservo_util root@$(PPMAC):/tmp