#Script imported from: Check Linearity LS.xml

#Pre-actions
#p = subproccess.Popen('/bin/sleep 2' +, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#p.communicate()
#ret = p.returncode
#if ret <> true:
#   raise Exception('Script returned  with an exit value not equal to 0')
caput('PO2TC-NCS-LS:MOTOR.VELO', '50')
caput('PO2TC-NCS-LS:DEMAND', '350')

#TODO: Set the diplay names of positioners and detectors
scan = ManualScan(['idCOUNT'], ['idMSTA', 'idRVAL', 'idRRBV', 'idRBV', 'idVAL', 'idATHM', 'idENCODERraw', 'idENCODER', 'idRDY', 'idILK', 'idDiff', 'idResult'] , [0.0], [200.0], [200])
scan.start()

#Creating channels: dimension 1
#PseudoPositioner idCOUNT
#ScalarDetector idMSTA
idMSTA = Channel('PO2TC-NCS-LS:MOTOR.MSTA', type = 'd')
#ScalarDetector idRVAL
idRVAL = Channel('PO2TC-NCS-LS:MOTOR.RVAL', type = 'd')
#ScalarDetector idRRBV
idRRBV = Channel('PO2TC-NCS-LS:MOTOR.RRBV', type = 'd')
#ScalarDetector idRBV
idRBV = Channel('PO2TC-NCS-LS:MOTOR.RBV', type = 'd')
#ScalarDetector idVAL
idVAL = Channel('PO2TC-NCS-LS:MOTOR.VAL', type = 'd')
#ScalarDetector idATHM
idATHM = Channel('PO2TC-NCS-LS:MOTOR.ATHM', type = 'd')
#ScalarDetector idENCODERraw
idENCODERraw = Channel('PO2TC-NCS-LS:ENCODERraw', type = 'd')
#ScalarDetector idENCODER
idENCODER = Channel('PO2TC-NCS-LS:ENCODER', type = 'd')
#ScalarDetector idRDY
idRDY = Channel('PO2TC-NCS-LS:RDY', type = 'd')
#ScalarDetector idILK
idILK = Channel('PO2TC-NCS-LS:ILK', type = 'd')

#Dimension 1
#PseudoPositioner idCOUNT
for setpoint1 in range(0, 200):
    readback1 = setpoint1
    sleep( 0.02 ) # Settling time
    #Detector idMSTA
    detector1 = idMSTA.get()
    #Detector idRVAL
    detector2 = idRVAL.get()
    #Detector idRRBV
    detector3 = idRRBV.get()
    #Detector idRBV
    detector4 = idRBV.get()
    #Detector idVAL
    detector5 = idVAL.get()
    #Detector idATHM
    detector6 = idATHM.get()
    #Detector idENCODERraw
    detector7 = idENCODERraw.get()
    #Detector idENCODER
    detector8 = idENCODER.get()
    #Detector idRDY
    detector9 = idRDY.get()
    #Detector idILK
    detector10 = idILK.get()
    #Manipulation idResult
    #Variable Mappings
    diff =  idDiff
    count =  setpoint1
    if (count < 160) and (diff < -5):
       return( 1, "The motor/encoder position difference signal exceeded the lower limit (-5 deg)" )
    if (count < 160) and (diff > 5):
       return( 2, "The motor/encoder position difference signal exceeded the upper limit (+5 deg)" )
    if (count > 160) and (diff < -0.5):
       idResult = ( 3, "Note: The position difference at the end of movement was too great (more than -0.5 deg)" )
    if (count > 160) and (diff > 0.5):
       idResult = ( 4, "Note: The position difference at the end of movement was too great (more than  0.5 deg)" )             
    if (count < 160):
       idResult = ( 0, "Note: The motor drive linearity check did not yet complete.")       
    else:
       idResult = ( 0, "The motor drive linearity was within the acceptable limits.")       
     
    #Manipulation idDiff
    #Variable Mappings
    a =  detector4
    b =  detector8
    count =  setpoint1
    idDiff = a-b
    scan.append ([setpoint1], [readback1], [detector1, detector2, detector3, detector4, detector5, detector6, detector7, detector8, detector9, detector10, idResult, idDiff])

#Closing channels
idMSTA.close()
idRVAL.close()
idRRBV.close()
idRBV.close()
idVAL.close()
idATHM.close()
idENCODERraw.close()
idENCODER.close()
idRDY.close()
idILK.close()

scan.end()

#Post-actions
caput('PO2TC-NCS-LS:MOTOR.VELO', '270')
caput('PO2TC-NCS-LS:DEMAND', '0')
