Client examples

"""
    Library containning function for handling the ZMX 
    
"""
from PyTango import *
import sys
import time

def nb_devices(nb_zmx):
    """The nb_devices function is used to printout the number of ZMX devices found for the given root name."""
    
    print "Number of found ZMX devices: " + str(nb_zmx)


def general_info(nb_zmx, zmx_axus, zmx_device_name, zmx_proxy):
    """The general_info function is used to printout the general info (name/voltage/temperature/error) for all ZMX devices."""

    for j in range(0,int(nb_zmx)):
        print str(zmx_axis[j]) + " " + zmx_proxy[j].AxisName + " (" + zmx_device_name[j] + ") " + str(zmx_proxy[j].IntermediateVoltage) + " " + str(zmx_proxy[j].Temperature) + " " + str(zmx_proxy[j].Error)

def axis_info(nb_zmx, zmx_proxy, axis_nb):
    """The axis_info function is used to printout the info of the axis corresponding to the given index."""

    if axis_nb < nb_zmx:          
        print "ZMX axis: " + str(axis_nb)
        print "Axis name: " + str(zmx_proxy[axis_nb].AxisName)
        print "Version: " + str(zmx_proxy[axis_nb].VersionPS)
        print "Activation: " + str(zmx_proxy[axis_nb].DeactivationStr)
        print "Step resolution: " + str(zmx_proxy[axis_nb].StepWidthStr)
        print "Run current: " + str(zmx_proxy[axis_nb].RunCurrent) + " A"
        print "Stop current: " + str(zmx_proxy[axis_nb].StopCurrent) + " A"
        print "Motor direction: " + str(zmx_proxy[axis_nb].PreferentialDirectionStr)
        print "Operation mode: " + str(zmx_proxy[axis_nb].OperationModeStr)
        print "Delay time: " + str(zmx_proxy[axis_nb].DelayTime) + " mS"
        print "Voltage: " + str(zmx_proxy[axis_nb].IntermediateVoltage) + " V"
        print "Temperature: " + str(zmx_proxy[axis_nb].Temperature) + " C"
        print "Power stage status: " + str(zmx_proxy[axis_nb].PowerStageStatus)
        print "Error: " + str(zmx_proxy[axis_nb].Error)
        print "Path output files: " + str(zmx_proxy[axis_nb].PathOutputFiles)
        print "Save automatically to EPROM: " + str(zmx_proxy[axis_nb].FlagSaveAutomatically)
        print "Changes not saved: " + str(zmx_proxy[axis_nb].FlagChangeNotSaved)
        print "Error in connection: " + str(zmx_proxy[axis_nb].ErrorZMXConnection)
    else:
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)

