#!/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 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 from pbtools.misc.gather import Gather 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 for the motion program 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 fel_per : FEL-period: time FEL-pulse to FEL-pulse in ms (needed for sync code) ''' 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 = '&{crdId}b{prgId}r'''.format(prgId=prgId, crdId=crdId) if sync_mode==0: try: del self.sync_prg except AttributeError: pass elif sync_mode in(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 Coord[{crdId}].TimeBaseSlew=1 //1E-4 is default Coord[{crdId}].DesTimeBase=0 while({flag0}==0){{}} Coord[{crdId}].Q[0]=-1 Gather.Enable=2 while({flag1}==0){{}} Coord[1].DesTimeBase=Sys.ServoPeriod ''' else: 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 ''' 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 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 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, crdId=1): 'runs the code sync_run which has been generated with setup_sync()' comm = self.comm if comm is None: return gpascii = comm.gpascii try: cmd=self.sync_run except AttributeError: raise 'Need to call setup sync before' gpascii.send_block(cmd) 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('Coord[{crdId}].Q[0]'.format(crdId=crdId), 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)