Created
March 26, 2020 20:52
-
-
Save ajeddeloh/3c1ec7941615557826fd92221f8a2717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial, time, cmath | |
import universal_usbtmc | |
import matplotlib.pyplot as plt | |
kernel_tmc = universal_usbtmc.import_backend("linux_kernel") | |
fngen = "/dev/ttyUSB0" | |
startFreq = 1000 | |
freqStep = 1000 | |
stopFreq = 50000 | |
resistor = 98 | |
scope = kernel_tmc.Instrument("/dev/usbtmc0") | |
def setup_channel(scope, channel): | |
scope.write(":chan%d:bwlimit 20M" % channel) | |
scope.write(":chan%d:coup ac" % channel) | |
scope.write(":chan%d:disp on" % channel) | |
scope.write(":chan%d:invert off" % channel) | |
scope.write(":chan%d:inv off" % channel) | |
scope.write(":chan%d:prob 1" % channel) | |
scope.write(":chan%d:offset 0" % channel) | |
scope.write(":chan%d:vern on" % channel) | |
scope.write(":chan%d:scale 0.05" % channel) | |
scope.write(":meas:source chan%d" % channel) | |
scope.write(":meas:vpp") | |
scope.write(":meas:clear all") | |
setup_channel(scope, 1) | |
setup_channel(scope, 2) | |
scope.write(":chan3:disp off") | |
scope.write(":chan4:disp off") | |
scope.write("meas:rphase") | |
scope.write(":trig:mode edge") | |
scope.write(":trig:coup hfr") | |
scope.write(":trig:coup hfr") | |
scope.write(":trig:edge:source chan1") | |
scope.write(":trig:edge:slope pos") | |
scope.write(":trig:edge:level 0") | |
scope.write(":run") | |
ser = serial.Serial(fngen, 115200) | |
ser.write(b'WFN0\n') # turn off ch2 | |
ser.readline() | |
ser.write(b'WMW00\n') # sine | |
ser.readline() | |
ser.write(b'WMA00.50\n') # 1Vpp | |
ser.readline() | |
fdata = [] | |
ldata = [] | |
rdata = [] | |
def getL(freq, vtotal, vind, phase): | |
vind = cmath.rect(vind, phase*cmath.pi/180) | |
k = vind/vtotal | |
z = (-k*resistor)/(k-1) | |
return z.imag/(2*cmath.pi*freq), z.real | |
def setchan2(scope): | |
# if Vpp is absurd (not in [-1,1]) the scale is too big, | |
# back off until it's not | |
scope.write(":acq:type normal") | |
time.sleep(.5) | |
scope.write(":meas:source chan2") | |
scale = float(scope.query(":chan2:scale?")) | |
vpp = float(scope.query(":meas:vpp?")) | |
while vpp < -2 or vpp > 8*scale: | |
scale = float(scope.query(":chan2:scale?")) | |
scope.write(":chan2:scale %e" % (scale*2)) | |
time.sleep(1) # settle? | |
vpp = float(scope.query(":meas:vpp?")) | |
# set the scale to be reasonable for measured vpp | |
scope.write(":chan2:scale %e" % (1.1 * vpp / 8)) | |
for freq in range(startFreq, stopFreq, freqStep): | |
ser.write(b'WMF%d\n'% (freq*1000000)) # 1Vpp | |
ser.readline() | |
time.sleep(.1) | |
scope.write(":tim:scale %e" % (0.1/freq)) | |
setchan2(scope) | |
scope.write(":acq:type aver") | |
scope.write(":acq:aver 256") | |
scope.write(":acq:mdepth 6000") | |
time.sleep(2) | |
vtotal = scope.query(":meas:vpp? chan1") | |
vind = scope.query(":meas:vpp? chan2") | |
phase = scope.query(":meas:rphase? chan1, chan2") | |
l, r = getL(freq, float(vtotal), float(vind), float(phase)) | |
print(l*1e6, r) | |
fdata.append(freq) | |
ldata.append(l*1e6) | |
rdata.append(r) | |
ser.close() | |
fig, ax = plt.subplots() | |
ax.plot(fdata, ldata) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment