fossasia / pslab-python

Python Library for PSLab Desktop: https://pslab.io
GNU General Public License v3.0
1.63k stars 226 forks source link

Capacitance Measurements Unstable #226

Closed AsCress closed 4 days ago

AsCress commented 1 week ago

After the latest update in the firmware (https://github.com/fossasia/pslab-firmware/pull/169), the capacitance measurement feature seems a bit unstable (tested using a PSLab V6 device running the updated firmware version (v3.0.1-rc1)). I tested with a couple of capacitors of different values (22 pF, 0.1uF, 100uF). Many times it seems as if the code goes in a loop and the status LED just keeps blinking without yielding any output. The implementation which I just worked on in Android (https://github.com/fossasia/pslab-android/pull/2510) is a bit different and yields fairly accurate results. I tried porting the python implementation directly in Android as well but it yields a similar issue (status LED keeps blinking without any output). I don't have much knowledge regarding how Android (Java) and Python (Windows, Linux, etc.) environments can be different. But, I propose that we can have a look and try a similar implementation in Python as well. Here, is a small explaination, this is our current code in python for measuring capacitance:

 def measure_capacitance(self) -> float:
        """Measure the capacitance of a capacitor connected between CAP and GND.

        Returns
        -------
        capacitance : 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.

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

Here, we are iterating through a set of predefined charge times (50000, 5000, 500, 50, 5), observing voltage outputs and then binary searching between the appropriate ones for finding a suitable charge time to yield an appropriate voltage and capacitance, given a suitable current range, which we select using an outer loop. In Android, we start with an initial value of charge time, observe the voltage output, fix the current range and then calculate a modified value of charge time directly using certain formulae (rather than searching for it), which yields an appropriate voltage output and hence, an appropriate capacitance value. My observation is that this saves us a couple of iterations (which, at least in Android is very important, as without this the UI would become irresponsive). I understand that the Android approach may have a bit high error rate than this one, however everything is upon us to discuss whether we want that one in Python as well or not. @bessman Let me know your views on this (if you test and find that the current implementation in Python is THAT unstable) and if you require any further clarification about the approach I am suggesting. If you feel this is nice, I'd be more that happy to try out my hands on Python and make a PR for you to review:)).

bessman commented 1 week ago

Many times it seems as if the code goes in a loop and the status LED just keeps blinking without yielding any output.

I haven't been able to reproduce this, in my tests I always get output within a second. But for larger caps, the curve fitting approach often fails to find a solution and returns nan.

Let me know your views on this

I think the capacitance measurement implementation in pslab-python is too complex and tries to do too much, without yielding satisfactory results. I also think there is considerable value in keeping the pslab-android and pslab-python implementations as similar as possible.

I'd be more that happy to try out my hands on Python and make a PR for you to review

Please do!