X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=research%2FTCS%2Finterface.py;h=11ed9b298ac79438ff15d7ed4015de19b137a79d;hb=56f525e2d01cb9e01d8a5d38130729c0a2072cd1;hp=86c5e823f3676793bcebbbc9c849f772f4c3c2a1;hpb=3423ad8d0015992dd3c4af656ffb745f7796464e;p=matches%2Fhonours.git diff --git a/research/TCS/interface.py b/research/TCS/interface.py index 86c5e823..11ed9b29 100755 --- a/research/TCS/interface.py +++ b/research/TCS/interface.py @@ -12,7 +12,7 @@ import time import serial import datetime -import collections +import odict # TODO: Insert variables for calibration purposes here. @@ -57,7 +57,7 @@ ser = serial.Serial( rtscts=0 ) -parameters = OrderedDict([ +parameters = odict.odict([ ("Accelerating Voltage" , None), ("Focus Voltage" , None), ("Deflection Voltage" , None), @@ -75,11 +75,11 @@ parameters = OrderedDict([ ("602 0.01 Battery" , None), ("602 0.003 Battery" , None), ("602 0.001 Battery" , None), - ("ADC Battery" , None), ("ADC Regulator" , None), ("Title" , None), ("Comment" , None), ("Data" , None), + ("Parameters last checked", None) ]) def getTime(): @@ -95,17 +95,48 @@ def set_exit_handler(func): import win32api win32api.SetConsoleCtrlHandler(func, True) except ImportError: - version = “.”.join(map(str, sys.version_info[:2])) - raise Exception(”pywin32 not installed for Python ” + version) + version = ".".join(map(str, sys.version_info[:2])) + raise Exception("pywin32 not installed for Python " + version) else: import signal signal.signal(signal.SIGTERM, func) + signal.signal(signal.SIGINT, func) + +def killed_handler(signal, frame): + reason = "" + sys.stdout.write("\n# Reason for killing program? ") + reason = sys.stdin.readline().strip("\r\n ") + for out in aquire["open_files"]: + sys.stdout.write("# Closing file " + str(out) + "\n") + out.write("# Recieved KILL signal.\n# Reason: " + str(reason)) + log_close(out) -def exit_handler(): -def init(): +def cleanup(): + for out in aquire["open_files"]: + out.write("# Program exits.\n") + log_close(out) + +def log_open(a, b): + result = open(a, b,0) + if (b == "w"): + result.write("# File opened at " + str(datetime.datetime.now()) + "\n") + aquire["open_files"].append(result) + return result + +def log_close(afile): + if (afile in aquire["open_files"]): + afile.write("# File closed at " + str(datetime.datetime.now()) + "\n") + aquire["open_files"].remove(afile) + + afile.close() + +def init(): + #import atexit + #atexit.register(cleanup) + set_exit_handler(killed_handler) aquire["start_date"] = getDate() @@ -126,19 +157,19 @@ def init(): print(ser.readline().strip("\r\n")) print(ser.readline().strip("\r\n")) - print("Writing config information to config.dat...") - output = open("config.dat", "w", 1) + #print("Writing config information to config.dat...") + #output = log_open("config.dat", "w", 1) - output.write("# Initialise " + str(datetime.datetime.now()) + "\n") + #output.write("# Initialise " + str(datetime.datetime.now()) + "\n") #for field in calibrate: # output.write("# calibrate["+str(field)+"] = "+str(calibrate[field]) + "\n") #output.write("\n") - for field in aquire: - output.write("# aquire["+str(field)+"] = "+str(aquire[field]) + "\n") + #for field in aquire: + # output.write("# aquire["+str(field)+"] = "+str(aquire[field]) + "\n") - output.write("# Ready " + str(datetime.datetime.now()) + "\n# EOF\n") - output.close() + #output.write("# Ready " + str(datetime.datetime.now()) + "\n# EOF\n") + #output.close() def main(): @@ -178,46 +209,55 @@ def main(): sweep += 1 -def checkList(output_file): +def checkList(): try: - input_file = open(getDate()+"/checklist", "r") + input_file = log_open(getDate()+"/checklist", "r") except: input_file = None if (input_file != None): for line in input_file: k = line.split("=") - item = k[0].strip(" \r\n") - value = k[1].strip(" \r\n") + item = None + if (len(k) >= 2): + item = k[0].strip("# \r\n") + value = k[1].strip("# \r\n") + if (item in parameters): parameters[item] = value print("Checklist found. Overwrite? [Y/n]") response = sys.stdin.readline().strip(" \r\n") if (response == "" or response == "y" or response == "Y"): + input_file = log_open(getDate()+"/checklist.old", "w") + for item in parameters: + input_file.write("# " + str(item) + " = " + str(parameters[item]) + "\n") + input_file.write("\n") input_file = None if (input_file == None): for item in parameters: + if item == "Parameters last checked": + continue sys.stdout.write("\""+str(item)+"\" = " + str(parameters[item]) + " New value?: ") response = sys.stdin.readline().strip("\r\n ") if (response != ""): parameters[item] = response sys.stdout.write("\n") + parameters["Parameters last checked"] = str(datetime.datetime.now()) - checklist = open(getDate()+"/checklist", "w", 0) + checklist = log_open(getDate()+"/checklist", "w") for item in parameters: checklist.write("# "+str(item) + " = " + str(parameters[item]) + "\n") - output_file.write("# "+str(item) + " = " + str(parameters[item]) + "\n") - + #output_file.write("# "+str(item) + " = " + str(parameters[item]) + "\n") + def record_data(ADC_channels, output, pollTime = None, dac_max = None): if (output != None): - output = [open(output, "w", 0), sys.stdout] - checkList(output[0]) + output = [log_open(output, "w"), sys.stdout] else: output = [sys.stdout] @@ -226,11 +266,11 @@ def record_data(ADC_channels, output, pollTime = None, dac_max = None): out.write("# aquire["+str(field)+"] = "+str(aquire[field]) + "\n") for out in output: - out.write("# Checklist:\n") + out.write("# Parameters:\n") - for field in checklist: + for field in parameters: for out in output: - out.write("# "+str(field)+" = " + str(checklist[field]) + "\n") + out.write("# "+str(field)+" = " + str(parameters[field]) + "\n") start_time = time.time() @@ -271,8 +311,11 @@ def record_data(ADC_channels, output, pollTime = None, dac_max = None): for channel in ADC_channels: read = readADC(channel) if read == False: - print("Abort data collection") - return False + for out in output: + print("# Abort data collection due to failed ADC read") + if out != sys.stdout: + log_close(out) + return False raw_adc.append((channel, read[0], read[1])) end_time = time.time() @@ -287,12 +330,12 @@ def record_data(ADC_channels, output, pollTime = None, dac_max = None): for out in output: if out != sys.stdout: - out.close() + log_close(out) return True def loadCalibration_ADC(channel): try: - input_file = open("calibrateADC"+str(channel)+".dat") + input_file = log_open("calibrateADC"+str(channel)+".dat") except: print("Couldn't find calibration file for ADC " + str(channel)) return False @@ -305,7 +348,7 @@ def loadCalibration_ADC(channel): else: split_line = l.split("\t") calibrate["ADC"][channel].append((float(split_line[0]), float(split_line[1]))) - input_file.close() + log_close(input_file) if (len(calibrate["ADC"][channel]) <= 0): print("Empty calibration file for ADC " + str(channel)) @@ -314,7 +357,7 @@ def loadCalibration_ADC(channel): def loadCalibration_DAC(): try: - input_file = open("calibrateDAC.dat") + input_file = log_open("calibrateDAC.dat") except: print("Couldn't find calibration file for DAC") return False @@ -333,7 +376,7 @@ def loadCalibration_DAC(): calibrate["DAC"].append((int(split_line[0]), float(split_line[1]))) - input_file.close() + log_close(input_file) if (len(calibrate["DAC"]) <= 0): print("Empty calibration file for DAC") @@ -442,7 +485,7 @@ def calibrateADC_usingDAC(channel, gain = True): return False calibrate["ADC"][channel] = [] - outfile = open("calibrateADC"+str(channel)+".dat", "w", 1) + outfile = log_open("calibrateADC"+str(channel)+".dat", "w") outfile.write("# Calibrate ADC " + str(channel) + "\n") outfile.write("# Start " + str(datetime.datetime.now()) + "\n") @@ -481,7 +524,7 @@ def calibrateADC_usingDAC(channel, gain = True): calibrate["ADC"][channel].append((value[0], input_value, value[1])) outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n") - outfile.close() + log_close(outfile) if (setDAC(0) == False): return False if (len(calibrate["ADC"][channel]) <= 0): @@ -491,7 +534,7 @@ def calibrateADC_usingDAC(channel, gain = True): def calibrateADC(channel): calibrate["ADC"][channel] = [] - outfile = open("calibrateADC"+str(channel)+".dat", "w", 1) + outfile = log_open("calibrateADC"+str(channel)+".dat", "w") outfile.write("# Calibrate ADC " + str(channel) + "\n") outfile.write("# Start " + str(datetime.datetime.now()) + "\n") @@ -516,7 +559,7 @@ def calibrateADC(channel): outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n") - outfile.close() + log_close(outfile) if (len(calibrate["ADC"][channel]) <= 0): print("Error: No calibration points taken for ADC " + str(channel)) return False @@ -526,7 +569,7 @@ def calibrateADC(channel): def calibrateDAC(): calibrate["DAC"] = [] - outfile = open("calibrateDAC.dat", "w", 1) + outfile = log_open("calibrateDAC.dat", "w") outfile.write("# Calibrate DAC\n") outfile.write("# Start " + str(datetime.datetime.now()) + "\n") @@ -568,7 +611,7 @@ def calibrateDAC(): level += stepSize outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n") - outfile.close() + log_close(outfile) if (len(calibrate["DAC"]) <= 0): print("Error: No calibration points taken for DAC") return False