Real-Time MCU Implementation of the SPICE Equiv...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Real-Time MCU Implementation of the SPICE Equivalent-Circuit Battery Model for Li-ion HILS

This document presents a real-time C implementation of Bee Technologies' LI-ION_BATTERY simple SPICE equivalent-circuit model on an MCU for a per-cell isolated 4-channel Li-ion battery Hardware-in-the-Loop Simulation (HILS) system. Rather than solving simultaneous circuit equations in real time, the model is reformulated into discrete-time recurrence equations split across two execution rates: 1 kHz (bm_step()): Updates state-of-charge (SOC) integration with self-discharge, discharge-rate smoothing, short- and long-term RC polarization voltages, effective SOC (SOC_eff), and open-circuit voltage (OCV) via a 22-point lookup table with correction to output v_core.200 kHz (cc_update() & control loop): Computes Vref = v_core - Rs' * Iout every PWM period so series resistance (Rs) voltage drops respond immediately to load steps without a 1 ms lag.Key numerical design choices include:Exact ZOH discretization ($\exp(-\Delta t/\tau)$): Prevents oscillation or divergence across varying time constants, capacities, and time-scaling factors (TSCALE from 1 to 3600, plus tau_mode options).Double-precision SOC integration: Avoids rounding loss of small 1 ms increments that occurs in single-precision float at low C-rates.Clamping SOC_eff < 0: Prevents extreme divergence seen in the original SPICE equation.In verification against the original SPICE model under identical current profiles (0.2C, 0.5C, and 1C constant-current discharges, as well as a 2C pulse discharge to 0.5C charge), the firmware matched SPICE closely with a maximum voltage difference of 1.19 mV (1C discharge) and a 0.19% difference in time to 2.75 V. Remaining tasks include confirming product-embedding licensing and $\tau \propto NS/C^2$ intent with Bee Technologies, and implementing the hardware layer (hal_stm32g4.c) for prototype comparison.

Tsuyoshi Horigome

December 02, 2026

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

12 slides
  1. How the SPICE Equivalent-Circuit Model Runs on the MCU Real-time

    implementation of the Bee Technologies "LI-ION_BATTERY" simple model Li-ion Battery HILS, Per-Cell Isolated 4-Channel Output Rev.A1 03 December 2026 Tsuyoshi Horigome
  2. The SPICE circuit becomes a set of 1 ms recurrences

    solved on the MCU 1 2 3 4 5 SPICE model Split into blocks Discretise in time Implement in C Check against SPICE li-ion_battery.sub Solves the circuit as simultaneous equations SOC integration / rate smoothing / OCV / RC polarisation Recurrence every 1 ms exact with exp(−Δt/τ) bm_step() runs at 1 kHz Rs drop on the 200 kHz side Same current applied max. 1.19 mV Division of work inside the MCU 1 kHz (every SYNC) bm_step(): updates SOC, rate, polarisation and OCV, outputs v_core Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU v_core, Rs′ 200 kHz (every PWM period) Vref = v_core − Rs′·Iout → voltage loop → current loop → PWM Output terminals The DUT sees the voltage of a “battery” 2
  3. The original SPICE circuit: an OCV source in series with

    Rs and two RC pairs Rts NS·0.03/C Rtl NS·0.034/C Terminal voltage Rs E_Ens NS × OCV(SOC_eff) PLUS NS·0.045/C 32.1 mΩ Cts 1800/C F Ctl τ ≈ 27.6 s SOC_eff → OCV source MINUS τ ≈ 260 s Current I (V_batt) SOC integration Discharge-rate smoothing G_G1 = I into C_capacity (3600·C F) R_self 10 MΩ (self-discharge) → voltage V(SOC) is the SOC E_rate = I / C smoothed by R1 1 Ω and C1 3 F (τ = 3 s) → V(rate) Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 15000/C F V = OCV − I·Rs − v_ts − v_tl Effective SOC → OCV (ETABLE) SOC_eff = LIMIT(SOC) − LIMIT(0.003 + 0.02942·rate) OCV = TABLE(22 points) + 0.13695 − 0.02·exp(−550·SOC_eff) 3
  4. Each SPICE element maps to one line of C code

    SPICE element Role C code (bm_step, every 1 ms) G_G1, C_capacity 3600·C Integrate current into SOC soc −= i·Δt / (3600·C) R_self 10 MΩ Self-discharge soc −= soc·Δt / (10M·3600·C) E_rate, R1 1 Ω, C1 3 F Smooth the discharge rate (3 s) rate = a·rate + (1−a)·i/C R_RT_S ∥ C_CT_S Short-term polarisation (27.6 s) v_ts = a_ts·v_ts + (1−a_ts)·Rts′·i R_RT_L ∥ C_CT_L Long-term polarisation (260 s) v_tl = a_tl·v_tl + (1−a_tl)·Rtl′·i E_Esoc, E_Eloss_rate Effective SOC soc_eff = clamp(soc) − clamp(0.003+0.02942·rate) ETABLE (TABLE + E2) Open-circuit voltage ocv = NS · bm_ocv_cell(soc_eff) R_RS Series resistance 200 kHz side: Vref = v_core − Rs′·Iout a = exp(−Δt′/τ), Δt′ = 1 ms × TSCALE. Rts′, Rtl′ and Rs′ are scaled by NS and C. The coefficients a are computed once in bm_init() whenever the settings change. Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 4
  5. RC polarisation is discretised exactly with exp(−Δt/τ) to avoid divergence

    Adopted: exact solution (ZOH) Rejected: forward Euler v[k+1] = a·v[k] + (1 − a)·R·i[k] a = exp(−Δt/τ) (0 < a < 1) v[k+1] = v[k] + (Δt/τ)·(R·i[k] − v[k]) Diverges for Δt > 2τ, oscillates for Δt > τ For any ratio of Δt to τ, v approaches R·i monotonically Example: C = 100 Ah, TSCALE = 60 (Δt = 60 ms), short-term τ_ts = 54/C² = 5.4 ms Forward Euler 1.2 1 0.8 0.6 0.4 0.2 0 500000 0 v / (R·i) v / (R·i) Exact (adopted) -500000 -1000000 -1500000 0 1 2 3 Step k Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 4 5 6 0 1 2 3 4 5 6 Step k 5
  6. Only SOC is integrated in double: in float the 1

    ms increment is lost to rounding SOC increment per 1 ms vs. the float step (5.96×10⁻⁸ for SOC 0.5–1) 1C 27.8×10⁻⁸ 0.2C 5.56×10⁻⁸ 0.1C 2.78×10⁻⁸ 4.7 steps → rounded to 5, a few % off Rounded to 1 step → falls 7.3 % too fast Below half a step → rounds to 0, SOC stops Dashed lines = float step (one division = 5.96×10⁻⁸) Fix Load The state variable soc is double (step 1.1×10⁻¹⁶); q_scale is double too. double is software-emulated, but only a few additions per 1 ms, so the load is small Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 6
  7. OCV: 22-point table interpolation plus correction; SOC_eff < 0 is

    held at 0 ① Effective SOC 4.2 SOC_eff = clamp(SOC, 0, 1) − clamp(0.003 + 0.02942·rate, 0, 1) The higher the current, the less capacity is available 4 3.8 ② Table interpolation OCV [V] 3.6 Linear search over 22 points (too few to need a binary search) 3.4 ③ Correction and series count 3.2 + 0.13695 − 0.02·exp(−550·SOC_eff), then × NS 3 2.8 ④ Below zero 2.6 0 0.1 0.2 0.3 0.4 0.5 SOC_eff 0.6 0.7 0.8 0.9 1 The original diverges (−100 kV in SPICE). Held at SOC_eff = 0 with an empty flag Dots = the 22 table points (correction included) Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 7
  8. Every 1 ms: read the current, update the states, then

    look up the OCV 1 Read current 1 ms average from ADS131M02 (fast-ADC average if missing) 2 Integrate SOC soc −= i·q_scale + soc·self_dis (double) 3 Smooth rate rate = a_rate·rate + (1−a_rate)·i/C 4 Update polarisation v_ts and v_tl by the exact discretisation Why this order SPICE transient analysis computes the terminal voltage from the state at each time point. Updating the states first and building the output from the updated values gave the smallest difference from SPICE. Initial state Starts with zero polarisation (a rested battery). SPICE .tran normally starts already polarised by the initial current, so uic was used in the comparison to match. 5 Look up OCV 6 Output v_core SOC_eff → table + correction → × NS v_core = OCV − v_ts − v_tl → to the 200 kHz side Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 8
  9. The Rs drop runs at 200 kHz: no 1 ms

    wait on a load step Terminal voltage around a load step (illustration) Load 0 → 5 A Computing everything at 1 kHz lags up to 1 ms Adopted: Rs drop is immediate (200 kHz) Polarisation part changes a little every 1 ms Time 0 ms 1 ms 2 ms 3 ms 4 ms 1 kHz: bm_step() 200 kHz: cc_update() Voltage loop → current loop SOC, rate, polarisation, OCV → updates v_core and Rs′ Vref = v_core − Rs′·Iout (Iout from the fast ADC every period) PWM drives the output voltage to Vref Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 9
  10. TSCALE fast-forwards time; tau_mode selects how the time constants scale

    100000 TSCALE (1–3600) Model time per step Δt′ = 1 ms × TSCALE. Equivalent to scaling C and τ by 1/TSCALE on the SPICE side. Results for TSCALE = 60 and 1 were confirmed to match. 10000 Time constant τ [s] (NS = 1) 1000 100 10 1 tau_mode 0.1 original (default): τ ∝ NS/C² as in the original equation. physical: τ fixed at the values for C = 1.4 Ah, NS = 1. The coefficients exp(−Δt′/τ) are recomputed in bm_init() when settings change. 0.01 0.001 0.1 0.2 0.5 1 1.4 2 5 10 20 50 100 Capacity C [Ah] τ_ts original (54/C²) τ_tl original (510/C²) Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU τ_ts physical (27.6 s) τ_tl physical (260 s) 10
  11. Verification: SPICE and firmware differ by at most 1.19 mV

    for the same current 1.19 mV Max. voltage difference (1C constant current) 0.19 % Difference in time to 2.75 V 0.48 mV 2C pulse → 0.5C charge Test Max. voltage diff. Time to 2.75 V (SPICE / FW) 0.2C constant-current discharge 0.67 mV 297.0 / 297.3 min 0.5C constant-current discharge 0.77 mV 117.7 / 117.8 min 1C constant-current discharge 1.19 mV 57.9 / 58.0 min 2C pulse → 0.5C charge 0.48 mV — Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 11
  12. Summary: map the circuit to recurrences, move only the fast

    part to 200 kHz 1 Exact discretisation 2 Recurrence with exp(−Δt/τ). No divergence even at extreme TSCALE or capacity 3 Rs only at 200 kHz The terminal voltage moves on a load step without waiting 1 ms SOC in double Avoids the 1 ms increment being lost in the float step 4 SOC_eff < 0 is held Avoids the divergence of the original equation (intentional difference) Still to do • Confirm with Bee Technologies the permission to embed the model in a product and the intent of the τ ∝ NS/C² equation • Implement the hardware layer (hal_stm32g4.c) and compare the prototype with the SPICE results Li-ion HILS, per-cell isolated 4-ch output | SPICE model on the MCU 12

More Decks by Tsuyoshi Horigome

Other Decks in Technology