Run It
Every instrument is mocked, so the example runs without hardware.
bash
git clone https://github.com/tofupilot/panelscd panelstofupilot run cell-balanceThe procedure
procedure.yaml
name: Cell Voltage Balanceversion: 1.0.0plugs: - key: cycler name: Battery cycler python: plugs.cycler:BatteryCyclermain: - key: cells name: Charge and discharge python: phases.cell_balance:charge_and_discharge measurements: - key: max_cell_delta name: Largest cell voltage spread unit: "mV" validators: - operator: "<=" expected_value: 100 ui: panel: panels/balance.htmlThe panel
panels/balance.html
<div class="inst" data-bind="ui.cell_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">CELL ΔV</text><text x="296" y="30" text-anchor="end" style="font: 600 8px ui-monospace, Menlo, monospace; fill: #7d8791">MEAN <tspan data-bind="ui.mean" data-format="0.000">0.000</tspan> V</text><text x="22" y="68" style="font: 600 30px ui-monospace, Menlo, monospace; fill: #f2f4f6"><tspan class="big" data-bind="ui.delta" data-format="0">0</tspan><tspan dx="6" style="font: 600 10px system-ui, sans-serif; letter-spacing: .1em; fill: #7d8791">mV</tspan></text><path d="M24 108.8H280M24 147.2H280" stroke="#22282f" stroke-dasharray="3 4"/><path d="M24 128H280" stroke="#3a424b"/><text x="286" y="111.3" text-anchor="start" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">+25</text><text x="286" y="149.7" text-anchor="start" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">−25</text><g data-bind="ui.d1"><rect class="bar" x="27.5" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d2"><rect class="bar" x="47.2" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d3"><rect class="bar" x="66.9" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d4"><rect class="bar" x="86.6" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d5"><rect class="bar" x="106.3" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d6"><rect class="bar" x="126.0" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d7"><rect class="bar" x="145.7" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d8"><rect class="bar" x="165.4" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d9"><rect class="bar" x="185.1" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d10"><rect class="bar" x="204.8" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d11"><rect class="bar" x="224.5" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d12"><rect class="bar" x="244.2" y="82" width="12.6" height="46" rx="2"/></g><g data-bind="ui.d13"><rect class="bar" x="263.9" y="82" width="12.6" height="46" rx="2"/></g><g class="bleed" data-bind="ui.b1"><circle cx="33.8" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b2"><circle cx="53.5" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b3"><circle cx="73.2" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b4"><circle cx="92.9" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b5"><circle cx="112.6" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b6"><circle cx="132.3" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b7"><circle cx="152.0" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b8"><circle cx="171.7" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b9"><circle cx="191.4" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b10"><circle cx="211.1" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b11"><circle cx="230.8" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b12"><circle cx="250.5" cy="186" r="2.4"/></g><g class="bleed" data-bind="ui.b13"><circle cx="270.2" cy="186" r="2.4"/></g><text x="33.8" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">1</text><text x="53.5" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">2</text><text x="73.2" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">3</text><text x="92.9" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">4</text><text x="112.6" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">5</text><text x="132.3" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">6</text><text x="152.0" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">7</text><text x="171.7" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">8</text><text x="191.4" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">9</text><text x="211.1" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">10</text><text x="230.8" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">11</text><text x="250.5" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">12</text><text x="270.2" y="199" text-anchor="middle" style="font: 600 6.5px ui-monospace, Menlo, monospace; letter-spacing: 0; fill: #4b545d">13</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); } .bar { fill: #4aa8ff; transform-box: fill-box; transform-origin: center bottom; transform: scaleY(clamp(-1, var(--value, 0) / 60, 1)); transition: transform .12s linear, fill .2s; } .inst[data-value=warn] .bar { fill: #f2c230; } .inst[data-value=alarm] .bar { fill: #e5383b; } .inst[data-value=warn] .big { fill: #f2c230 !important; } .inst[data-value=alarm] .big { fill: #ff8a84 !important; } .bleed circle { fill: #22282f; transition: fill .2s; } .bleed[data-value=true] circle { fill: #22d3ee; }</style>The phase
phases/cell_balance.py
"""Charges and discharges the pack while the BMS reads the 13 cells. Thepanel draws `ui.d1` to `ui.d13`, each cell's deviation from the mean in mV,lights `ui.b1` to `ui.b13` while a cell is balanced, and colors the spread`ui.delta` with `ui.cell_state`: amber past 50 mV, red past 100 mV. The runrecords the largest spread."""import timedef charge_and_discharge(ui, measurements, cycler): peak = 0.0 for _ in range(cycler.length()): s = cycler.read() cells = s["cells"] mean = sum(cells) / len(cells) for k, (volts, bleeding) in enumerate(zip(cells, s["balancing"]), start=1): setattr(ui, f"d{k}", round((volts - mean) * 1000, 1)) setattr(ui, f"b{k}", bleeding) delta = (max(cells) - min(cells)) * 1000 ui.delta = round(delta) ui.mean = round(mean, 3) ui.cell_state = "alarm" if delta > 100 else "warn" if delta > 50 else "ok" peak = max(peak, delta) time.sleep(0.1) measurements.max_cell_delta = round(peak) 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()