From 585b8fee593b6d4da61d111c096f4342802d4cea Mon Sep 17 00:00:00 2001 From: Christian Butterweck Date: Mon, 1 Jan 2024 13:10:38 +0100 Subject: [PATCH] Correction of target-SOC calculation --- scripts/controller.sh | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/scripts/controller.sh b/scripts/controller.sh index 6abd001..ba3945e 100644 --- a/scripts/controller.sh +++ b/scripts/controller.sh @@ -659,25 +659,28 @@ is_charging_economical() { get_target_soc() { local megajoule=$1 local result="" - - for ((i = 1; i < ${#config_matrix_target_soc_weather[@]}; i++)); do + for ((i = 0; i < ${#config_matrix_target_soc_weather[@]}; i++)); do IFS=' ' read -ra line <<< "${config_matrix_target_soc_weather[$i]}" - - if awk -v megajoule="$megajoule" -v lower="${config_matrix_target_soc_weather[i-1]%% *}" \ - -v upper="${line[0]}" 'BEGIN {exit !(megajoule >= lower && megajoule < upper)}'; then - result=$(awk -v megajoule="$megajoule" -v lower="${config_matrix_target_soc_weather[i-1]%% *}" \ - -v upper="${line[0]}" -v lower_soc="${config_matrix_target_soc_weather[i-1]##* }" -v upper_soc="${line[1]}" \ - 'BEGIN {printf "%.0f", lower_soc + (megajoule - lower) * (upper_soc - lower_soc) / (upper - lower)}') - break + if (( i < ${#config_matrix_target_soc_weather[@]} - 1 )); then + next_line="${config_matrix_target_soc_weather[$((i + 1))]}" + IFS=' ' read -ra next_line <<< "$next_line" + + if awk -v megajoule="$megajoule" -v lower="${line[0]}" -v upper="${next_line[0]}" \ + 'BEGIN {exit !(megajoule >= lower && megajoule < upper)}'; then + result=$(awk -v megajoule="$megajoule" -v lower="${line[0]}" \ + -v upper="${next_line[0]}" -v lower_soc="${line[1]}" -v upper_soc="${next_line[1]}" \ + 'BEGIN {printf "%.0f", lower_soc + (megajoule - lower) * (upper_soc - lower_soc) / (upper - lower)}') + break + fi fi - if awk -v megajoule="$megajoule" -v lower="${config_matrix_target_soc_weather[1]%% *}" \ - 'BEGIN {exit !(megajoule <= lower)}'; then - result="${config_matrix_target_soc_weather[1]##* }" + if [ $i -eq 0 ] && \ + awk -v megajoule="$megajoule" -v lower="${line[0]}" 'BEGIN {exit !(megajoule < lower)}'; then + result="${line[1]}" break fi - if (( i == ${#config_matrix_target_soc_weather[@]} - 1 )) && \ + if [ $i -eq ${#config_matrix_target_soc_weather[@]} - 1 ] && \ awk -v megajoule="$megajoule" -v upper="${line[0]}" 'BEGIN {exit !(megajoule >= upper)}'; then result="${line[1]}" break @@ -687,6 +690,7 @@ get_target_soc() { echo "${result:-"No target SoC found."}" } + # Function to manage charging manage_charging() { local action=$1