Battery State of Charge#
The Battery component estimates a rechargeable pack’s state of charge (SoC) from its voltage and, where the board measures it, its current, with no fuel gauge IC. Feed it each reading (from an ADC, an INA226 / INA219, …) and it keeps the estimate.
A pack’s voltage only tells its charge at rest: charging lifts it (the charge current through the pack’s resistance, plus a polarization that takes minutes to relax), and a charger’s constant-voltage stage holds it at the full voltage. espp::Battery therefore combines the rest-voltage curve, corrected by I x R with the pack’s resistance learned from current steps, with counting the charge in or out while current flows. The count is pulled slowly toward the voltage’s estimate, less at high current, and bounded by it. A charge ends at 100 % when the charger reports it done or the current has tapered at the full voltage.
The curves are espp::piecewise_linear points (cell volts -> SoC), so any cell can be described; the chemistry presets supply typical ones:
espp::BatteryLipo (battery_lipo.hpp): lithium polymer (pouch) cells.
espp::BatteryLion (battery_lion.hpp): lithium-ion (cylindrical NMC: 18650, 21700, camera packs such as the NP-F series).
API Reference#
Header File#
Classes#
-
class Battery : public espp::BaseComponent#
State of charge (SoC) of a rechargeable battery pack, estimated from its voltage and, where the board measures it, its current.
A pack’s voltage only tells its state of charge at rest. Charging lifts it (the charge current through the pack’s resistance, plus polarization that takes minutes to relax), and a load pulls it down. A charger in its constant-voltage stage holds it at the full voltage whatever the charge. So a voltage-only estimate reads 95-100 % for a pack that is much emptier as soon as it starts charging. This class instead combines:
**The rest voltage.** The cell’s rest (open-circuit) voltage curve at the measured voltage, corrected by the current through the pack’s resistance (I x R). The resistance is learned from the voltage step of every current step (a charger switching on or off). At rest (the charger off and no current for Config::rest_time) the estimate is this value.
**Counting.** While current flows, the charge in or out (current x time / capacity) moves the estimate. Meanwhile it is pulled slowly (Config::voltage_correction_time) toward the rest-voltage estimate, so a poor starting point heals instead of persisting. The rest-voltage estimate also bounds it: from above while charging (the voltage still reads high by the polarization), from below while discharging.
**The end of a charge.** 100 % once the charger reports it is done (Reading::charge_complete), or once the current has tapered below Config::full_current_ma at the full voltage. It holds 100 % until a rest reading falls below Config::full_hold_percent, so a full pack does not drop to its relaxed ~95 % the moment charging stops, or until the pack has given up 1 % of its capacity: a device that runs on it never rests, so counting takes over from there. Neither ends a charge while the pack discharges.
**Voltage only** (Reading::current_measured false): the rest curve, or Config::charge_curve while the charger is on.
Call update() with each reading of the battery, every second or few. The class holds no hardware. Each chemistry preset (BatteryLipo, BatteryLion) supplies its cell curve, and any Config may be customized with other curves (espp::piecewise_linear points: cell volts -> SoC in [0, 1]).
Battery Example#
// a 2S Li-ion pack (e.g. a camera NP-F550), 2000 mAh espp::BatteryLion battery( {.num_cells = 2, .capacity_mah = 2000.0f, .internal_resistance_ohm = 0.15f}); // Call update() with each reading: the pack voltage, the current into it // (positive while charging) and whether the charger is on. In a real // application these come from the board (e.g. an INA226) and update() takes // the reading's time itself; the simulation passes its own clock. Pack pack; // (the simulated pack, at 55 %) auto t = std::chrono::steady_clock::now(); const float rest_v = pack.rest_voltage(); auto state = battery.update({.voltage_v = rest_v, .charge_current_ma = 0.0f, .charger_on = false}, t); logger.info("at rest: {:.2f} V -> {:.1f} %", rest_v, state.soc_percent);
Subclassed by espp::BatteryLion, espp::BatteryLipo
Public Types
-
using Curve = std::vector<std::pair<float, float>>#
(cell voltage in volts, state of charge in [0, 1]) points, sorted by voltage; interpolated with espp::piecewise_linear (clamped at the ends)
Public Functions
-
inline explicit Battery(const Config &config)#
Construct the estimator.
- Parameters:
config – The pack and estimate configuration
-
inline State update(const Reading &reading)#
Update the estimate with a reading taken now.
- Parameters:
reading – The pack’s voltage, current and charger state
- Returns:
The estimate
-
inline State update(const Reading &reading, std::chrono::steady_clock::time_point now)#
Update the estimate with a reading taken at a given time.
- Parameters:
reading – The pack’s voltage, current and charger state
now – When the reading was taken (steady clock)
- Returns:
The estimate
-
inline void reset()#
Forget the pack: the next reading starts a new estimate (call it when the pack is removed or replaced). The learned resistance goes back to Config::internal_resistance_ohm.
-
inline float soc_from_rest_voltage(float pack_voltage_v) const#
The SoC the rest curve gives for a pack voltage.
- Parameters:
pack_voltage_v – The pack’s rest (open-circuit) voltage
- Returns:
SoC in percent, 0 - 100
-
inline const Config &config() const#
The configuration in use (full_current_ma filled in).
- Returns:
The configuration
-
inline const std::string &get_name() const#
Get the name of the component
Note
This is the tag of the logger
- Returns:
A const reference to the name of the component
-
template<typename T = void>
inline void set_log_tag(const std::string_view&)# Not available: the tag is fixed at construction (pass it to the constructor). Enable CONFIG_ESPP_LOGGER_MUTABLE_TAG (menuconfig: ESPP Logger -> Mutable tag) to change it afterwards.
-
inline espp::Logger::Verbosity get_log_level() const#
Get the log level for the logger
See also
See also
- Returns:
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)#
Set the log level for the logger
See also
See also
- Parameters:
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)#
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters:
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const#
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns:
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)#
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters:
rate_limit – The rate limit to use for the logger
-
struct Config#
Configuration of the pack and of the estimate.
Public Members
-
std::string name = {"Battery"}#
Name, for the logger’s tag.
-
size_t num_cells = {1}#
Cells in series.
-
float capacity_mah = {1000.0f}#
The pack’s capacity.
-
espp::Battery::Curve charge_curve = {}#
Optional, per cell: voltage while charging -> SoC, used only when the current is not measured
-
float full_cell_voltage = {4.2f}#
The charger’s constant-voltage level, per cell.
-
float full_current_ma = {0.0f}#
The charge is complete once the current tapers below this at the full voltage (0: capacity / 20)
-
float full_hold_percent = {95.0f}#
After a completed charge, 100 % until a rest reading falls below this (or the pack has discharged 1 % of its capacity)
-
float internal_resistance_ohm = {0.1f}#
The pack’s (cells, protection, wiring), for the I x R correction, until a current step measures it
-
float resistance_learn_rate = {0.25f}#
How far each measured step moves the resistance (0: keep internal_resistance_ohm)
-
float charge_efficiency = {0.99f}#
Fraction of the charge in that the pack keeps.
-
std::chrono::milliseconds voltage_correction_time = std::chrono::minutes(10)#
Time constant of the pull toward the rest-voltage estimate while current flows (0: none)
-
std::chrono::milliseconds rest_time = std::chrono::seconds(60)#
Charger off and no current this long: at rest
-
float rest_current_ma = {5.0f}#
Below this (either way) no current is flowing.
-
std::chrono::milliseconds max_count_gap = std::chrono::minutes(5)#
Longer gaps between readings count nothing
-
std::string name = {"Battery"}#
-
struct Reading#
One reading of the pack.
Public Members
-
float voltage_v = {0.0f}#
The pack’s voltage.
-
float charge_current_ma = {0.0f}#
Current into the pack: positive while charging, negative while discharging (if the board sees it)
-
bool charger_on = {false}#
Whether the charger is charging (or may be: enabled, not paused, and its status, if known, says charging)
-
bool charge_complete = {false}#
The charger reports the charge done (its status pin; not while the pack discharges)
-
bool current_measured = {true}#
False: charge_current_ma is not known (voltage only).
-
float voltage_v = {0.0f}#
-
struct State#
The estimate after a reading.
Public Members
-
float soc_percent = {0.0f}#
The estimate, 0 - 100.
-
float voltage_soc_percent = {0.0f}#
The rest curve alone at the I x R corrected voltage.
-
float resistance_ohm = {0.0f}#
The pack’s resistance in use (learned).
-
float counted_mah = {0.0f}#
Charge counted since the last rest reading (in: +).
-
bool charging = {false}#
Charge current is flowing (or, not measured, the charger is on)
-
bool full = {false}#
A charge completed (and holds: full_hold_percent).
-
bool at_rest = {false}#
The last reading was a rest reading.
-
float soc_percent = {0.0f}#
Header File#
Classes#
-
class BatteryLipo : public espp::Battery#
A lithium polymer (LiPo, pouch cell) pack: 3.7 V nominal, 4.2 V full per cell.
Battery with a LiPo cell’s typical rest-voltage curve. Real cells differ, and so do the same cells with age and temperature. For a better estimate, measure your cell’s curve and construct an espp::Battery with it, starting from BatteryLipo::default_config().
Battery Example#
// a 2S Li-ion pack (e.g. a camera NP-F550), 2000 mAh espp::BatteryLion battery( {.num_cells = 2, .capacity_mah = 2000.0f, .internal_resistance_ohm = 0.15f}); // Call update() with each reading: the pack voltage, the current into it // (positive while charging) and whether the charger is on. In a real // application these come from the board (e.g. an INA226) and update() takes // the reading's time itself; the simulation passes its own clock. Pack pack; // (the simulated pack, at 55 %) auto t = std::chrono::steady_clock::now(); const float rest_v = pack.rest_voltage(); auto state = battery.update({.voltage_v = rest_v, .charge_current_ma = 0.0f, .charger_on = false}, t); logger.info("at rest: {:.2f} V -> {:.1f} %", rest_v, state.soc_percent);
Public Types
-
using Curve = std::vector<std::pair<float, float>>#
(cell voltage in volts, state of charge in [0, 1]) points, sorted by voltage; interpolated with espp::piecewise_linear (clamped at the ends)
Public Functions
-
inline explicit BatteryLipo(const Config &config)#
Construct a LiPo pack estimator.
- Parameters:
config – The common settings
-
inline State update(const Reading &reading)#
Update the estimate with a reading taken now.
- Parameters:
reading – The pack’s voltage, current and charger state
- Returns:
The estimate
-
inline State update(const Reading &reading, std::chrono::steady_clock::time_point now)#
Update the estimate with a reading taken at a given time.
- Parameters:
reading – The pack’s voltage, current and charger state
now – When the reading was taken (steady clock)
- Returns:
The estimate
-
inline State state() const#
The estimate after the last update().
- Returns:
The estimate
-
inline void reset()#
Forget the pack: the next reading starts a new estimate (call it when the pack is removed or replaced). The learned resistance goes back to Config::internal_resistance_ohm.
-
inline float soc_from_rest_voltage(float pack_voltage_v) const#
The SoC the rest curve gives for a pack voltage.
- Parameters:
pack_voltage_v – The pack’s rest (open-circuit) voltage
- Returns:
SoC in percent, 0 - 100
-
inline const Config &config() const#
The configuration in use (full_current_ma filled in).
- Returns:
The configuration
-
inline const std::string &get_name() const#
Get the name of the component
Note
This is the tag of the logger
- Returns:
A const reference to the name of the component
-
template<typename T = void>
inline void set_log_tag(const std::string_view&)# Not available: the tag is fixed at construction (pass it to the constructor). Enable CONFIG_ESPP_LOGGER_MUTABLE_TAG (menuconfig: ESPP Logger -> Mutable tag) to change it afterwards.
-
inline espp::Logger::Verbosity get_log_level() const#
Get the log level for the logger
See also
See also
- Returns:
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)#
Set the log level for the logger
See also
See also
- Parameters:
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)#
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters:
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const#
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns:
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)#
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters:
rate_limit – The rate limit to use for the logger
Public Static Functions
-
static inline Battery::Config default_config(const Config &config)#
A Battery::Config for a LiPo pack, to customize further.
- Parameters:
config – The common settings
- Returns:
The full configuration, with the LiPo curve and full voltage
Public Static Attributes
-
static const Curve rest_curve = {{3.27f, 0.00f}, {3.61f, 0.05f}, {3.69f, 0.10f}, {3.71f, 0.15f}, {3.73f, 0.20f}, {3.75f, 0.25f}, {3.77f, 0.30f}, {3.79f, 0.35f}, {3.80f, 0.40f}, {3.82f, 0.45f}, {3.84f, 0.50f}, {3.85f, 0.55f}, {3.87f, 0.60f}, {3.91f, 0.65f}, {3.95f, 0.70f}, {3.98f, 0.75f}, {4.02f, 0.80f}, {4.08f, 0.85f}, {4.11f, 0.90f}, {4.15f, 0.95f}, {4.20f, 1.00f},}#
A LiPo cell’s typical rest (open-circuit) voltage -> SoC.
-
struct Config#
The common settings of a LiPo pack (the rest of Battery::Config keeps its defaults)
-
using Curve = std::vector<std::pair<float, float>>#
Header File#
Classes#
-
class BatteryLion : public espp::Battery#
A lithium-ion (Li-ion, cylindrical NMC cell: 18650, 21700, camera packs such as the NP-F series) pack: 3.6-3.7 V nominal, 4.2 V full per cell.
Battery with a typical NMC cell’s rest-voltage curve. It sits lower than a LiPo’s through the middle of the charge (3.80 V is ~50 % here, ~40 % on BatteryLipo’s). Real cells differ, and so do the same cells with age and temperature. For a better estimate, measure your cell’s curve and construct an espp::Battery with it, starting from BatteryLion::default_config().
Battery Example#
// a 2S Li-ion pack (e.g. a camera NP-F550), 2000 mAh espp::BatteryLion battery( {.num_cells = 2, .capacity_mah = 2000.0f, .internal_resistance_ohm = 0.15f}); // Call update() with each reading: the pack voltage, the current into it // (positive while charging) and whether the charger is on. In a real // application these come from the board (e.g. an INA226) and update() takes // the reading's time itself; the simulation passes its own clock. Pack pack; // (the simulated pack, at 55 %) auto t = std::chrono::steady_clock::now(); const float rest_v = pack.rest_voltage(); auto state = battery.update({.voltage_v = rest_v, .charge_current_ma = 0.0f, .charger_on = false}, t); logger.info("at rest: {:.2f} V -> {:.1f} %", rest_v, state.soc_percent);
Public Types
-
using Curve = std::vector<std::pair<float, float>>#
(cell voltage in volts, state of charge in [0, 1]) points, sorted by voltage; interpolated with espp::piecewise_linear (clamped at the ends)
Public Functions
-
inline explicit BatteryLion(const Config &config)#
Construct a Li-ion pack estimator.
- Parameters:
config – The common settings
-
inline State update(const Reading &reading)#
Update the estimate with a reading taken now.
- Parameters:
reading – The pack’s voltage, current and charger state
- Returns:
The estimate
-
inline State update(const Reading &reading, std::chrono::steady_clock::time_point now)#
Update the estimate with a reading taken at a given time.
- Parameters:
reading – The pack’s voltage, current and charger state
now – When the reading was taken (steady clock)
- Returns:
The estimate
-
inline State state() const#
The estimate after the last update().
- Returns:
The estimate
-
inline void reset()#
Forget the pack: the next reading starts a new estimate (call it when the pack is removed or replaced). The learned resistance goes back to Config::internal_resistance_ohm.
-
inline float soc_from_rest_voltage(float pack_voltage_v) const#
The SoC the rest curve gives for a pack voltage.
- Parameters:
pack_voltage_v – The pack’s rest (open-circuit) voltage
- Returns:
SoC in percent, 0 - 100
-
inline const Config &config() const#
The configuration in use (full_current_ma filled in).
- Returns:
The configuration
-
inline const std::string &get_name() const#
Get the name of the component
Note
This is the tag of the logger
- Returns:
A const reference to the name of the component
-
template<typename T = void>
inline void set_log_tag(const std::string_view&)# Not available: the tag is fixed at construction (pass it to the constructor). Enable CONFIG_ESPP_LOGGER_MUTABLE_TAG (menuconfig: ESPP Logger -> Mutable tag) to change it afterwards.
-
inline espp::Logger::Verbosity get_log_level() const#
Get the log level for the logger
See also
See also
- Returns:
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)#
Set the log level for the logger
See also
See also
- Parameters:
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)#
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters:
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const#
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns:
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)#
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters:
rate_limit – The rate limit to use for the logger
Public Static Functions
-
static inline Battery::Config default_config(const Config &config)#
A Battery::Config for a Li-ion pack, to customize further.
- Parameters:
config – The common settings
- Returns:
The full configuration, with the Li-ion curve and full voltage
Public Static Attributes
-
static const Curve rest_curve = {{3.00f, 0.00f}, {3.30f, 0.02f}, {3.45f, 0.05f}, {3.53f, 0.10f}, {3.62f, 0.20f}, {3.68f, 0.30f}, {3.74f, 0.40f}, {3.80f, 0.50f}, {3.87f, 0.60f}, {3.94f, 0.70f}, {4.02f, 0.80f}, {4.10f, 0.90f}, {4.19f, 1.00f},}#
A Li-ion (NMC) cell’s typical rest (open-circuit) voltage -> SoC.
-
struct Config#
The common settings of a Li-ion pack (the rest of Battery::Config keeps its defaults)
-
using Curve = std::vector<std::pair<float, float>>#