SFELPHOTON-1335:rework triggerSync
This commit is contained in:
@@ -45,16 +45,17 @@ class MotionBase:
|
||||
self.meta.update(kwargs)
|
||||
|
||||
def setup_sync(self, crdId=1, prgId=2,verbose=False, timeOfs=0.,timeCor=0.):
|
||||
'''setup the timing synchronization for the motion program
|
||||
kwargs:
|
||||
'''setup the timing synchronization code snipplets for the motion program
|
||||
- sync_run: command to start the motion program
|
||||
- sync_prg: code snipplet at top of the motion program used for synchronization if required
|
||||
- sync_cmd: Deltatau command to excutable for synchronization if required
|
||||
starts the triggerSync on the Deltatau if required
|
||||
|
||||
args in self.meta:
|
||||
sync_mode : default=2
|
||||
0 : no sync at all
|
||||
1 : synchronize start
|
||||
2 : synchronize start and adapt motion speed
|
||||
this function generates the code blocks:
|
||||
self.sync_wait and self.sync_run
|
||||
sync_wait can be put in the program to force a timing sync
|
||||
sync_run are the commands to run the whole program
|
||||
|
||||
sync_flag : default=0
|
||||
bit 0=1 : simulated start trigger
|
||||
@@ -63,7 +64,7 @@ class MotionBase:
|
||||
1 : simulated start and real frame trigger
|
||||
2 : real start and simulated frame trigger
|
||||
3 : simulated start and frame trigger
|
||||
fel_per : FEL-period: time FEL-pulse to FEL-pulse in ms (needed for sync code)
|
||||
fel_per : FEL-period: time FEL-pulse to FEL-pulse in ms (needed for triggerSync executable)
|
||||
'''
|
||||
sync_mode=self.meta['sync_mode']
|
||||
sync_flag=self.meta['sync_flag']
|
||||
@@ -74,62 +75,68 @@ class MotionBase:
|
||||
_log.info({0: 'real start trigger', 1: 'simulated start trigger'}[sync_flag&1])
|
||||
_log.info({0: 'real frame trigger', 2: 'simulated frame trigger'}[sync_flag&2])
|
||||
|
||||
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, crdId=crdId)
|
||||
self.sync_run = f'&{crdId}b{prgId}r'
|
||||
|
||||
if sync_mode==0:
|
||||
try:
|
||||
del self.sync_prg
|
||||
del self.sync_cmd
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
elif sync_mode in(1,2):
|
||||
else: # sync_mode 1,2
|
||||
#frequence jitter 50Hz Swissgrid:
|
||||
#https://www.swissgrid.ch/de/home/operation/grid-data/current-data.html#
|
||||
flag0='Coord[{crdId}].Q[10]'.format(crdId=crdId) if sync_flag&1 else 'Gate3[1].Chan[0].UserFlag'
|
||||
flag1='Coord[{crdId}].Q[11]'.format(crdId=crdId) if sync_flag&2 else 'Gate3[1].Chan[1].UserFlag'
|
||||
if sync_mode==1:
|
||||
prg = f'\n{flag0}=0\n' if sync_flag&1 else ''
|
||||
prg += f'''
|
||||
Coord[{crdId}].Q[0]=-2 // motion program started and armed
|
||||
|
||||
# Gather.Enable=2 is done in the triggerSync executable
|
||||
# triggerSync waits for Coord[1].Q[0]=-3 and then sets Coord[1].DesTimeBase to continue motion
|
||||
# triggerSync set Coord[1].DesTimeBase to the servoPeriod to match last interval of frame triggers if sync_mode==1
|
||||
# triggerSync modifies Coord[1].DesTimeBase if sync_mode==2
|
||||
|
||||
if sync_mode==1: # triggerSync:trigsync_func not called, therefore need to make own start synchronization
|
||||
flag0=f'Coord[{crdId}].Q[10]' if sync_flag&1 else 'Gate3[1].Chan[0].UserFlag'
|
||||
flag1=f'Coord[{crdId}].Q[11]' if sync_flag&2 else 'Gate3[1].Chan[1].UserFlag'
|
||||
prg= f'''
|
||||
Coord[{crdId}].TimeBaseSlew=1 //1E-4 is default
|
||||
Coord[{crdId}].DesTimeBase=0
|
||||
Coord[{crdId}].Q[0]=-3 // motion program waiting at start position
|
||||
Coord[{crdId}].Q[0]=-2 // motion program started and armed
|
||||
while({flag0}==0){{}}
|
||||
Coord[{crdId}].Q[0]=-1
|
||||
Gather.Enable=2
|
||||
while({flag1}==0){{}}
|
||||
Coord[1].DesTimeBase=Sys.ServoPeriod
|
||||
Coord[{crdId}].Q[0]=0 //
|
||||
'''
|
||||
else:
|
||||
else: #sync_mode==2: triggerSync:trigsync_func called
|
||||
prg = f'''
|
||||
//Gather.Enable=2 is done in the triggerSync program
|
||||
Coord[1].TimeBaseSlew=1 //1E-4 is default
|
||||
Coord[1].DesTimeBase=0
|
||||
Coord[1].Q[0]=-3 //motion program started and waits. Arm(-2) is done in triggerSync program
|
||||
Coord[{crdId}].TimeBaseSlew=1 //1E-4 is default
|
||||
Coord[{crdId}].DesTimeBase=0
|
||||
Coord[{crdId}].Q[0]=-3 //motion program waiting at start position
|
||||
'''
|
||||
|
||||
self.sync_prg = prg
|
||||
#download and start triggerSync code
|
||||
comm=self.comm
|
||||
if comm is None:
|
||||
_log.info('simulated');return
|
||||
|
||||
mode=0 #mode to pass to triggerSync program
|
||||
mode=0 #for triggerSync
|
||||
# mode: (triggerSync executable)
|
||||
# bit0:1: sync mode
|
||||
# bit1:2: simulate start trigger
|
||||
# bit2:4: simulate frame trigger
|
||||
# bit3:8: verbose
|
||||
if sync_mode==2:mode+=1 #synchronize
|
||||
mode+=sync_flag*2 #simulated or real triggers
|
||||
if verbose: mode+=8
|
||||
if sync_mode==2 or mode&4:
|
||||
sftp = comm.sftp
|
||||
dst = '/tmp/triggerSync'
|
||||
src = os.path.abspath(os.path.join(os.path.dirname(__file__), 'triggerSync'))
|
||||
sftp.put(src, dst)
|
||||
sftp.chmod(dst, 0o755)
|
||||
cmd = 'LD_LIBRARY_PATH=/opt/ppmac/libppmac/ ' + dst
|
||||
#cmd+=' %d %g %g %g'%(mode,pt2pt_time,timeOfs,timeCor)
|
||||
cmd+=' %d %g %g'%(mode,fel_per+timeCor,timeOfs)
|
||||
self.cmdSync = cmd
|
||||
print ('starting '+cmd)
|
||||
self.syncShell=comm.shell_channel(cmd)
|
||||
#syncChan=sc=comm._client.exec_command(cmd) #this is less simple to communicate with
|
||||
sftp = comm.sftp
|
||||
dst = '/tmp/triggerSync'
|
||||
src = os.path.abspath(os.path.join(os.path.dirname(__file__), 'triggerSync'))
|
||||
sftp.put(src, dst)
|
||||
sftp.chmod(dst, 0o755)
|
||||
cmd = 'LD_LIBRARY_PATH=/opt/ppmac/libppmac/ ' + dst
|
||||
#cmd+=' %d %g %g %g'%(mode,pt2pt_time,timeOfs,timeCor)
|
||||
cmd+=' %d %g %g'%(mode,fel_per+timeCor,timeOfs)
|
||||
self.sync_cmd = cmd
|
||||
|
||||
def homing(self):
|
||||
if self.comm is None: return
|
||||
@@ -143,21 +150,30 @@ Coord[1].Q[0]=-3 //motion program started and waits. Arm(-2) is done in triggerS
|
||||
time.sleep(1)
|
||||
if gpascii.get_variable('Motor[1].HomeComplete', int)==1:
|
||||
break
|
||||
print('homing done')
|
||||
|
||||
|
||||
|
||||
_log.info('homing done')
|
||||
|
||||
def run(self, crdId=1):
|
||||
'runs the code sync_run which has been generated with setup_sync()'
|
||||
try:
|
||||
run=self.sync_run
|
||||
except AttributeError:
|
||||
raise 'Need to call setup sync before'
|
||||
|
||||
comm = self.comm
|
||||
if comm is None: return
|
||||
gpascii = comm.gpascii
|
||||
|
||||
try:
|
||||
cmd=self.sync_run
|
||||
cmd=self.sync_cmd
|
||||
except AttributeError:
|
||||
raise 'Need to call setup sync before'
|
||||
gpascii.send_block(cmd)
|
||||
pass
|
||||
else:
|
||||
if gpascii.get_variable('Gather.Enable', type_=int)==0:
|
||||
raise 'Gather.Enable must be !=0'
|
||||
_log.info('starting '+cmd)
|
||||
self.sync_shell=comm.shell_channel(cmd)
|
||||
#syncChan=sc=comm._client.exec_command(cmd) #this is less simple to communicate with
|
||||
gpascii.send_block(run)
|
||||
|
||||
def wait_armed(self,crdId=1): #wait until motors are at first position
|
||||
sm=self.meta['sync_mode']
|
||||
@@ -168,7 +184,7 @@ Coord[1].Q[0]=-3 //motion program started and waits. Arm(-2) is done in triggerS
|
||||
if comm is None: return
|
||||
gpascii = comm.gpascii
|
||||
while (True):
|
||||
val = gpascii.get_variable('Coord[{crdId}].Q[0]'.format(crdId=crdId), type_=int)
|
||||
val = gpascii.get_variable(f'Coord[{crdId}].Q[0]', type_=int)
|
||||
if val==-2:
|
||||
break
|
||||
time.sleep(.1)
|
||||
@@ -196,9 +212,9 @@ Coord[1].Q[0]=-3 //motion program started and waits. Arm(-2) is done in triggerS
|
||||
if comm is None: return
|
||||
gpascii = comm.gpascii
|
||||
gpascii.send_block('Coord[1].Q[10]=1')
|
||||
else:
|
||||
import CaChannel
|
||||
pvTrigger = CaChannel.CaChannel('SAR-CVME-TIFALL5-EVG0:SoftEvt-EvtCode-SP.VAL')
|
||||
pvTrigger.searchw()
|
||||
pvTrigger.putw(254)
|
||||
#else:
|
||||
# import CaChannel
|
||||
# pvTrigger = CaChannel.CaChannel('SAR-CVME-TIFALL5-EVG0:SoftEvt-EvtCode-SP.VAL')
|
||||
# pvTrigger.searchw()
|
||||
# pvTrigger.putw(254) # ??? does this send a trigger or just set the event code ???
|
||||
|
||||
|
||||
@@ -17,19 +17,26 @@ verbose bits:
|
||||
0x20 plot pvt trajectory (before motion)
|
||||
0x40 print sync details
|
||||
|
||||
mode:
|
||||
0 unused
|
||||
1 pvt motion
|
||||
2 unused
|
||||
3 pvt motion using inverse fft velocity
|
||||
4 pvt motion short code using grid parameters
|
||||
5 pvt motion short code using grid parameters. Instead of continous motion it moves and waits as give in the parameter time
|
||||
|
||||
if mode&1:
|
||||
self.plot_trajectory()
|
||||
sync:
|
||||
0 real start and frame trigger with sync
|
||||
1 direct start
|
||||
2 simulated start and frame trigger no sync
|
||||
3 simulated start and frame trigger with sync
|
||||
4 simulated start real frame trigger no sync
|
||||
5 simulated start real frame trigger with sync
|
||||
|
||||
if mode&2:
|
||||
self.plot_pos_error()
|
||||
|
||||
if mode&4:
|
||||
self.plot_bode(xy=(3,1),mode=31,db=True) # FX
|
||||
self.plot_bode(xy=(2,0),mode=31,db=True) # FY
|
||||
|
||||
if mode&8:
|
||||
self.plot_trigger_jitter()
|
||||
if motion not possible check the DesTimeBase:
|
||||
Coord[1].DesTimeBase
|
||||
> Coord[1].DesTimeBase=0 // this is blocked
|
||||
Coord[1].DesTimeBase=Sys.ServoPeriod // to unblock
|
||||
|
||||
|
||||
Gather motor order
|
||||
@@ -71,7 +78,7 @@ if __name__=="__main__":
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(base,'../../../PBTools')))
|
||||
else:
|
||||
base=os.path.abspath(os.path.dirname(__file__))
|
||||
sys.path.insert(0, os.path.join(base,'PBTools'))
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(base,'../../PBTools')))
|
||||
sys.path.insert(0, os.path.join(base,'PBSwissMX/python'))
|
||||
#_log.info(sys.path)
|
||||
|
||||
@@ -652,7 +659,7 @@ class ShapePath(MotionBase):
|
||||
address=("Motor[1].ActPos","Motor[2].ActPos","Motor[1].DesPos","Motor[2].DesPos","Gate3[1].Chan[1].UserFlag")
|
||||
#address=("Motor[1].ActPos","Motor[2].ActPos","Motor[1].DesPos","Motor[2].DesPos","Gate3[1].Chan[1].UserFlag","EncTable[20].PrevEnc")
|
||||
#address=("Motor[1].ActPos", "Motor[2].ActPos", "Motor[1].DesPos", "Motor[2].DesPos", "Gate3[1].Chan[1].UserFlag","Coord[1].TimeBase","EncTable[20].DeltaPos")
|
||||
gtMaxLn=gt.set_address(*address)
|
||||
gtMaxLn=gt.set_address(*address) # sets also Gather.Enable=1
|
||||
if acq_per is None:
|
||||
if numPts is None:
|
||||
numPts=self.mot_pts.shape[0]
|
||||
@@ -1016,11 +1023,11 @@ f(P100>0)
|
||||
self.rec=rec=gt.upload()
|
||||
|
||||
try:
|
||||
syncShell=self.syncShell
|
||||
sync_shell=self.sync_shell
|
||||
except AttributeError: pass
|
||||
else:
|
||||
print(syncShell.sync())
|
||||
del self.syncShell
|
||||
print(sync_shell.sync())
|
||||
del self.sync_shell
|
||||
|
||||
pts=self.mot_pts
|
||||
|
||||
@@ -1066,23 +1073,30 @@ if __name__=='__main__':
|
||||
comm=gather=None
|
||||
|
||||
gp=GenPath()
|
||||
#real start and frame trigger with sync
|
||||
#sp = ShapePath(comm, gather, args.verbose)
|
||||
|
||||
# direct start
|
||||
#sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=0)
|
||||
sync=args.sync
|
||||
mode=args.mode
|
||||
|
||||
#simulated start and frame trigger no sync
|
||||
#sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=1,sync_flag=3)
|
||||
|
||||
#simulated start and frame trigger with sync
|
||||
#sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=2,sync_flag=3)
|
||||
|
||||
#simulated start real frame trigger no sync
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=1,sync_flag=1)
|
||||
|
||||
#simulated start real frame trigger with sync
|
||||
#sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=2,sync_flag=1)
|
||||
if sync==0:
|
||||
#real start and frame trigger with sync
|
||||
sp = ShapePath(comm, gather, args.verbose)
|
||||
elif sync==1:
|
||||
# direct start
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=0)
|
||||
elif sync==2:
|
||||
#simulated start and frame trigger no sync
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=1,sync_flag=3)
|
||||
elif sync==3:
|
||||
#simulated start and frame trigger with sync
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=2,sync_flag=3)
|
||||
elif sync==4:
|
||||
#simulated start real frame trigger no sync
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=1,sync_flag=1)
|
||||
elif sync==5:
|
||||
#simulated start real frame trigger with sync
|
||||
sp = ShapePath(comm, gather, args.verbose,fel_per=10,sync_mode=2,sync_flag=1)
|
||||
else:
|
||||
raise(ValueError(f'unsupported sync:{sync}'))
|
||||
|
||||
fn='/tmp/shapepath'
|
||||
#fn =unique_filename('ShapePathAnalyser/records/19_01_24/spiral')
|
||||
@@ -1130,7 +1144,8 @@ if __name__=='__main__':
|
||||
gp.grid(w=10,h=15,pitch=120,rnd=0,ofs=(-1000,-1200));gp.sort(grp_sz=15)
|
||||
|
||||
# for motion mode 4,5
|
||||
grid={'pos':(-1000, -1200), 'pitch':(120, 120), 'count':(10, 15)}
|
||||
#grid={'pos':(-1000, -1200), 'pitch':(120, 120), 'count':(10, 15)}
|
||||
grid={'pos':(-1000, -1200), 'pitch':(120, 120), 'count':(20, 25)}
|
||||
|
||||
# mode:0 unused
|
||||
# mode:1 pvt motion
|
||||
@@ -1139,7 +1154,6 @@ if __name__=='__main__':
|
||||
# mode:4 pvt motion short code using grid parameters
|
||||
# mode:5 pvt motion short code using grid parameters. Instead of continous motion it moves and waits as give in the parameter time
|
||||
#mode=3;scale=0
|
||||
mode=5
|
||||
if mode==5:
|
||||
tmove=20 # time to move in ms
|
||||
twait=130# time to move in ms
|
||||
@@ -1160,6 +1174,8 @@ if __name__=='__main__':
|
||||
sp.setup_motion(fnPrg=fn+'.prg',scale=1.,cnt=1,dwell=100,mode=4,grid=grid)
|
||||
elif mode==5:
|
||||
sp.setup_motion(fnPrg=fn+'.prg',scale=1.,cnt=1,dwell=100,mode=5,tmove=tmove, twait=twait, grid=grid)
|
||||
else:
|
||||
raise(ValueError(f'unsupported mode:{mode}'))
|
||||
|
||||
if sp.comm:
|
||||
sp.setup_gather()
|
||||
@@ -1169,7 +1185,7 @@ if __name__=='__main__':
|
||||
_log.info('wait_armed')
|
||||
sp.wait_armed() # wait until motors are at first position
|
||||
_log.info('trigger')
|
||||
sp.trigger(0.5) #send a start trigger (if needed) ater given time
|
||||
sp.trigger(0.5) #send a start trigger (if needed) after given time
|
||||
_log.info('progress..')
|
||||
if not comm is None:
|
||||
while True:
|
||||
@@ -1185,13 +1201,15 @@ if __name__=='__main__':
|
||||
def parse_args():
|
||||
'main command line interpreter function'
|
||||
(h, t)=os.path.split(sys.argv[0]);cmd='\n '+(t if len(h)>3 else sys.argv[0])+' '
|
||||
exampleCmd=('-v15',
|
||||
'--host=SAR-CPPM-EXPMX1 -v 0x5d',
|
||||
'--host=localhost:10001:10002 -v 0x59'
|
||||
exampleCmd=('-v0xff',
|
||||
'--host=SAR-CPPM-EXPMX1 -v0x5d',
|
||||
'--host=localhost:10001:10002 -v0x59',
|
||||
'--host=SAR-CPPM-EXPMX1 -v0x5d -m5',
|
||||
)
|
||||
epilog=__doc__+'\nExamples:'+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
|
||||
parser=argparse.ArgumentParser(epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("-m", "--mode", type=lambda x:int(x, 0), help="mode (see bitmasks) default=0x%(default)x", default=0xff)
|
||||
parser.add_argument("-m", "--mode", type=lambda x:int(x, 0), help="mode default=0x%(default)x", default=4)
|
||||
parser.add_argument("-s", "--sync", type=lambda x:int(x, 0), help="sync default=0x%(default)x", default=2)
|
||||
parser.add_argument('-v', '--verbose', type=lambda x:int(x, 0), dest='verbose', help='verbosity bits (see below) default=0x%(default)x', default=0x00)
|
||||
parser.add_argument('--host', help='hostname', default=None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user