Skip to main content

Crate battery_estimator

Crate battery_estimator 

Source
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 thiserror for error handling
  • no_std compatible - 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:

TypeFull ChargeCutoffDescription
LiPo4.2V3.2VStandard Lithium Polymer
LiFePO43.65V3.0VLithium Iron Phosphate (long cycle life)
LiIon4.2V3.3VStandard Lithium Ion
Lipo410Full340Cutoff4.1V3.4VConservative 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

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
CurvePoint
A single point on a voltage-SOC curve
SocEstimator
SOC estimator

Enums§

BatteryChemistry
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