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

@@ -23,7 +23,7 @@ Gather.Enable : 1: after .setup_gather() called
Gather.Samples : number af gathered data samples
'''
import os, sys
import os,sys,time
sys.path.insert(0,os.path.expanduser('~/Documents/prj/SwissFEL/PBTools/'))
#import pbtools.misc.pp_comm as pp_comm -> pp_comm.PPComm
from pbtools.misc.pp_comm import PPComm
@@ -31,86 +31,114 @@ from pbtools.misc.gather import Gather
class MotionBase:
def __init__(self, comm, gather, verbose):
def __init__(self, comm, gather, verbose,**kwargs):
self.comm=comm
self.gather=gather
self.verbose=verbose
#ServoPeriod=0.2 #ms, Sys.ServoPeriod is dependent of !common() macro
ServoPeriod=comm.gpascii.servo_period*1000
def setup_sync(self, crdId=1, prgId=2, plcId=2, encId=8, mode=0, **kwargs):
self.meta={'srv_per':ServoPeriod,'pt2pt_time':40,'sync_flag':0,'sync_mode':2}
self.meta.update(kwargs)
def setup_sync(self, crdId=1, prgId=2,verbose=False):
'''setup the timing synchronization for the motion program
mode=0 : no sync at all
mode=1 : synchronize start
mode=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
flag=0 : real start and frame trigger
flag=1 : simulated start and real frame trigger
flag=2 : real start and simulated frame trigger
flag=3 : simulated start and frame trigger
kwargs:
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
bit 1=2 : simulated frame trigger
0 : real start and frame trigger
1 : simulated start and real frame trigger
2 : real start and simulated frame trigger
3 : simulated start and frame trigger
pt2pt_time : time point to point (needed sor sync code)
'''
if mode==0:
sync_mode=self.meta['sync_mode']
sync_flag=self.meta['sync_flag']
pt2pt_time=self.meta['pt2pt_time']
print({0:'no sync at all',1:'sync at start only',2:'sync on each shot'}[sync_mode])
if sync_mode!=0:
print({0: 'real start trigger', 1: 'simulated start trigger'}[sync_flag&1])
print({0: 'real frame trigger', 2: 'simulated frame trigger'}[sync_flag&2])
if sync_mode==0:
try:
del self.sync_prg
except AttributeError:
pass
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, crdId=crdId)
elif mode in(1,2):
elif sync_mode in(1,2):
#frequence jitter 50Hz Swissgrid:
#https://www.swissgrid.ch/de/home/operation/grid-data/current-data.html#
flag=kwargs.get('flag',0);
pt2pt_time=kwargs.get('pt2pt_time',40);
flag0='Coord[1].Q[10]' if flag&1 else 'Gate3[1].Chan[0].UserFlag'
flag1='Coord[1].Q[11]' if flag&2 else 'Gate3[1].Chan[1].UserFlag'
if mode==1:
flag0='Coord[1].Q[10]' if sync_flag&1 else 'Gate3[1].Chan[0].UserFlag'
flag1='Coord[1].Q[11]' if sync_flag&2 else 'Gate3[1].Chan[1].UserFlag'
if sync_mode==1:
prg = '''
Coord[1].Q[1]=-2
Coord[1].TimeBaseSlew=1 //1E-4 is default
Coord[1].TimeBaseSlew=1 //1E-4 is default
Coord[1].DesTimeBase=0
while({flag0}==0){{}}
Coord[1].Q[1]=-1
Gather.Enable=2
while({flag1}==0){{}}
Coord[1].DesTimeBase=Sys.ServoPeriod
'''.format(plcId=plcId, crdId=crdId, flag0=flag0, flag1=flag1)
'''.format(crdId=crdId, flag0=flag0, flag1=flag1)
else:
prg = '''
//Gather.Enable=2 is done in the sync program
Coord[1].TimeBaseSlew=1 //1E-4 is default
Coord[1].DesTimeBase=0
Coord[1].Q[1]=-1
'''.format(plcId=plcId, crdId=crdId, flag0=flag0, flag1=flag1)
'''.format(crdId=crdId, flag0=flag0, flag1=flag1)
self.sync_prg = prg
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, plcId=plcId, crdId=crdId)
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, crdId=crdId)
#download and start triggerSync code
comm=self.comm
trigMode=0;
if mode==2:trigMode+=1 #synchronize
trigMode+=flag*2 #simulated or real triggers
trigMode+=8 # verbose
if mode==2 or trigMode&4:
arg=0 #argument to pass to triggerSync program
if sync_mode==2:arg+=1 #synchronize
arg+=sync_flag*2 #simulated or real triggers
if verbose: arg+=8
if sync_mode==2 or arg&4:
sftp = comm.sftp
dst = '/tmp/triggerSync'
src = os.path.abspath(os.path.join(os.path.dirname(__file__), '../src/triggerSync/triggerSync'))
sftp.put(src, dst)
sftp.chmod(dst, 0o755)
cmd = 'LD_LIBRARY_PATH=/opt/ppmac/libppmac/ ' + dst
cmd+=' %g %d'%(pt2pt_time,trigMode)
cmd+=' %g %d'%(pt2pt_time,arg)
self.cmdSync = cmd
print ('starting '+cmd)
self.syncShell=comm.shell_channel(cmd)
#self.syncChan=sc=comm._client.exec_command(cmd)
#ch=sc[1].channel
#ch.settimeout(1)
#sc[1].channel.setblocking(False)
#sc[1].read(100)
if flag&1:
print('set Coord[1].Q[10]=1 to start motion')
#syncChan=sc=comm._client.exec_command(cmd) #this is less simple to communicate with
def homing(self):
if self.comm is None: return
comm = self.comm
gpascii = comm.gpascii
#Motor[1].HomeComplete
if gpascii.get_variable('Motor[1].HomeComplete', int)==0:
gpascii.send_block('enable plc 1')
while(True):
sys.stdout.write('.');sys.stdout.flush()
time.sleep(1)
if gpascii.get_variable('Motor[1].HomeComplete', int)==1:
break
print('homing done')
def run(self):
'runs the code sync_run which has been generated with setup_sync()'
@@ -122,4 +150,18 @@ class MotionBase:
raise 'Need to call setup sync before'
gpascii.send_block(cmd)
def trigger(self,wait=.5):
if self.meta['sync_mode']==0:
return # no trigger at all
time.sleep(wait)
if self.meta['sync_flag']&1:
comm = self.comm
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)