|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | +#include <stddef.h> |
| 5 | + |
| 6 | +// ----------------------------------------------------------------------------- |
| 7 | +// Open Circuit Voltage (OCV) map configuration |
| 8 | +// |
| 9 | +// OCV_ARRAY must expand to 11 integer millivolt values, corresponding to: |
| 10 | +// |
| 11 | +// 100%, 90%, 80%, 70%, 60%, 50%, 40%, 30%, 20%, 10%, 0% |
| 12 | +// |
| 13 | +// in *descending* voltage order. |
| 14 | +// |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | + |
| 17 | +#ifndef OCV_ARRAY |
| 18 | + #ifdef CELL_TYPE_LIFEPO4 |
| 19 | + #define OCV_ARRAY 3400, 3350, 3320, 3290, 3270, 3260, 3250, 3230, 3200, 3120, 3000 |
| 20 | + #elif defined(CELL_TYPE_LEADACID) |
| 21 | + #define OCV_ARRAY 2120, 2090, 2070, 2050, 2030, 2010, 1990, 1980, 1970, 1960, 1950 |
| 22 | + #elif defined(CELL_TYPE_ALKALINE) |
| 23 | + #define OCV_ARRAY 1580, 1400, 1350, 1300, 1280, 1250, 1230, 1190, 1150, 1100, 1000 |
| 24 | + #elif defined(CELL_TYPE_NIMH) |
| 25 | + #define OCV_ARRAY 1400, 1300, 1280, 1270, 1260, 1250, 1240, 1230, 1210, 1150, 1000 |
| 26 | + #elif defined(CELL_TYPE_LTO) |
| 27 | + #define OCV_ARRAY 2700, 2560, 2540, 2520, 2500, 2460, 2420, 2400, 2380, 2320, 1500 |
| 28 | + #else |
| 29 | + // Default Li-Ion / Li-Po |
| 30 | + #define OCV_ARRAY 4190, 4050, 3990, 3890, 3800, 3720, 3630, 3530, 3420, 3300, 3100 |
| 31 | + #endif |
| 32 | +#endif |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Convert a battery voltage (in millivolts) to approximate state-of-charge (%), |
| 37 | + * using the OCV curve defined by OCV_ARRAY. |
| 38 | + * |
| 39 | + * Assumes: |
| 40 | + * - OCV_ARRAY has 11 entries, in descending order. |
| 41 | + * - These correspond to 100, 90, 80, ..., 0 percent. |
| 42 | + * - The input batteryMilliVolts is in the same scale as the OCV values. |
| 43 | + * |
| 44 | + * Returns an integer percentage in [0, 100] or -1 on error. |
| 45 | + */ |
| 46 | +int batteryPercentFromMilliVolts(uint16_t batteryMilliVolts); |
0 commit comments