import subprocess import time import traceback running = False counts = {} def countHLS(motor): global counts try: time.sleep(1.0) counts[motor]=0 def on_change(value): global counts print "LimitSW changed on" , motor, "to", value if int(value) == 1: counts[motor]=counts[motor]+1 with Channel(motor+".HLS", callback=on_change, monitored=True, name="test") as ch: if int(ch.read()) == 1: counts[motor]=1 while(running==True): time.sleep(0.1) print "High Limit Switch motor "+motor +" reached "+ str(counts[motor]) + " times\n" return counts[motor] except: print "Exception counting "+str(motor) traceback.print_exc() raise def countLLS(motor): global counts try: time.sleep(1.0) counts[motor]=0 def on_change(value): global counts print "LimitSW changed on" , motor, "to", value if int(value) == 1: counts[motor]=counts[motor]+1 with Channel(motor+".LLS", callback=on_change, monitored=True, name="test") as ch: if int(ch.read()) == 1: counts[motor]=1 while(running==True): time.sleep(0.1) print "Low Limit Switch motor "+motor +" reached "+ str(counts[motor]) + " times\n" return counts[motor] except: print "Exception counting "+str(motor) traceback.print_exc() raise def init(): try: global running running = True subprocess.call("X_X11MA_init-AU.sh", shell=True) # Positions at limit switches are not checked time.sleep(1.0) caput('X11MA-OP2-AVsize',20) caput('X11MA-OP2-AHsize',20) running = False return 0 except: print "Exception in init" traceback.print_exc() raise #Fork and join try: futures = fork(init,(countHLS,("X11MA-OP2-AU:TRY1",)), \ (countLLS,("X11MA-OP2-AU:TRY4",)), \ (countHLS,("X11MA-OP2-AU:TRZ1",)), \ (countLLS,("X11MA-OP2-AU:TRZ4",)), \ (countHLS,("X11MA-OP2-BP1:TRY",)), \ (countHLS,("X11MA-OP2-BP1:TRZ",)) ) ret = join(futures) #check limit switch status after init limitsw=True for i in range(1,6): if(ret[i]!=1): limitsw=False print "Limit switch status for all motors: %s" %("OK" if limitsw == True else "Not OK") #final status report if(limitsw==True): print "init-AU: ==> done. OK!" set_return("OK") else: print "init-AU: ==> not done. NOT OK!" set_return("Not OK") except: set_return("Error")