Expand description
§Battery SOC (State of Charge) Estimator
A lightweight, no_std compatible Rust library for estimating
battery state-of-charge (SOC) from voltage measurements. Designed specifically
for embedded systems and microcontrollers using fixed-point arithmetic
for maximum efficiency without FPU.
§Features
- Fixed-point arithmetic - Uses
fixedcrate for efficient integer-based calculations - No floating-point unit required - Perfect for microcontrollers without FPU
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),
}§Fixed-Point API
For embedded systems without FPU, use the fixed-point API for maximum efficiency:
use battery_estimator::{BatteryChemistry, SocEstimator, Fixed};
use fixed::types::I16F16;
let estimator = SocEstimator::new(BatteryChemistry::LiPo);
// Use fixed-point voltage for calculations
let voltage = Fixed::from_num(3.7);
let soc = estimator.estimate_soc_fixed(voltage).unwrap();
// Convert back to float if needed
println!("Battery SOC: {:.1}%", soc.to_num::<f32>());§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, Fixed};
// Create estimator with temperature compensation
let estimator = SocEstimator::with_temperature_compensation(
BatteryChemistry::LiPo,
Fixed::from_num(25.0), // Nominal temperature (°C)
Fixed::from_num(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 calculationsEstimatorConfig- Configuration for SOC estimator (compensation settings)BatteryChemistry- Supported battery typesCurve- Voltage-SOC curve representationCurvePoint- Individual voltage-SOC data pointFixed- Fixed-point type alias (I16F16)Error- Error types for estimation failurescompensate_temperature- Temperature compensation functioncompensate_aging- Aging compensation function
Modules§
- 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
- Estimator
Config - SOC estimator configuration
- SocEstimator
- SOC estimator
Enums§
- Battery
Chemistry - Battery chemistry types supported by the library
- Error
- Errors that can occur during battery SOC estimation
Constants§
- MAX_
CURVE_ POINTS - Maximum number of points allowed in a voltage curve
Functions§
- compensate_
aging - Applies aging compensation to SOC value (floating-point API)
- compensate_
aging_ fixed - Applies aging compensation to SOC value using fixed-point arithmetic
- compensate_
temperature - Applies temperature compensation to SOC value (floating-point API)
- compensate_
temperature_ fixed - Applies temperature compensation to SOC value using fixed-point arithmetic
- default_
temperature_ compensation - Applies default temperature compensation (floating-point API)
- default_
temperature_ compensation_ fixed - Applies default temperature compensation using fixed-point arithmetic