Skip to content

Commit

Permalink
Missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Jan 11, 2025
1 parent 94f36f7 commit 9776829
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions apps/predbat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,37 @@ def get_discharge_rate_curve(soc, discharge_rate_setting, soc_max, battery_rate_
max_discharge_rate = battery_rate_max_discharge * battery_discharge_power_curve.get(soc_percent, 1.0)
return max(min(discharge_rate_setting, max_discharge_rate), battery_rate_min)

def find_battery_temperature_cap(battery_temperature, battery_temperature_curve, soc_max, max_rate):
"""
Find the battery temperature cap
"""
battery_temperature_idx = min(battery_temperature, 20)
battery_temperature_idx = max(battery_temperature_idx, -20)
if battery_temperature_idx in battery_temperature_curve:
battery_temperature_adjust = battery_temperature_curve[battery_temperature_idx]
elif battery_temperature_idx > 0:
battery_temperature_adjust = battery_temperature_curve.get(20, 1.0)
else:
battery_temperature_adjust = battery_temperature_curve.get(0, 1.0)
battery_temperature_rate_cap = soc_max * battery_temperature_adjust / 60.0

return min(battery_temperature_rate_cap, max_rate)

def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, battery_charge_power_curve, set_charge_low_power, charge_low_power_margin, battery_rate_min, battery_rate_max_scaling, battery_loss, log_to):
def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, battery_charge_power_curve, set_charge_low_power, charge_low_power_margin, battery_rate_min, battery_rate_max_scaling, battery_loss, log_to, battery_temperature=20, battery_temperature_curve={}):
"""
Find the lowest charge rate that fits the charge slow
"""
margin = charge_low_power_margin
target_soc = round(target_soc, 2)

# Temperature cap
max_rate_cap = find_battery_temperature_cap(battery_temperature, battery_temperature_curve, soc_max, max_rate)

# Real achieved max rate
max_rate_real = get_charge_rate_curve(soc, max_rate, soc_max, max_rate, battery_charge_power_curve, battery_rate_min)
max_rate_real = min(max_rate_real, max_rate_cap)
max_rate_real *= battery_rate_max_scaling

if set_charge_low_power:
minutes_left = window["end"] - minutes_now - margin
abs_minutes_left = window["end"] - minutes_now
Expand All @@ -198,22 +221,22 @@ def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, ba
if abs_minutes_left < 0:
if log_to:
log_to("Low power mode: abs_minutes_left {} < 0, default to max rate".format(abs_minutes_left))
return max_rate
return max_rate, max_rate_real

# If we already have reached target go back to max
if round(soc, 2) >= target_soc:
if log_to:
log_to("Low power mode: soc {} >= target_soc {}, default to max rate".format(soc, target_soc))
return max_rate
return max_rate, max_rate_real

# Work out the charge left in kw
charge_left = round(target_soc - soc, 2)

# If we can never hit the target then go to max
if round(max_rate * abs_minutes_left, 2) <= charge_left:
if round(max_rate_real * abs_minutes_left, 2) <= charge_left:
if log_to:
log_to("Low power mode: Can't hit target: max_rate * abs_minutes_left = {} <= charge_left {}, default to max rate".format(max_rate * abs_minutes_left, charge_left))
return max_rate
log_to("Low power mode: Can't hit target: max_rate * abs_minutes_left = {} <= charge_left {}, minutes_left {} window_end {} minutes_now {} default to max rate".format(max_rate_real * abs_minutes_left, charge_left, abs_minutes_left, window["end"], minutes_now))
return max_rate, max_rate_real

# What's the lowest we could go?
min_rate = charge_left / abs_minutes_left
Expand All @@ -222,6 +245,7 @@ def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, ba
# Apply the curve at each rate to pick one that works
rate_w = max_rate * MINUTE_WATT
best_rate = max_rate
best_rate_real = max_rate_real
highest_achievable_rate = 0

while rate_w >= 400:
Expand All @@ -232,12 +256,14 @@ def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, ba
# Compute over the time period, include the completion time
for minute in range(0, minutes_left, PREDICT_STEP):
rate_scale = get_charge_rate_curve(charge_now, rate, soc_max, max_rate, battery_charge_power_curve, battery_rate_min)
rate_scale = min(rate_scale, max_rate_cap)
highest_achievable_rate = max(highest_achievable_rate, rate_scale)
rate_scale *= battery_rate_max_scaling
charge_amount = rate_scale * PREDICT_STEP * battery_loss
charge_now += charge_amount
if round(charge_now, 2) >= target_soc:
best_rate = rate
best_rate_real = rate_scale
break
# if log_to:
# log_to("Low Power mode: rate: {} minutes: {} SOC: {} Target SOC: {} Charge left: {} Charge now: {} Rate scale: {} Charge amount: {} Charge now: {} best rate: {} highest achievable_rate {}".format(
Expand All @@ -251,13 +277,14 @@ def find_charge_rate(minutes_now, soc, window, target_soc, max_rate, soc_max, ba
if log_to:
log_to("Low Power mode: best rate {} >= highest achievable rate {}, default to max rate".format(best_rate * MINUTE_WATT, highest_achievable_rate * MINUTE_WATT))
best_rate = max_rate
best_rate_real = max_rate_real

if log_to:
log_to(
"Low Power mode: minutes left: {} absolute: {} SOC: {} Target SOC: {} Charge left: {} Max rate: {} Min rate: {} Best rate: {}".format(
minutes_left, abs_minutes_left, soc, target_soc, charge_left, max_rate * MINUTE_WATT, min_rate * MINUTE_WATT, best_rate * MINUTE_WATT
"Low Power mode: minutes left: {} absolute: {} SOC: {} Target SOC: {} Charge left: {} Max rate: {} Min rate: {} Best rate: {} Best rate real: {} Battery temp {} Temp cap {}".format(
minutes_left, abs_minutes_left, soc, target_soc, charge_left, max_rate * MINUTE_WATT, min_rate * MINUTE_WATT, best_rate * MINUTE_WATT, best_rate_real * MINUTE_WATT, battery_temperature, max_rate_cap * MINUTE_WATT
)
)
return best_rate
else:
return max_rate
return best_rate, best_rate_real
else:
return max_rate, max_rate_real

0 comments on commit 9776829

Please sign in to comment.