diff --git a/main.py b/main.py index 966e326..c1db694 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from matplotlib.pylab import sca from PIL.ImageChops import offset from siglent_sdg.siglent import SiglentGen from asyncio import sleep @@ -16,7 +17,7 @@ import matplotlib.pyplot as plt # R =3.4 ohm SINE_FREQ = 100e3 -CYCLE_COUNT = 10 +CYCLE_COUNT = 1000 DIV_COUNT = 10 @@ -25,18 +26,18 @@ OSC_CHANNEL_A = 1 # GEN signal OSC_CHANNEL_B = 2 # OUT GEN_CHANNEL = siglent.ChannelID.CH1 -UPPER_BOUND_DIV = 7 -LOWER_BOUND_DIV = 3 +UPPER_BOUND_DIV = 3.5 +LOWER_BOUND_DIV = 1 TARGET_DIV = (UPPER_BOUND_DIV + LOWER_BOUND_DIV) / 2 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.5 +R0 = 19.82 AMPLITUDE = 10 -COUNT = 50 +COUNT = 100 class ImpedanceAnalyzer: @@ -55,7 +56,10 @@ class ImpedanceAnalyzer: self.dc = 0 - self.scales = [10] * (CHANNEL_COUNT + 1) + self.scales = [1] * (CHANNEL_COUNT + 1) + + # for i in self.osc.channels + # se return @@ -75,6 +79,11 @@ class ImpedanceAnalyzer: channel_A_data = self.getScaledWaveform(OSC_CHANNEL_A) channel_B_data = self.getScaledWaveform(OSC_CHANNEL_B) + self.osc.single() + time.sleep(2) + channel_A_data = self.osc.getChannel(OSC_CHANNEL_A).getWaveform() + channel_B_data = self.osc.getChannel(OSC_CHANNEL_B).getWaveform() + time_array = self.osc.getChannel(OSC_CHANNEL_A).genTimeArray(channel_A_data) v_a = dft(channel_A_data, time_array, freq) @@ -92,15 +101,41 @@ class ImpedanceAnalyzer: return z def getScaledWaveform(self, ch): - data = self.osc.getChannel(ch).getWaveform() - 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): - self.autoscale(ch) - time.sleep(2) # change back to 1 + while True: self.osc.single() + time.sleep(1) data = self.osc.getChannel(ch).getWaveform() + + minimum = np.min(data) + maximum = np.max(data) + + scale_range = self.osc.getChannel(ch).getScaleRange() + if ( + maximum / self.scales[ch] > UPPER_BOUND_DIV + or minimum / self.scales[ch] < -UPPER_BOUND_DIV + ): + self.scales[ch] = self.scales[ch] * 2 + if self.scales[ch] > scale_range[1]: + self.scales[ch] = scale_range[1] + self.osc.getChannel(ch).setVScale(self.scales[ch]) + break + elif ( + minimum / self.scales[ch] < LOWER_BOUND_DIV + and minimum / self.scales[ch] > -LOWER_BOUND_DIV + ): + self.scales[ch] = self.scales[ch] / 2 + if self.scales[ch] < scale_range[0]: + self.scales[ch] = scale_range[0] + self.osc.getChannel(ch).setVScale(self.scales[ch]) + break + else: + break + + self.osc.getChannel(ch).setVScale(self.scales[ch]) + print( + f"Channel {ch} settings: min: {minimum}, max: {maximum} scale:{self.scales[ch]}" + ) + return data def getSweep(self, start: float, stop: float, samples): @@ -126,22 +161,31 @@ class ImpedanceAnalyzer: data = self.osc.getChannel(channel).getWaveform() time.sleep(1) # rms = self.calculateVRMS(data) - min = numpy.dat - vpp = rms * 2 * np.sqrt(2) + minimum = np.min(data) + maximum = np.max(data) - if LOWER_BOUND_DIV < vpp / self.scales[channel] < UPPER_BOUND_DIV: - break - elif ( - vpp / self.scales[channel] > UPPER_BOUND_DIV - or self.osc.getChannel(channel).getVrms()[0] > 1000 + scale_range = self.osc.getChannel(channel).getScaleRange() + + if ( + maximum / self.scales[channel] > UPPER_BOUND_DIV + or minimum / self.scales[channel] < -UPPER_BOUND_DIV ): - self.scales[channel] = self.osc.getChannel(channel).clampVscale( - self.scales[channel] * 2 - ) + self.scales[channel] = self.scales[channel] * 2 + if self.scales[channel] > scale_range[1]: + self.scales[channel] = scale_range[1] + self.osc.getChannel(channel).setVScale(self.scales[channel]) + break + elif ( + minimum / self.scales[channel] < LOWER_BOUND_DIV + and minimum / self.scales[channel] > -LOWER_BOUND_DIV + ): + self.scales[channel] = self.scales[channel] / 2 + if self.scales[channel] < scale_range[0]: + self.scales[channel] = scale_range[0] + self.osc.getChannel(channel).setVScale(self.scales[channel]) + break else: - self.scales[channel] = self.osc.getChannel(channel).clampVscale( - vpp / TARGET_DIV - ) + break self.osc.getChannel(channel).setVScale(self.scales[channel]) print( @@ -229,7 +273,7 @@ def main(): # imp.PlotBH(10e3, AMPLITUDE) # return 0 - z, f = imp.getSweep(2, 7, COUNT) + z, f = imp.getSweep(5, 5, 1) z_real = np.real(z) z_imag = np.imag(z)