Browse Source

better battery percentage

Thomas Buck 7 months 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
     uint32_t c = RGB_565(0xFF, 0x00, 0x00);
99
     uint32_t c = RGB_565(0xFF, 0x00, 0x00);
100
     if (lipo_charging()) {
100
     if (lipo_charging()) {
101
         //                     "Batt:   99.9%   (4.20V)"
101
         //                     "Batt:   99.9%   (4.20V)"
102
-        snprintf(s, sizeof(s), "Batt: Charging! (%.2fV)", v);
102
+        snprintf(s, sizeof(s), "Batt:     Charging     ");
103
         c = RGB_565(0xFF, 0xFF, 0x00);
103
         c = RGB_565(0xFF, 0xFF, 0x00);
104
     } else {
104
     } else {
105
         float percentage = lipo_percentage(v);
105
         float percentage = lipo_percentage(v);

+ 20
- 1
src/lipo.c View File

18
  * See <http://www.gnu.org/licenses/>.
18
  * See <http://www.gnu.org/licenses/>.
19
  */
19
  */
20
 
20
 
21
+#define LIPO_USE_PERCENTAGE_CURVE
22
+
21
 #include <math.h>
23
 #include <math.h>
22
 
24
 
23
 #include "stdbool.h"
25
 #include "stdbool.h"
37
 // Pin used for ADC 0
39
 // Pin used for ADC 0
38
 #define PICO_FIRST_ADC_PIN 26
40
 #define PICO_FIRST_ADC_PIN 26
39
 
41
 
42
+#ifndef LIPO_USE_PERCENTAGE_CURVE
40
 static const float full_battery = 4.1f;
43
 static const float full_battery = 4.1f;
41
 static const float empty_battery = 3.2f;
44
 static const float empty_battery = 3.2f;
45
+#endif // ! LIPO_USE_PERCENTAGE_CURVE
46
+
42
 static const float low_pass_factor = 0.9f;
47
 static const float low_pass_factor = 0.9f;
43
 
48
 
44
 bool lipo_charging(void) {
49
 bool lipo_charging(void) {
110
 }
115
 }
111
 
116
 
112
 float lipo_percentage(float voltage) {
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
     if (percentage >= 99.9f) {
133
     if (percentage >= 99.9f) {
115
         percentage = 99.9f;
134
         percentage = 99.9f;
116
     } else if (percentage < 0.0f) {
135
     } else if (percentage < 0.0f) {

Loading…
Cancel
Save