-
-
Notifications
You must be signed in to change notification settings - Fork 77
pbio/drv/battery/battery_ev3.c: Implement EV3 battery driver #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This reads the values from the ADC and scales them according to the ideal scaling factors.
Without this, the hub can falsely detect low battery when booting.
Done. Also added a workaround for detecting low battery on bootup (it's added in the ADC driver, unlike the REVISIT in the STM32 implementation). |
// Battery current is read across a 0.05 ohm equivalent shunt resistor | ||
// which is then connected to an op-amp configured with a gain of 16 | ||
// (non-inverting). This yields 1 A = 0.8 V. | ||
*value = ((uint32_t)(value_raw) * 5000 * 10) / (1023 * 8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to move scaling to mV to the ADC driver on all platforms. Can be a different PR though. I think David might be working on ADC at the moment.
// Battery voltage is read through a voltage divider consisting of two | ||
// 100 K resistors, which halves the voltage. The ADC returns 10 LSBs, | ||
// where full scale is 5 V. | ||
*value = ((uint32_t)(value_raw) * 2 * 5000) / 1023; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Linux kernel, for the battery voltage, I also took into account the VCE of Q19B that I somehow figured to be 50 mV (don't remember how I came up with that number). And I also added a bit more to account for the shunt resistor which depends on the current.
I also think we need to first slow down the SPI on the ADC a bit more to get more accurate readings. Below are some data points I took (by enabling debug prints in the DCM). As we can see, the {W,C2T,T2C}DELAYs are already helping quite a bit, but the values are still skewed a bit compared to running at a slower SCLK rate. It looks like 1MHz is the magic value to let us actually read a saturated 5V on the ADC. (Note: the values are what I set
|
This reads the values from the ADC and scales them according to the ideal scaling factors.
Voltage was tested and reads in the correct order of magnitude, slightly lower (~100 mV) when compared against a cheap DMM without traceable calibration.
Current appears reasonable (116 mA) and increases when I squeeze a running motor (up to 500 mA).
Fixes pybricks/support#2311