Skip to content

Commit ed61541

Browse files
committed
Improve resiliency of SoC calculation
Without this commit, the SoC calculation task would crash and not recover if the upper and lower SoC bounds are the same. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 31605c6 commit ed61541

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/frequenz/sdk/timeseries/battery_pool/_metric_calculator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,17 @@ def calculate(
397397
#
398398
# Therefore, the variables are named with a `_x100` suffix.
399399
usable_capacity_x100 = capacity * (soc_upper_bound - soc_lower_bound)
400-
soc_scaled = (
401-
(soc - soc_lower_bound) / (soc_upper_bound - soc_lower_bound) * 100.0
402-
)
400+
if math.isclose(soc_upper_bound, soc_lower_bound):
401+
if soc < soc_lower_bound:
402+
soc_scaled = 0.0
403+
else:
404+
soc_scaled = 100.0
405+
else:
406+
soc_scaled = (
407+
(soc - soc_lower_bound)
408+
/ (soc_upper_bound - soc_lower_bound)
409+
* 100.0
410+
)
403411
# we are clamping here because the SoC might be out of bounds
404412
soc_scaled = min(max(soc_scaled, 0.0), 100.0)
405413
timestamp = max(timestamp, metrics.timestamp)

0 commit comments

Comments
 (0)