Skip to main content

Crate battery_estimator

Crate battery_estimator 

Source
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 fixed crate for efficient integer-based calculations
  • No floating-point unit required - Perfect for microcontrollers without FPU
  • 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),
}

§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:

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, 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

Modules§

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
EstimatorConfig
SOC estimator configuration
SocEstimator
SOC estimator

Enums§

BatteryChemistry
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

Type Aliases§

Fixed
Fixed-point type for internal calculations
I16F16
FixedI32 with 16 integer bits and 16 fractional bits.