Files
PBSwissMX/python/MXMotion.py
2019-01-18 12:00:18 +01:00

129 lines
4.5 KiB
Python

#!/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)
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 os, sys
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):
self.comm=comm
self.gather=gather
self.verbose=verbose
def setup_sync(self, crdId=1, prgId=2, plcId=2, encId=8, mode=0, **kwargs):
'''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
'''
if mode==0:
try:
del self.sync_prg
except AttributeError:
pass
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, crdId=crdId)
elif mode==1:
# code block to insert in the program
# - waits untis <flag> is false
# - waits raising edge of <flag> and the sets DesTimeBase=ServoPeriod
# flag ='PowerBrick[0].GpioData[0].0.1==1'
flag0='Gate3[1].Chan[0].UserFlag'
flag1='Gate3[1].Chan[1].UserFlag'
flag0='P0';flag1='P1'
prg = '''
Coord[1].Q[1]=-2
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)
self.sync_prg = prg
self.sync_run = '&{crdId}b{prgId}r'''.format(prgId=prgId, plcId=plcId, crdId=crdId)
elif mode==2:
#frequence jitter 50Hz Swissgrid:
#https://www.swissgrid.ch/de/home/operation/grid-data/current-data.html#
flag0='Gate3[1].Chan[0].UserFlag'
flag1='Gate3[1].Chan[1].UserFlag'
flag0='P0';flag1='P1'
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)
self.sync_prg=prg
self.sync_run='&{crdId}b{prgId}r'''.format(prgId=prgId, plcId=plcId, crdId=crdId)
#download and start triggerSync code
comm=self.comm
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)
self.cmdSync=cmd='LD_LIBRARY_PATH=/opt/ppmac/libppmac/ '+dst
#self.syncShell=comm.shell_channel(cmd)
#self.syncChan=sc=comm._client.exec_command(cmd)
#ch=sc[1].channel
#ch.settimeout(1)
#print ('starting '+cmd)
def run(self):
'runs the code sync_run which has been generated with setup_sync()'
comm = self.comm
gpascii = comm.gpascii
try:
cmd=self.sync_run
except AttributeError:
raise 'Need to call setup sync before'
gpascii.send_block(cmd)
dst='/tmp/triggerSync'
src=os.path.abspath(os.path.join(os.path.dirname(__file__), '../src/triggerSync/triggerSync'))
cmd='LD_LIBRARY_PATH=/opt/ppmac/libppmac/ '+dst
# self.syncShell=comm.shell_channel(cmd)
try:
cmd=self.cmdSync
except AttributeError: pass
else:
import time
time.sleep(1)
self.syncChan=sc=comm._client.exec_command(cmd)