PocketSprite / 8bkc-sdk

SDK for programming apps for the PocketSprite
64 stars 20 forks source link

Improve kchal_get_bat_pct() estimation #15

Open rorosaurus opened 6 years ago

rorosaurus commented 6 years ago

Improve kchal_get_bat_pct() state of charge estimation.

Right now we assume a linear relationship between voltage and state of charge. LiPo batteries follow somewhat predictable curve, based on the rate of discharge. image We should compare the recorded rough average rate of discharge, to determine the C rating for average use, then update the kchal_get_bat_pct() to approximate this curve.

Absolutely accuracy is not the goal, just something roughly within 5% should be good enough, I think.

rorosaurus commented 6 years ago

Also would be a good idea to smooth the reading of the voltage to begin with. @Spritetm mentioned lowpass or hysteresis function might be good options. I'm wondering if something like a rolling average would be easiest?

rorosaurus commented 6 years ago

To cheaply map V to a rough battery %, I did some curve fitting trial and error and eventually came up with these two curves, using a piecewise.

image

I'm content with these, and they certainly hit the goal of being within ~5%.

if v > 3.66 y = -11940.85 + 7812.571*x - 1676.957*x^2 + 118.9048*x^3
if v <= 3.66 y = 1.621552 + 7.644802e-17*e^(+10.92907*x)

Full list of mapped values: batteryEstimator.txt

rorosaurus commented 6 years ago

As mentioned above, we also need to stabilize kchal_get_bat_mv() in some way, to get less erratic readings across small time frames.

A good example of lowpass filter which we could mimic: https://github.com/JonHub/Filters/blob/master/FilterOnePole.cpp