return (v - 2**(bits-1)) >> 2
def mux_a_offset(x, c, verbose=True):
+ adc.mux(4)
#xi = int(x[0])
xi = int(x)
mux.otaA.offset = xi
return array((mv,))
def mux_b_offset(x, c, verbose=True):
+ adc.mux(5)
xi = int(x)
mux.otaB.offset = xi
mux.write()
xi = int(x)
amux.otaA.offset = xi
amux.write()
+ adc.mux(6)
sleep(MUX_CAL_DELAY)
adc.read()
values = []
xi = int(x)
amux.otaB.offset = xi
amux.write()
+ adc.mux(7)
sleep(MUX_CAL_DELAY)
adc.read()
values = []
-dac.vina(1.25)
-dac.vinb(1.25)
-dac.vcmi(1.25)
+# TODO: commented out, ensure ADC is init'ed in the right places
+#adc.reset()
+#adc.triggerMode(adc.MODE_IDLE)
+#adc.average(16)
+#adc.convst_spi(1)
-adc.reset()
-adc.triggerMode(adc.MODE_IDLE)
-adc.average(16)
-adc.convst_spi(1)
+#adc.channelMode(4, adc.SE) #even channel only
+#adc.channelGain(4, 1)
+#adc.channelGain(5, 1)
-adc.channelMode(4, adc.SE) #even channel only
-adc.channelGain(4, 1)
-adc.channelGain(5, 1)
-
-adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+#adc.triggerMode(adc.MODE_MANUAL_MANUAL)
#
if name == 'chain':
N_CHANNELS = 48
+
+ #setup adc
+ adc.reset()
+ adc.triggerMode(adc.MODE_IDLE)
+
+ adc.average(16)
+ adc.convst_spi(1)
+ adc.channelMode(4, adc.SE) #even channel only
+ adc.channelGain(4, 1)
+ adc.channelGain(5, 1)
+
+ adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+
def func(values, converging):
return chain_offsets(
values,
(int(mux.otaA.offset), int(mux.otaB.offset))
)
+ #setup mux to buffer
+ mux.otaA.mode = mux.otaA.MUX_BUF
+ mux.otaB.mode = mux.otaB.MUX_BUF
+ mux.write()
+
#setup initial guess, just use the current values
x0 = zeros((2, N_CHANNELS), dtype=int)
for i in range(N_CHANNELS):
x0[j,i] = chain.h[i].ota[j].offset
offsets, stats = secant_opt(func, x0, limits,
elements=elements, verbose=verbose)
+
#
# Main pad buffer + mux
#
elif name == 'mux':
+ adc.reset()
+ adc.triggerMode(adc.MODE_IDLE)
+
+ adc.average(16)
+ adc.convst_spi(1)
+ adc.channelMode(4, adc.SE) #even channel only
+ adc.channelGain(4, 1)
+ adc.channelGain(5, 1)
+ adc.mux(4)
+
+ adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+
#save mux state
- old_mode = [mux.ota[i].mode for i in range(2)]
+ #old_mode = [mux.ota[i].mode for i in range(2)]
mux.otaA.mode = mux.otaA.CAL_CMP
mux.otaB.mode = mux.otaB.CAL_CMP
+ mux.otaA.fast = 1
+ mux.otaB.fast = 1
+ mux.otaA.gain = 15
+ mux.otaB.gain = 15
mux.write()
- adc.mux(4)
+
def func(values, converging):
outA = mux_a_offset(values[0], converging[0], verbose)
out[0] = outA
out[1] = outB
return out
- return array([outA, outB])
+ #return array([outA, outB])
#setup initial guess, just use the current values
x0 = zeros((2,), dtype=int)
elements=elements, verbose=verbose)
#restore mux state
- for i,mode in enumerate(old_mode):
- mux.ota[i].mode = mode
- mux.write()
+ #for i,mode in enumerate(old_mode):
+ #mux.ota[i].mode = mode
+ #mux.write()
+
#
# Secondary arbitrary function, 16 channels
#
# Secondary pad buffer + mux
#
elif name == 'amux':
+ adc.reset()
+ adc.triggerMode(adc.MODE_IDLE)
+ adc.average(16)
+ adc.convst_spi(1)
+ adc.channelMode(6, adc.SE) #even channel only
+ adc.channelGain(6, 1)
+ adc.channelGain(7, 1)
+ adc.mux(6)
+ adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+
#save mux state
old_mode = [amux.ota[i].mode for i in range(2)]
amux.otaA.mode = amux.otaA.CAL_CMP
amux.otaB.mode = amux.otaB.CAL_CMP
amux.write()
- adc.mux(6)
def func(values, converging):
outA = amux_a_offset(values[0], converging[0], verbose)
-
-while False:
- import datetime as dt
- if 1:
- mux.otaA.mode = mux.otaA.CAL_BUF
- mux.otaA.fast = 1
- mux.otaA.gain = 15
- mux.write()
- print
- print 'Calibrating mux otaA'
- adc.mux(4)
- resA, tmp = secant_opt(
- mux_a_offset,
- zeros((1,), dtype=int),
- [-128, 127],
- )
- muxA_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
- print '*** mux.otaA.offset =', muxA_offset, ' @', resA
- else:
- resA = 53
- muxA_offset = 0.0
-
-
- if 1:
- mux.otaB.mode = mux.otaB.CAL_BUF
- mux.otaB.fast = 1
- mux.otaB.gain = 15
- mux.write()
- print
- print 'Calibrating mux otaB'
- adc.mux(5)
- #resB = secant_opt(mux_b_offset, 0, [-128, 127])
- resB, tmp = secant_opt(
- mux_b_offset,
- zeros((1,), dtype=int),
- [-128, 127],
- )
- muxB_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
- print '*** mux.otaB.offset =', muxB_offset, ' @', resB
- else:
- resB = 59
- muxB_offset = 0.0
+def do_the_deed():
+ while True:
+ import datetime as dt
+ if 1:
+ mux.otaA.mode = mux.otaA.CAL_CMP
+ mux.otaA.fast = 1
+ mux.otaA.gain = 15
+ mux.write()
+ print
+ print 'Calibrating mux otaA'
+ adc.mux(4)
+ resA, tmp = secant_opt(
+ mux_a_offset,
+ zeros((1,), dtype=int),
+ [-128, 127],
+ )
+ muxA_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
+ print '*** mux.otaA.offset =', muxA_offset, ' @', resA
+ else:
+ resA = 53
+ muxA_offset = 0.0
- if 0:
- offset_chain_a = []
- for n in range(48):
+ if 1:
+ mux.otaB.mode = mux.otaB.CAL_CMP
+ mux.otaB.fast = 1
+ mux.otaB.gain = 15
+ mux.write()
print
- print 'Calibrating chain %02i A' % n
- adc.triggerMode(adc.MODE_IDLE)
- adc.mux(4)
- adc.triggerMode(adc.MODE_MANUAL_MANUAL)
- mux.selA = n
- mux.selB = n
+ print 'Calibrating mux otaB'
+ adc.mux(5)
+ #resB = secant_opt(mux_b_offset, 0, [-128, 127])
+ resB, tmp = secant_opt(
+ mux_b_offset,
+ zeros((1,), dtype=int),
+ [-128, 127],
+ )
+ muxB_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
+ print '*** mux.otaB.offset =', muxB_offset, ' @', resB
+ else:
+ resB = 59
+ muxB_offset = 0.0
+
+
+ if 0:
+ offset_chain_a = []
+ for n in range(48):
+ print
+ print 'Calibrating chain %02i A' % n
+ adc.triggerMode(adc.MODE_IDLE)
+ adc.mux(4)
+ adc.triggerMode(adc.MODE_MANUAL_MANUAL)
+ mux.selA = n
+ mux.selB = n
+ mux.otaA.mode = mux.otaA.MUX_BUF
+ mux.otaB.mode = mux.otaB.MUX_BUF
+ mux.write()
+ nos = secant_opt(lambda x: chain_a_offset(x, n, muxA_offset), resA, (-128, 127))
+ mean_os = mean([offset2signed(adc.read(), 16) for i in range(1000)])
+ print '*** chain.h[%02i].otaA.offset =' % n, mean_os, ' @', nos
+ chain_outputs(n)
+
+ if 1:
mux.otaA.mode = mux.otaA.MUX_BUF
mux.otaB.mode = mux.otaB.MUX_BUF
mux.write()
- nos = secant_opt(lambda x: chain_a_offset(x, n, muxA_offset), resA, (-128, 127))
- mean_os = mean([offset2signed(adc.read(), 16) for i in range(1000)])
- print '*** chain.h[%02i].otaA.offset =' % n, mean_os, ' @', nos
- chain_outputs(n)
-
- if 1:
- mux.otaA.mode = mux.otaA.MUX_BUF
- mux.otaB.mode = mux.otaB.MUX_BUF
- mux.write()
- #def secant_opt(func, x0, limits, elements=None, x1scale=0.1, maxiter=50):
+ #def secant_opt(func, x0, limits, elements=None, x1scale=0.1, maxiter=50):
- chain_os, stats = secant_opt(
- lambda x,y: chain_offsets(x, y, (muxA_offset, muxB_offset)),
- resA*ones((2,N_CHANNELS), dtype=int),
- (-128, 127),
- verbose=True,
- )
+ chain_os, stats = secant_opt(
+ lambda x,y: chain_offsets(x, y, (muxA_offset, muxB_offset)),
+ resA*ones((2,N_CHANNELS), dtype=int),
+ (-128, 127),
+ verbose=True,
+ )
- print '-'*80
- print 'offsets:', chain_os[:N_CHANNELS]
- print 'N-evals:', stats['nevals'][:N_CHANNELS]
- print '-'*80
- print
+ print '-'*80
+ print 'offsets:', chain_os[:N_CHANNELS]
+ print 'N-evals:', stats['nevals'][:N_CHANNELS]
+ print '-'*80
+ print
- #back to h[0] for consistency
- mux.selA = 0
- mux.selB = 0
- mux.write()
+ #back to h[0] for consistency
+ mux.selA = 0
+ mux.selB = 0
+ mux.write()
- fname = '%s.npz' % (dt.datetime.now().strftime('%Y-%m-%d_%H%M%S'), )
- print '*** writing to %s ***' % fname
- savez(fname,
- muxres=(resA, resB),
- muxos=(muxA_offset, muxB_offset),
- chaincal=chain_os,
- **stats
- )
- break
+ fname = '%s.npz' % (dt.datetime.now().strftime('%Y-%m-%d_%H%M%S'), )
+ print '*** writing to %s ***' % fname
+ savez(fname,
+ muxres=(resA, resB),
+ muxos=(muxA_offset, muxB_offset),
+ chaincal=chain_os,
+ **stats
+ )
+ break