This commit is contained in:
2026-04-10 12:56:49 +02:00
parent a67b700699
commit f11223744e
+28 -14
View File
@@ -12,9 +12,11 @@ from scipy.integrate import cumulative_trapezoid
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# R =3.4 ohm
SINE_FREQ = 100e3 SINE_FREQ = 100e3
CYCLE_COUNT = 100 CYCLE_COUNT = 10
DIV_COUNT = 10 DIV_COUNT = 10
@@ -23,16 +25,18 @@ OSC_CHANNEL_A = 1 # GEN signal
OSC_CHANNEL_B = 2 # OUT OSC_CHANNEL_B = 2 # OUT
GEN_CHANNEL = siglent.ChannelID.CH1 GEN_CHANNEL = siglent.ChannelID.CH1
UPPER_BOUND_DIV = 8 UPPER_BOUND_DIV = 7
LOWER_BOUND_DIV = 3 LOWER_BOUND_DIV = 3
TARGET_DIV = (UPPER_BOUND_DIV + LOWER_BOUND_DIV) / 2 TARGET_DIV = (UPPER_BOUND_DIV + LOWER_BOUND_DIV) / 2
PATH = "/home/zychlix/Desktop/pomiary/out" PATH = "/home/zychlix/Desktop/pomiary/out"
PATH_BH = "/home/zychlix/Desktop/pomiary/out_BH"
PATH_BH_RAW = "/home/zychlix/Desktop/pomiary/out_BH_raw"
R0 = 99.4 R0 = 99.5
AMPLITUDE = 3 AMPLITUDE = 10
COUNT = 200 COUNT = 50
class ImpedanceAnalyzer: class ImpedanceAnalyzer:
@@ -52,6 +56,7 @@ class ImpedanceAnalyzer:
self.dc = 0 self.dc = 0
self.scales = [10] * (CHANNEL_COUNT + 1) self.scales = [10] * (CHANNEL_COUNT + 1)
return return
def getImpedance(self, freq: float): def getImpedance(self, freq: float):
@@ -77,7 +82,9 @@ class ImpedanceAnalyzer:
print(f"{freq} V_a {v_a} V_o {v_o}") print(f"{freq} V_a {v_a} V_o {v_o}")
z = R0 * (v_a - v_o) / v_o # z = R0 * (v_o) / v_o #resistor to gnd
z = R0 * (v_o) / (v_a - v_o) # resistor to input
# z = v_a / (v_o / R0) # z = v_a / (v_o / R0)
self.debug_voltages.append([np.abs(v_a), np.abs(v_o)]) self.debug_voltages.append([np.abs(v_a), np.abs(v_o)])
@@ -87,8 +94,10 @@ class ImpedanceAnalyzer:
def getScaledWaveform(self, ch): def getScaledWaveform(self, ch):
data = self.osc.getChannel(ch).getWaveform() data = self.osc.getChannel(ch).getWaveform()
vpp = self.calculateVRMS(data) * 2 * np.sqrt(2) vpp = self.calculateVRMS(data) * 2 * np.sqrt(2)
print(f"VPP measured from rms: {vpp} on channel {ch}: SCALE {self.scales[ch]}")
if not (LOWER_BOUND_DIV < vpp / self.scales[ch] < UPPER_BOUND_DIV): if not (LOWER_BOUND_DIV < vpp / self.scales[ch] < UPPER_BOUND_DIV):
# self.autoscale(ch) self.autoscale(ch)
time.sleep(2) # change back to 1 time.sleep(2) # change back to 1
self.osc.single() self.osc.single()
data = self.osc.getChannel(ch).getWaveform() data = self.osc.getChannel(ch).getWaveform()
@@ -116,7 +125,8 @@ class ImpedanceAnalyzer:
data = self.osc.getChannel(channel).getWaveform() data = self.osc.getChannel(channel).getWaveform()
time.sleep(1) time.sleep(1)
rms = self.calculateVRMS(data) # rms = self.calculateVRMS(data)
min = numpy.dat
vpp = rms * 2 * np.sqrt(2) vpp = rms * 2 * np.sqrt(2)
if LOWER_BOUND_DIV < vpp / self.scales[channel] < UPPER_BOUND_DIV: if LOWER_BOUND_DIV < vpp / self.scales[channel] < UPPER_BOUND_DIV:
@@ -132,6 +142,7 @@ class ImpedanceAnalyzer:
self.scales[channel] = self.osc.getChannel(channel).clampVscale( self.scales[channel] = self.osc.getChannel(channel).clampVscale(
vpp / TARGET_DIV vpp / TARGET_DIV
) )
self.osc.getChannel(channel).setVScale(self.scales[channel]) self.osc.getChannel(channel).setVScale(self.scales[channel])
print( print(
f"Autoscaled channel: {channel}, RMS: {rms}, Vpp:{vpp}, Scale:{self.scales[channel]}" f"Autoscaled channel: {channel}, RMS: {rms}, Vpp:{vpp}, Scale:{self.scales[channel]}"
@@ -140,13 +151,13 @@ class ImpedanceAnalyzer:
return return
def PlotBH(self, freq, amplitude): def PlotBH(self, freq, amplitude):
setWindowSize(self.osc, 1, freq) setWindowSize(self.osc, 2, freq)
self.gen.channels[GEN_CHANNEL].apply_sine(freq, amplitude, self.dc) self.gen.channels[GEN_CHANNEL].apply_sine(freq, amplitude, self.dc)
self.gen.channels[GEN_CHANNEL].set_output(True) self.gen.channels[GEN_CHANNEL].set_output(True)
self.osc.run() self.osc.run()
time.sleep(5) time.sleep(2)
self.osc.single() self.osc.single()
@@ -166,9 +177,11 @@ class ImpedanceAnalyzer:
B = ( B = (
cumulative_trapezoid(v_l, time_array, initial=0) / 1 cumulative_trapezoid(v_l, time_array, initial=0) / 1
) # Correct to proper values ) # Correct to proper values
H = current_data / R0 # As well as in here H = current_data # As well as in here
plt.plot(H, B) plt.plot(H, B)
self.exportZtoCSV(np.array([B, H]).T, PATH_BH_RAW)
self.exportZtoCSV(np.array([B, H]).T, PATH_BH)
plt.show() plt.show()
@@ -205,7 +218,7 @@ def main():
# imp = ImpedanceAnalyzer("TCPIP::10.112.1.2::INSTR", "TCPIP::10.112.1.3::INSTR") # imp = ImpedanceAnalyzer("TCPIP::10.112.1.2::INSTR", "TCPIP::10.112.1.3::INSTR")
imp = ImpedanceAnalyzer("TCPIP::192.168.1.4::INSTR", "TCPIP::192.168.1.5::INSTR") imp = ImpedanceAnalyzer("TCPIP::192.168.1.2::INSTR", "TCPIP::192.168.1.3::INSTR")
imp.gen.channels[GEN_CHANNEL.CH1].set_output(True) imp.gen.channels[GEN_CHANNEL.CH1].set_output(True)
@@ -213,9 +226,9 @@ def main():
# imp.autoscale(2) # imp.autoscale(2)
imp.PlotBH(3e3, AMPLITUDE) # imp.PlotBH(10e3, AMPLITUDE)
return 0 # return 0
z, f = imp.getSweep(2, 7, COUNT) z, f = imp.getSweep(2, 7, COUNT)
z_real = np.real(z) z_real = np.real(z)
@@ -241,6 +254,7 @@ def main():
ax[1].legend() ax[1].legend()
imp.exportZtoCSV(np.array([f, z]).T, PATH) imp.exportZtoCSV(np.array([f, z]).T, PATH)
plt.savefig(PATH + ".png")
plt.show() plt.show()