|
@@ -18,6 +18,8 @@
|
18
|
18
|
* See <http://www.gnu.org/licenses/>.
|
19
|
19
|
*/
|
20
|
20
|
|
|
21
|
+#define LIPO_USE_PERCENTAGE_CURVE
|
|
22
|
+
|
21
|
23
|
#include <math.h>
|
22
|
24
|
|
23
|
25
|
#include "stdbool.h"
|
|
@@ -37,8 +39,11 @@
|
37
|
39
|
// Pin used for ADC 0
|
38
|
40
|
#define PICO_FIRST_ADC_PIN 26
|
39
|
41
|
|
|
42
|
+#ifndef LIPO_USE_PERCENTAGE_CURVE
|
40
|
43
|
static const float full_battery = 4.1f;
|
41
|
44
|
static const float empty_battery = 3.2f;
|
|
45
|
+#endif // ! LIPO_USE_PERCENTAGE_CURVE
|
|
46
|
+
|
42
|
47
|
static const float low_pass_factor = 0.9f;
|
43
|
48
|
|
44
|
49
|
bool lipo_charging(void) {
|
|
@@ -110,7 +115,21 @@ float lipo_voltage(void) {
|
110
|
115
|
}
|
111
|
116
|
|
112
|
117
|
float lipo_percentage(float voltage) {
|
113
|
|
- float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
|
|
118
|
+ float percentage;
|
|
119
|
+
|
|
120
|
+#ifdef LIPO_USE_PERCENTAGE_CURVE
|
|
121
|
+ /*
|
|
122
|
+ * Try to linearize the LiPo discharge curve.
|
|
123
|
+ * https://electronics.stackexchange.com/a/551667
|
|
124
|
+ *
|
|
125
|
+ * Seems to work relatively well, although
|
|
126
|
+ * "stopping" at 3.5V feels a bit high to me.
|
|
127
|
+ */
|
|
128
|
+ percentage = 123.0f - (123.0f / powf(1.0f + powf(voltage / 3.7f, 80.0f), 0.165f));
|
|
129
|
+#else // LIPO_USE_PERCENTAGE_CURVE
|
|
130
|
+ percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
|
|
131
|
+#endif // LIPO_USE_PERCENTAGE_CURVE
|
|
132
|
+
|
114
|
133
|
if (percentage >= 99.9f) {
|
115
|
134
|
percentage = 99.9f;
|
116
|
135
|
} else if (percentage < 0.0f) {
|