From 7c7d85f9bdc4715500cbf901fcdd8eb605d2668e Mon Sep 17 00:00:00 2001
From: Eddy Pedroni <eddy@0xf7.com>
Date: Wed, 1 Jun 2022 22:12:02 +0200
Subject: Minor improvements

---
 .pylintrc                                          |  4 +++-
 lab_control/frequency_response.py                  | 14 ++++++------
 lab_control/function_generator.py                  |  3 ---
 lab_control/measurement.py                         |  6 ++++++
 lab_control/oscilloscope.py                        |  3 ---
 .../test/frequency_response_measurement_test.py    | 25 ++++++++++++++++------
 lab_control/test/mock_lab.py                       | 17 +++++++++------
 7 files changed, 44 insertions(+), 28 deletions(-)
 create mode 100644 lab_control/measurement.py

diff --git a/.pylintrc b/.pylintrc
index f56d259..2944895 100644
--- a/.pylintrc
+++ b/.pylintrc
@@ -311,7 +311,9 @@ good-names=i,
            Run,
            _,
            uut,
-           ch
+           ch,
+           fg,
+           osc
 
 # Good variable names regexes, separated by a comma. If names match any regex,
 # they will always be accepted
diff --git a/lab_control/frequency_response.py b/lab_control/frequency_response.py
index ca2002e..af399b1 100644
--- a/lab_control/frequency_response.py
+++ b/lab_control/frequency_response.py
@@ -1,13 +1,14 @@
 from lab_control.function_generator import FunctionGenerator
 from lab_control.oscilloscope import Oscilloscope
+from lab_control.measurement import Measurement
 
-class FrequencyResponseMeasurement:
+class FrequencyResponseMeasurement(Measurement):
     def __init__(self):
-        self.minFrequency = None
-        self.maxFrequency = None
-        self.steps = None
-        self.functionGeneratorChannel = None
-        self.oscilloscopeChannel = None
+        self.minFrequency = 20e0
+        self.maxFrequency = 16e3
+        self.steps = 50
+        self.functionGeneratorChannel = 1
+        self.oscilloscopeChannel = 1
         self.measurementDone = False
         self.data = None
 
@@ -22,4 +23,3 @@ class FrequencyResponseMeasurement:
             self.data.append((frequency, response))
 
         self.measurementDone = True
-
diff --git a/lab_control/function_generator.py b/lab_control/function_generator.py
index a403784..20f1b8c 100644
--- a/lab_control/function_generator.py
+++ b/lab_control/function_generator.py
@@ -5,9 +5,6 @@ class FunctionGenerator:
     This interface specifies the common API for all
     supported function generator devices.
     """
-    def __init__(self):
-        pass
-
     def setOn(self, channel: int) -> None:
         """ Enable channel. """
 
diff --git a/lab_control/measurement.py b/lab_control/measurement.py
new file mode 100644
index 0000000..78f393e
--- /dev/null
+++ b/lab_control/measurement.py
@@ -0,0 +1,6 @@
+from lab_control.function_generator import FunctionGenerator
+from lab_control.oscilloscope import Oscilloscope
+
+class Measurement:
+    def measure(self, osc: Oscilloscope, fg: FunctionGenerator) -> None:
+        pass
diff --git a/lab_control/oscilloscope.py b/lab_control/oscilloscope.py
index e4b5a89..167879c 100644
--- a/lab_control/oscilloscope.py
+++ b/lab_control/oscilloscope.py
@@ -5,9 +5,6 @@ class Oscilloscope:
     This interface specifies the common API for all
     supported oscilloscope devices.
     """
-    def __init__(self):
-        pass
-
     def measureAmplitude(self, channel: int) -> float:
         """ Return amplitude measurement on specific channel. """
 
diff --git a/lab_control/test/frequency_response_measurement_test.py b/lab_control/test/frequency_response_measurement_test.py
index caea62a..5a73b3e 100644
--- a/lab_control/test/frequency_response_measurement_test.py
+++ b/lab_control/test/frequency_response_measurement_test.py
@@ -11,6 +11,13 @@ def mockLab():
 def uut(mockLab):
     return FrequencyResponseMeasurement()
 
