Expand description
§Battery SOC (State of Charge) Estimator
A lightweight, zero-dependency, no_std compatible Rust library for estimating
battery state-of-charge (SOC) from voltage measurements. Designed specifically
for embedded systems and microcontrollers.
§Features
- Minimal dependencies - Only depends on
thiserrorfor error handling no_stdcompatible - Works in embedded environments- No heap allocations - Uses only stack memory and fixed-size arrays
- Multiple battery chemistries - Built-in support for LiPo, LiFePO4, Li-Ion
- Temperature compensation - Correct SOC readings based on temperature
- Aging compensation - Adjust for battery capacity degradation over time
- Custom voltage curves - Define your own voltage-SOC relationships
- Conservative battery curves - Extended battery life with conservative thresholds
§Quick Start
use battery_estimator::{BatteryChemistry, SocEstimator};
// Create estimator for a standard LiPo battery
let estimator = SocEstimator::new(BatteryChemistry::LiPo);
// Estimate SOC at 3.7V (nominal voltage)
match estimator.estimate_soc(3.7) {
Ok(soc) => println!("Battery SOC: {:.1}%", soc),
Err(e) => println!("Error estimating SOC: {}", e),
}§Battery Types
The library supports multiple battery chemistries with built-in voltage curves:
| Type | Full Charge | Cutoff | Description |
|---|---|---|---|
LiPo | 4.2V | 3.2V | Standard Lithium Polymer |
LiFePO4 | 3.65V | 3.0V | Lithium Iron Phosphate (long cycle life) |
LiIon | 4.2V | 3.3V | Standard Lithium Ion |
Lipo410Full340Cutoff | 4.1V | 3.4V | Conservative LiPo (extended life) |
§Temperature Compensation
use battery_estimator::{BatteryChemistry, SocEstimator};
// Create estimator with temperature compensation
let estimator = SocEstimator::with_temperature_compensation(
BatteryChemistry::LiPo,
25.0, // Nominal temperature (°C)
0.0005 // Temperature coefficient
);
// Estimate SOC with current temperature
match estimator.estimate_soc_compensated(3.7, 20.0) {
Ok(soc) => println!("Temperature-compensated SOC: {:.1}%", soc),
Err(e) => println!("Error: {}", e),
}§Custom Voltage Curves
use battery_estimator::{SocEstimator, Curve, CurvePoint};
// Define a custom voltage-SOC curve
const CUSTOM_CURVE: Curve = Curve::new(&[
CurvePoint::new(3.0, 0.0), // 3.0V = 0%
CurvePoint::new(3.5, 50.0), // 3.5V = 50%
CurvePoint::new(4.0, 100.0), // 4.0V = 100%
]);
// Create estimator with custom curve
let estimator = SocEstimator::with_custom_curve(&CUSTOM_CURVE);§Module Structure
SocEstimator- Main estimator struct for SOC calculationsBatteryChemistry- Supported battery typesCurve- Voltage-SOC curve representationCurvePoint- Individual voltage-SOC data pointError- Error types for estimation failurescompensate_temperature- Temperature compensation functioncompensate_aging- Aging compensation function
Modules§
- default_
curves - Predefined battery voltage curves
- prelude
- Prelude module for convenient imports
Structs§
- Curve
- A voltage-to-SOC curve for battery state-of-charge estimation
- Curve
Point - A single point on a voltage-SOC curve
- SocEstimator
- SOC estimator
Enums§
- Battery
Chemistry - Battery chemistry types supported by the library
- Error
- Errors that can occur during battery SOC estimation
Functions§
- compensate_
aging - Applies aging compensation to SOC value
- compensate_
temperature - Applies temperature compensation to SOC value
- default_
temperature_ compensation - Applies default temperature compensation