| Crates.io | battery-estimator |
| lib.rs | battery-estimator |
| version | 0.1.5 |
| created_at | 2026-01-11 03:52:25.303666+00 |
| updated_at | 2026-01-23 12:24:04.437531+00 |
| description | A no_std, zero-dependency library for estimating battery SOC from voltage |
| homepage | |
| repository | https://github.com/KingingWang/battery-estimator |
| max_upload_size | |
| id | 2035163 |
| size | 120,229 |
A lightweight, no_std compatible Rust library for estimating battery State of Charge (SOC) from voltage measurements. Designed specifically for embedded systems with zero dependencies and no heap allocations.
no_std compatible - Perfect for embedded systems and microcontrollersAdd this to your Cargo.toml:
[dependencies]
battery-estimator = "0.1.5"
use battery_estimator::{BatteryChemistry, SocEstimator};
fn main() {
// Create estimator for a standard LiPo battery
let estimator = SocEstimator::new(BatteryChemistry::LiPo);
// Estimate SOC at 3.7V
match estimator.estimate_soc(3.7) {
Ok(soc) => println!("Battery SOC: {:.1}%", soc),
Err(e) => println!("Error estimating SOC: {}", e),
}
}
use battery_estimator::{BatteryChemistry, SocEstimator};
fn main() {
// Create estimator with temperature compensation enabled
let estimator = SocEstimator::with_temperature_compensation(
BatteryChemistry::LiPo,
25.0, // Nominal temperature (°C)
0.0005 // Temperature coefficient
);
// Estimate SOC at 3.7V with current temperature of 20°C
match estimator.estimate_soc_compensated(3.7, 20.0) {
Ok(soc) => println!("Temperature-compensated SOC: {:.1}%", soc),
Err(e) => println!("Error: {}", e),
}
}
Note: estimate_soc_compensated only applies compensation when the estimator is configured with compensation enabled. Use with_temperature_compensation() or with_all_compensation() to enable it.
use battery_estimator::{SocEstimator, Curve, CurvePoint};
fn main() {
// 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);
// Use the estimator
match estimator.estimate_soc(3.75) {
Ok(soc) => println!("Custom curve SOC: {:.1}%", soc),
Err(e) => println!("Error: {}", e),
}
}
| Battery Type | Full Charge | Cutoff Voltage | Description |
|---|---|---|---|
LiPo |
4.2V | 3.2V | Standard Lithium Polymer battery |
LiFePO4 |
3.65V | 3.0V | Lithium Iron Phosphate battery (longer cycle life) |
LiIon |
4.2V | 3.3V | Standard Lithium Ion battery |
Lipo410Full340Cutoff |
4.1V | 3.4V | Conservative LiPo curve (extended battery life) |
The Lipo410Full340Cutoff variant uses conservative charge/discharge thresholds:
use battery_estimator::{BatteryChemistry, SocEstimator};
fn main() {
// Create estimator with aging compensation for a 2-year-old battery
let estimator = SocEstimator::with_aging_compensation(
BatteryChemistry::LiPo,
2.0, // Battery age in years
0.02 // Aging factor (2% capacity loss per year)
);
match estimator.estimate_soc_compensated(3.7, 25.0) {
Ok(soc) => println!("Age-compensated SOC: {:.1}%", soc),
Err(e) => println!("Error: {}", e),
}
}
Note: estimate_soc_compensated only applies compensation when the estimator is configured with compensation enabled. Use with_aging_compensation() or with_all_compensation() to enable it.
use battery_estimator::{BatteryChemistry, SocEstimator};
fn main() {
// Create estimator with both temperature and aging compensation
let estimator = SocEstimator::with_all_compensation(
BatteryChemistry::LiPo,
25.0, // Nominal temperature
0.0005, // Temperature coefficient
2.0, // Battery age in years
0.02 // Aging factor
);
match estimator.estimate_soc_compensated(3.7, 20.0) {
Ok(soc) => println!("Fully compensated SOC: {:.1}%", soc),
Err(e) => println!("Error: {}", e),
}
}
The library is designed for minimal memory usage:
u8 and u16 where possible instead of larger integersFor detailed API documentation, visit docs.rs.
See the examples/ directory for complete examples:
basic.rs - Comprehensive testing of all battery typescustom_curve.rs - Using custom voltage curvesprecise_test.rs - Precise voltage testing with 0.01V resolutiontemperature_compensation_test.rs - Temperature compensation demonstrationRun examples with:
cargo run --example basic
cargo run --example custom_curve
cargo run --example precise_test
cargo run --example temperature_compensation_test
Run the test suite:
cargo test
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.