Browse Source

better battery percentage

Thomas Buck 1 year ago
parent
commit
a34e195b0e
2 changed files with 21 additions and 2 deletions
  1. 1
    1
      src/image.c
  2. 20
    1
      src/lipo.c

+ 1
- 1
src/image.c View File

@@ -99,7 +99,7 @@ void draw_battery_indicator(void) {
99 99
     uint32_t c = RGB_565(0xFF, 0x00, 0x00);
100 100
     if (lipo_charging()) {
101 101
         //                     "Batt:   99.9%   (4.20V)"
102
-        snprintf(s, sizeof(s), "Batt: Charging! (%.2fV)", v);
102
+        snprintf(s, sizeof(s), "Batt:     Charging     ");
103 103
         c = RGB_565(0xFF, 0xFF, 0x00);
104 104
     } else {
105 105
         float percentage = lipo_percentage(v);

+ 20
- 1
src/lipo.c View File

@@ -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) {

Loading…
Cancel
Save