1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#!/usr/bin/env python
"""
Epics scan record
"""
from .. import Device, poll
import threading
NUM_POSITIONERS = 4
NUM_TRIGGERS = 4
NUM_DETECTORS = 70
class Scan(Device):
"""
A Device representing an Epics sscan record.
"""
attrs = ('VAL', 'SMSG', 'CMND', 'NPTS', 'EXSC', 'NAME', 'PDLY',
'PAUS', 'CPT', 'DDLY')
pos_attrs = ('PV', 'SP', 'EP', 'SI', 'CP', 'WD', 'PA', 'AR', 'SM')
trig_attrs = ('PV', 'NV')
_alias = {'device': 'P1PV',
'start': 'P1SP',
'end': 'P1EP',
'step': 'P1SI',
'table': 'P1PA',
'absrel': 'P1AR',
'mode': 'P1SM',
'npts': 'NPTS',
'execute': 'EXSC',
'trigger': 'T1PV',
'pause': 'PAUS',
'current_point': 'CPT'}
def __init__(self, name, **kwargs):
"""
Initialize the scan.
name: The name of the scan record.
"""
attrs = list(self.attrs)
for i in range(1, NUM_POSITIONERS+1):
for a in self.pos_attrs:
attrs.append('P%i%s' % (i, a))
for i in range(1, NUM_TRIGGERS+1):
for a in self.trig_attrs:
attrs.append('T%i%s' % (i, a))
for i in range(1, NUM_DETECTORS+1):
attrs.append('D%2.2iPV' % i)
self.waitSemaphore = threading.Semaphore(0)
Device.__init__(self, name, delim='.', attrs=attrs, **kwargs)
for attr, pv in Scan._alias.items():
self.add_pv('%s.%s' % (name,pv), attr)
# make sure this is really a sscan!
rectype = self.get('RTYP')
if rectype != 'sscan':
raise ScanException("%s is not an Epics Scan" % name)
self.put('SMSG', '')
def run(self, wait=False, timeout=86400):
"""
Execute the scan, optionally waiting for completion
Arguments
---------
wait whether to wait for completion, True/False (default False)
timeout maximum time to wait in seconds, default=86400 (1 day).
"""
self.put('EXSC', 1, wait=wait, timeout=timeout)
def _onDone(self, **kwargs):
if kwargs['value'] == 0:
self.waitSemaphore.release()
def reset(self):
"""Reset scan, clearing positioners, detectors, triggers"""
self.put('NPTS', 0)
for i in range(1, NUM_TRIGGERS+1):
self.clear_trigger(i)
for i in range(1, NUM_POSITIONERS+1):
self.clear_positioner(i)
for i in range(1, NUM_DETECTORS+1):
self.clear_detector(i)
poll(1.e-3, 1.0)
def _print(self):
print('PV = %s' % self.get('P1PV'))
print('SP = %s' % self.get('P1SP'))
print('EP = %s' % self.get('P1EP'))
print('NPTS = %s' % self.get('NPTS'))
print('T = %s' % self.get('T1PV'))
def clear_detector(self, idet=1):
"""completely clear a detector
Arguments
---------
idet index of detector (1 through 70, default 1)
"""
self.put("D%2.2iPV" % idet, '')
poll(1.e-3, 1.0)
def add_detector(self, detector):
"""add a detector to a scan definition
Arguments
---------
detector name of detector pv
Returns
-------
idet index of detector set
"""
idet = None
for _idet in range(1, NUM_DETECTORS+1):
poll(1.e-3, 1.0)
if len(self.get('D%2.2iPV' % _idet)) < 2:
idet = _idet
break
if idet is None:
raise ScanException("%i Detectors already defined." % (NUM_DETECTORS))
self.put("D%2.2iPV" % idet, detector, wait=True)
return idet
def clear_trigger(self, itrig=1):
"""completely clear a trigger
Arguments
---------
itrig index of trigger (1 through 4, default 1)
"""
self.put("T%iPV" % itrig, '')
poll(1.e-3, 1.0)
def add_trigger(self, trigger, value=1.0):
"""add a trigger to a scan definition
Arguments
---------
trigger name of trigger pv
value value to send to trigger (default 1.0)
Returns
-------
itrig index of trigger set
"""
itrig = None
for _itrig in range(1, NUM_TRIGGERS+1):
poll(1.e-3, 1.0)
if len(self.get('T%iPV' % _itrig)) < 2:
itrig = _itrig
break
if itrig is None:
raise ScanException("%i Triggers already defined." % (NUM_TRIGGERS))
self.put("T%iPV" % itrig, trigger, wait=True)
self.put("T%iCD" % itrig, value, wait=True)
return itrig
def clear_positioner(self, ipos=1):
"""completely clear a positioner
Arguments
---------
ipos index of positioner (1 through 4, default 1)
"""
for attr in self.pos_attrs:
nulval = 0
if attr == 'PV': nulval = ''
if attr == 'PA': nulval = [0]
self.put("P%i%s" % (ipos, attr), nulval)
self.put("R%iPV" % ipos, '')
poll(1.e-3, 1.0)
def add_positioner(self, drive, readback=None,
start=None, stop=None, step=None,
center=None, width=None,
mode='linear', absolute=True, array=None):
"""add a positioner to a scan definition
Arguments
----------
drive name of drive pv
readback name of readback pv (defaults to .RBV if drive ends in .VAL)
mode positioner mode ('linear', 'table', fly', default 'linear')
absolute whether to use absolute values (True/False, default True)
start start value
stop stop value
step step value
center center value
width width value
array array of values for table or fly mode
Returns
-------
ipos index of positioner set
"""
ipos = None
for _ipos in range(1, NUM_POSITIONERS+1):
poll(1.e-3, 1.0)
if len(self.get('P%iPV' % _ipos)) < 2:
ipos = _ipos
break
if ipos is None:
raise ScanException("%i Positioners already defined." % (NUM_POSITIONERS))
self.put('P%iPV' % ipos, drive, wait=True)
if readback is None and drive.endswith('.VAL'):
readback = drive[:-4] + '.RBV'
if readback is not None:
self.put('R%iPV' % ipos, readback)
# set relative/absolute
if absolute:
self.put('P%iAR' % ipos, 0)
else:
self.put('P%iAR' % ipos, 1)
# set mode
smode = 0
if mode.lower().startswith('table'):
smode = 1
elif mode.lower().startswith('fly'):
smode = 2
self.put('P%iSM' % ipos, smode)
# start, stop, step, center, width
if start is not None:
self.put('P%iSP' % ipos, start)
if stop is not None:
self.put('P%iEP' % ipos, stop)
if step is not None:
self.put('P%iSI' % ipos, step)
if center is not None:
self.put('P%iCP' % ipos, center)
if width is not None:
self.put('P%iWD' % ipos, width)
# table or fly mode
if smode in (1, 2) and array is not None:
self.put('P%iPA' % ipos, array)
poll(1.e-3, 1.0)
return ipos
def set_positioner(self, ipos, drive=None, readback=None,
start=None, stop=None, step=None,
center=None, width=None,
mode=None, absolute=None, array=None):
"""change a positioner setting in a scan definition
all settings are optional, and will leave other settings unchanged
Arguments
----------
drive name of drive pv
readback name of readback pv
mode positioner mode ('linear', 'table', fly', default 'linear')
absolute whether to use absolute values (True/False, default True)
start start value
stop stop value
step step value
center center value
width width value
array array of values for table or fly mode
Notes
-----
This allows changing a scan, for example:
s = Scan('XXX:scan1')
ipos1 = s.add_positioner('XXX:m1.VAL', start=-1, stop=1, step=0.1)
....
s.run()
Then changing the scan definition with
s.set_positioner(ipos1, start=0, stop=0.2, step=0.01)
s.run()
"""
if ipos is None:
raise ScanException("must give positioner index")
if drive is not None:
self.put('P%iPV' % ipos, drive)
if readback is not None:
self.put('R%iPV' % ipos, readback)
if start is not None:
self.put('P%iSP' % ipos, start)
if stop is not None:
self.put('P%iEP' % ipos, stop)
if step is not None:
self.put('P%iSI' % ipos, step)
if center is not None:
self.put('P%iCP' % ipos, center)
if width is not None:
self.put('P%iWD' % ipos, width)
if array is not None:
self.put('P%iPA' % ipos, array)
if absolute is not None:
if absolute:
self.put('P%iAR' % ipos, 0)
else:
self.put('P%iAR' % ipos, 1)
if mode is not None:
smode = 0
if mode.lower().startswith('table'):
smode = 1
elif mode.lower().startswith('fly'):
smode = 2
self.put('P%iSM' % ipos, smode)
poll(1.e-3, 1.0)
def after_scan(self, mode):
"""set after scan mode"""
self.put("PASM", mode, wait=True)
def positioner_delay(self, pdelay):
"""set positioner delay in seconds"""
self.put("PDLY", pdelay, wait=True)
def detector_delay(self, pdelay):
"""set detector delay in seconds"""
self.put("DDLY", pdelay, wait=True)
class ScanException(Exception):
""" raised to indicate a problem with a scan"""
def __init__(self, msg, *args):
Exception.__init__(self, *args)
self.msg = msg
def __str__(self):
return str(self.msg)
|