part 2
This commit is contained in:
@@ -9,14 +9,13 @@
|
||||
tuning functions for ESB-MX
|
||||
|
||||
Modes:
|
||||
1 full recording current step
|
||||
2 full recording open loop
|
||||
3 full recording closed loop
|
||||
4 plot the full bode recording
|
||||
5 plot the full bode recording with an approximation model
|
||||
6 plot all raw acquired data files
|
||||
7 custom chirp test
|
||||
8 generate observer code (after files generated with matlab)
|
||||
bit 0=1: record/plot current step
|
||||
bit 1=2: custom chirp record/plot for IdCmd->ActPos transfer function
|
||||
bit 1=4: custom chirp record/plot for DesPos->ActPos transfer function
|
||||
bit 2=8: plot the full bode recording
|
||||
bit 3=16: plot the full bode recording with an approximation model
|
||||
bit 4=32: plot all raw acquired data files
|
||||
bit 5=64: generate observer code (after files generated with matlab)
|
||||
|
||||
-> check https://github.com/klauer/ppmac for fast data gathering server which supports
|
||||
phase gathering -> not yet compiling: /home/zamofing_t/Documents/prj/SwissFEL/PowerBrickInspector/ppmac/fast_gather
|
||||
@@ -42,14 +41,14 @@ class MXTuning(Tuning):
|
||||
|
||||
def __init__(self,comm,gather):
|
||||
Tuning.__init__(self,comm,gather)
|
||||
self.homed=False
|
||||
|
||||
|
||||
def init_stage(self,force=True):
|
||||
def init_stage(self,fn=''):
|
||||
comm=self.comm
|
||||
if comm is None: return
|
||||
gpascii=comm.gpascii
|
||||
if force==False and gpascii.get_variable('Motor[1].HomeComplete', int) == 1:
|
||||
if comm is None or self.homed or os.path.isfile(fn):
|
||||
return
|
||||
gpascii=comm.gpascii
|
||||
|
||||
sys.stdout.write('homing stage');sys.stdout.flush()
|
||||
gpascii.send_line('enable plc1')
|
||||
@@ -61,9 +60,10 @@ class MXTuning(Tuning):
|
||||
break
|
||||
sys.stdout.write('.');sys.stdout.flush()
|
||||
time.sleep(.2)
|
||||
self.homed=True
|
||||
|
||||
def bode_model_plot(self, mot,base):
|
||||
self.bode_full_plot(mot,base)
|
||||
def bode_model_plot(self, mot):
|
||||
self.bode_full_plot(mot,self.baseDir)
|
||||
fig=plt.gcf()
|
||||
_N=1.#E-3 # normalization factor: -> moves 3 decades to right but has factors around 1
|
||||
# s -> ms
|
||||
@@ -115,7 +115,7 @@ class MXTuning(Tuning):
|
||||
print(mdl)
|
||||
d={'num':num.coeffs,'num1':num1.coeffs,'num2':num2.coeffs,'numc':numc.coeffs,
|
||||
'den':den.coeffs,'den1':den1.coeffs,'den2':den2.coeffs,'denc':denc.coeffs}
|
||||
fn=os.path.join(base,'model%d.mat'%mot)
|
||||
fn=os.path.join(self.baseDir,'model%d.mat'%mot)
|
||||
import scipy.io
|
||||
scipy.io.savemat(fn, mdict=d)
|
||||
print('save to matlab file:'+fn)
|
||||
@@ -189,7 +189,7 @@ class MXTuning(Tuning):
|
||||
print(mdl)
|
||||
d={'num':num.coeffs,'num1':num1.coeffs,'num2':num2.coeffs,'num3':num3.coeffs,'num4':num4.coeffs,'num5':num5.coeffs,'numc':numc.coeffs,
|
||||
'den':den.coeffs,'den1':den1.coeffs,'den2':den2.coeffs,'den3':den3.coeffs,'den4':den4.coeffs,'den5':den5.coeffs,'denc':denc.coeffs}
|
||||
fn=os.path.join(base,'model%d.mat'%mot)
|
||||
fn=os.path.join(self.baseDir,'model%d.mat'%mot)
|
||||
import scipy.io
|
||||
scipy.io.savemat(fn, mdict=d)
|
||||
print('save to matlab file:'+fn)
|
||||
@@ -345,6 +345,147 @@ EXPORT_SYMBOL(obsvr_servo_ctrl_{motid});'''.format(motid=motid)
|
||||
plt.plot(t,data[:,0]-data[:,1])
|
||||
plt.show()
|
||||
|
||||
def run(self,mode):
|
||||
#plt.ion()
|
||||
if mode&1: # full recording current step
|
||||
plt.close('all')
|
||||
self.homed=False
|
||||
for mot in (1, 2):
|
||||
fn=os.path.join(self.baseDir, 'curr_step%d.npz' % mot)
|
||||
self.init_stage(fn)
|
||||
self.bode_current(motor=mot, magMove=1000, magPhase=500, dwell=10, file=fn)
|
||||
plt.show(block=False)
|
||||
f=np.load(fn)
|
||||
fn=fn[:-3]+'mat'
|
||||
import scipy.io
|
||||
scipy.io.savemat(fn, mdict=f)
|
||||
print('save to matlab file:'+fn)
|
||||
|
||||
if mode&2:
|
||||
plt.close('all')
|
||||
self.homed=False
|
||||
motLst = (1, 2) # (2,)#
|
||||
#recType:
|
||||
# IA 0 IqCmd,ActPos (for plant transfer function)
|
||||
# DA 1 DesPos,ActPos (for regulation transfer function)
|
||||
# all 2 DesPos,ActPos,IqCmd,IqMeas,IqVolts (for current, plant and regulation transfer function)
|
||||
|
||||
|
||||
#>>>>> IqCmd->ActPos transfer function (plant) using closed loop for low frequencies
|
||||
for mot in motLst:
|
||||
fn = os.path.join(self.baseDir, 'chirp_IA_%da.npz' % mot)
|
||||
self.init_stage(fn)
|
||||
self.custom_chirp(motor=mot, minFrq=1, maxFrq=15, amp=1000, tSec=15, recType=0,openLoop=False, file=fn)
|
||||
self.homed=False
|
||||
|
||||
#>>>>> IqCmd->ActPos transfer function (plant) using open loop for high frequencies
|
||||
for ext,amp,minFrq,maxFrq,tSec in (('b', 10, 10, 100*1.5, 30),
|
||||
('c', 50, 100, 300*1.5, 30),
|
||||
('d', 50, 300, 1000*1.5, 10),
|
||||
('e', 100, 1000, 2000, 10)):
|
||||
self.homed=False
|
||||
for mot in motLst:
|
||||
fn = os.path.join(self.baseDir, 'chirp_IA_%d%s.npz' % (mot,ext))
|
||||
self.init_stage(fn)
|
||||
self.custom_chirp(motor=mot, minFrq=minFrq, maxFrq=maxFrq, amp=amp, tSec=tSec, recType=0,openLoop=True, file=fn)
|
||||
|
||||
if mode&4:
|
||||
plt.close('all')
|
||||
self.homed = False
|
||||
motLst = (1, 2) # (2,)#
|
||||
#>>>>> desPos->actPos transfer function (regulation) using closed loop
|
||||
#motor1: 0dB at 20.4 Hz
|
||||
#motor2: 0dB at 11.3 Hz
|
||||
#1000um ampl. 15Hz ca. 2A current
|
||||
#100um ampl. 15Hz ca. 200mA current
|
||||
#100um at 11.3 Hz needs 100mA ->2A at sqrt(20)*11.3 =50Hz
|
||||
#20um at 11.3 Hz needs 20mA ->2A at sqrt(100)*11.3 =113Hz
|
||||
# 5um at 11.3 Hz needs 5mA ->2A at sqrt(2000/)*11.3 =226Hz
|
||||
# 1um at 11.3 Hz needs 1mA ->2A at sqrt(2000)*11.3 =505Hz
|
||||
|
||||
#n times freq, -> n^2 current
|
||||
for ext,amp,minFrq,maxFrq,tSec in (('a', 100, 1, 30*1.5, 10),
|
||||
('b', 20, 30, 75*1.5, 15),
|
||||
('c', 5, 75, 150*1.5, 5),
|
||||
('d', 1, 150, 750, 5)):
|
||||
self.homed=False
|
||||
for mot in motLst:
|
||||
fn = os.path.join(self.baseDir, 'chirp_DA_%d%s.npz' % (mot,ext))
|
||||
self.init_stage(fn)
|
||||
self.custom_chirp(motor=mot, minFrq=minFrq, maxFrq=maxFrq, amp=amp, tSec=tSec, recType=1,openLoop=False, file=fn)
|
||||
|
||||
#>>>>> all data for different transfer function
|
||||
for ext,amp,minFrq,maxFrq,tSec in (('a', 5, 10, 250, 10),):
|
||||
self.homed=False
|
||||
for mot in motLst:
|
||||
fn = os.path.join(self.baseDir, 'chirp_all_%d%s.npz' % (mot,ext))
|
||||
self.init_stage(fn)
|
||||
self.custom_chirp(motor=mot, minFrq=minFrq, maxFrq=maxFrq, amp=amp, tSec=tSec, recType=2,openLoop=False, file=fn)
|
||||
|
||||
if os.path.isfile(fn):
|
||||
f = np.load(fn)
|
||||
data = f['data']
|
||||
meta = f['meta'].item()
|
||||
meta['file'] = file
|
||||
if len(meta['address']) == 4:
|
||||
for xy in ((0, 1), (0, 3), (2, 3)):
|
||||
self.bode_plot(data, xy=xy, mode=25, **meta)
|
||||
|
||||
if mode&8: #plot the full bode recording
|
||||
plt.close('all')
|
||||
self.bode_full_plot(mot=1,base=self.baseDir)
|
||||
self.bode_full_plot(mot=2,base=self.baseDir)
|
||||
|
||||
if mode&16: #plot the full bode recording with an approximation model
|
||||
plt.close('all')
|
||||
self.bode_model_plot(mot=1)
|
||||
self.bode_model_plot(mot=2)
|
||||
|
||||
if mode&32: # plot all raw acquired data files
|
||||
# display bode plots
|
||||
import glob
|
||||
for fn in glob.glob(os.path.join(self.baseDir,'*.npz')):
|
||||
fh = np.load(fn)
|
||||
meta = fh['meta'].item()
|
||||
data = fh['data']
|
||||
self.bode_plot(data, mode=25, **meta)
|
||||
|
||||
if mode&64: #generater code
|
||||
#before this can be done, the observer controller has to be designed with matlab:
|
||||
#s.a.ESB_MX/matlab/Readme.md
|
||||
#clear;
|
||||
#clear global;
|
||||
#close all;
|
||||
#[mot1,mot2]=identifyFxFyStage();
|
||||
#[pb]=simFxFyStage(mot1);
|
||||
#[ssc]=StateSpaceControlDesign(mot1);
|
||||
#[pb]=simFxFyStage(mot2);
|
||||
#[ssc]=StateSpaceControlDesign(mot2);
|
||||
#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')
|
||||
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')
|
||||
fnh=os.path.join(base,'usr_code/usrcode.h')
|
||||
s=open(fn_ht).read()
|
||||
s=s.replace('<usr_header>',hdr1+'\n\n'+hdr2)
|
||||
fh=open(fnh,'w')
|
||||
fh.write(s)
|
||||
fh.close()
|
||||
print(fnh+' generated.')
|
||||
s=open(fn_ct).read()
|
||||
s=s.replace('<usr_code>',prog1+'\n\n'+prog2)
|
||||
fh=open(fnc,'w')
|
||||
fh.write(s)
|
||||
fh.close()
|
||||
print(fnc+' generated.')
|
||||
print('now compile it looking at PBTools/pbtools/usr_servo_phase/usrServoSample')
|
||||
|
||||
print('done')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
@@ -390,8 +531,7 @@ Examples:'''+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
|
||||
|
||||
parser.add_argument('--host', help='hostname', metavar='HOST', default='SAR-CPPM-EXPMX1')
|
||||
parser.add_argument('--mode', '-m', type=int, help='modes (see below)', default=1)
|
||||
parser.add_argument('--dir', help='dir', default=None)
|
||||
|
||||
parser.add_argument('--dir', help='dir', default='MXTuning')
|
||||
args=parser.parse_args()
|
||||
|
||||
#plt.ion()
|
||||
@@ -403,156 +543,9 @@ Examples:'''+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
|
||||
comm = PPComm(host=args.host)
|
||||
gt = Gather(comm)
|
||||
tune=MXTuning(comm,gt)
|
||||
base='MXTuning'
|
||||
if args.dir is not None:
|
||||
base=args.dir
|
||||
assert(os.path.exists(base))
|
||||
#base=os.path.join(base,args.dir)
|
||||
#if not os.path.exists(base):
|
||||
# os.mkdir(base)
|
||||
|
||||
|
||||
mode=args.mode
|
||||
#plt.ion()
|
||||
if mode==1: # full recording current step
|
||||
homed=False
|
||||
for mot in (1, 2):
|
||||
fn=os.path.join(base, 'curr_step%d.npz' % mot)
|
||||
if not os.path.isfile(fn) and not homed: tune.init_stage();homed=True
|
||||
fig=plt.figure(mot)
|
||||
tune.bode_current(motor=mot, magMove=1000, magPhase=500, dwell=10, file=fn,fig=fig)
|
||||
plt.show(block=False)
|
||||
f=np.load(fn)
|
||||
fn=fn[:-3]+'mat'
|
||||
import scipy.io
|
||||
scipy.io.savemat(fn, mdict=f)
|
||||
print('save to matlab file:'+fn)
|
||||
|
||||
|
||||
elif mode==2: # full recording open loop
|
||||
for mot in (1, 2):
|
||||
fsin=os.path.join(base, 'sine_ol_mot%d.npz' % (mot))
|
||||
fcrp=os.path.join(base, 'chirp_ol_mot%d' % (mot))
|
||||
if not os.path.isfile(fsin): tune.init_stage();plt.close('all')
|
||||
tune.bode_sine(openloop=True, file=fsin, motor=mot)
|
||||
plt.show(block=False)
|
||||
for ext,amp,minFrq,maxFrq,tSec in (('a', 10, 10, 300,30),
|
||||
('b', 50,100, 500,30),
|
||||
('c', 50,300,1500,10),
|
||||
('d',100,300,2000,10)):
|
||||
f=fcrp+ext+'.npz'
|
||||
if not os.path.isfile(f): tune.init_stage();plt.close('all')
|
||||
tune.bode_chirp(openloop=True, file=f, motor=mot, amp=amp, minFrq=minFrq, maxFrq=maxFrq, tSec=tSec)
|
||||
plt.show(block=False)
|
||||
elif mode==3: # full recording closed loop
|
||||
for mot in (1,2):
|
||||
#fsin=os.path.join(base, 'sine_cl_mot%d.npz' % (mot))
|
||||
fcrp=os.path.join(base, 'chirp_cl_mot%d' % (mot))
|
||||
#if not os.path.isfile(fsin): tune.init_stage();plt.close('all')
|
||||
#tune.bode_sine(openloop=False, file=fsin, motor=mot)
|
||||
|
||||
#the generated program is prog 999 (only during acquisition)
|
||||
# Gather.MaxLines=174762 -> ca 15 sec. is max.
|
||||
#with higher frequency and amplitudes often is in DacLimit !
|
||||
for ext,amp,minFrq,maxFrq,tSec in (('a',100, 1, 200,15),
|
||||
('b', 20,10, 300,5),
|
||||
('c', 5,10, 300,5),
|
||||
('d', 1,10,1000,5),
|
||||
):
|
||||
f=fcrp+ext+'.npz'
|
||||
if not os.path.isfile(f): tune.init_stage();plt.close('all')
|
||||
tune.bode_chirp(openloop=False, file=f, motor=mot, amp=amp, minFrq=minFrq, maxFrq=maxFrq, tSec=tSec)
|
||||
plt.show(block=False)
|
||||
elif mode==4: #plot the full bode recording
|
||||
tune.bode_full_plot(mot=1,base=base)
|
||||
tune.bode_full_plot(mot=2,base=base)
|
||||
elif mode==5: #plot the full bode recording with an approximation model
|
||||
tune.bode_model_plot(mot=1,base=base)
|
||||
tune.bode_model_plot(mot=2,base=base)
|
||||
|
||||
elif mode == 6: # plot all raw acquired data files
|
||||
# display bode plots
|
||||
import glob
|
||||
for fn in glob.glob(os.path.join(base,'*.npz')):
|
||||
if os.path.basename(fn).startswith('sine_ol_mot'):
|
||||
tune.bode_sine(openloop=True, file=fn)
|
||||
if os.path.basename(fn).startswith('chirp_ol_mot'):
|
||||
tune.bode_chirp(openloop=True, file=fn)
|
||||
if os.path.basename(fn).startswith('sine_cl_mot'):
|
||||
tune.bode_sine(openloop=False, file=fn)
|
||||
if os.path.basename(fn).startswith('chirp_cl_mot'):
|
||||
tune.bode_chirp(openloop=False, file=fn)
|
||||
elif mode==7: #further tests
|
||||
fn='/tmp/cst_chirp0.npz'
|
||||
motLst=(1, 2)#(2,)#
|
||||
init_stage=False
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'cst_chirp_s_ol_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=100,maxFrq=1000,amp=50,tSec=15,mode=0,file=fn)
|
||||
init_stage=False
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'cst_chirp_s_cl1_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=1,maxFrq=100,amp=20,tSec=15,mode=1,file=fn)
|
||||
init_stage=False
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'cst_chirp_s_cl2a_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=1,maxFrq=30,amp=100,tSec=30,mode=2,file=fn)
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'cst_chirp_s_cl2b_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=20,maxFrq=150,amp=10,tSec=30,mode=2,file=fn)
|
||||
init_stage=False
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'cst_chirp_s_cl2c_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=100,maxFrq=300,amp=1,tSec=30,mode=2,file=fn)
|
||||
elif mode==8: #generater code
|
||||
#before this can be done, the observer controller has to be designed with matlab:
|
||||
#s.a.ESB_MX/matlab/Readme.md
|
||||
#clear;
|
||||
#clear global;
|
||||
#close all;
|
||||
#[mot1,mot2]=identifyFxFyStage();
|
||||
#[pb]=simFxFyStage(mot1);
|
||||
#[ssc]=StateSpaceControlDesign(mot1);
|
||||
#[pb]=simFxFyStage(mot2);
|
||||
#[ssc]=StateSpaceControlDesign(mot2);
|
||||
#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)=tune.usr_servo_gen_code('/tmp/ssc1.mat')
|
||||
(hdr2,prog2)=tune.usr_servo_gen_code('/tmp/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')
|
||||
fnh=os.path.join(base,'usr_code/usrcode.h')
|
||||
s=open(fn_ht).read()
|
||||
s=s.replace('<usr_header>',hdr1+'\n\n'+hdr2)
|
||||
fh=open(fnh,'w')
|
||||
fh.write(s)
|
||||
fh.close()
|
||||
print(fnh+' generated.')
|
||||
s=open(fn_ct).read()
|
||||
s=s.replace('<usr_code>',prog1+'\n\n'+prog2)
|
||||
fh=open(fnc,'w')
|
||||
fh.write(s)
|
||||
fh.close()
|
||||
print(fnc+' generated.')
|
||||
print('now compile it looking at PBTools/pbtools/usr_servo_phase/usrServoSample')
|
||||
elif mode==9: #SCRATCH
|
||||
motLst=(1, 2)#(2,)#
|
||||
init_stage=False
|
||||
for mot in motLst:
|
||||
fn=os.path.join(base, 'scratch_%d.npz' % mot)
|
||||
if not init_stage and not os.path.isfile(fn):tune.init_stage();init_stage=True
|
||||
tune.custom_chirp(motor=mot,minFrq=1,maxFrq=20,amp=200,tSec=15,mode=2,file=fn)
|
||||
elif mode==10: # record stage x and probe encoder
|
||||
tune.check_fast_stage()
|
||||
print('done')
|
||||
plt.show()
|
||||
tune.baseDir=args.dir
|
||||
assert(os.path.exists(tune.baseDir))
|
||||
tune.run(args.mode)
|
||||
#------------------ Main Code ----------------------------------
|
||||
#ssh_test()'/tmp/usrcode.c'
|
||||
ret=parse_args()
|
||||
|
||||
Reference in New Issue
Block a user