From f3c5fafe5f87c3616136edbf258bd19b84ce4c30 Mon Sep 17 00:00:00 2001 From: Thierry Zamofing Date: Wed, 13 Feb 2019 11:03:11 +0100 Subject: [PATCH] automate make process --- .gitignore | 1 + MXfastStageDoc/MXfastStage.tex | 20 +++++++++-- matlab/StateSpaceControlDesign.m | 23 ++++++++---- matlab/identifyFxFyStage.m | 6 +++- python/MXTuning.py | 60 ++++++++++++++++++++++---------- python/usr_code/Makefile | 19 +++++++++- 6 files changed, 101 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 12bb875..b7319cf 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/MXfastStageDoc/MXfastStage.tex b/MXfastStageDoc/MXfastStage.tex index 684123b..2067ccc 100644 --- a/MXfastStageDoc/MXfastStage.tex +++ b/MXfastStageDoc/MXfastStage.tex @@ -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} diff --git a/matlab/StateSpaceControlDesign.m b/matlab/StateSpaceControlDesign.m index b7cb4ce..49010fd 100644 --- a/matlab/StateSpaceControlDesign.m +++ b/matlab/StateSpaceControlDesign.m @@ -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 diff --git a/matlab/identifyFxFyStage.m b/matlab/identifyFxFyStage.m index 0b357a5..d6ffc1a 100644 --- a/matlab/identifyFxFyStage.m +++ b/matlab/identifyFxFyStage.m @@ -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); diff --git a/python/MXTuning.py b/python/MXTuning.py index 1eda88c..e66fa84 100755 --- a/python/MXTuning.py +++ b/python/MXTuning.py @@ -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: diff --git a/python/usr_code/Makefile b/python/usr_code/Makefile index 24e55d4..aa76b5a 100644 --- a/python/usr_code/Makefile +++ b/python/usr_code/Makefile @@ -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