def set_activation(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_activation function is used to set the activation value of the given axis ( 0 activate, 1 deactivate ) ."""

    if axis_nb > (nb_zmx - 1):
            print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:        
        if tmp_value != 0 and tmp_value != 1:
            print "Not allowed to set this value. 0 -> activate, 1 -> deactivate"
        else:       
            zmx_proxy[axis_nb].write_attribute("Deactivation",tmp_value)


def set_stepresolution(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_stepresolution function is used to set the step resolution of the given axis."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        if tmp_value < 0 or tmp_value > 13:
            print "Not allowed to set this value. Allowed values from 0 to 13"
        else:       
            zmx_proxy[axis_nb].write_attribute("StepWidth",tmp_value)

def set_preferentialdirection(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_activation function is used to set the preferential direction of the given axis ( 0 negative, 1 positive ) ."""

    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        if tmp_value != 0 and tmp_value != 1:
            print "Not allowed to set this value. 0 -> negative, 1 -> positive"
        else:       
            zmx_proxy[axis_nb].write_attribute("PreferentialDirection",tmp_value)
  
 
def set_delaytime(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_delaytime function is used to set the delay time of the given axis."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        if tmp_value < 0 or tmp_value > 15:
            print "Not allowed to set this value. Allowed values from 0 to 15"
        else:       
            zmx_proxy[axis_nb].write_attribute("DelayTime",tmp_value) 

 
def set_pathoutputfiles(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_pathoutputfiles function is used to set the path for the output files of the given axis."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].write_attribute("PathOutputFiles",tmp_value)


def set_autosavetoeprom(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The set_autosavetoeprom function is used to set/unset the automatic write to eprom of the given axis."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        if tmp_value != 0 and tmp_value != 1:
            print "Not allowed to set this value. 0 -> not auto save, 1 -> auto save"
        else:       
            zmx_proxy[axis_nb].write_attribute("FlagSaveAutomatically",tmp_value)
                   
def loadfromoutputfile(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The loadfromoutputfile function is used to load parameters from an external file."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].LoadFromOutputFile(tmp_value)


def savetooutputfile(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The savetooutputfile function is used to save parameters to en external file."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].SaveToOutputFile(tmp_value)

def readfilecontent(nb_zmx, zmx_proxy, axis_nb, tmp_value):
    """The readfilecontent function is used to read the parameters stored in an ouput file."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        contents = zmx_proxy[axis_nb].ReadFileContent(tmp_value)
        for element in contents:
            print element
            
def readfilesinoutputpath(nb_zmx, zmx_proxy, axis_nb):
    """The readfilesinoutputpath function is used to read the files in the ouput path."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        files = zmx_proxy[axis_nb].ReadFilesInOutputPath()
        for element in files:
            print element

def deleteeprom(nb_zmx, zmx_proxy, axis_nb):
    """The deleteeprom function is used to delete the EPROM contents."""
 
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else: 
        zmx_proxy[axis_nb].DeleteEPROM()


def writeeprom(nb_zmx, zmx_proxy, axis_nb):
    """The writeeprom function is used to write current settings to the EPROM."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].WriteEPROM()


def reset(nb_zmx, zmx_proxy, axis_nb):
    """The reset function is used to reset the power stage."""
 
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:   
        zmx_proxy[axis_nb].Reset()

def revminus(nb_zmx, zmx_proxy, axis_nb):
    """The revminus function is used to  perform a motor test: one motor rotation with preset run current (negative direction)."""

    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].RevMinus()

def revplus(nb_zmx, zmx_proxy, axis_nb):
    """The revplus function is used to  perform a motor test: one motor rotation with preset run current (positive direction)."""

    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].RevPlus()

def reseterrorzmxconnection(nb_zmx, zmx_proxy, axis_nb):
    """The reseterrorzmxconnection function is used to reset the flag indicating error in ZMX connection."""
    if axis_nb > (nb_zmx - 1):
        print "Not axis existing for that index. Maximum index value: " + str(nb_zmx - 1)
    else:
        zmx_proxy[axis_nb].ResetErrorZMXConnection()

#
# main
#
try :

    root_name =  "petra3/zmxps/"
    db = PyTango.Database()
    name_dev_ask = root_name + "*"
    devices = db.get_device_exported(name_dev_ask)
    zmx_proxy = []
    zmx_device_name = []
    zmx_axis = []
    nb_zmx = len(devices)
    j = 0
    for name in devices:
        zmx_device_name.append(name)
        zmx_proxy.append(DeviceProxy(name))
        zmx_axis.append(j)
        j = j + 1
    
    if( len( sys.argv) == 1):
        nb_devices(nb_zmx)
        general_info(nb_zmx,zmx_axis,zmx_device_name,zmx_proxy)
        print "                 "
        print "Run 'python zmx.py help' to see program options"
        print "                 "
    else:
        if(sys.argv[1] == "help"):
            print "Usage:"
            print "-> to read axis info"
            print "python zmx.py axis_info axis_number"
            print "-> to change activation value"
            print "python zmx.py set_activation axis_number value_to_set"
            print "with value_to_set from 0 to 1:  0 = active, 1 = deactive"
            print "-> to change step resolution"
            print "python zmx.py set_stepresolution axis_number value_to_set"
            print "with value_to_set from 0 to 13:  0 = 1/1, 1 = 1/2, 2 = 1/2.5, 3 = 1/4, 4 = 1/8, 5 = 1/8, 6 = 1/10, 7 = 1/16, 8 = 1/20, 9 = 1/32, 10 = 1/64, 11 = 1/128, 12 = 1/256, 13 = 1/512"
            print "-> to change preferential direction"
            print "python zmx.py set_preferentialdirection axis_number value_to_set"
            print "with value_to_set from 0 to 1:  0 = negative, 1 = positive"
            print "-> to change delay time"
            print "python zmx.py set_delaytime axis_number value_to_set"
            print "with value_to_set from 0 to 15: 0=1, 1=2,2=4,3=6,4=8,5=10,6=12,7=14,8=16,9=20,10=40, 11=60,12=100, 13=200, 14=500, 15=1000 (mS)"
            print "-> to set path for output files"
            print "python zmx.py set_pathoutputfiles axis_number new_path"
            print "-> to change flag for auto save to EPROM"
            print "python zmx.py set_autosavetoeprom axis_number value_to_set"
            print "with value_to_set from 0 to 1:  0 -> not auto save, 1 -> auto save"
            print "-> to load settings from output file"
            print "python zmx.py loadfromoutputfile axis_number file_to_load"
            print "with file_to_load the name of the file to be load, it has to be in the pathoutputfiles directory"
            print "-> to save settings to output file"
            print "python zmx.py savetooutputfile axis_number file_name"
            print "with file_name the name of the file, it will be in the pathoutputfiles directory"
            print "-> to read file content"
            print "python zmx.py readfilecontent axis_number file_name"
            print "with file_name the name of the file, that has to be in the pathoutputfiles directory"
            print "-> to read file names in pathoutputfiles directory"
            print "python zmx.py readfilesinoutputpath axis_number"
            print "-> to delete EPROM"
            print "python zmx.py deleteeprom axis_number"
            print "-> to write to EPROM"
            print "python zmx.py writeeprom axis_number"
            print "-> to reset power stage"
            print "python zmx.py reset axis_number"
            print "-> to perform motor test (negative direction)"
            print "python zmx.py revminus axis_number"
            print "-> to perform motor test (positive direction)"
            print "python zmx.py revplus axis_number"
            print "-> to reset the error in ZMX connection"
            print "python zmx.py reseterrorzmxconnection axis_number"

        elif(sys.argv[1] == "axis_info"):
            axis_info(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "set_activation"):
            print "Setting activation value to " + str(sys.argv[3])
            set_activation(nb_zmx, zmx_proxy, int(sys.argv[2]),int(sys.argv[3]) )
        elif(sys.argv[1] == "set_stepresolution"):
            print "Setting step resolution to " + str(sys.argv[3])
            set_stepresolution(nb_zmx, zmx_proxy, int(sys.argv[2]),int(sys.argv[3]))
        elif(sys.argv[1] == "set_preferentialdirection"):
            print "Setting preferential direction to " + str(sys.argv[3])
            set_preferentialdirection(nb_zmx, zmx_proxy, int(sys.argv[2]),int(sys.argv[3]))
        elif(sys.argv[1] == "set_delaytime"):
            print "Setting delay time to " + str(sys.argv[3])
            set_delaytime(nb_zmx, zmx_proxy, int(sys.argv[2]),int(sys.argv[3]))
        elif(sys.argv[1] == "set_pathoutputfiles"):
            print "Setting path for output files to " + str(sys.argv[3])
            set_pathoutputfiles(nb_zmx, zmx_proxy, int(sys.argv[2]),str(sys.argv[3]))
        elif(sys.argv[1] == "set_autosavetoeprom"):
            print "Changing auto save flag to " + str(sys.argv[3])
            set_autosavetoeprom(nb_zmx, zmx_proxy, int(sys.argv[2]),int(sys.argv[3]))
        elif(sys.argv[1] == "loadfromoutputfile"):
            print "Loading settings from file " + str(sys.argv[3])
            loadfromoutputfile(nb_zmx, zmx_proxy, int(sys.argv[2]),str(sys.argv[3]))
        elif(sys.argv[1] == "savetooutputfile"):
            print "Save settings to file " + str(sys.argv[3])
            savetooutputfile(nb_zmx, zmx_proxy, int(sys.argv[2]),str(sys.argv[3]))
        elif(sys.argv[1] == "readfilecontent"):
            print "Reading contents of file " + str(sys.argv[3])
            readfilecontent(nb_zmx, zmx_proxy, int(sys.argv[2]),str(sys.argv[3]))
        elif(sys.argv[1] == "readfilesinoutputpath"):
            print "Reading files in pathoutputfiles directory"
            readfilesinoutputpath(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "deleteeprom"):
            print "Deleting EPROM"
            deleteeprom(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "writeeprom"):
            print "Writting EPROM"
            writeeprom(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "reset"):
            print "Reset power stage"
            reset(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "revminus"):
            print "Perform motor test (negative direction)"
            revminus(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "revplus"):
            print "Perform motor test (positive direction)"
            revplus(nb_zmx, zmx_proxy, int(sys.argv[2]))
        elif(sys.argv[1] == "reseterrorzmxconnection"):
            print "Setting error ZMX connection to false"
            reseterrorzmxconnection(nb_zmx, zmx_proxy, int(sys.argv[2]))
        else:
            print "Run 'python zmx.py help' to see program options"
                    
                 
    
except Exception, inst :
    print "Failed with exception !"
    print sys.exc_info()[0]
    print type(inst)     # the exception instance
    print inst.args      # arguments stored in .args