Move a Motor Asynchronously

Here is an example for executing an asynchronous move by a Macro class.

#!/usr/bin/env python
# 
# file name: /home/pXXuser/sardanaMacros/MoveMotorSync_class.py 
#
"""this module contains some demo code"""

__all__ = ["MoveMotorAsync_class"]

from sardana.macroserver.macro import Macro, Type
import time
import PyTango

class MoveMotorAsync_class(Macro):
    """moves a motor to a target position, asynchronously"""
    param_def = [
        [ "mot1", Type.Moveable, None, "the motor to be moved" ],
        [ "target", Type.Float, None, "the target position" ],
        ]
    def run(self, mot1, target):
        self.output( dir(mot1))
        self.output( "MoveMotorAsync: %s is at %g %s " %
                     (mot1.name, mot1.position, mot1.state))
        mot1.write_attribute( "position", target)
        while mot1.state == PyTango.DevState.MOVING:
            time.sleep(0.1)
            self.output( "MoveMotorAsync: %s now at %g %s " %
                         (mot1.name,
                          mot1.position,
                          mot1.state))
        self.output( "MoveMotorAsync: %s finally at %g %s " %
                     (mot1.name,
                      mot1.position,
                      mot1.state))

Notice that mot1.write_attribute( "position", target) has been used to move the motor. We use this syntax and not mot1.position = target because the latter form was partially not supported.



2019-11-13