-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAPIC_GUI.py
471 lines (375 loc) · 16.9 KB
/
MAPIC_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from tkinter import *
import tkinter.ttk as ttk
import numpy
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import time
from array import array
import MAPIC_functions as MAPIC
import json
from scipy.stats import norm
import scipy.optimize as sciop
#==================================================================================#
# SETUP
# Reload previous setup from json file,
# create new apic object, connect + setup class variables for socket connection.
# init root window
# setup each frame with a label and allocate sizes with grid.
#==================================================================================#
fp = open("MAPIC_utils/MAPIC_config.json","r") # load config file in read mode
default = json.load(fp) # load default settings dictionary
fp.close()
def load_settings():
''' Write default settings to the pyboard. '''
apic.writeI2C(default['gainpos'],0)
time.sleep(0.1)
apic.writeI2C(default['threshpos'],1)
time.sleep(0.1)
apic.setpolarity(setpolarity=default['polarity'])
time.sleep(0.1)
apic.drain_socket()
def checkerror():
# POPUP BOX WITH ERROR STATUS
return None
apic = MAPIC.APIC(default['timeout'],default['ipv4']) # connect to the APIC
#load_settings()
root = Tk()
root.title('MAPIC GUI')
root.wm_iconbitmap('MAPIC_utils/MAPIC.ico')
I2Cframe = LabelFrame(root,text='I2C Digital Potentiometer Control')
I2Cframe.grid(row=1,column=1,columnspan=5,rowspan=4)
ADCframe = LabelFrame(root,text='DAQ')
ADCframe.grid(row=5,column=1,columnspan=3,rowspan=2,sticky=W)
diagnostic = LabelFrame(root,text='Calibration Tools')
diagnostic.grid(row=5,column=5,rowspan=2,columnspan=2)
polarityframe = LabelFrame(root,text='Polarity Switch.')
polarityframe.grid(row=5,column=4,rowspan=2)
histframe = LabelFrame(root, text='Graph Config')
histframe.grid(row=7,column=1, columnspan=3,rowspan=5,sticky=NW)
#==================================================================================#
# GAUSSIAN FIT SECTION
# The inputs for this are contained in the histogram section.
# Fit a normal distribution to the histogram bins.
#==================================================================================#
nlowbound = StringVar()
nhighbound = StringVar()
def normfit():
binx = []
biny = []
for x, y in zip(apic.binedges[:-1],apic.binvals):
if x >= int(nlowbound.get()) and x <= int(nhighbound.get()):
binx.append(x)
biny.append(y)
else:
pass
binx = numpy.array(binx)
biny = numpy.array(biny)
apic.mean, apic.std = norm.fit(binx)
def gaussian(x,A):
return A*numpy.exp(-0.5*((x-apic.mean)/apic.std)**2)
global ax
popt, pcov = sciop.curve_fit(gaussian, binx, biny)
print(popt)
biny = gaussian(binx,popt[0])
ax.plot( binx, biny, color='r') #legend =r'$, \bar x = $' + str(apic.mean) + r'$, \sigma = $' + str(apic.std) + ', Resolution = ' + str((2*FWHMval)/apic.mean) )
#==================================================================================#
# I2C TOOLS FRAME:
# Define read/write functions and map to buttons/sliders for each pot.
# Define scan function to return the two I2C addresses.
#==================================================================================#
# See apic.readI2C and apic.I2C for explanation of functions.
def read():
try:
apic.readI2C()
Ireadlabel.config(text='Gain: %i , Threshold: %i' % (apic.posGAIN,apic.posTHRESH))
except:
apic.errorstatus = 'TIMEOUT'
def scan():
try:
apic.scanI2C()
Iscanlabel.config(text=str(apic.I2Caddrs))
except:
apic.errorstatus = 'TIMEOUT'
# Define int variables for the widgets to reference
var0 = IntVar()
var1 = IntVar()
def write0():
gain = var0.get() # retrieve 8 bit int value from the position of the slider
apic.writeI2C(gain,0) # call the write function with this value
def write1():
threshold = var1.get()
apic.writeI2C(threshold,1)
# Widget definitions and placement
Iread = Button(I2Cframe,text='POTENTIOMETER VALS',command=read, width=18)
Iread.grid(row=1,column=1)
Ireadlabel = Label(I2Cframe,text='---')
Ireadlabel.grid(row=2,column=1)
Iscan = Button(I2Cframe,text='I2C ADDRESS LIST',command=scan,width=18)
Iscan.grid(row=3,column=1)
Iscanlabel = Label(I2Cframe,text='---',bd=10)
Iscanlabel.grid(row=4,column=1)
# GAIN POT BUTTON
W0B = Button(I2Cframe,text='SET GAIN',command=write0, width=15)
W0B.grid(row=1,column=5,sticky=W)
W0S = Scale(I2Cframe,orient=HORIZONTAL,tickinterval=32,resolution=1,
from_=0,to=255,length=300,variable=var0)
W0S.grid(row=1,column=3,columnspan=2)
# THRESHOLD POT BUTTON
W1B = Button(I2Cframe,text='SET THRESHOLD',command=write1, width=15)
W1B.grid(row=3,column=5, sticky=W)
W1S = Scale(I2Cframe,orient=HORIZONTAL,tickinterval=32,resolution=1,
from_=0,to=255,length=300,variable=var1)
W1S.grid(row=3,column=3,columnspan=2)
div = Label(I2Cframe, text=' ').grid(row=1,column=2,rowspan=4)
#==================================================================================#
# ADC CONTROL FRAME
# ADC_IT_POLL: is legacy python ADC Datalogging routine.
# ADC_DMA: is designed to be used with the DMA stream and so contains the bit shifting
# needed to extract data. Also the plotting is more advanced.
#==================================================================================#
numadc=StringVar()
def ADC_IT_POLL():
apic.drain_socket()
progress['value'] = 0 # reset progressbar
datapoints = int(numadc.get()) # get desired number of samples from the tkinter text entry
apic.ADC_IT_poll(datapoints,progress,root) # take data using ADC_IT_poll protocol
global histogram
histogram = plt.Figure(dpi=100)
global ax
ax = histogram.add_subplot(111)
apic.savedata(apic.data,'adc') # save raw data
apic.raw_dat_count += 1
apic.data = numpy.average(apic.setunits(apic.data, default['units']), axis=1) # average the ADC peak data over the columns
apic.data = apic.data[apic.data>0] # remove zeros (controvertial feature)
apic.binvals, apic.binedges, patchs = ax.hist(apic.data,apic.bins,apic.boundaries,color='b', edgecolor='black')
# set titles and axis labels
ax.set_title(default['title'])
ax.set_xlabel(default['xlabel']+ (" (%s)") % (apic.units))
ax.set_ylabel(default['ylabel'])
# add the plot to the gui
global bar1
bar1 = FigureCanvasTkAgg(histogram, root)
bar1.get_tk_widget().grid(row=1,column=7,columnspan=1,rowspan=10)
apic.drain_socket() # drain socket to clear interrupt overflows
def ADC_DMA():
progress['value'] = 0 # reset progressbar
datapoints = int(numadc.get()) # get desired number of samples from the tkinter text entry
apic.adc_peak_find(datapoints,progress,root)
global histogram
histogram = plt.Figure(dpi=100)
global ax
ax = histogram.add_subplot(111)
ax.minorticks_on()
ax.tick_params(axis='y', which ='major',direction='in', width=1, length=16,right=True,left=True )
ax.tick_params(axis='y', which='minor',direction='in',width =1, length=8,right=True,left=True )
ax.tick_params(axis='x', which ='major',direction='in', width=1, length=5,bottom=True,top=True )
ax.tick_params(axis='x', which='minor',direction='in',width =1, length=3,bottom=True,top=True)
apic.data = apic.setunits(apic.data, default['units'])
# apic.data_time -> time with us resolution in same order as above
apic.binvals, apic.binedges, patchs = ax.hist(apic.data,apic.bins,apic.boundaries,color='b', edgecolor='black')
ax.set_title(default['title'])
ax.set_xlabel(default['xlabel']+ (" (%s)") % (apic.units))
ax.set_ylabel(default['ylabel'])
if nlowbound.get == "" or nhighbound.get() == "":
pass
else:
normfit()
# add the plot to the gui
global bar1
bar1 = FigureCanvasTkAgg(histogram, root)
bar1.get_tk_widget().grid(row=1,column=7,columnspan=1,rowspan=10)
apic.savedata(apic.data,'adc') # save data
apic.savedata(apic.data_time,'time') # save time data
apic.raw_dat_count += 1
# Add ADC frame widgets
ADCi_label = Label(ADCframe, text='Interrupt Samples:')
ADCi_label.grid(row=1,column=1)
ADCi_entry = Entry(ADCframe,textvariable=numadc)
ADCi_entry.grid(row=1,column=2)
ADC_out = Button(ADCframe, command=ADC_DMA,text='Start',width=10)#,state=DISABLED)
ADC_out.grid(row=1,column=3)
progress = ttk.Progressbar(ADCframe,value=0,maximum=apic.samples,length=350) # add a progress bar
progress.grid(row=2,column=1,columnspan=3)
#==================================================================================#
# POLARITY FRAME
#==================================================================================#
POL = IntVar()
def pselect():
apic.setpolarity(setpolarity=POL.get())
# Add polarity widgets
ppolarity = Radiobutton(polarityframe,command=pselect,text='Positive',value=1,variable=POL)
ppolarity.grid(row=1,column=1,sticky=W)
npolarity = Radiobutton(polarityframe,command=pselect,text='Negative',value=0,variable=POL)
npolarity.grid(row=2,column=1,sticky=W)
#==================================================================================#
# HISTOGRAM FRAME
#==================================================================================#
titlestr = StringVar()
xstr = StringVar()
ystr = StringVar()
cbins = StringVar()
unitvar = StringVar()
lowbound = StringVar()
highbound = StringVar()
# CLEAR HISTOGRAM + SET NEW OPTIONS
def set_t():
ax.cla()
ax.set_title(titlestr.get())
ax.set_xlabel(xstr.get())
apic.title = titlestr.get()
apic.xlabel = xstr.get()+(" (%s)" % (unitvar.get()))
apic.ylabel = ystr.get()
apic.bins = int(cbins.get())
ax.set_ylabel(ystr.get())
apic.boundaries = (int(lowbound.get()),int(highbound.get()))
ax.minorticks_on()
ax.tick_params(axis='y', which ='major',direction='in', width=1, length=16,right=True,left=True )
ax.tick_params(axis='y', which='minor',direction='in',width =1, length=8,right=True,left=True )
ax.tick_params(axis='x', which ='major',direction='in', width=1, length=6,bottom=True,top=True )
ax.tick_params(axis='x', which='minor',direction='in',width =1, length=3,bottom=True,top=True)
apic.data = apic.setunits(apic.data,unitvar.get())
apic.binvals, apic.binedges, patchs = ax.hist(apic.data, int(cbins.get()), (int(lowbound.get()),int(highbound.get())), color='b', edgecolor='black')
if nlowbound.get == "" or nhighbound.get() == "":
pass
else:
normfit()
bar1 = FigureCanvasTkAgg(histogram, root)
bar1.get_tk_widget().grid(row=1,column=7,columnspan=1,rowspan=10)
# SAVE HISTOGRAM WITH CURRENT SETTINGS
def savefig():
figtemp = plt.figure()
ax1 = figtemp.add_subplot(111)
ax1.set_title(titlestr.get())
apic.title = titlestr.get()
apic.xlabel = xstr.get()+(" (%s)" % (unitvar.get()))
ax1.set_xlabel(apic.xlabel)
apic.ylabel = ystr.get()
apic.bins = int(cbins.get())
ax1.set_ylabel(ystr.get())
apic.boundaries = (int(lowbound.get()),int(highbound.get()))
ax1.minorticks_on()
ax1.tick_params(axis='y', which ='major',direction='in', width=1, length=16,right=True,left=True )
ax1.tick_params(axis='y', which='minor',direction='in',width =1, length=8,right=True,left=True )
ax1.tick_params(axis='x', which ='major',direction='in', width=1, length=5,bottom=True,top=True )
ax1.tick_params(axis='x', which='minor',direction='in',width =1, length=3,bottom=True,top=True)
apic.data = apic.setunits(apic.data,unitvar.get())
apic.binvals, apic.binedges, patchs = ax1.hist(apic.data, int(cbins.get()), (int(lowbound.get()),int(highbound.get())), color='b', edgecolor='black')
figtemp.savefig('histdata\histogram'+apic.createfileno(apic.raw_dat_count-1)+'.png')
ewidth = 35
t_entr = Entry(histframe, textvariable = titlestr, width =ewidth)
t_entr.grid(row=1,column=2,columnspan=2)
x_entr = Entry(histframe, textvariable = xstr, width = ewidth)
x_entr.grid(row=2,column=2,columnspan=2)
y_entr = Entry(histframe, textvariable = ystr,width = ewidth)
y_entr.grid(row=3,column=2,columnspan=2)
bins_entr = Entry(histframe,textvariable = cbins,width = ewidth)
bins_entr.grid(row=4,column=2, columnspan=2)
lowbound_entr = Entry(histframe, textvariable=lowbound, width = int(ewidth/2))
lowbound_entr.grid(row=5,column=2)
highbound_entr = Entry(histframe, textvariable=highbound, width = int(ewidth/2))
highbound_entr.grid(row=5,column=3)
t_entr.insert([0],default['title'])
x_entr.insert([0],default['xlabel']+ (" (%s)") % (apic.units))
y_entr.insert([0], default['ylabel'])
bins_entr.insert([0], default['bins'])
lowbound_entr.insert([0],default['boundaries'][0])
highbound_entr.insert([0], default['boundaries'][1])
mvbutton = Radiobutton(histframe,text='mV',value='mV',variable=unitvar)
mvbutton.grid(row=1,column=4,sticky=W)
adubutton = Radiobutton(histframe,text='ADU',value='ADU',variable=unitvar)
adubutton.grid(row=2,column=4,sticky=W)
setbutton = Button(histframe, text='SET', command=set_t, width = 5)
setbutton.grid(row=3,column=4)
savebutton = Button(histframe, text='SAVE', command=savefig, width = 5)
savebutton.grid(row=4,column=4)
normal_high_bound = Entry(histframe,textvariable=nhighbound, width=int(ewidth/2))
normal_high_bound.grid(row=6,column=3)
normal_low_bound = Entry(histframe,textvariable=nlowbound, width=int(ewidth/2))
normal_low_bound.grid(row=6,column=2)
t_label = Label(histframe, text = 'TITLE:')
t_label.grid(row=1,column=1,sticky=W)
x_label = Label(histframe, text = 'X AXIS:')
x_label.grid(row=2,column=1,sticky=W)
y_label = Label(histframe, text = 'Y AXIS:')
y_label.grid(row=3,column=1,sticky=W)
bins_label = Label(histframe, text= 'BINS:')
bins_label.grid(row=4,column=1,sticky=W)
bound_label = Label(histframe, text='BOUNDS')
bound_label.grid(row=5,column=1,sticky=W)
normfitbounds_label = Label(histframe, text='NORMFIT')
normfitbounds_label.grid(row=6,column=1,sticky=W)
#==================================================================================#
# CALIBRATION FRAME
#==================================================================================#
def f(x,a,b,c):
''' Second order tranfer function to fit to pulse strecher input/output curve.\n
f(x,a,b,c)\n
Arguments:\n
\t x: x axis data
\t a: x**2 coefficient
\t b: gradient fit param
\t c: offset fit param'''
return a*x**2 + b*x + c
def calibrate():
apic.calibration()
cf1, cf2 = sciop.curve_fit(f, apic.inputpulses, apic.outputpulses)
a,b,c = cf1
fig = plt.figure()
global ax2
ax2 = fig.add_subplot(122)
ax2.plot(apic.inputpulses,apic.outputpulses,label='Output/Input Transfer Curve', color='b')
ax2.plot(apic.inputpulses,f(apic.inputpulses,a,b,c),
label='y = %fx^2 + %fx + %fc' % (a,b,c),linestyle='--', color='r')
ax2.legend()
fig.savefig('calibration.png')
# Set apic objects for the gain/offset of the fit
apic.calibgradient = b
apic.caliboffset = c
#caliblabel.config(text='y=%fx^2+%fx+%f' % (a,b,c))
apic.drain_socket()
# ADCout.config(state=NORMAL)
def rateaq():
''' Acquire the rate of the source. '''
rate = apic.rateaq()
ratelabel.config(text=str(rate)+'Hz')
apic.drain_socket()
calibration = Button(diagnostic,text='CALIBRATE GAIN',
command=calibrate, width = 14)
calibration.grid(row=1,column=1,sticky=W)
ratebutton = Button(diagnostic,text='MEASURE RATE'
,command = rateaq, width = 14)
ratebutton.grid(row=2,column=1,sticky=W)
ratelabel = Label(diagnostic,text='---')
ratelabel.grid(row=2,column=2)
caliblabel = Label(diagnostic,text='---')
caliblabel.grid(row=1,column=2)
#==================================================================================#
# TOP MENU FUNCTIONS + OPTIONS
#==================================================================================#
menubar = Menu(root)
def quit():
apic.sock.close()
apic.sockdma.close()
root.quit()
def savesettings():
''' Save updated config settings so that setup is preserved on restart. '''
fp = open("MAPIC_utils/MAPIC_config.json","w")
default['calibgradient'] = apic.calibgradient
default['timeout'] = apic.tout
default['gainpos'] = apic.posGAIN
default['threshpos'] = apic.posTHRESH
default['polarity'] = apic.polarity
default['title'] = apic.title
default['bins'] = apic.bins
default['boundaries'] = apic.boundaries
json.dump(default,fp,indent=1)
fp.close()
# create a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Load',command=load_settings)
filemenu.add_command(label='Save', command=savesettings)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="Menu", menu=filemenu)
root.config(menu=menubar) # display menubar
root.mainloop() # run main gui program