Automatic commit. Thu Aug 9 17:00:03 WST 2012
[matches/honours.git] / research / TCS / interface.py
1 #!/usr/bin/python
2
3 # @file "interface.py"
4 # @author Sam Moore
5 # @purpose AVR Butterfly Datalogger control for Total Current Spectroscopy Measurements
6 #       The AVR responds to a limited number of RS232 commands to read ADC channels and set the DAC
7 #       This script can be used to control the DAC and monitor initial energy & sample current over RS232
8
9 import sys
10 import os
11 import time
12 import serial
13 import datetime
14
15 import odict
16
17
18 # TODO: Insert variables for calibration purposes here.
19 calibrate = {
20         "ADC_Counts" : [2**10,2**10,2**10,2**10,2**10,2**10,2**10], # Maxed counts on ADC channels (10 bits = 2**10 = 1024)
21         "DAC_Counts" : 2**12, # Maxed counts on DAC channel (12 bits = 2**12 = 4096)
22
23         # Calibration data for DAC and ADC
24         "DAC" : None, 
25         "ADC" : [None, None, None, None, None, None, None, None],
26
27         # Data used for rough calibration if above data is not present
28         "Vref" : 3.3, # The output of the voltage regulator (Vref = Vcc) relative to signal ground
29         "ADC_Rin" : [None, None, None, None, 15000, 1100, 1100, 1100],
30         "ADC_Rvar" : [None, None, None, None, 1000, 5000, 10000, 50000],
31         "DAC_Gain" : 1
32 }
33
34 # TODO: Adjust aqcuisition parameters here
35 aquire = { "DAC_Sweep" : "0.0 + 25.0*int(step/120)", # DAC Sweep value (t is in STEPS, not seconds!)
36         "ADC_Averages" : 200,
37         #"ADC_Vi" : 5, # ADC channel to read back Vi (set by DAC) through
38         #"ADC_Is" : 4, # ADC channel to read back Is through
39         #"ADC_Ie" : 4, # ADC channel to read back Ie through
40         "DAC_Settle" : 0.0, # Time in seconds to wait for DAC to stabilise
41         #"response_wait" : 0.2, # Time to wait in seconds between sending data and reading back
42         "start_date" : None,
43         "open_files" : []
44 }
45
46 #Setup the serial connection parameters
47 ser = serial.Serial(
48         port="/dev/ttyUSB0", # Modify as needed (note: in linux need to run `sudo chmod a+rw /dev/ttyUSBX' to set permissions)
49
50         # Do not change the values below here (unless AVR butterfly is reprogrammed to use different values)
51         baudrate=4800,
52         parity="N",
53         stopbits=1,
54         bytesize=8,
55         timeout=None,
56         xonxoff=0,
57         rtscts=0
58 )
59
60 parameters = odict.odict([
61         ("Accelerating Voltage" , None),
62         ("Focus Voltage" , None),
63         ("Deflection Voltage" , None),
64         ("Venault Voltage" , None),
65         ("Initial Voltage" , None),
66         ("Heating Current" , None),
67         ("Heating Voltage" , None),
68         ("Chamber Pressure" , None),
69         ("610B Zero" , None),
70         ("602 Zero" , None),
71         ("610B Scale" , None),
72         ("602 Scale" , None),
73         ("602 0.1 Battery" , None),
74         ("602 0.03 Battery" , None),
75         ("602 0.01 Battery" , None),
76         ("602 0.003 Battery" , None),
77         ("602 0.001 Battery" , None), 
78         ("ADC Regulator" , None),
79         ("Sample", None),
80         ("Title" , None),
81         ("Comment" , None),
82         ("Data" , None),
83         ("Parameters last checked", None)
84 ])
85
86 def getTime():
87         return str(datetime.datetime.now()).split(" ")[1].split(".")[0].replace(":","")
88
89 def getDate():
90         return str(datetime.datetime.now()).split(" ")[0]
91
92 # Used for when I press Control-C to stop things
93 def set_exit_handler(func):
94     if os.name == "nt":
95         try:
96             import win32api
97             win32api.SetConsoleCtrlHandler(func, True)
98         except ImportError:
99             version = ".".join(map(str, sys.version_info[:2]))
100             raise Exception("pywin32 not installed for Python " + version)
101     else:
102         import signal
103         signal.signal(signal.SIGTERM, func)
104         signal.signal(signal.SIGINT, func)
105
106 def killed_handler(signal, frame):
107         reason = ""
108         sys.stdout.write("\n# Reason for killing program? ")
109         reason = sys.stdin.readline().strip("\r\n ")
110         for out in aquire["open_files"]:
111                 sys.stdout.write("# Closing file " + str(out) + "\n")
112                 out.write("# Recieved KILL signal.\n# Reason: " + str(reason) + "\n")
113                 log_close(out)
114
115         
116
117 def cleanup():
118         for out in aquire["open_files"]:
119                 out.write("# Program exits.\n")
120                 log_close(out)
121
122 def log_open(a, b):
123         result = open(a, b,0)
124         if (b == "w"):
125                 result.write("# File opened at " + str(datetime.datetime.now()) + "\n")
126                 aquire["open_files"].append(result)
127         return result
128
129 def log_close(afile):
130         if (afile in aquire["open_files"]):
131                 afile.write("# File closed at " + str(datetime.datetime.now()) + "\n")
132                 aquire["open_files"].remove(afile)
133         
134         afile.close()
135
136
137 def init():
138         #import atexit
139         #atexit.register(cleanup)
140         set_exit_handler(killed_handler)
141         
142         aquire["start_date"] = getDate()
143
144
145
146         # Connect serial
147         ser.open()
148         ser.isOpen()
149 #       print("Waiting for \"# hello\" from device...")
150 #       while (ser.readline().strip("\r\n") != "# hello"):
151 #               pass
152         #while (ser.readline().strip("\r\n ") != "#"):
153         #       pass
154         time.sleep(1.0)
155
156         ser.write("a "+str(aquire["ADC_Averages"]) + "\r\n")
157         print(ser.readline().strip("\r\n"))
158         print(ser.readline().strip("\r\n"))
159         print(ser.readline().strip("\r\n"))
160
161         #print("Writing config information to config.dat...")
162         #output = log_open("config.dat", "w", 1)
163
164         #output.write("# Initialise " + str(datetime.datetime.now()) + "\n")
165
166         #for field in calibrate:
167         #       output.write("# calibrate["+str(field)+"] = "+str(calibrate[field]) + "\n")
168         #output.write("\n")
169         #for field in aquire:
170         #       output.write("# aquire["+str(field)+"] = "+str(aquire[field]) + "\n")
171
172         #output.write("# Ready " + str(datetime.datetime.now()) + "\n# EOF\n")
173         #output.close()
174
175 def main():
176
177         init()
178         
179         # I haven't ever used calibrated results, and yet this code is still here, why???
180         #if (loadCalibration_DAC() == False):
181         #       if (calibrateDAC() == False):
182         #               return -1
183         #if (loadCalibration_ADC(aquire["ADC_Is"]) == False):
184         #       if (calibrateADC_usingDAC(aquire["ADC_Is"], False) == False):
185         #               if (calibrateADC(aquire["ADC_Is"]) == False):
186         #                       return -1
187
188         #if (loadCalibration_ADC(aquire["ADC_Vi"]) == False):
189         #       if (calibrateADC_usingDAC(aquire["ADC_Vi"], True) == False):
190         #               if (calibrateADC(aquire["ADC_Vi"]) == False):
191         #                       return -1
192         
193
194         # Make directory for today, backup calibration files
195         os.system("mkdir -p " + getDate())
196         #os.system("cp *.dat " + getDate() +"/")
197
198         checkList()
199
200         
201         
202                 
203
204         # Experiment
205         # TODO: Modify data to record here
206         sweep = 1
207         while True:
208                 os.system("mkdir -p " + getDate())
209                 record_data([4, 5], getDate()+"/"+str(getTime())+".dat", None, 4000)
210                 sweep += 1
211         
212
213 def checkList():
214         try:
215                 input_file = log_open(getDate()+"/checklist", "r")
216         except:
217                 input_file = None
218
219         if (input_file != None):
220                 for line in input_file:
221                         k = line.split("=")
222                         item = None
223                         if (len(k) >= 2):
224                                 item = k[0].strip("# \r\n")
225                                 value = k[1].strip("# \r\n")
226
227                         if (item in parameters):
228                                 parameters[item] = value
229         
230                 print("Checklist found. Overwrite? [Y/n]")
231                 response = sys.stdin.readline().strip(" \r\n")
232                 if (response == "" or response == "y" or response == "Y"):
233                         input_file = log_open(getDate()+"/checklist.old", "w")
234                         for item in parameters:
235                                 input_file.write("# " + str(item) + " = " + str(parameters[item]) + "\n")
236                         input_file.write("\n")
237                         log_close(input_file)
238                         input_file = None
239         
240         if (input_file == None):
241                 for item in parameters:
242                         if item == "Parameters last checked":
243                                 continue
244                         sys.stdout.write("\""+str(item)+"\" = " + str(parameters[item]) + " New value?: ")
245                         response = sys.stdin.readline().strip("\r\n ")
246                         if (response != ""):
247                                 parameters[item] = response
248                         sys.stdout.write("\n")
249                 parameters["Parameters last checked"] = str(datetime.datetime.now())
250                         
251
252         checklist = log_open(getDate()+"/checklist", "w")
253         for item in parameters:
254                 checklist.write("# "+str(item) + " = " + str(parameters[item]) + "\n")
255                 #output_file.write("# "+str(item) + " = " + str(parameters[item]) + "\n")
256         log_close(checklist)
257         
258
259 def record_data(ADC_channels, output, pollTime = None, dac_max = None):
260         
261         if (output != None):
262                 output = [log_open(output, "w"), sys.stdout]
263         else:
264                 output = [sys.stdout]
265
266         for field in aquire:
267                 for out in output:
268                         out.write("# aquire["+str(field)+"] = "+str(aquire[field]) + "\n")
269         
270         for out in output:
271                 out.write("# Parameters:\n")
272
273         for field in parameters:
274                 for out in output:
275                         out.write("# "+str(field)+" = " + str(parameters[field]) + "\n")
276
277
278         start_time = time.time()
279         
280         for out in output:
281                 out.write("\n")
282                 out.write("# Experiment " + str(datetime.datetime.now()) + "\n")
283                 out.write("# Polling for " + str(pollTime) + "s.\n")
284                 out.write("\n")
285                 out.write("# Data:\n")
286                 out.write("# time\tDAC")
287                 for channel in ADC_channels:
288                         out.write("\tADC"+str(channel))
289                 out.write("\n")
290
291         step = 0
292         dacValue = int(eval(aquire["DAC_Sweep"]))
293         if (setDAC(dacValue) == False):
294                 setDAC(dacValue)
295         while (pollTime == None or time.time() < start_time + pollTime):
296                 if (aquire["DAC_Sweep"] != None):
297                         nextDacValue = int(eval(aquire["DAC_Sweep"]))
298                         if (nextDacValue != dacValue):
299                                 dacValue = nextDacValue
300                                 if (dacValue < 0):
301                                         break
302                                 setDAC(dacValue)
303                         step += 1
304                 
305
306                 if (dac_max != None and dacValue >= dac_max):
307                         break
308
309                 measure_start = time.time()
310
311                 raw_adc = []
312
313                 for channel in ADC_channels:
314                         read = readADC(channel)
315                         if read == False:
316                                 for out in output:              
317                                         print("# Abort data collection due to failed ADC read")                                 
318                                         if out != sys.stdout:
319                                                 log_close(out)
320                                         return False
321                         raw_adc.append((channel, read[0], read[1]))
322
323                 end_time = time.time()
324
325                 for out in output:
326                         out.write(str((measure_start + (end_time - measure_start)/2.0 - start_time)))
327                         out.write("\t"+str(dacValue))
328                         for adc in raw_adc:
329                                 out.write("\t" + str(adc[1]) + "\t" + str(adc[2]))
330                         out.write("\n") 
331         
332                 
333         for out in output:              
334                 if out != sys.stdout:
335                         log_close(out)
336         return True
337
338 def loadCalibration_ADC(channel):
339         try:
340                 input_file = log_open("calibrateADC"+str(channel)+".dat")
341         except:
342                 print("Couldn't find calibration file for ADC " + str(channel))
343                 return False
344         
345         calibrate["ADC"][channel] = []
346         for l in input_file:
347                 l = l.split("#")[0].strip("\r\n ")
348                 if (l == ""):
349                         continue
350                 else:
351                         split_line = l.split("\t")
352                         calibrate["ADC"][channel].append((float(split_line[0]), float(split_line[1])))
353         log_close(input_file)
354
355         if (len(calibrate["ADC"][channel]) <= 0):
356                 print("Empty calibration file for ADC " + str(channel))
357                 return False
358         return True
359
360 def loadCalibration_DAC():
361         try:
362                 input_file = log_open("calibrateDAC.dat")
363         except:
364                 print("Couldn't find calibration file for DAC")
365                 return False
366         
367         calibrate["DAC"] = []
368         for l in input_file:
369                 #print("Line is: "+str(l))
370                 l = l.split("#")[0].strip("\r\n ")
371                 if (l == ""):
372                         continue
373                 else:
374                         split_line = l.split("\t")
375                         if (len(split_line) >= 3):
376                                 calibrate["DAC"].append((int(split_line[0]), float(split_line[1]), float(split_line[2])))
377                         else:
378                                 calibrate["DAC"].append((int(split_line[0]), float(split_line[1])))
379
380
381         log_close(input_file)
382
383         if (len(calibrate["DAC"]) <= 0):
384                 print("Empty calibration file for DAC")
385                 return False
386         return True
387
388 def getADC_Voltage(channel, counts):
389         if (calibrate["ADC"][channel] == None or len(calibrate["ADC"][channel]) <= 0):
390                 if (calibrate["ADC_Rin"][channel] != None and calibrate["ADC_Rvar"][channel] != None):
391                         print("Warning: Using rough calibration for ADC"+str(channel) + " = " + str(counts))
392                         ratio = float(calibrate["ADC_Rin"][channel]) / (float(calibrate["ADC_Rin"][channel]) + float(calibrate["ADC_Rvar"][channel]))
393                         return ratio * (float(counts) / float(calibrate["ADC_Counts"][channel]) * Vref)
394                 else:
395                         print("Error: No calibration for ADC"+str(channel))
396                         return False
397
398         c = calibrate["ADC"][channel]
399         valueIndex = 1
400         for i in range(0, len(c)-1):
401                 if (c[i][0] <= counts and i + 1 < len(c)):
402                         grad = (float(c[i+1][valueIndex]) - float(c[i][valueIndex])) / (float(c[i+1][0]) - float(c[i][0]))
403                         value = float(c[i][valueIndex]) + grad * (float(counts) - float(c[i][0]))
404                         return value
405
406         
407         print("Warning: Extrapolating outside calibration range for DAC = " + str(counts))
408
409         grad = (float(c[len(c)-1][valueIndex]) - float(c[len(c)-2][valueIndex])) / (float(c[len(c)-1][0]) -  float(c[len(c)-2][0]))
410         value = float(c[len(c)-1][valueIndex]) + grad * (float(counts) - float(c[len(c)-1][0]))
411                 
412 def readADC(channel):
413         #for i in range(0, aquire["ADC_Averages"]):
414         #       ser.write("r "+str(channel)+"\r") # Send command to datalogger
415                 #time.sleep(aquire["response_wait"])
416         #       response = ser.readline().strip("#\r\n ")
417         #       if (response != "r "+str(channel)):
418         #               print("Received wierd response reading ADC ("+response+")")
419         #               return False                    
420                 #time.sleep(aquire["response_wait"])
421         #       values.append(int(ser.readline().strip("\r\n ")))
422         #       adc_sum += float(values[len(values)-1])
423         ser.write("r "+str(channel)+"\r")
424         response = ser.readline().strip("#\r\n")
425         if (response != "r "+str(channel)):
426                 print("Received wierd response reading ADC ("+response+")")
427                 return False                    
428         return ser.readline().strip("\r\n").split(" ")
429
430 def getDAC_Voltage(counts, gain = True):
431         if (calibrate["DAC"] == None or len(calibrate["DAC"]) <= 0):
432                 if (calibrate["DAC_Gain"] != None):
433                         print("Warning: Using rough calibration for DAC = " + str(counts))
434                         return float(counts) / float(calibrate["DAC_Counts"]) * float(calibrate["Vref"]) * float(calibrate["DAC_Gain"])
435                 else:
436                         print("Error: No calibrate for DAC")
437                         return False
438
439         valueIndex = 1
440         if (gain == False):
441                 valueIndex = 2
442                 if (len(calibrate["DAC"][0]) < 3):
443                         print("Error: No data for unamplified DAC")
444                         return False
445         
446         c = calibrate["DAC"]
447         if (len(c) == 1):
448                 print("Warning: Only one point in calibration data!")
449                 return float(c[0][valueIndex])
450         
451         for i in range(0, len(c)-1):
452                 if (c[i][0] <= counts and i + 1 < len(c)):
453                         grad = (float(c[i+1][valueIndex]) - float(c[i][valueIndex])) / (float(c[i+1][0]) - float(c[i][0]))
454                         value = float(c[i][valueIndex]) + grad * (float(counts) - float(c[i][0]))
455                         return value
456
457         
458         print("Warning: Extrapolating outside calibration range for DAC = " + str(counts))
459
460         grad = (float(c[len(c)-1][valueIndex]) - float(c[len(c)-2][valueIndex])) / (float(c[len(c)-1][0]) -  float(c[len(c)-2][0]))
461         value = float(c[len(c)-1][valueIndex]) + grad * (float(counts) - float(c[len(c)-1][0]))
462         return value
463
464
465 def setDAC(level):
466         
467         ser.write("d "+str(level)+"\r") 
468         
469         response = ser.readline().strip("#\r\n ")
470         if (response != "d "+str(level)):
471                 print("Recieved wierd response setting DAC to "+str(level) + " ("+response+")")
472                 return False
473         #time.sleep(aquire["response_wait"])
474         #time.sleep(aquire["DAC_Settle"])
475         #time.sleep(aquire["DAC_Settle"])
476         response = ser.readline().strip("#\r\n ")
477         if (response != "DAC "+str(level)):
478                 print("Recieved wierd response setting DAC to "+str(level) + " ("+response+")")
479                 return False
480         #time.sleep(aquire["response_wait"])
481         time.sleep(aquire["DAC_Settle"])
482                 
483
484 def calibrateADC_usingDAC(channel, gain = True):
485         if (calibrate["DAC"] == None):
486                 print("ERROR: DAC is not calibrated, can't use to calibrate ADC!")
487                 return False
488
489         calibrate["ADC"][channel] = []
490         outfile = log_open("calibrateADC"+str(channel)+".dat", "w")
491         outfile.write("# Calibrate ADC " + str(channel) + "\n")
492         outfile.write("# Start " + str(datetime.datetime.now()) + "\n")
493
494         print("Connect DAC output to ADC " + str(channel) + " and press enter\n")
495         sys.stdin.readline()
496         
497         for dac in calibrate["DAC"]:
498                 if (setDAC(dac[0]) == False):
499                         return False
500                 
501                 value = readADC(channel)
502                 if (value == False):
503                         return False
504                 canadd = (len(calibrate["ADC"][channel]) <= 0)
505                 if (canadd == False):
506                         canadd = True
507                         for adc in calibrate["ADC"][channel]:
508                                 if adc[0] == value[0]:
509                                         canadd = False
510                                         break
511
512                 if (canadd):            
513
514                         if gain:
515                                 input_value = dac[1]
516                         else:
517                                 input_value = dac[2]
518
519                         #input_value = getDAC_Voltage(dac[0], gain)
520                         if (input_value == False):
521                                 return False
522         
523                         outfile.write(str(value[0]) + "\t" + str(input_value) + "\t" + str(value[1]) + "\n")
524                         print(str(value[0]) + "\t" + str(input_value) + "\t" + str(value[1]))
525
526                         calibrate["ADC"][channel].append((value[0], input_value, value[1]))
527
528         outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n")
529         log_close(outfile)
530         if (setDAC(0) == False):
531                 return False
532         if (len(calibrate["ADC"][channel]) <= 0):
533                 print("Error: No calibration points taken for ADC " + str(channel))
534                 return False
535         return True
536
537 def calibrateADC(channel):      
538         calibrate["ADC"][channel] = []
539         outfile = log_open("calibrateADC"+str(channel)+".dat", "w")
540         outfile.write("# Calibrate ADC " + str(channel) + "\n")
541         outfile.write("# Start " + str(datetime.datetime.now()) + "\n")
542
543         print("Calibrating ADC...\n")
544         print("Enter measured voltage, empty line stops.\n")
545
546         while True:
547                 read = sys.stdin.readline().strip("\r\n ")
548                 if (read == ""):
549                         break
550                 input_value = float(read)
551                 output_value = readADC(channel)
552                 if (output_value == False):
553                         return False
554                 
555                 calibrate["ADC"][channel].append((output_value[0], input_value))
556                 print(str(output_value[0]) + "\t" + str(input_value))
557                 
558         calibrate["ADC"][channel].sort(lambda e : e[1], reverse = False)
559         
560         
561         
562
563         outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n")
564         log_close(outfile)
565         if (len(calibrate["ADC"][channel]) <= 0):
566                 print("Error: No calibration points taken for ADC " + str(channel))
567                 return False
568
569         return True
570
571 def calibrateDAC():
572         calibrate["DAC"] = []
573
574         outfile = log_open("calibrateDAC.dat", "w")
575         outfile.write("# Calibrate DAC\n")
576         outfile.write("# Start " + str(datetime.datetime.now()) + "\n")
577
578         print("Calibrating DAC...")
579         sys.stdout.write("Number of counts to increment per step: ")
580         read = sys.stdin.readline().strip("\r\n")
581         if (read == ""):
582                 return False
583         stepSize = max(1, int(read))
584         sys.stdout.write("\n")
585
586         outfile.write("# Increment by " + str(stepSize) + "\n")
587         print("Input measured DAC voltage for each level; empty input ends calibration.")
588         print("You may optionally input the DAC voltage before it is amplified too.")
589
590         level = 0
591         while (level < calibrate["DAC_Counts"]):
592
593                 setDAC(level)
594
595                 sys.stdout.write(str(level) + " ?")
596                 read = sys.stdin.readline().strip("\r\n ")
597                 if (read == ""):
598                         break
599                 else:
600                         read = read.split(" ")
601                         if (len(calibrate["DAC"]) > 0 and len(calibrate["DAC"][len(calibrate["DAC"])-1]) != len(read)):
602                                 print("Either give one value for EVERY point, or two values for EVERY point. Don't mess around.")
603                                 return False
604                         if (len(read) == 2):
605                                 calibrate["DAC"].append((level, float(read[0]), float(read[1])))
606                                 outfile.write(str(level) + "\t" + str(float(read[0])) + "\t" + str(float(read[1])) + "\n")
607                         else:
608                                 calibrate["DAC"].append((level, float(read[0])))
609                                 outfile.write(str(level) + "\t" + str(float(read[0])) + "\n")
610                 
611
612
613                 level += stepSize
614
615         outfile.write("# Stop " + str(datetime.datetime.now()) + "\n# EOF\n")
616         log_close(outfile)
617         if (len(calibrate["DAC"]) <= 0):
618                 print("Error: No calibration points taken for DAC")
619                 return False
620         return setDAC(0)
621
622 # Run the main function
623 if __name__ == "__main__":
624         sys.exit(main())

UCC git Repository :: git.ucc.asn.au