From 586478f487ad03f0774f8f50bc40c6c029b6176d Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sun, 5 Jun 2022 11:24:36 +0200 Subject: Added frequency response measurement initial steps --- lab_control/test/frequency_response_test.py | 21 ++++++++++++++++----- lab_control/test/mock_lab.py | 13 ++++++++++++- lab_control/test/mock_sds1000xe_device.py | 15 +++++++++++---- lab_control/test/sds1000xe_test.py | 14 ++++++++++++++ 4 files changed, 53 insertions(+), 10 deletions(-) (limited to 'lab_control/test') diff --git a/lab_control/test/frequency_response_test.py b/lab_control/test/frequency_response_test.py index 2997413..61fd021 100644 --- a/lab_control/test/frequency_response_test.py +++ b/lab_control/test/frequency_response_test.py @@ -3,6 +3,7 @@ import pytest from lab_control.test.mock_lab import MockLab from lab_control.frequency_response import FrequencyResponseMeasurement from lab_control.measurement import getLinearRange +from lab_control.function_generator import FunctionGenerator @pytest.fixture def mockLab(): @@ -14,7 +15,6 @@ def uut(mockLab): def prepareRampResponse(uut, mockLab): # Expect a ramp response from 0.5 to 1.5 * input amplitude - inputAmplitude = 2.0 minScale = 0.5 maxScale = 1.5 @@ -25,15 +25,14 @@ def prepareRampResponse(uut, mockLab): mockLab.connectChannels(uut.functionGeneratorChannel, uut.oscilloscopeChannel) mockLab.setTestFunction(uut.oscilloscopeChannel, testFunction) - mockLab.setAmplitude(uut.functionGeneratorChannel, inputAmplitude) - mockLab.setOn(uut.functionGeneratorChannel) - return [(f, testFunction(f) * inputAmplitude) for f in getLinearRange(uut.minFrequency, uut.maxFrequency, uut.steps)] + return [(f, testFunction(f) * uut.inputAmplitude) for f in getLinearRange(uut.minFrequency, uut.maxFrequency, uut.steps)] def test_frequencyResponseDefaults(uut): assert uut.minFrequency == 20e0 assert uut.maxFrequency == 16e3 assert uut.steps == 50 + assert uut.inputAmplitude == 0.1 assert uut.functionGeneratorChannel == 1 assert uut.oscilloscopeChannel == 1 @@ -43,14 +42,26 @@ def test_frequencyResponseRamp(uut, mockLab): uut.steps = 11 uut.functionGeneratorChannel = 1 uut.oscilloscopeChannel = 1 + uut.inputAmplitude = 2.0 + + fg = mockLab.getFunctionGeneratorChannel(uut.functionGeneratorChannel) + osc = mockLab.getOscilloscopeChannel(uut.oscilloscopeChannel) expectedData = prepareRampResponse(uut, mockLab) assert not uut.measurementDone - assert uut.data == None + assert uut.data is None + assert fg.amplitude is None + assert fg.function is None + assert not fg.on + assert osc.voltsPerDiv is None uut.measure(mockLab, mockLab) + assert fg.amplitude == uut.inputAmplitude + assert fg.function == FunctionGenerator.SINE + assert fg.on + assert osc.voltsPerDiv == uut.inputAmplitude / 6 assert uut.measurementDone assert uut.data == expectedData diff --git a/lab_control/test/mock_lab.py b/lab_control/test/mock_lab.py index 40893ca..75ba1bf 100644 --- a/lab_control/test/mock_lab.py +++ b/lab_control/test/mock_lab.py @@ -9,11 +9,13 @@ class MockLab(FunctionGenerator, Oscilloscope): self.on = False self.frequency = None self.amplitude = None + self.function = None class OscChannelState: def __init__(self): self.testFunction = None self.connectedChannel = None + self.voltsPerDiv = None def __init__(self): self.fgChannels = [MockLab.FGChannelState() for i in range(0, 2)] @@ -32,7 +34,7 @@ class MockLab(FunctionGenerator, Oscilloscope): self.fgChannels[channel - 1].amplitude = amplitude def setFunction(self, channel: int, function: int) -> None: - pass + self.fgChannels[channel - 1].function = function def measureAmplitude(self, channel: int) -> float: fgChannel = self.oscChannels[channel - 1].connectedChannel @@ -50,8 +52,17 @@ class MockLab(FunctionGenerator, Oscilloscope): def measureFrequency(self, channel: int) -> float: pass + def setVoltsPerDivision(self, channel: int, volts: float) -> None: + self.oscChannels[channel - 1].voltsPerDiv = volts + def setTestFunction(self, channel: int, f: Callable[[float], float]) -> None: self.oscChannels[channel - 1].testFunction = f def connectChannels(self, fg: int, osc: int) -> None: self.oscChannels[osc - 1].connectedChannel = self.fgChannels[fg - 1] + + def getFunctionGeneratorChannel(self, channel: int): + return self.fgChannels[channel - 1] + + def getOscilloscopeChannel(self, channel: int): + return self.oscChannels[channel - 1] diff --git a/lab_control/test/mock_sds1000xe_device.py b/lab_control/test/mock_sds1000xe_device.py index b4dd222..68c2471 100644 --- a/lab_control/test/mock_sds1000xe_device.py +++ b/lab_control/test/mock_sds1000xe_device.py @@ -24,8 +24,8 @@ class MockSDS1000XEDevice: self._mainThread = threading.Thread(target=self._mainLoop) self._mainThread.start() - # Mock measured values - self._channels = [{"AMPL" : None} for i in range(0, 4)] + # Mock internal values + self._channels = [{"AMPL" : None, "VDIV" : None} for i in range(0, 4)] def _mainLoop(self) -> None: self._clientSocket, _ = _serverSocket.accept() @@ -35,7 +35,7 @@ class MockSDS1000XEDevice: while not self._stopFlag: try: request = self._clientSocket.recv(4096).decode() - response = self._handleRequest(request) + response = self._handleRequest(request.strip()) if response is not None: self._clientSocket.send(response.encode()) except TimeoutError as e: @@ -44,7 +44,7 @@ class MockSDS1000XEDevice: self._clientSocket.close() def _handleRequest(self, request: str) -> str: - m = re.search(r"C(?P\d):(?P\w+)\?\s(?P\w+)", request) + m = re.search(r"C(?P\d):(?P\w+)\??\s(?P.+)", request) if not m: return None @@ -61,6 +61,10 @@ class MockSDS1000XEDevice: else: response = f"C{m.group('channel')}:PAVA {arg},{value:.6E}{unit}" return response + elif opcode == "VDIV": + arg = float(m.group("arg").rstrip("V")) + self._channels[channelIndex]["VDIV"] = arg + return None def stop(self) -> None: self._stopFlag = True @@ -78,3 +82,6 @@ class MockSDS1000XEDevice: def setFrequency(self, channel: int, value: float) -> None: self._channels[channel - 1]["FREQ"] = value + def getVoltsPerDivision(self, channel: int) -> float: + return self._channels[channel - 1]["VDIV"] + diff --git a/lab_control/test/sds1000xe_test.py b/lab_control/test/sds1000xe_test.py index 6f3de80..3774d52 100644 --- a/lab_control/test/sds1000xe_test.py +++ b/lab_control/test/sds1000xe_test.py @@ -1,4 +1,5 @@ import pytest +import time from lab_control.sds1000xe import SDS1000XE from lab_control.test.mock_sds1000xe_device import MockSDS1000XEDevice @@ -48,3 +49,16 @@ def test_invalidChannel(uut, mockDevice): with pytest.raises(AssertionError): m(t) +def test_setVoltsPerDivision(uut, mockDevice): + testValues = [5e-3, 50e-3, 1e0, 5e0, 10e0, 100e0] + + for channel in SDS1000XE.AVAILABLE_CHANNELS: + assert mockDevice.getVoltsPerDivision(channel) is None + + for value in testValues: + uut.setVoltsPerDivision(channel, value) + + time.sleep(0.1) # Allow time for the mock to receive and process the request + + assert mockDevice.getVoltsPerDivision(channel) == value + -- cgit v1.2.3