/usr/lib/python2.7/dist-packages/sardana/macroserver/macros/standard.py is in python-sardana 1.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | ##############################################################################
##
## This file is part of Sardana
##
## http://www.tango-controls.org/static/sardana/latest/doc/html/index.html
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Sardana is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Sardana is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with Sardana. If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################
"""This is the standard macro module"""
__all__ = ["ct", "mstate", "mv", "mvr", "pwa", "pwm", "set_lim", "set_lm",
"set_pos", "settimer", "uct", "umv", "umvr", "wa", "wm"]
__docformat__ = 'restructuredtext'
import datetime
from taurus.console.table import Table
from PyTango import DevState
from sardana.macroserver.macro import Macro, macro, Type, ParamRepeat, ViewOption
################################################################################
#
# Motion related macros
#
################################################################################
class _wm(Macro):
"""Show motor positions"""
param_def = [
['motor_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to move']),
None, 'List of motor to show'],
]
def prepare(self, *motor_list, **opts):
self.table_opts = {}
def run(self, *motor_list):
show_dial = self.getViewOption(ViewOption.ShowDial)
motor_width = 9
motor_names = []
motor_pos = []
motor_list = list(motor_list)
motor_list.sort()
for motor in motor_list:
name = motor.getName()
motor_names.append([name])
pos = motor.getPosition(force=True)
if pos is None:
pos = float('NAN')
if show_dial:
dial_pos = motor.getDialPosition(force=True)
if dial_pos is None:
dial_pos = float('NAN')
motor_pos.append((pos,dial_pos))
else:
motor_pos.append((pos,))
motor_width = max(motor_width,len(name))
fmt = '%c*.%df' % ('%',motor_width - 5)
table = Table(motor_pos, elem_fmt=[fmt],
col_head_str=motor_names, col_head_width=motor_width,
**self.table_opts)
for line in table.genOutput():
self.output(line)
class _wum(Macro):
"""Show user motor positions"""
param_def = [
['motor_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to move']),
None, 'List of motor to show'],
]
def prepare(self, *motor_list, **opts):
self.table_opts = {}
def run(self, *motor_list):
show_dial = self.getViewOption(ViewOption.ShowDial)
motor_width = 9
motor_names = []
motor_pos = []
motor_list = list(motor_list)
motor_list.sort()
for motor in motor_list:
name = motor.getName()
motor_names.append([name])
pos = motor.getPosition(force=True)
if pos is None:
pos = float('NAN')
motor_pos.append((pos,))
motor_width = max(motor_width,len(name))
fmt = '%c*.%df' % ('%',motor_width - 5)
table = Table(motor_pos, elem_fmt=[fmt],
col_head_str=motor_names, col_head_width=motor_width,
**self.table_opts)
for line in table.genOutput():
self.output(line)
class wu(Macro):
"""Show all user motor positions"""
def prepare(self, **opts):
self.all_motors = self.findObjs('.*', type_class=Type.Moveable)
self.table_opts = {}
def run(self):
nr_motors = len(self.all_motors)
if nr_motors == 0:
self.output('No motor defined')
return
self.output('Current positions (user) on %s'%datetime.datetime.now().isoformat(' '))
self.output('')
self.execMacro('_wum',*self.all_motors, **self.table_opts)
class wa(Macro):
"""Show all motor positions"""
def prepare(self, **opts):
self.all_motors = self.findObjs('.*', type_class=Type.Moveable)
self.table_opts = {}
def run(self):
nr_motors = len(self.all_motors)
if nr_motors == 0:
self.output('No motor defined')
return
show_dial = self.getViewOption(ViewOption.ShowDial)
if show_dial:
self.output('Current positions (user, dial) on %s'%datetime.datetime.now().isoformat(' '))
else:
self.output('Current positions (user) on %s'%datetime.datetime.now().isoformat(' '))
self.output('')
self.execMacro('_wm',*self.all_motors, **self.table_opts)
class pwa(Macro):
"""Show all motor positions in a pretty table"""
def run(self):
self.execMacro('wa', **Table.PrettyOpts)
class set_lim(Macro):
"""Sets the software limits on the specified motor hello"""
param_def = [
['motor', Type.Moveable, None, 'Motor name'],
['low', Type.Float, None, 'lower limit'],
['high', Type.Float, None, 'upper limit']
]
def run(self, motor, low, high):
name = motor.getName()
self.debug("Setting user limits for %s" % name)
motor.getPositionObj().setLimits(low,high)
self.output("%s limits set to %.4f %.4f (user units)" % (name, low, high))
class set_lm(Macro):
"""Sets the dial limits on the specified motor"""
param_def = [
['motor', Type.Motor, None, 'Motor name'],
['low', Type.Float, None, 'lower limit'],
['high', Type.Float, None, 'upper limit']
]
def run(self, motor, low, high):
name = motor.getName()
self.debug("Setting dial limits for %s" % name)
motor.getDialPositionObj().setLimits(low,high)
self.output("%s limits set to %.4f %.4f (dial units)" % (name, low, high))
class set_pos(Macro):
"""Sets the position of the motor to the specified value"""
param_def = [
['motor', Type.Motor, None, 'Motor name'],
['pos', Type.Float, None, 'Position to move to']
]
def run(self, motor, pos):
name = motor.getName()
old_pos = motor.getPosition(force=True)
motor.definePosition(pos)
self.output("%s reset from %.4f to %.4f" % (name, old_pos, pos))
class set_user_pos(Macro):
"""Sets the USER position of the motor to the specified value (by changing OFFSET and keeping DIAL)"""
param_def = [
['motor', Type.Motor, None, 'Motor name'],
['pos', Type.Float, None, 'Position to move to']
]
def run(self, motor, pos):
name = motor.getName()
old_pos = motor.getPosition(force=True)
offset_attr = motor.getAttribute('Offset')
old_offset = offset_attr.read().value
new_offset = pos - (old_pos - old_offset)
offset_attr.write(new_offset)
self.output("%s reset from %.4f (offset %.4f) to %.4f (offset %.4f)" % (name, old_pos, old_offset, pos, new_offset))
class wm(Macro):
"""Show the position of the specified motors."""
param_def = [
['motor_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to see where it is']),
None, 'List of motor to show'],
]
def prepare(self, *motor_list, **opts):
self.table_opts = {}
def run(self, *motor_list):
motor_width = 10
motor_names = []
motor_pos = []
show_dial = self.getViewOption(ViewOption.ShowDial)
for motor in motor_list:
name = motor.getName()
motor_names.append([name])
posObj = motor.getPositionObj()
upos = map(str, [posObj.getMaxValue(), motor.getPosition(force=True), posObj.getMinValue()])
pos_data = [''] + upos
if show_dial:
dPosObj = motor.getDialPositionObj()
dpos = map(str, [dPosObj.getMaxValue(), motor.getDialPosition(force=True), dPosObj.getMinValue()])
pos_data += [''] + dpos
motor_pos.append(pos_data)
elem_fmt = (['%*s'] + ['%*s'] * 3) * 2
row_head_str = ['User', ' High', ' Current', ' Low']
if show_dial:
row_head_str += ['Dial', ' High', ' Current', ' Low']
table = Table(motor_pos, elem_fmt=elem_fmt, row_head_str=row_head_str,
col_head_str=motor_names, col_head_width=motor_width,
**self.table_opts)
for line in table.genOutput():
self.output(line)
class wum(Macro):
"""Show the user position of the specified motors."""
param_def = [
['motor_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to see where it is']),
None, 'List of motor to show'],
]
def prepare(self, *motor_list, **opts):
self.table_opts = {}
def run(self, *motor_list):
motor_width = 10
motor_names = []
motor_pos = []
for motor in motor_list:
name = motor.getName()
motor_names.append([name])
posObj = motor.getPositionObj()
upos = map(str, [posObj.getMaxValue(), motor.getPosition(force=True), posObj.getMinValue()])
pos_data = [''] + upos
motor_pos.append(pos_data)
elem_fmt = (['%*s'] + ['%*s'] * 3) * 2
row_head_str = ['User', ' High', ' Current', ' Low',]
table = Table(motor_pos, elem_fmt=elem_fmt, row_head_str=row_head_str,
col_head_str=motor_names, col_head_width=motor_width,
**self.table_opts)
for line in table.genOutput():
self.output(line)
class pwm(Macro):
"""Show the position of the specified motors in a pretty table"""
param_def = [
['motor_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to move']),
None, 'List of motor to show'],
]
def run(self, *motor_list):
self.execMacro('wm', *motor_list, **Table.PrettyOpts)
class mv(Macro):
"""Move motor(s) to the specified position(s)"""
param_def = [
['motor_pos_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to move'],
['pos', Type.Float, None, 'Position to move to']),
None, 'List of motor/position pairs'],
]
def run(self, *motor_pos_list):
motors, positions = [], []
for motor, pos in motor_pos_list:
motors.append(motor)
positions.append(pos)
self.debug("Starting %s movement to %s", motor.getName(), pos)
motion = self.getMotion(motors)
state, pos = motion.move(positions)
if state != DevState.ON:
self.warning("Motion ended in %s", state)
msg = []
for motor in motors:
msg.append(motor.information())
self.info("\n".join(msg))
class mstate(Macro):
"""Prints the state of a motor"""
param_def = [['motor', Type.Moveable, None, 'Motor to check state']]
def run(self, motor):
self.info("Motor %s" % str(motor.getState()))
class umv(Macro):
"""Move motor(s) to the specified position(s) and update"""
param_def = mv.param_def
def prepare(self, *motor_pos_list, **opts):
self.all_names = []
self.all_pos = []
self.print_pos = False
for motor, pos in motor_pos_list:
self.all_names.append([motor.getName()])
pos, posObj = motor.getPosition(force=True), motor.getPositionObj()
self.all_pos.append([pos])
posObj.subscribeEvent(self.positionChanged, motor)
def run(self, *motor_pos_list):
self.print_pos = True
pars = []
for motor, pos in motor_pos_list:
pars.append(motor)
pars.append(pos)
self.execMacro('mv', *pars)
self.finish()
def finish(self):
self._clean()
self.printAllPos()
def on_abort(self):
self.finish()
def _clean(self):
for motor, pos in self.getParameters():
posObj = motor.getPositionObj()
try:
posObj.unsubscribeEvent(self.positionChanged, motor)
except Exception, e:
print str(e)
raise e
def positionChanged(self, motor, position):
idx = self.all_names.index([motor.getName()])
self.all_pos[idx] = [position]
if self.print_pos:
self.printAllPos()
def printAllPos(self):
motor_width = 10
table = Table(self.all_pos, elem_fmt=['%*.4f'],
col_head_str=self.all_names, col_head_width=motor_width)
self.outputBlock(table.genOutput())
self.flushOutput()
class mvr(Macro):
"""Move motor(s) relative to the current position(s)"""
param_def = [
['motor_disp_list',
ParamRepeat(['motor', Type.Moveable, None, 'Motor to move'],
['disp', Type.Float, None, 'Relative displacement']),
None, 'List of motor/displacement pairs'],
]
def run(self, *motor_disp_list):
motor_pos_list = []
for motor, disp in motor_disp_list:
pos = motor.getPosition(force=True)
if pos is None:
self.error("Cannot get %s position" % motor.getName())
return
else:
pos += disp
motor_pos_list.extend([motor, pos])
self.execMacro('mv', *motor_pos_list)
class umvr(Macro):
"""Move motor(s) relative to the current position(s) and update"""
param_def = mvr.param_def
def run(self, *motor_disp_list):
motor_pos_list = []
for motor, disp in motor_disp_list:
pos = motor.getPosition(force=True)
if pos is None:
self.error("Cannot get %s position" % motor.getName())
return
else:
pos += disp
motor_pos_list.extend([motor, pos])
self.execMacro('umv', *motor_pos_list)
################################################################################
#
# Data acquisition related macros
#
################################################################################
class ct(Macro):
"""Count for the specified time on the active measurement group"""
env = ('ActiveMntGrp',)
param_def = [
['integ_time', Type.Float, 1.0, 'Integration time']
]
def prepare(self, integ_time, **opts):
mnt_grp_name = self.getEnv('ActiveMntGrp')
self.mnt_grp = self.getObj(mnt_grp_name, type_class=Type.MeasurementGroup)
def run(self, integ_time):
self.output("hello")
if self.mnt_grp is None:
self.error('ActiveMntGrp is not defined or has invalid value')
return
self.debug("Counting for %s sec", integ_time)
self.outputDate()
self.output('')
self.flushOutput()
state, data = self.mnt_grp.count(integ_time)
names, counts = [], []
for ch_info in self.mnt_grp.getChannelsInfo():
names.append(' %s' % ch_info.label)
ch_data = data.get(ch_info.full_name)
if ch_info.shape > [1]:
counts.append(list(ch_data.shape))
else:
counts.append(ch_data)
table = Table([counts], row_head_str=names, row_head_fmt='%*s',
col_sep=' = ')
for line in table.genOutput():
self.output(line)
class uct(Macro):
"""Count on the active measurement group and update"""
env = ('ActiveMntGrp',)
param_def = [
['integ_time', Type.Float, 1.0, 'Integration time']
]
def prepare(self, integ_time, **opts):
self.print_value = False
mnt_grp_name = self.getEnv('ActiveMntGrp')
self.mnt_grp = self.getObj(mnt_grp_name, type_class=Type.MeasurementGroup)
if self.mnt_grp is None:
return
names, nan = self.mnt_grp.getChannelLabels(), float('nan')
self.names = [ [n] for n in names ]
self.values = len(names)*[ [nan] ]
self.channels = self.mnt_grp.getChannelAttrExs()
for ch_attr_ex in self.channels:
ch_attr_ex.subscribeEvent(self.counterChanged, ch_attr_ex)
def printAllValues(self):
ch_width = 10
table = Table(self.values, elem_fmt=['%*.4f'], col_head_str=self.names,
col_head_width=ch_width)
self.outputBlock(table.genOutput())
self.flushOutput()
def counterChanged(self, ch_attr, value):
idx = self.channels.index(ch_attr)
self.values[idx] = [value]
if self.print_value:
self.printAllValues()
def run(self, integ_time):
if self.mnt_grp is None:
self.error('ActiveMntGrp is not defined or has invalid value')
return
self.print_value = True
state, data = self.mnt_grp.count(integ_time)
for ch_attr_ex in self.mnt_grp.getChannelAttrExs():
ch_attr_ex.unsubscribeEvent(self.counterChanged, ch_attr_ex)
self.printAllValues()
class settimer(Macro):
"""Defines the timer channel for the active measurement group"""
env = ('ActiveMntGrp',)
param_def = [
['timer', Type.ExpChannel, None, 'Timer'],
]
def run(self,timer):
mnt_grp_name = self.getEnv('ActiveMntGrp')
mnt_grp = self.getObj(mnt_grp_name, type_class=Type.MeasurementGroup)
if mnt_grp is None:
self.error('ActiveMntGrp is not defined or has invalid value.\n' \
'please define a valid active measurement group ' \
'before setting a timer')
return
try:
mnt_grp.setTimer(timer.getName())
except Exception,e:
self.output(str(e))
self.output("%s is not a valid channel in the active measurement group" % timer)
@macro([['message', ParamRepeat(['message_item', Type.String, None, 'message item to be reported']), None, 'message to be reported']])
def report(self, *message):
"""Logs a new record into the message report system (if active)"""
self.report(' '.join(message))
|