from pathlib import Path import time from lab_control.function_generator import FunctionGenerator from lab_control.oscilloscope import Oscilloscope from lab_control.measurement import Measurement, getLinearRange class FrequencyResponseMeasurement(Measurement): def __init__(self): self.minFrequency = 20e0 self.maxFrequency = 16e3 self.steps = 50 self.inputAmplitude = 0.1 self.functionGeneratorChannel = 1 self.oscilloscopeChannel = 1 self.measurementDone = False self.data = None def measure(self, osc: Oscilloscope, fg: FunctionGenerator) -> None: frequencies = getLinearRange(self.minFrequency, self.maxFrequency, self.steps) self.data = [] # Initial set up fg.setAmplitude(self.functionGeneratorChannel, self.inputAmplitude) fg.setFunction(self.functionGeneratorChannel, FunctionGenerator.SINE) fg.setOn(self.functionGeneratorChannel) osc.setVoltsPerDivision(self.oscilloscopeChannel, self.inputAmplitude / (osc.getDivisionsDisplayed() - 2)) for f in frequencies: fg.setFrequency(self.functionGeneratorChannel, f) #time.sleep(0.2) response = osc.measureAmplitude(self.oscilloscopeChannel) self.data.append((f, response)) self.measurementDone = True def dumpToCSV(self, path: Path) -> None: if self.data is None: return with open(path, "w") as f: f.write("Frequency (Hz),Amplitude (V)") for entry in self.data: f.write(f"\n{entry[0]},{entry[1]}")