Battery SOC Estimator
A lightweight, no_std compatible Rust library for estimating battery State of Charge (SOC) from voltage measurements with temperature and aging compensation. Designed specifically for embedded systems with minimal dependencies and no heap allocations.
Features
- ✅ Minimal dependencies - Only depends on
thiserrorfor error handling (no runtime overhead) - ✅
no_stdcompatible - Perfect for embedded systems and microcontrollers - ✅ No heap allocations - Uses only stack memory and fixed-size arrays
- ✅ Multiple battery chemistries - Built-in support for LiPo, LiFePO4, Li-Ion, and conservative curves
- ✅ Temperature compensation - Correct SOC readings based on temperature effects
- ✅ Aging compensation - Adjust for battery capacity degradation over time
- ✅ Custom voltage curves - Define your own voltage-SOC relationships
- ✅ Input validation - Safe handling of invalid inputs (NaN, Infinity, negative values)
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Quick Start
Basic Usage
use ;
Temperature Compensation
use ;
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.
Custom Voltage Curve
use ;
Supported Battery Types
| 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) |
Conservative Battery Curve
The Lipo410Full340Cutoff variant uses conservative charge/discharge thresholds:
- Lower full charge voltage (4.1V instead of 4.2V) - Reduces stress on battery
- Higher cutoff voltage (3.4V instead of 3.2V) - Prevents deep discharge
- Result: Extended battery cycle life at the cost of reduced usable capacity
Advanced Usage
Aging Compensation
use ;
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.
Combined Compensation
use ;
Memory Footprint
The library is designed for minimal memory usage:
- No heap allocations: All data is stack-allocated
- Optimized for embedded: Uses
u8andu16where possible instead of larger integers
Performance
- Fast estimation: O(log n) using binary search for curve interpolation
- Deterministic execution time: No dynamic memory allocation
- Optimized boundary checks: Cached min/max SOC values for O(1) lookups
- Const-friendly: Curve creation and validation use
const fnfor compile-time safety
API Documentation
For detailed API documentation, visit docs.rs.
Examples
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 demonstration
Run examples with:
Testing
Run the test suite:
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- Designed for embedded systems and microcontrollers
- Optimized for minimal memory footprint and fast execution
- Tested on various battery chemistries and voltage curves