+def test_frequencyResponseDefaults(uut):
+    assert uut.minFrequency == 20e0
+    assert uut.maxFrequency == 16e3
+    assert uut.steps == 50
+    assert uut.functionGeneratorChannel == 1
+    assert uut.oscilloscopeChannel == 1
+
 def test_frequencyResponseRamp(mockLab, uut):
     uut.minFrequency = 100.0
     uut.maxFrequency = 200.0
@@ -18,18 +25,22 @@ def test_frequencyResponseRamp(mockLab, uut):
     uut.functionGeneratorChannel = 1
     uut.oscilloscopeChannel = 1
 
-    # Expect a ramp response from 0 to 2 V
-    minAmplitude = 0.0
-    maxAmplitude = 2.0
+    # Expect a ramp response from 0.5 to 1.5 * input amplitude
+    inputAmplitude = 1.0
+    minScale = 0.5
+    maxScale = 1.5
 
-    def amplitudeFunction(f: float) -> float:
+    def testFunction(f: float) -> float:
         assert f >= uut.minFrequency and f <= uut.maxFrequency
         frequencyPu = (f - uut.minFrequency) / (uut.maxFrequency - uut.minFrequency)
-        return minAmplitude + maxAmplitude * frequencyPu
+        return minScale + maxScale * frequencyPu
 
     mockLab.connectChannels(uut.functionGeneratorChannel, uut.oscilloscopeChannel)
-    mockLab.setAmplitudeFunction(uut.oscilloscopeChannel, amplitudeFunction)
-    expectedData = [(f, amplitudeFunction(f)) for f in [100.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0, 200.0]]
+    mockLab.setTestFunction(uut.oscilloscopeChannel, testFunction)
+    mockLab.setAmplitude(uut.functionGeneratorChannel, inputAmplitude)
+    mockLab.setOn(uut.functionGeneratorChannel)
+
+    expectedData = [(f, testFunction(f) * inputAmplitude) for f in [100.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0, 200.0]]
 
     assert not uut.measurementDone
     assert uut.data == None
diff --git a/lab_control/test/mock_lab.py b/lab_control/test/mock_lab.py
index 718f319..40893ca 100644
--- a/lab_control/test/mock_lab.py
+++ b/lab_control/test/mock_lab.py
@@ -12,8 +12,8 @@ class MockLab(FunctionGenerator, Oscilloscope):
 
     class OscChannelState:
         def __init__(self):
-            self.amplitudeFunction = None
-            self.fgChannel = None
+            self.testFunction = None
+            self.connectedChannel = None
 
     def __init__(self):
         self.fgChannels = [MockLab.FGChannelState() for i in range(0, 2)]
@@ -35,8 +35,11 @@ class MockLab(FunctionGenerator, Oscilloscope):
         pass
 
     def measureAmplitude(self, channel: int) -> float:
-        fgChannel = self.oscChannels[channel - 1].fgChannel
-        return self.oscChannels[channel - 1].amplitudeFunction(self.fgChannels[fgChannel].frequency)
+        fgChannel = self.oscChannels[channel - 1].connectedChannel
+        frequency = fgChannel.frequency if fgChannel.on else 0.0
+        amplitude = fgChannel.amplitude if fgChannel.on else 0.0
+
+        return self.oscChannels[channel - 1].testFunction(frequency) * amplitude
 
     def measurePeakToPeak(self, channel: int) -> float:
         pass
@@ -47,8 +50,8 @@ class MockLab(FunctionGenerator, Oscilloscope):
     def measureFrequency(self, channel: int) -> float:
         pass
 
-    def setAmplitudeFunction(self, channel: int, f: Callable[[float], float]) -> None:
-        self.oscChannels[channel - 1].amplitudeFunction = f
+    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].fgChannel = fg - 1
+        self.oscChannels[osc - 1].connectedChannel = self.fgChannels[fg - 1]
-- 
cgit v1.2.3