Battery SOC Estimator
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.
Features
- ✅ Zero dependencies - No external crates required
- ✅
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
- ✅ 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 charge/discharge thresholds
Installation
Add this to your Cargo.toml:
[]
= "0.1.3"
Quick Start
Basic Usage
use ;
Temperature Compensation
use ;
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 ;
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(n)
- Deterministic execution time: No dynamic memory allocation
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