Skip to content

Commit

Permalink
feat: Improved algorithm for measuring capacitance
Browse files Browse the repository at this point in the history
  • Loading branch information
AsCress authored and bessman committed Jul 12, 2024
1 parent c66fcac commit 9dc5d44
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
52 changes: 13 additions & 39 deletions pslab/instrument/multimeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Multimeter(Oscilloscope):
_CAPACITOR_DISCHARGED_VOLTAGE = 0.01 * max(INPUT_RANGES["CAP"])

def __init__(self, device: SerialHandler = None):
self._stray_capacitance = 5e-11
self._stray_capacitance = 46e-12
super().__init__(device)

def measure_resistance(self) -> float:
Expand Down Expand Up @@ -125,48 +125,22 @@ def measure_capacitance(self) -> float:
Capacitance in Farad.
"""
for current_range in self._CURRENTS_RANGES:
for i, charge_time in enumerate([50000, 5000, 500, 50, 5]):
voltage, _ = self._measure_capacitance(current_range, 0, charge_time)

if voltage < self._CAPACITOR_CHARGED_VOLTAGE:
if i:
return self._binary_search_capacitance(
current_range, charge_time, charge_time * 10
)
else:
break # Increase current.
charge_time = 10
for _ in range(10):
if charge_time > 50000:
break # Increase current.
voltage, capacitance = self._measure_capacitance(
current_range, 0, charge_time
)
if 0.98 < voltage / self._CAPACITOR_CHARGED_VOLTAGE < 1.02:
return capacitance
charge_time = int(
charge_time * self._CAPACITOR_CHARGED_VOLTAGE / voltage
)

# Capacitor too big, use alternative method.
return self._measure_rc_capacitance()

def _binary_search_capacitance(
self,
current_range: int,
low_charge_time: int,
high_charge_time: int,
) -> float:
charge_time = (high_charge_time + low_charge_time) // 2
voltage, capacitance = self._measure_capacitance(
current_range,
0,
charge_time,
)

if voltage / self._CAPACITOR_CHARGED_VOLTAGE < 0.98:
return self._binary_search_capacitance(
current_range,
charge_time,
high_charge_time,
)
elif voltage / self._CAPACITOR_CHARGED_VOLTAGE > 1.02:
return self._binary_search_capacitance(
current_range,
low_charge_time,
charge_time,
)
else:
return capacitance

def _set_cap(self, state, charge_time):
"""Set CAP HIGH or LOW."""
self._device.send_byte(CP.ADC)
Expand Down
2 changes: 1 addition & 1 deletion tests/recordings/multimeter/test_measure_capacitance.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [136, 19], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [108, 107], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [122, 63], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [129, 41], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [132, 30], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [6, 25], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [71, 22], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [166, 23], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [246, 22]], [[], [], [], [1], [], [], [], [1], [], [], [147, 47, 1], [], [], [], [1], [], [], [184, 3, 1], [], [], [], [1], [], [], [58, 3, 1], [], [], [], [], [255, 15, 1], [], [], [140, 215, 1], [], [], [], [1], [], [], [255, 5, 1], [], [], [], [1], [], [], [42, 4, 1], [], [], [], [], [122, 12, 1], [], [], [173, 170, 1], [], [], [], [1], [], [], [23, 5, 1], [], [], [], [1], [], [], [162, 3, 1], [], [], [], [], [255, 15, 1], [], [], [131, 214, 1], [], [], [], [1], [], [], [227, 5, 1], [], [], [], [1], [], [], [149, 3, 1], [], [], [], [], [254, 15, 1], [], [], [254, 216, 1], [], [], [], [1], [], [], [236, 5, 1], [], [], [], [1], [], [], [167, 3, 1], [], [], [], [], [253, 15, 1], [], [], [50, 215, 1], [], [], [], [1], [], [], [173, 5, 1], [], [], [], [1], [], [], [145, 3, 1], [], [], [], [], [250, 15, 1], [], [], [226, 213, 1], [], [], [], [1], [], [], [188, 5, 1], [], [], [], [1], [], [], [22, 4, 1], [], [], [], [], [125, 15, 1], [], [], [71, 208, 1], [], [], [], [1], [], [], [124, 5, 1], [], [], [], [1], [], [], [107, 3, 1], [], [], [], [], [2, 14, 1], [], [], [178, 189, 1], [], [], [], [1], [], [], [223, 5, 1], [], [], [], [1], [], [], [186, 3, 1], [], [], [], [], [197, 14, 1], [], [], [69, 199, 1], [], [], [], [1], [], [], [141, 5, 1], [], [], [], [1], [], [], [158, 3, 1], [], [], [], [], [96, 14, 1]]]
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [10, 0], [2], [10], [5], [11], [2], [1], [0], [139, 6], [2], [10], [5], [2], [21], [0], [80, 195], [2], [10], [5], [11], [2], [1], [0], [236, 21]], [[], [], [], [1], [], [], [], [1], [], [], [193, 225, 1], [], [], [], [1], [], [], [169, 2, 1], [], [], [], [1], [], [], [116, 0, 1], [], [], [], [], [22, 0, 1], [], [], [196, 0, 1], [], [], [], [], [76, 4, 1], [], [], [227, 61, 1], [], [], [], [1], [], [], [31, 1, 1], [], [], [], [], [31, 14, 1]]]
2 changes: 1 addition & 1 deletion tests/recordings/multimeter/test_measure_resistance.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [7]], [[], [], [], [1], [], [], [], [1], [], [], [157, 170, 1]]]
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [7]], [[], [], [], [1], [], [], [], [1], [], [], [118, 169, 1]]]
2 changes: 1 addition & 1 deletion tests/recordings/multimeter/test_voltmeter_autorange.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [8], [1], [0], [2], [10], [3], [2], [8], [1], [7]], [[], [], [], [1], [], [], [], [1], [], [], [], [1], [], [], [71, 128, 1], [], [], [], [1]]]
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [8], [1], [0], [2], [10], [3], [2], [8], [1], [7]], [[], [], [], [1], [], [], [], [1], [], [], [], [1], [], [], [255, 127, 1], [], [], [], [1]]]

0 comments on commit 9dc5d44

Please sign in to comment.