#!/usr/bin/env python import sys import math import time import signal from epics import caget,caput from BCALModule import SubDetector us = None # Global variable. Will be used by main() and interupt_handler(). ds = None # Global variable. Will be used by main() and interupt_handler(). start = None end = None start_red_color = '\033[91;1m' default_color = '\033[0m' auto_pulsing_stat = "BCAL:pulser:auto_pulsing_stat" def main(): global us,ds,start,end start = time.time() wait_time = 0.1 # retry delay (s) stat = False print signal.signal(signal.SIGINT,interrupt_handler) while 1: while not stat: stat = script_running_allow() time.sleep(0.1) sides, quadrants, single,freq, width, npulses, temp, offset, led_bias, led_bias_list, columns, quiet = option_handler(sys.argv) print "Initializing..." #led_width: 200 ns, led_frequancy: 1 Hz, led_npulses: 60, led_bias = 6 V, led_lv = 5 V print """ #your configuration is: freq : %.1f width : %d npulses : %d """%(freq,width,npulses) print "led bias list: ", led_bias_list print "PRES Crtl + c to exit (SIG_INT)" us = SubDetector('U',sim=False,quiet=quiet) ds = SubDetector('D',sim=False,quiet=quiet) sdDict = {'U':us,'D':ds} print "Setting configuration ......" us.configure_led_only(led_bias=led_bias_list[0],led_frequency=freq,led_width=width,led_npulses=npulses) ds.configure_led_only(led_bias=led_bias_list[0],led_frequency=freq,led_width=width,led_npulses=npulses) print "Turning on LV for LED ......" us.turn_led_lv_on() # Turn on +5V on LED, -5V # MPPC and LED ds.turn_led_lv_on() # Turn on +5V on LED, -5V # MPPC and LED print "Turning on BIAS for LED ......" us.turn_led_bias_on()# Turn on bias (LED) ds.turn_led_bias_on()# Turn on bias (LED) print "Turning LED pulser OFF ......" us.turn_led_pulser_off() # Turn off LED pulser ds.turn_led_pulser_off() # Turn off LED pulser print "Waiting until led lv is on" while not us.is_led_lv_on(): # Wait until LED low voltage, and LED bias are on time.sleep(wait_time) while not ds.is_led_lv_on(): # Wait until LED low voltage, and LED bias are on time.sleep(wait_time) print "led lv is ready " global start_red_color global default_color for bias in led_bias_list: if not stat: break print "Setting up %.1f V on led bias"%bias for s in sides: # Select Upstream or Downstream. if not stat: break sub_detector = sdDict[s] sub_detector.set_led_bias_value(bias) print "Waiting until led bias is on" time.sleep(wait_time*5) while not sub_detector.is_led_bias_on(): # all must be on time.sleep(wait_time) print "ready to pulse on %s"%("upstream" if s == 'U' else "downstream") for c in columns: stat = script_running_allow() if not stat: break sub_detector.turn_led_pulser_on(c) # Turn pulser on over all quadrants on selected column. print "pulsing column: %d"%c l=sub_detector.quadrant[1].led[1] time.sleep(float(l.npulses_set)/float(l.frequency_set)*1.1+0.1) # Wait over 20% more time than the while not sub_detector.is_led_pulser_off(): # all must be off time.sleep(wait_time) sub_detector.turn_led_pulser_off() # Set corresponding start mask to 0. This function return after the pulser is off. Double check. time.sleep(wait_time) print "pulser turned off" # Turning everything off before exit. #us.turn_led_lv_off() #us.turn_led_bias_off() #us.turn_mppc_lv_off() #ds.turn_led_lv_off() #ds.turn_led_bias_off() #ds.turn_mppc_lv_off() end = time.time() print "All done!\nElapsed time: %f (min)" % (float(end-start)/60.0) def option_handler(argv): global start_red_color global default_color sides=['U','D'] quadrants = range(1,5) columns = range(1,5) single = False freq = 1 width = 200 npulses = 60 temp = 18.0 offset = 1.2 led_bias = 6.0 led_bias_list=[6.2,6.7] quiet=True try : f = open("config") except: do_exit() for line in f: ll=line.split() if ll[0] == "freq": try : freq=float(ll[1]) except ValueError: print 'freq must be a float number' do_exit() if ll[0] == "width": try : width=int(ll[1]) except ValueError: print 'width tag must be followed by an int number' do_exit() if ll[0] == "npulses": try : npulses=float(ll[1]) except ValueError: print 'npulses must be an integer number' doexit() if ll[0] == "led_bias_list": try: bl = ll[1] except IndexError: print start_red_color + "Error: missing column bias list after led_bias_list tag" do_exit() try : bl = map(lambda x: float(x),bl.split(',')) except ValueError: print start_red_color + "Error: bias should be floats between 0 and 10.0 comma separated" do_exit() if max(bl)<10.0 and min(bl)>0: led_bias_list=bl else: print start_red_color + "Error: should be floats between 0 and 10.0 comma separated','" do_exit() argv = '' if len(argv) > 1: if '-d' in argv: argv =[] if '-s' in argv: try: s = argv[argv.index('-s')+1] except IndexError: print start_red_color + "Error: missing side (U or D) after [-s] option" sys.exit(1) if s in sides: sides = s else: print start_red_color + "Error: side must be U or D" sys.exit(1) """ if '-q' in argv: try: q = argv[argv.index('-q')+1] except IndexError: print start_red_color + "Error: missing quadrant (1 to 4) after [-q] option" sys.exit(1) try : q = map(lambda x: int(x),q.split(',')) except ValueError: print "quadrant must be integers from 1 to 4" sys.exit(1) if reduce(lambda x,y: x and y, map(lambda x: x in quadrants,q)): quadrants = q else: print start_red_color + "Error: quadrant must contain integers in [1,4] separated by ','" sys.exit(1)""" if '-c' in argv: try: c = argv[argv.index('-c')+1] except IndexError: print start_red_color + "Error: missing column (1 to 4) after [-c] option" sys.exit(1) try : c = map(lambda x: int(x),c.split(',')) except ValueError: print "columns must be integers from 1 to 4" sys.exit(1) if reduce(lambda x,y: x and y, map(lambda x: x in columns,c)): columns = c else: print start_red_color + "Error: column must contain integers in [1,4] separated by ','" sys.exit(1) if '-f' in argv: try: freq = float(argv[argv.index('-f')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing frequency after [-f] option" sys.exit(1) if (freq < 0.047) or (freq > 100e6): print start_red_color + "Error: Frequency must be in [0.047, 100e6] Hz." sys.exit(1) if '-w' in argv: try: width = int(argv[argv.index('-w')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing width after [-w] option" sys.exit(1) if (width < 10) or (width > 1000): print start_red_color + "Error: Width must be in [10, 1000] ns. Resolution 10 ns." sys.exit(1) if '-n' in argv: try: npulses = int(argv[argv.index('-n')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing npulses after [-n] option" sys.exit(1) if (npulses < 1) or (npulses > 2.147483647e9): print start_red_color + "Error: Number of pulses must be in [1, 2.147483647e9]." sys.exit(1) if '-t' in argv: try: temp = float(argv[argv.index('-t')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing temperature after [-t] option" sys.exit(1) if (temp < 5) or (temp > 30): print start_red_color + "Error: Temperature must be in [5, 30] C." sys.exit(1) if '-o' in argv: try: offset = float(argv[argv.index('-o')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing offset after [-o] option" sys.exit(1) if (offset < 0) or (offset > 1.5): print start_red_color + "Error: Offset must be in [0, 1.5] V." sys.exit(1) if '-b' in argv: try: led_bias = float(argv[argv.index('-b')+1]) except (IndexError, ValueError): print start_red_color + "Error: missing led bias after [-b] option" sys.exit(1) if (led_bias < 0) or (led_bias > 10): print start_red_color + "Error: LED bias must be in [0, 10] V." sys.exit(1) if '-bl' in argv: try: bl = argv[argv.index('-bl')+1] except IndexError: print start_red_color + "Error: missing bias after option -bl" sys.exit(1) try : bl = map(lambda x: float(x),bl.split(',')) except ValueError: print "bias must be floats from 0 to 10" sys.exit(1) if max(bl)<10.0 and min(bl)>0: led_bias_list=bl else: print start_red_color + "Error: should be floats between 0 and 10.0 comma separated','" sys.exit(1) if '--single' in argv: single = True if '-v' in argv or '--verbose' in argv: quiet=False if '-h' in argv or '--help' in argv: print''' %s [option value] options: -s < U | D >: Select side U for upstream or D for downstream. If this option is not given, it will run through both sides. -c < c_1,c_2,... >: Select columns with numbers 'c_i' in {1,...,4}. If this option is not given, it will run through all columns. -f < freq >: Set pulser frequency in Hz, range [0.047, 100e6]. Default value %.1f Hz -w < width >: Set pulser width in ns, range [10, 1000] in 10 ns steps. Default value %d ns -n < npulses >: Set pulser number of pulses before stop, range [1, 2.147483647e9]. Default value %d -bl : Set the bias list to be run. Default 6.2,6.7. -d : Run with default configuration: -v,--verbose: Show the epics comands sent -h,--help: Show this help. Note: Ctrl-c interruption handled, it will turn all off and then exit. Example: %s -bl 6.2,6.7 -c 1,2 -f 1 -w 200 -n 60 It will run with 6.2 and 6.7 (V). Frequency 1 Hz, width 200 ns, 60 pulses (~1min run). It will go throug columns 1 and 2. Autor: Orlando Soto, March, 2016 '''%(argv[0],freq,width,npulses, argv[0]) sys.exit(0) return sides,quadrants,single,freq,width,npulses,temp,offset,led_bias,led_bias_list,columns,quiet def interrupt_handler(signum,frame): signal.signal(signal.SIGINT,signal.SIG_DFL) #start_red_color = '\033[91;1m' #default_color = '\033[0m' print 'Ctrl-c pressed, leaving configurations as it is' try: pass #us.turn_led_lv_off() #us.turn_led_bias_off() # us.turn_mppc_lv_off() # us.turn_mppc_bias_off() #ds.turn_led_lv_off() #ds.turn_led_bias_off() # ds.turn_mppc_lv_off() # ds.turn_mppc_bias_off() except NameError: print 'us and ds objects not created yet, nothing to do' except AttributeError: print 'us and ds objects empty' # print default_color + 'Bye' print "Normal exit." end = time.time() print "All done!\nElapsed time: %f (min)" % (float(end-start)/60.0) sys.exit(0) return def script_running_allow(): global start_red_color global default_color global auto_pulsing_stat ret = None try : f = open("config") # go_on = False # for line in f: # if line.split()[0] == "run_allow": # if int(line.split()[1]) == 1: # go_on = True f.close() ret = caget(auto_pulsing_stat) except IOError: ret = -1 print start_red_color +'file "config" not found.' + default_color if ret==0: return False elif ret==1: return True elif ret == None: print start_red_color +"check if epics variable %s exist!"%(auto_pulsing_stat)+ default_color do_exit() elif ret == -1: do_exit() def do_exit(): global start_red_color global default_color global end print start_red_color + """ You have to fill config file under this directory. the content must be like this: freq 1 #Hz width 200 #ns npulses 60 led_bias_list 6.2,6.7 #V """ + default_color end = time.time() print "All done!\nElapsed time: %f (min)" % (float(end-start)/60.0) sys.exit(0) return if __name__=="__main__": main()