#!/usr/bin/env python # *-----------------------------------------------------------------------* # | | # | Copyright (c) 2016 by Paul Scherrer Institute (http://www.psi.ch) | # | | # | Author Thierry Zamofing (thierry.zamofing@psi.ch) | # *-----------------------------------------------------------------------* ''' Coord[1].Q[0] : sync points when using setup_sync(mode=2) -3: when armed (at start position) -2: triggerSync waiting for start trigger 0: after setup_sync(mode=2) call idx: index of position during run -1: pvt motion is finished Coord[1].Q[1] : sync trigger: when setup_sync(mode=1 or 2) 0: idle 1: waiting for trigger 2: running Gather.Enable : 1: after .setup_gather() called 2: during data gathering 0: when gather finished Gather.Samples : number af gathered data samples ''' import logging _log=logging.getLogger(__name__) import os,sys,time class MotionBase: 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 if comm is None: ServoPeriod=.2 else: ServoPeriod=comm.gpascii.servo_period*1000 self.meta={'srv_per':ServoPeriod,'fel_per':10,'sync_flag':0,'sync_mode':2} self.meta.update(kwargs) def setup_sync(self, crdId=1, prgId=2,verbose=False, timeOfs=0.,timeCor=0.): '''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 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 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'] fel_per=self.meta['fel_per'] _log.info({0:'no sync at all',1:'sync at start only',2:'sync on each shot'}[sync_mode]) if sync_mode!=0: _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 = f'&{crdId}b{prgId}r' if sync_mode==0: try: del self.sync_prg del self.sync_cmd except AttributeError: pass else: # sync_mode 1,2 #frequence jitter 50Hz Swissgrid: #https://www.swissgrid.ch/de/home/operation/grid-data/current-data.html# # 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 // 'start/frame event system' armed (triggerSync not used in these modes) while({flag0}==0){{}} // wait start trigger Coord[{crdId}].Q[0]=-1 // start trigger received Gather.Enable=2 // start acquire date while({flag1}==0){{}} // wait first frame trigger Coord[1].DesTimeBase=Sys.ServoPeriod //run motion Coord[{crdId}].Q[0]=0 // indicates that motion moves to first measurement point ''' else: #sync_mode==2: triggerSync:trigsync_func called 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 ''' self.sync_prg = prg #download and start triggerSync code comm=self.comm if comm is None: _log.info('simulated');return 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 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 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 _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_cmd except AttributeError: 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'] if sm==0: return else: comm = self.comm if comm is None: return gpascii = comm.gpascii while (True): val = gpascii.get_variable(f'Coord[{crdId}].Q[0]', type_=int) if val==-2: break time.sleep(.1) def progress(self): #returns the progress of the motion sm=self.meta['sync_mode'] comm = self.comm gpascii = comm.gpascii ge = gpascii.get_variable('Gather.Enable', type_=int) if ge == 0: return -1 cnt = gpascii.get_variable('Gather.Samples', type_=int) try: cnt*=self.meta['srv_per']*self.meta['acq_per']/self.meta['pt2pt_time'] except KeyError as e: _log.error(f'{e} -> failed') return cnt 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 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) # ??? does this send a trigger or just set the event code ???