fossasia / pslab-python

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

Bug: Autorange not working for oscilloscope #171

Closed cynthi8 closed 3 years ago

cynthi8 commented 3 years ago

Issue:

  1. Reading from the multimeter
  2. Reading from the oscilloscope returns incorrect values.

Test Code

from pslab import ScienceLab
psl = ScienceLab()
voltage = psl.multimeter.measure_voltage("CH1")
print(voltage)
oscilloscope_capture = psl.oscilloscope.capture("CH1", 1, 1)
print(oscilloscope_capture)

Result

3.3352564102564104
[array([0.]), array([13.33296703])]

I would expect the oscilloscope to return a value around 3.3 V.

KartikeyDubeyKD commented 3 years ago

I would like to work on this issue

cynthi8 commented 3 years ago

Ok, go for it! Should be a quick fix. Checking out #159 might give you some context on how previous auto gain setting bugs were fixed.

bessman commented 3 years ago

@Kartik125 Go ahead. The problem is that the state of the analog input gain is not shared between different instances that use the analog inputs. The easiest way to fix this is for every Oscilliscope instance to always set the most recently selected gain before starting capture.

A more robust solution would be to make AnalogInput into a shared multiton, but that would require significantly more work and the extra complexity may or may not be worth it.

KartikeyDubeyKD commented 3 years ago

I'll keep in mind

On Fri, Feb 26, 2021 at 3:02 PM Alexander Bessman notifications@github.com wrote:

@Kartik125 https://github.com/Kartik125 Go ahead. The problem is that the state of the analog input gain is not shared between different instances that use the analog inputs. The easiest way to fix this is for every Oscilliscope instance to always set the most recently selected gain before starting capture.

A more robust solution would be to make AnalogInput into a shared multiton, but that would require significantly more work and the extra complexity may or may not be worth it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fossasia/pslab-python/issues/171#issuecomment-786526652, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM5NTD6SE2HY6NGEF2GK5SDTA5TC7ANCNFSM4YH4KQPQ .

KartikeyDubeyKD commented 3 years ago

can you guide me where can I find this issue I'm new at this, I will be pleased if you tell me

bessman commented 3 years ago

The issue is caused by the actual gain being different from what the oscilloscope instance thinks it is. Since there is no way to query the device to find out the gain, each oscilloscope instance must keep track of what the gain is. If there are multiple oscilloscope instances, one of them can change the gain without the other instances knowing about it. The next time such an instance captures samples, it might think that the gain is, for example, 1 when it is really 4. Then the returned values will be off by a factor of 4.

In order to solve that, each oscilloscope instance should first set the gain to whatever it thinks it is before capturing samples.

On L134 in oscilloscope.py, before the self._capture call, call self._set_gain for each of self._channels["CH1"] and self._channels["CH2"] (the other channels do not support gain) and set the gain to the value stored in channel.gain.

Note that since this will change the traffic signature of Oscilloscope.capture, it will cause several tests to fail. If you have a PSLab device, you should record new serial traffic for any failing tests and include that in the pull request. See CONTRIBUTING.md for instructions on how to do that. If you do not have a PSLab device I can help you record new traffic after you have opened a pull request.

KartikeyDubeyKD commented 3 years ago

Thanks, I'll check it out.

On Sat, 27 Feb 2021, 18:40 Alexander Bessman, notifications@github.com wrote:

The issue is caused by the actual gain being different from what the oscilloscope instance thinks it is. Since there is no way to query the device to find out the gain, each oscilloscope instance must keep track of what the gain is. If there are multiple oscilloscope instances, one of them can change the gain without the other instances knowing about it. The next time such an instance captures samples, it might think that the gain is, for example, 1 when it is really 4. Then the returned values will be off by a factor of 4.

In order to solve that, each oscilloscope instance should first set the gain to whatever it thinks it is before capturing samples.

On L134 https://github.com/fossasia/pslab-python/blob/development/pslab/instrument/oscilloscope.py#L134 in oscilloscope.py, before the self._capture call, call self._set_gain for each channel in self._channels and set the gain to the value stored in channel.gain.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fossasia/pslab-python/issues/171#issuecomment-787070650, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM5NTD7BEDP64SWSXBO24UTTBDVOBANCNFSM4YH4KQPQ .

bessman commented 3 years ago

Any progress on this, @Kartik125?

KartikeyDubeyKD commented 3 years ago

Yes, I am working on it.

On Sun, Mar 7, 2021 at 1:29 AM Alexander Bessman notifications@github.com wrote:

Any progress on this, @Kartik125 https://github.com/Kartik125?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fossasia/pslab-python/issues/171#issuecomment-792042897, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM5NTD2KIP4JLQ742TN7RP3TCKCS3ANCNFSM4YH4KQPQ .