Skip to content

Commit 9dc5d44

Browse files
AsCressbessman
authored andcommitted
feat: Improved algorithm for measuring capacitance
1 parent c66fcac commit 9dc5d44

File tree

4 files changed

+16
-42
lines changed

4 files changed

+16
-42
lines changed

pslab/instrument/multimeter.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Multimeter(Oscilloscope):
3131
_CAPACITOR_DISCHARGED_VOLTAGE = 0.01 * max(INPUT_RANGES["CAP"])
3232

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

3737
def measure_resistance(self) -> float:
@@ -125,48 +125,22 @@ def measure_capacitance(self) -> float:
125125
Capacitance in Farad.
126126
"""
127127
for current_range in self._CURRENTS_RANGES:
128-
for i, charge_time in enumerate([50000, 5000, 500, 50, 5]):
129-
voltage, _ = self._measure_capacitance(current_range, 0, charge_time)
130-
131-
if voltage < self._CAPACITOR_CHARGED_VOLTAGE:
132-
if i:
133-
return self._binary_search_capacitance(
134-
current_range, charge_time, charge_time * 10
135-
)
136-
else:
137-
break # Increase current.
128+
charge_time = 10
129+
for _ in range(10):
130+
if charge_time > 50000:
131+
break # Increase current.
132+
voltage, capacitance = self._measure_capacitance(
133+
current_range, 0, charge_time
134+
)
135+
if 0.98 < voltage / self._CAPACITOR_CHARGED_VOLTAGE < 1.02:
136+
return capacitance
137+
charge_time = int(
138+
charge_time * self._CAPACITOR_CHARGED_VOLTAGE / voltage
139+
)
138140

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

142-
def _binary_search_capacitance(
143-
self,
144-
current_range: int,
145-
low_charge_time: int,
146-
high_charge_time: int,
147-
) -> float:
148-
charge_time = (high_charge_time + low_charge_time) // 2
149-
voltage, capacitance = self._measure_capacitance(
150-
current_range,
151-
0,
152-
charge_time,
153-
)
154-
155-
if voltage / self._CAPACITOR_CHARGED_VOLTAGE < 0.98:
156-
return self._binary_search_capacitance(
157-
current_range,
158-
charge_time,
159-
high_charge_time,
160-
)
161-
elif voltage / self._CAPACITOR_CHARGED_VOLTAGE > 1.02:
162-
return self._binary_search_capacitance(
163-
current_range,
164-
low_charge_time,
165-
charge_time,
166-
)
167-
else:
168-
return capacitance
169-
170144
def _set_cap(self, state, charge_time):
171145
"""Set CAP HIGH or LOW."""
172146
self._device.send_byte(CP.ADC)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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]]]
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]]]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [7]], [[], [], [], [1], [], [], [], [1], [], [], [157, 170, 1]]]
1+
[[[2], [8], [1], [0], [2], [8], [2], [0], [2], [10], [7]], [[], [], [], [1], [], [], [], [1], [], [], [118, 169, 1]]]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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]]]
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 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy