State of Charge

State of charge as the cycler's software shows it: a large reading, twenty segments that go amber then red when low, and the charge moved so far.

STATIONCLI/PROCEDUREState of Charge

Charge and discharge

PASS00:22.85Charge and discharge (1/1)Passed

Run It

Every instrument is mocked, so the example runs without hardware.

bash
git clone https://github.com/tofupilot/panelscd panelstofupilot run battery-soc

The procedure

procedure.yaml
name: State of Chargeversion: 1.0.0plugs:  - key: cycler    name: Battery cycler    python: plugs.cycler:BatteryCyclermain:  - key: soc    name: Charge and discharge    python: phases.battery_soc:charge_and_discharge    measurements:      - key: soc_end_of_charge        name: SOC at the end of the charge        unit: "%"        validators:          - operator: ">="            expected_value: 95          - operator: "<="            expected_value: 100    ui:      panel: panels/soc.html

The panel

panels/soc.html
<div class="inst" data-bind="ui.soc_state"><svg viewBox="0 0 320 210" role="img"><rect x="1" y="1" width="318" height="208" rx="10" fill="#0d1014" stroke="#20262c" stroke-width="1.5"/><text x="24" y="30" text-anchor="start" style="font: 600 7.5px system-ui, sans-serif; letter-spacing: .16em; fill: #7d8791">STATE OF CHARGE</text><text x="296" y="30" text-anchor="end" data-bind="ui.step" style="font: 600 8px ui-monospace, Menlo, monospace; letter-spacing: .04em; fill: #7d8791">—</text><text x="22" y="82" style="font: 600 46px ui-monospace, Menlo, monospace; fill: #f2f4f6"><tspan class="big" data-bind="ui.soc" data-format="0">0</tspan><tspan dx="8" style="font: 600 13px system-ui, sans-serif; fill: #7d8791">%</tspan></text><g class="segs" data-bind="ui.soc"><rect x="24.0" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 0) * 1, 1)"/><rect x="37.7" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 5) * 1, 1)"/><rect x="51.4" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 10) * 1, 1)"/><rect x="65.1" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 15) * 1, 1)"/><rect x="78.8" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 20) * 1, 1)"/><rect x="92.5" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 25) * 1, 1)"/><rect x="106.2" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 30) * 1, 1)"/><rect x="119.9" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 35) * 1, 1)"/><rect x="133.6" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 40) * 1, 1)"/><rect x="147.3" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 45) * 1, 1)"/><rect x="161.0" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 50) * 1, 1)"/><rect x="174.7" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 55) * 1, 1)"/><rect x="188.4" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 60) * 1, 1)"/><rect x="202.1" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 65) * 1, 1)"/><rect x="215.8" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 70) * 1, 1)"/><rect x="229.5" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 75) * 1, 1)"/><rect x="243.2" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 80) * 1, 1)"/><rect x="256.9" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 85) * 1, 1)"/><rect x="270.6" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 90) * 1, 1)"/><rect x="284.3" y="104" width="10.5" height="34" rx="2.5" style="opacity: clamp(.13, (var(--value, 0) - 95) * 1, 1)"/></g><text x="24" y="164" text-anchor="start" style="font: 600 7.5px system-ui, sans-serif; letter-spacing: .16em; fill: #7d8791">AH CHARGED</text><text x="130" y="164" text-anchor="start" style="font: 600 7.5px system-ui, sans-serif; letter-spacing: .16em; fill: #7d8791">AH DISCHARGED</text><text x="236" y="164" text-anchor="start" style="font: 600 7.5px system-ui, sans-serif; letter-spacing: .16em; fill: #7d8791">RATED</text><text x="24" y="186" text-anchor="start" data-bind="ui.ah_in" data-format="0.0" style="font: 600 15px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #f2f4f6">0.0</text><text x="130" y="186" text-anchor="start" data-bind="ui.ah_out" data-format="0.0" style="font: 600 15px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #f2f4f6">0.0</text><text x="236" y="186" text-anchor="start" style="font: 600 15px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #7d8791">50 Ah</text></svg></div><style>  .inst { width: min(560px, 100%); margin: 0 auto; }  .inst svg { display: block; width: 100%; height: auto; }  .inst text { font-family: system-ui, sans-serif; }  .dp rect { fill: var(--off); }  .dp.on rect, .dp[data-value=true] rect { fill: var(--on); }  .led circle { fill: #2a2f35; transition: fill .15s; }  .led[data-value=true] circle { fill: var(--led, #38e07a); }  .ann text { fill: var(--off); transition: fill .15s; }  .ann[data-value=true] text { fill: var(--on); }  .d7 rect { fill: var(--off); transition: fill .08s; }  .d7[data-value="0"] :is(.sa, .sb, .sc, .sd, .se, .sf) { fill: var(--on); }  .d7[data-value="1"] :is(.sb, .sc) { fill: var(--on); }  .d7[data-value="2"] :is(.sa, .sb, .sd, .se, .sg) { fill: var(--on); }  .d7[data-value="3"] :is(.sa, .sb, .sc, .sd, .sg) { fill: var(--on); }  .d7[data-value="4"] :is(.sb, .sc, .sf, .sg) { fill: var(--on); }  .d7[data-value="5"] :is(.sa, .sc, .sd, .sf, .sg) { fill: var(--on); }  .d7[data-value="6"] :is(.sa, .sc, .sd, .se, .sf, .sg) { fill: var(--on); }  .d7[data-value="7"] :is(.sa, .sb, .sc) { fill: var(--on); }  .d7[data-value="8"] :is(.sa, .sb, .sc, .sd, .se, .sf, .sg) { fill: var(--on); }  .d7[data-value="9"] :is(.sa, .sb, .sc, .sd, .sf, .sg) { fill: var(--on); }  .d7[data-value="-"] :is(.sg) { fill: var(--on); }  .d7[data-value="E"] :is(.sa, .sd, .se, .sf, .sg) { fill: var(--on); }  .d7[data-value="U"] :is(.sb, .sc, .sd, .se, .sf) { fill: var(--on); }  .d7[data-value="r"] :is(.se, .sg) { fill: var(--on); }  .segs rect { fill: #4aa8ff; transition: opacity .2s, fill .2s; }  .inst[data-value=warn] .segs rect { fill: #f2c230; }  .inst[data-value=alarm] .segs rect { fill: #e5383b; }  .inst[data-value=warn] .big { fill: #f2c230 !important; }  .inst[data-value=alarm] .big { fill: #ff8a84 !important; }</style>

The phase

phases/battery_soc.py
"""Charges and discharges the pack. The panel shows `ui.soc` as a reading andtwenty segments, `ui.soc_state` amber under 20 % and red under 10 %, the stepand the charge moved (`ui.ah_in`, `ui.ah_out`). The run records the SOCreached by the charge."""import timeHOURS_PER_SAMPLE = 0.3 * 120 / 3600  # 0.3 s of cycle, replayed at 120×def charge_and_discharge(ui, measurements, cycler):    peak, ah_in, ah_out = 0.0, 0.0, 0.0    for _ in range(cycler.length()):        s = cycler.read()        soc, amps = s["soc"], s["current"]        ah_in += max(amps, 0) * HOURS_PER_SAMPLE        ah_out += max(-amps, 0) * HOURS_PER_SAMPLE        ui.soc = round(soc, 1)        ui.soc_state = "alarm" if soc < 10 else "warn" if soc < 20 else "ok"        ui.step = {"CC": "CC CHARGE", "CV": "CV CHARGE", "DSG": "DISCHARGE"}.get(s["step"], "REST")        ui.ah_in = round(ah_in, 1)        ui.ah_out = round(ah_out, 1)        peak = max(peak, soc)        time.sleep(0.1)    measurements.soc_end_of_charge = round(peak, 1)    time.sleep(1.5)

The mocked instrument

