Find an optic barrier( Lichtschranke, P23)

#!/bin/env python
# 
# file name: $HOME/sardanaMacros/lsFind.py
#
import PyTango
import time
"""
this macro finds a reference mark defined by a Lichtschranke
"""

hsh = { 
    'dett': { 'ireg': 'eh_ireg06', 'start': 22, 'stop': 24, 'velReduction': 0.1},
}

__all__ = ["lsFind"]

from sardana.macroserver.macro import Macro, Type

class lsFind(Macro):
    """
    find a reference mark (optical barrier). An input register is read while 
    a motor is moving. If the ireg goes to 1, the motor is stopped.
    The coarse search is done at full speed, the fine search is done
    with reduces velocity.
    """
    param_def = [
        [ "motorLS", Type.Motor, None, "the motor to be moved over the reference mark" ],
        ]
    def run(self, motorLS):

        if not hsh.has_key( motorLS.name):
            self.output( "lsFind: failed to identify %s" % motorLS.name)
            return

        try:
            motor = PyTango.DeviceProxy( motorLS.name)
            ireg = PyTango.DeviceProxy( hsh[motorLS.name]['ireg'])
        except Exception, e:
            self.output( "lsFind: failed to create proxies, %s" % (motorLS.name))
            self.output( "lsFind: %s" % repr( e))
            return 

        start = hsh[motorLS.name]['start']
        stop = hsh[motorLS.name]['stop']
        velReduction = hsh[motorLS.name]['velReduction']
        self.output( "Motor: %s" % motor.name())
        self.output( "IReg:  %s" % ireg.name())
        self.output( "start:  %g" % start)
        self.output( "stop:  %g" % stop)
        self.output( "velReduction:  %g" % velReduction)
        
        velOld = motor.velocity

        self.output( "Coarse: %s is at %g, move to start %g" % 
                     (motor.name(), motor.position, start))
        self.mv( motorLS, start)

        self.output( "Coarse: %s is at %g, move to stop %g" % 
                     (motor.name(), motor.position, stop))
        motor.position = stop

        while motor.state() == PyTango.DevState.MOVING:
            if ireg.value == 1:
                self.output( "coarse: ireg == 1 at %g, stopping" % motor.position)
                motor.stop()
                break
        else:
            self.output( "find-coarse failed, change interval or reduce speed")
            return 

        rangeFine = 0.2
        self.output( "Fine: %s is at %g, move to start %g" % 
                     (motor.name(), motor.position, motor.position - rangeFine))
        self.mv( motorLS, motor.position - rangeFine)
        
        motor.velocity = motor.velocity * velReduction
        
        self.output( "fine: %s is at %g, move to stop %g" % 
                     (motor.name(), motor.position, motor.position + 2.*rangeFine))
        
        motor.position = motor.position + 2.*rangeFine

        while motor.state() == PyTango.DevState.MOVING:
            if ireg.value == 1:
                self.output( "ireg == 1 at %g, stopping" % motor.position)
                motor.stop()
                break

        motor.velocity = velOld



2019-11-13