Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/modules/common/component_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def _calculate_powers_and_currents(currents: Optional[List[Optional[float]]],
return currents, powers, voltages


def check_currents_power_sign(currents: Optional[List[Optional[float]]], power: float) -> bool:
"""Check if the sign of the sum of currents matches the power sign or both zero."""
return any([
sum(currents) < 0 and power < 0,
sum(currents) > 0 and power > 0,
sum(currents) == 0 and power == 0
])


@auto_str
class BatState:
def __init__(
Expand All @@ -71,7 +80,7 @@ def __init__(
if _check_none(currents):
currents = [0.0]*3
else:
if not ((sum(currents) < 0 and power < 0) or (sum(currents) > 0 and power > 0)):
if not check_currents_power_sign(currents, power):
log.debug("currents sign wrong "+str(currents))
self.currents = currents

Expand Down Expand Up @@ -131,7 +140,7 @@ def __init__(
if _check_none(currents):
currents = [0.0]*3
else:
if not ((sum(currents) < 0 and power < 0) or (sum(currents) > 0 and power > 0)):
if not check_currents_power_sign(currents, power):
log.debug("currents sign wrong "+str(currents))
self.currents = currents
self.power = power
Expand Down