plugs/cycler.py
"""Mock instrument replaying a simulated 13S NMC, 50 Ah pack through a CC-CVcharge, a rest and a 75 A discharge."""import mathCELL_OFFSETS = [-0.42, 0.18, 0.05, -0.12, 0.5, -0.3, 0.1, 0.32, -0.05, -0.5, 0.24, 0.0, -0.2]SENSORS = [("CELL 1", -1.5), ("CELL 4", 0.0), ("CELL 7", 1.2), ("BUSBAR", 4.6),           ("CELL 10", -0.8), ("CELL 13", 0.4), ("INLET", -2.0), ("OUTLET", 0.9)]STEPS = [("CC", 0, 18), ("CV", 18, 28), ("REST", 28, 33), ("DSG", 33, 53), ("REST", 53, 60)]CYCLE_S, DT = 60, 0.3def _lerp(a, b, x):    return a + (b - a) * min(1.0, max(0.0, x))def _ease(x):    return 0.5 - 0.5 * math.cos(math.pi * min(1.0, max(0.0, x)))def _wob(t, k):    return math.sin(t * k) * 0.5 + math.sin(t * k * 2.3) * 0.25def pack_state(t):    """The simulated 13S NMC, 50 Ah pack at cycle time t (s): CC 50 A to    54.6 V, CV to 2.5 A, rest, 75 A discharge with a current spike, rest."""    bleed = False    if t < 18:        x = t / 18        soc, i = _lerp(20, 78, x), 50 + _wob(t, 3) * 0.4        v, temp, delta = 46.2 + 8.4 * x ** 1.6, _lerp(24, 31, x), _lerp(8, 14, x)    elif t < 28:        x = (t - 18) / 10        soc, i = _lerp(78, 99, _ease(x)), 2.5 + 47.5 * math.exp(-3.2 * x)        v, temp = 54.6, _lerp(31, 33, x)        delta = _lerp(14, 26, _ease(x * 1.4)) - _lerp(0, 8, (x - 0.6) / 0.4)        bleed = True    elif t < 33:        x = (t - 28) / 5        soc, i, v = 99, 0.0, _lerp(54.6, 54.0, _ease(x))        temp, delta, bleed = _lerp(33, 30, x), _lerp(18, 10, x), x < 0.6    elif t < 53:        x = (t - 33) / 20        spike = -17 * math.sin(math.pi * (t - 38) / 2.5) if 38 < t < 40.5 else 0.0        soc, i = _lerp(99, 22, x), -75 + spike + _wob(t, 2.7) * 0.6        v = 53.4 - 5.2 * x - 1.6 * x ** 6 + spike * 0.03        temp, delta = _lerp(30, 50, x ** 0.9), _lerp(10, 58, x ** 2.2)    else:        x = (t - 53) / 7        soc, i, v = 22, 0.0, _lerp(46.6, 47.4, _ease(x))        temp, delta = _lerp(50, 47, x), _lerp(58, 44, x)    step = next((name for name, a, b in STEPS if a <= t < b), "REST")    temps = [temp + o * (0.4 + (temp - 24) / 30) + _wob(t + k, 1.1) * 0.15 for k, (_, o) in enumerate(SENSORS)]    cells = [v / 13 + o * delta / 1000 for o in CELL_OFFSETS]    balancing = [bleed and o > 0.15 for o in CELL_OFFSETS]    acir = 9.6 * (1 - 0.006 * (temp - 25)) + 0.02 * math.sin(t * 2.3)    return {"step": step, "soc": soc, "voltage": v, "current": i, "temperatures": temps,            "cells": cells, "balancing": balancing, "acir_mohm": acir}class _Replay:    def __init__(self):        self.t = 0.0    def length(self):        return int(CYCLE_S / DT)    def _next(self):        state = pack_state(self.t)        self.t += DT        return stateclass BatteryCycler(_Replay):    """Mock battery cycler channel. Replace it with your cycler's driver."""    def read(self):        """The channel and the pack it reads: step, SOC %, V, A (charge        positive), cell voltages and balancing from the BMS."""        return self._next()

Run your first test in minutes