This commit is contained in:
2019-01-24 10:59:36 +00:00
parent dab7fb940c
commit 92a88d8463
2 changed files with 175 additions and 190 deletions

View File

@@ -15,6 +15,7 @@ verbose bits:
4 upload progress
8 plot gather path
16 plot pvt trajectory (before motion)
32 print sync details
Gather motor order
@@ -40,7 +41,7 @@ Enc 7: Interferometer X
'''
from __future__ import print_function
import os, sys, time
import os, sys
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
@@ -62,9 +63,9 @@ def gen_pvt(p,v, p2pt, ts):
!!! it is assumed, that the time intervals are constant !!!
'''
n=int(p2pt/ts)
pvt=np.ndarray((p.shape[0]-1)*n)*0
pvt=np.ndarray((p.shape[0]-1)*n)
tt=np.arange(0,p2pt,ts)
tt=np.arange(0,p2pt,ts)[:n]
for i in range(p.shape[0]-1):
d=p[i]
c=v[i]
@@ -75,21 +76,20 @@ def gen_pvt(p,v, p2pt, ts):
return pvt
class DebugPlot:
def __init__(self,spObj=None,fn=None):
if spObj is not None:
self.set_data(spObj)
elif fn is not None:
self.load_npz(fn)
else:
def __init__(self,obj=None):
if obj is None:
self.load_npz()
elif type(obj)==str:
self.load_npz(obj)
else:
self.set_data(obj)
@staticmethod
def plot_gen_pvt(self,pv):
# pv is an array of posx posy velx vely
#pv=pv[5:10,:]
#pv=pv[5:-4,:]
p2pt=self.meta['pt2pt_time'] # ms step between samples
ts=self.meta['timebase'] # sampling time in ms
ts=self.meta['srv_per'] # sampling time in ms
n=int(p2pt/ts) # servo cycle between samples
k=pv.shape[0] # number of unique samples
t=np.arange(0, p2pt*k, p2pt) # time array of trajectory
@@ -183,24 +183,27 @@ class DebugPlot:
l2=l[:,0]**2+l[:,1]**2
try:
ofs=l2.argmin()
except ValueError:
break#print(l2[ofs])
except ValueError as e:
raise e #should never happen
break
idx+=ofs
idxInPos.append(idx)
idxInPos = np.array(idxInPos)
#select only triggers on a target point
i=np.abs(idxTrigger - idxInPos[1]).argmin()
j=np.abs(idxTrigger - idxInPos[-2]).argmin()+1
self.idxTrigger=idxTrigger[i:j]
self.idxInPos=idxInPos[1:-1]
i=max(0,np.abs(idxTrigger-idxInPos[1]).argmin()-1)
j=i+idxInPos.shape[0]
idxTrigger=idxTrigger[i:j]
self.idxInPos=idxInPos
self.idxTrigger=idxTrigger
def plot_trigger_jitter(self):
self.analyze_trigger()
ts=self.meta['timebase']
ts=self.meta['srv_per']*self.meta['acq_per']
idxTrigger=self.idxTrigger
idxInPos=self.idxInPos
jitter = idxTrigger-idxInPos
n=min(idxTrigger.shape[0],idxInPos.shape[0])-1
jitter = idxTrigger[1:n]-idxInPos[1:n]
pts=self.pts # X,Y array
rec=self.rec # yA,xA,yD,xD,trig
@@ -213,14 +216,15 @@ class DebugPlot:
rec = self.rec # yA,xA,yD,xD,trig
ts=self.meta['timebase']
ts=self.meta['srv_per']*self.meta['acq_per']
fig = plt.figure('shot position error')
ax = fig.add_subplot(1, 1, 1)
#errx = rec[idxTrigger, 1] - rec[idxInPos, 3]
#erry = rec[idxTrigger, 0] - rec[idxInPos, 2]
errx = rec[idxTrigger, 1] - pts[1:-1, 0]
erry = rec[idxTrigger, 0] - pts[1:-1, 1]
n=idxTrigger.shape[0]
errx = rec[idxTrigger, 1] - pts[:n, 0]
erry = rec[idxTrigger, 0] - pts[:n, 1]
err = np.sqrt(errx ** 2 + erry ** 2)
hl = []
@@ -239,7 +243,7 @@ class DebugPlot:
def plot_pos_error(self):
rec = self.rec # yA,xA,yD,xD,trig
ts=self.meta['timebase']
ts=self.meta['srv_per']*self.meta['acq_per']
fig = plt.figure('position error')
ax = fig.add_subplot(1, 1, 1)
@@ -340,7 +344,7 @@ class DebugPlot:
pts=self.pts # X,Y array
rec = self.rec # yA,xA,yD,xD,trig
strMot=('FY.act','FX.act','FY.des','FX.des')
ts=meta['timebase']*1E-3 #0.2ms
ts=self.meta['srv_per']*self.meta['acq_per']*1E-3 #0.2ms
num=rec.shape[0]
#rngMin=int(.01/ts);rngMax=int(num-1.001/ts) #0.01s from start 1.01 sec before end
rngMin=int(.01/ts);rngMax=rngMin+int((pts.shape[0]-2)*.01/ts)
@@ -417,14 +421,15 @@ class DebugPlot:
def set_data(self,spObj):
self.meta=spObj.meta
self.pts=spObj.points
self.rec=spObj.rec
try: pvt=self.pvt
try: self.rec=spObj.rec
except AttributeError: pass
try: self.pvt=spObj.pvt
except AttributeError: pass
class ShapePath(MotionBase):
def __init__(self,comm, gather, verbose):
MotionBase.__init__(self,comm, gather, verbose)
def __init__(self,comm, gather, verbose,**kwargs):
MotionBase.__init__(self,comm, gather, verbose, **kwargs)
def gen_swissmx_points(self,flipx=False,flipy=False,ofs=(0,0),width=1000):
'generathe a path that writes swissfel'
@@ -607,7 +612,7 @@ class ShapePath(MotionBase):
self.ptsCorr=ptsCorr
print(ptsCorr)
def setup_gather(self,acq_per=1,sim=False):
def setup_gather(self,acq_per=1):
'''
setup the channels to gather
kwargs:
@@ -617,16 +622,14 @@ class ShapePath(MotionBase):
comm=self.comm
gt=self.gather
gt.set_phasemode(False)
if sim:
if self.meta['sync_flag']&2:
address=("Motor[1].ActPos","Motor[2].ActPos","Motor[1].DesPos","Motor[2].DesPos","Coord[1].Q[11]")
else:
address=("Motor[1].ActPos","Motor[2].ActPos","Motor[1].DesPos","Motor[2].DesPos","Gate3[1].Chan[1].UserFlag")
gt.set_address(*address)
gt.set_property(MaxSamples=1000000, Period=acq_per)
ServoPeriod= .2 #0.2ms #Sys.ServoPeriod is dependent of !common() macro
#ServoPeriod=comm.gpascii.servo_period
self.meta = {'timebase': ServoPeriod*acq_per,'address':address}
self.meta.update({'acq_per':acq_per,'address':address})
def setup_coord_trf(self):
if self.comm is None: return
@@ -666,9 +669,9 @@ class ShapePath(MotionBase):
if comm is not None:
gpascii=comm.gpascii
# this uses Coord[1].Tm and limits with MaxSpeed
elif mode in (1,3): #### pvt motion
if mode in (1,3): #### pvt motion
pt2pt_time=self.meta['pt2pt_time']
ts=self.meta['timebase']
ts=self.meta['srv_per']
scale=kwargs.get('scale', 1.)
cnt=kwargs.get('cnt', 1) # move path multiple times
dwell=kwargs.get('dwell', 100) # synchronization mark all n points
@@ -707,7 +710,7 @@ class ShapePath(MotionBase):
pv[ 1:-1,(2,3)] = v[:n]*scale
verb=self.verbose
if verb&16:
self.pvt=DebugPlot(self).plot_gen_pvt(pv)
dp=DebugPlot(self);self.pvt=dp.plot_gen_pvt(pv)
plt.show()
pv[1:-1, (2, 3)]*=CoordFeedTime #scaling for Deltatau
@@ -784,23 +787,47 @@ if __name__=='__main__':
# '%(levelname)-8s %(message)s'),
# datefmt='%m-%d %H:%M',
# )
def trigger(wait=.5):
import CaChannel
time.sleep(wait)
pvTrigger = CaChannel.CaChannel('SAR-CVME-TIFALL5-EVG0:SoftEvt-EvtCode-SP.VAL')
pvTrigger.searchw()
pvTrigger.putw(254)
def unique_filename(fnBase):
i = 0;
while (True):
fn=fnBase+('%0.3d'%i)
i+=1
if not os.path.exists(fn+'.npz'):
print('save to '+fn+'.*')
break
return fn
def run_test(args):
dp=DebugPlot();dp.plot_gather();return
#dp=DebugPlot();dp.plot_gather();return
#args.host=None
if args.host is None:
comm=gather=None
else:
comm = PPComm(host=args.host)
gather = Gather(comm)
sp = ShapePath(comm, gather, args.verbose)
#real start and frame trigger with sync
#sp = ShapePath(comm, gather, args.verbose)
# direct start
#sp = ShapePath(comm, gather, args.verbose,sync_mode=0)
#simulated start and frame trigger no sync
#sp = ShapePath(comm, gather, args.verbose,sync_mode=1,sync_flag=3)
#simulated start and frame trigger with sync
#sp = ShapePath(comm, gather, args.verbose,sync_mode=2,sync_flag=3)
#simulated start real frame trigger no sync
#sp = ShapePath(comm, gather, args.verbose,sync_mode=1,sync_flag=1)
#simulated start real frame trigger with sync
sp = ShapePath(comm, gather, args.verbose,sync_mode=2,sync_flag=1)
fn='/tmp/shapepath'
#fn =unique_filename('ShapePathAnalyser/records/19_01_24/spiral')
# Gather.MaxLines=116508
# ts=0.2ms
# max_num_points=(MaxLines*ts-1000ms)/(+acq_per*pt2pt_time*ts)
@@ -814,128 +841,44 @@ if __name__=='__main__':
# 10ms 3 6860
# 10ms 4 9180
#sp.gen_swissmx_points(width=1000,ofs=(-500,0))
#sp.gen_swissfel_points(width=1000,ofs=(-500,0))
#fn='/home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/PBMotionAnalyzer/records/mode1'
#fn='/home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/record/grid_delay_0002'
# /home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/record/chip000_sortedXXXX_9x9'
#fn='/sf/bernina/data/p17592/res/20181203/imprints/chip000_preloc_min10um_sortedX_refs_goaround/chip000_sortedY_9x9.npz'
#fn='PBMotionAnalyzer/records/rand50um_25Hz'
#sp.setup_coord_trf()
#sp.points=np.zeros((2,2))
sp.meta={'timebase':.2,'pt2pt_time':40}
#sp.gather_upload(fnRec=fn+'.npz')
#Tools.plot_gather(sp)
#return
xy=False
#sp.gen_rand_points(n=14, scale=1000);sp.sort_points(xy=xy)
#print(sp.points[:,0])
#sp.gen_swissmx_points(width=1000, ofs=(-500, 0));
#sp.gen_spiral_points(rStart=100,rInc=10,numSeg=4,numCir=60, ofs=(0, 0))
sp.gen_spiral_points(rStart=100,rInc=130,numSeg=4,numCir=2, ofs=(0, 0))
#sp.gen_spiral_points(rStart=100,rInc=20,numSeg=8,numCir=32, ofs=(0, 0))
#sp.gen_closed_shifted()
#sp.gen_grid_points(w=10,h=10,pitch=100,rnd=0,ofs=(0,0));sp.sort_points(False);
#sp.gen_grid_points(w=1,h=10,pitch=100,rnd=0,ofs=(0,0))
#sp.setup_motion(fnPrg=fn + '.prg', mode=1, scale=0)
#sp.setup_motion(fnPrg=fn + '.prg', mode=1, scale=1)
#works: direct start
#sp.setup_gather(acq_per=1)
#sp.setup_sync(mode=0)
#works: simulated start and frame trigger no sync
#sp.setup_gather(acq_per=1,sim=True)
#sp.setup_sync(mode=1,flag=3)
#works: simulated start and frame trigger with sync
#sp.setup_gather(acq_per=1,sim=True)
#sp.setup_sync(mode=2,flag=3)
#works: simulated start real frame trigger no sync
#sp.setup_gather(acq_per=1)
#sp.setup_sync(mode=1,flag=1)
#works: simulated start real frame trigger with sync
sp.setup_gather(acq_per=1)
sp.setup_sync(mode=2,flag=1)
sp.setup_coord_trf() # reset to shape path system
sp.setup_motion(fnPrg=fn + '.prg', mode=3, scale=1,dwell=10)
#sp.setup_motion(fnPrg=fn + '.prg', mode=1, scale=0,dwell=10)
sp.run()
sp.gather_upload(fnRec=fn+'.npz')
dp=DebugPlot(obj=sp);dp.plot_gather(mode=11)
exit(0)
# fn='/home/zamofing_t/Documents/prj/SwissFEL/epics_ioc_modules/ESB_MX/python/data/'+time.strftime('%y-%m-%d-%H_%M_%S')
#>>>point source and sorting<<<
#sp.points = np.array([[100,523],[635,632],[756,213]])
#sp.points = np.array([[0, 0],[100, 0],[200, 0],[300, 0],[400, 0],[400, 100],[300, 100],[200, 100],[100, 100],[0, 100],[10, 200],[100, 200],[200, 200],[300, 200],[400, 200],[410, 300],[300, 300],[200, 300],[100, 300],[0, 300],[0, 400],[100, 400],[200, 400],[300, 400],[400, 400]])
#sp.gen_rand_points(n=107, scale=1000);sp.sort_points(xy=xy)
#sp.gen_rand_points(n=107, scale=1000);sp.sort_points(xy=xy)
#fh=np.load('/sf/bernina/data/p17592/res/20181203/imprints/chip000_preloc_min10um_sortedX_refs_goaround/chip000_preloc_min10um_sortedX_refs_goaround_0529.npz')
#sp.points=fh['pts']
sp.gen_grid_points(w=6,h=6,pitch=100,rnd=0,ofs=(0,0));sp.sort_points(False);
sp.gen_closed_shifted()
sp.points=sp.points-+sp.points[0,:]+(-967,1446)
#1412.33 -2862.37
#1446.62 -967.7128125
#sp.plot_points(sp.points);plt.show()
#xy=False
#sp.gen_grid_points(w=6,h=6,pitch=100,rnd=0,ofs=(0,0));sp.sort_points(False);
#sp.gen_grid_points(w=100,h=100,pitch=10,rnd=.2)
#sp.gen_swissfel_points(width=1000,ofs=(-500,0));sp.sort_points(xy=xy)
#sp.gen_grid_points(w=10,h=10,pitch=50,rnd=.2)
#sp.gen_grid_points(w=100,h=100,pitch=50,rnd=.2)
#sp.gen_closed_shifted()
#sp.gen_swissmx_points(width=1000,ofs=(-500,0))
#sp.gen_swissfel_points(width=1000,ofs=(-500,0))
#sp.gen_rand_points(n=14, scale=1000);sp.sort_points(xy=xy)
#sp.gen_swissmx_points(width=1000, ofs=(-500, 0));
#sp.gen_spiral_points(rStart=100,rInc=10,numSeg=4,numCir=60, ofs=(0, 0))
#sp.gen_spiral_points(rStart=100,rInc=130,numSeg=4,numCir=2, ofs=(0, 0))
#sp.gen_grid_points(w=10,h=10,pitch=100,rnd=0,ofs=(0,0));sp.sort_points(False);
#sp.gen_grid_points(w=1,h=10,pitch=100,rnd=0,ofs=(0,0))
sp.gen_spiral_points(rStart=100,rInc=20,numSeg=8,numCir=32, ofs=(0, 0))
#sp.gen_closed_shifted()
sp.setup_gather(acq_per=1)
sp.setup_sync(verbose=args.verbose&32)
#sp.setup_gather(acq_per=1) #Gather.MaxLines=116508 580pts
sp.setup_gather(acq_per=2)
#setup_sync(self, crdId=1, prgId=2, plcId=2, mode=0, **kwargs):
#sp.setup_sync() #no sync at all
#sp.setup_sync(mode=1) #sync with timing system (PROG)
sp.setup_sync(mode=2) #sync with timing system and PLC to sync speed (PROG)
sp.setup_coord_trf() # reset to shape path system
sp.setup_motion(fnPrg=fn + '.prg', mode=1)
#sp.gen_grid_points(w=2,h=2,pitch=50,rnd=.2);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1,acq_per=1)
#sp.gen_swissmx_points(width=1000,ofs=(-500,0));sp.setup_motion(fnPrg=fn+'.prg',mode=1,acq_per=1)
sp.setup_motion(fnPrg=fn+'.prg', mode=3, scale=1,dwell=10)
#sp.setup_motion(fnPrg=fn + '.prg', mode=1, scale=1,dwell=10)
#sp.setup_motion(fnPrg=fn + '.prg', mode=1, scale=0,dwell=10)
sp.homing()
sp.run()
sp.trigger(0.5) #send a start trigger (if needed) ater given time
sp.gather_upload(fnRec=fn+'.npz')
dp=DebugPlot(sp);dp.plot_gather(mode=11)
#sp.gen_grid_points(w=30,h=30,pitch=50,rnd=.2);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=200,h=200,pitch=50,rnd=.2);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=2,h=20,pitch=50,rnd=0);sp.setup_motion(fnPrg=fn+'.prg',mode=1,acq_per=1)
#sp.gen_rand_points(n=500, scale=1000);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1,acq_per=1)
#sp.gen_grid_points(w=30,h=30,pitch=50,rnd=0.2);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1,)
#sp.gen_rand_points(n=400, scale=1000);sp.sort_points(xy);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.plot_points(sp.points);plt.show()
#sp.gen_swissmx_points(width=2000,flipx=True,flipy=True,ofs=(1014,-281));sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=20,h=20,pitch=20,rnd=0,ofs=(-464,1754));sp.sort_points(True);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=20,h=20,pitch=20,rnd=0,ofs=(-160,3700));sp.sort_points(False);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=5,h=5,pitch=100,rnd=0,ofs=(-1590,-1070));sp.sort_points(False);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=5,h=5,pitch=100,rnd=0,ofs=(-1580,-1070));sp.sort_points(False);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=5,h=5,pitch=100,rnd=0,ofs=(-1580,-1060));sp.sort_points(False);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#sp.gen_grid_points(w=5,h=5,pitch=100,rnd=0,ofs=(-1590,-1060));sp.sort_points(False);sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#>>>setup gather and sync<<<
#sp.setup_gather()
#sp.setup_sync()
#>>>setup motion program<<<
#sp.setup_motion(fnPrg=fn+'.prg',mode=1)
#>>>run gather and plot trajectory<<<
#return
sp.run()
#trigger(0.5)
sp.gather_upload(fnRec=fn+'.npz')
Tools.plot_gather(sp,mode=11)
from optparse import OptionParser, IndentedHelpFormatter