From 3aa3d8ead9772ecf1f87399ddbcaa8f81ac285f4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 19 Nov 2023 20:31:18 +0200 Subject: [PATCH 1/8] Add ramped charge value for Tesla --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 8 +++++--- Software/src/battery/TESLA-MODEL-3-BATTERY.h | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index e29f1c55..23760e20 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -196,9 +196,11 @@ void update_values_tesla_model_3_battery() { //This function maps all the value //The allowed charge power behaves strangely. We instead estimate this value if (SOC == 10000) { - max_target_charge_power = 0; //When battery is 100% full, set allowed charge W to 0 - } else { - max_target_charge_power = 15000; //Otherwise we can push 15kW into the pack! + max_target_charge_power = 0; // When battery is 100% full, set allowed charge W to 0 + } else if (SOC >= 9500 && SOC <= 9999) { // When battery is between 95-99.99%, ramp the value between Max<->0 + max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (SOC - 9500) / 500); + } else { // Outside the specified SOC range, set the charge power to the maximum + max_target_charge_power = MAXCHARGEPOWERALLOWED; } stat_batt_power = (volts * amps); //TODO: check if scaling is OK diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.h b/Software/src/battery/TESLA-MODEL-3-BATTERY.h index 1d9081d0..15d207c8 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.h +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.h @@ -7,7 +7,8 @@ #define ABSOLUTE_MAX_VOLTAGE \ 4030 // 403.0V,if battery voltage goes over this, charging is not possible (goes into forced discharge) -#define ABSOLUTE_MIN_VOLTAGE 2450 // 245.0V if battery voltage goes under this, discharging further is disabled +#define ABSOLUTE_MIN_VOLTAGE 2450 // 245.0V if battery voltage goes under this, discharging further is disabled +#define MAXCHARGEPOWERALLOWED 15000 // 15000W we use a define since the value supplied by Tesla is always 0 // These parameters need to be mapped for the Inverter extern uint16_t SOC; From 19e33727f54ab4fff2af782c71f22064e4b5a70e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 20 Nov 2023 18:58:13 +0200 Subject: [PATCH 2/8] Raise max voltage per cell LFP --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 23760e20..9d83bda7 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -156,7 +156,7 @@ static const char* hvilStatusState[] = {"NOT OK", #define MIN_CELL_VOLTAGE_NCA_NCM 2950 //Battery is put into emergency stop if one cell goes below this value #define MAX_CELL_DEVIATION_NCA_NCM 500 //LED turns yellow on the board if mv delta exceeds this value -#define MAX_CELL_VOLTAGE_LFP 3450 //Battery is put into emergency stop if one cell goes over this value +#define MAX_CELL_VOLTAGE_LFP 3500 //Battery is put into emergency stop if one cell goes over this value #define MIN_CELL_VOLTAGE_LFP 2800 //Battery is put into emergency stop if one cell goes over this value #define MAX_CELL_DEVIATION_LFP 150 //LED turns yellow on the board if mv delta exceeds this value From 1e91bd69aa2b98a622aefc3687c640edb481d6f3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 20 Nov 2023 19:10:31 +0200 Subject: [PATCH 3/8] Tweak ramp if-statement --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 9d83bda7..41f9cf8c 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -196,8 +196,8 @@ void update_values_tesla_model_3_battery() { //This function maps all the value //The allowed charge power behaves strangely. We instead estimate this value if (SOC == 10000) { - max_target_charge_power = 0; // When battery is 100% full, set allowed charge W to 0 - } else if (SOC >= 9500 && SOC <= 9999) { // When battery is between 95-99.99%, ramp the value between Max<->0 + max_target_charge_power = 0; // When battery is 100% full, set allowed charge W to 0 + } else if (SOC > 9500) { // When battery is between 95-99.99%, ramp the value between Max<->0 max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (SOC - 9500) / 500); } else { // Outside the specified SOC range, set the charge power to the maximum max_target_charge_power = MAXCHARGEPOWERALLOWED; From 2f6d6cfae846da65a61e3729087fcf3f6b23e041 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 20 Nov 2023 23:14:36 +0200 Subject: [PATCH 4/8] Fix division error --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 41f9cf8c..b28c8a18 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -198,7 +198,7 @@ void update_values_tesla_model_3_battery() { //This function maps all the value if (SOC == 10000) { max_target_charge_power = 0; // When battery is 100% full, set allowed charge W to 0 } else if (SOC > 9500) { // When battery is between 95-99.99%, ramp the value between Max<->0 - max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (SOC - 9500) / 500); + max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (SOC - 9500) / 500.0); } else { // Outside the specified SOC range, set the charge power to the maximum max_target_charge_power = MAXCHARGEPOWERALLOWED; } From 6801e8392985de5d4410de0bd1bebd853d8dce25 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 21 Nov 2023 21:13:30 +0200 Subject: [PATCH 5/8] Rework ramp logic --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index b28c8a18..f4d619f3 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -157,7 +157,7 @@ static const char* hvilStatusState[] = {"NOT OK", #define MAX_CELL_DEVIATION_NCA_NCM 500 //LED turns yellow on the board if mv delta exceeds this value #define MAX_CELL_VOLTAGE_LFP 3500 //Battery is put into emergency stop if one cell goes over this value -#define MIN_CELL_VOLTAGE_LFP 2800 //Battery is put into emergency stop if one cell goes over this value +#define MIN_CELL_VOLTAGE_LFP 2800 //Battery is put into emergency stop if one cell goes below this value #define MAX_CELL_DEVIATION_LFP 150 //LED turns yellow on the board if mv delta exceeds this value void update_values_tesla_model_3_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus @@ -195,12 +195,12 @@ void update_values_tesla_model_3_battery() { //This function maps all the value } //The allowed charge power behaves strangely. We instead estimate this value - if (SOC == 10000) { - max_target_charge_power = 0; // When battery is 100% full, set allowed charge W to 0 - } else if (SOC > 9500) { // When battery is between 95-99.99%, ramp the value between Max<->0 - max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (SOC - 9500) / 500.0); - } else { // Outside the specified SOC range, set the charge power to the maximum - max_target_charge_power = MAXCHARGEPOWERALLOWED; + max_target_charge_power = MAXCHARGEPOWERALLOWED; + if (soc_vi > 950) { // When battery is between 95-99.99% real SOC, ramp the value between Max<->0 + max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (soc_vi - 950) / 50.0); + } + if (SOC == 10000) { // When battery is defined as 100% full, set allowed charge W to 0 + max_target_charge_power = 0; } stat_batt_power = (volts * amps); //TODO: check if scaling is OK From bf292a14573b493882c4fbf0a8c118c12764481a Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 21 Nov 2023 21:15:57 +0200 Subject: [PATCH 6/8] Add discharge SOC% safety --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index f4d619f3..4a1eaccc 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -193,6 +193,9 @@ void update_values_tesla_model_3_battery() { //This function maps all the value } else { max_target_discharge_power = temporaryvariable; } + if (SOC < 20) { // When battery is lower than 0.20% , set allowed discharge W to 0 + max_target_discharge_power = 0; + } //The allowed charge power behaves strangely. We instead estimate this value max_target_charge_power = MAXCHARGEPOWERALLOWED; From 36deffd9e930cfd3c80a3c07d515b55f4c904e72 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Nov 2023 22:13:09 +0200 Subject: [PATCH 7/8] Rework ramp logic --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 4a1eaccc..325f5ac2 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -198,12 +198,12 @@ void update_values_tesla_model_3_battery() { //This function maps all the value } //The allowed charge power behaves strangely. We instead estimate this value - max_target_charge_power = MAXCHARGEPOWERALLOWED; - if (soc_vi > 950) { // When battery is between 95-99.99% real SOC, ramp the value between Max<->0 - max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (soc_vi - 950) / 50.0); - } - if (SOC == 10000) { // When battery is defined as 100% full, set allowed charge W to 0 + if (SOC == 10000) { // When scaled SOC is 100%, set allowed charge power to 0 max_target_charge_power = 0; + } else if (soc_vi > 950) { // When real SOC is between 95-99.99%, ramp the value between Max<->0 + max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (soc_vi - 950) / 50.0); + } else { // No limits, max charging power allowed + max_target_charge_power = MAXCHARGEPOWERALLOWED; } stat_batt_power = (volts * amps); //TODO: check if scaling is OK From e2d3c4f023e36e2f98f351f7faf16cb3d4549a7c Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Nov 2023 22:23:54 +0200 Subject: [PATCH 8/8] Rework discharge calculation --- Software/src/battery/TESLA-MODEL-3-BATTERY.cpp | 17 +++++++---------- Software/src/battery/TESLA-MODEL-3-BATTERY.h | 2 ++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 325f5ac2..28e5a72f 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -26,7 +26,6 @@ CAN_frame_t TESLA_221_2 = { .MsgID = 0x221, .data = {0x61, 0x15, 0x01, 0x00, 0x00, 0x00, 0x20, 0xBA}}; //Contactor Frame 221 - hv_up_for_drive -static uint32_t temporaryvariable = 0; static uint32_t total_discharge = 0; static uint32_t total_charge = 0; static uint16_t volts = 0; // V @@ -186,15 +185,13 @@ void update_values_tesla_model_3_battery() { //This function maps all the value remaining_capacity_Wh = (expected_energy_remaining * 100); //Scale up 60.3kWh -> 60300Wh - //Calculate the allowed discharge power, cap it if it gets too large - temporaryvariable = (max_discharge_current * volts); - if (temporaryvariable > 60000) { - max_target_discharge_power = 60000; - } else { - max_target_discharge_power = temporaryvariable; - } - if (SOC < 20) { // When battery is lower than 0.20% , set allowed discharge W to 0 + // Define the allowed discharge power + max_target_discharge_power = (max_discharge_current * volts); + // Cap the allowed discharge power if battery is empty, or discharge power is higher than the maximum discharge power allowed + if (SOC == 0) { max_target_discharge_power = 0; + } else if (max_target_discharge_power > MAXDISCHARGEPOWERALLOWED) { + max_target_discharge_power = MAXDISCHARGEPOWERALLOWED; } //The allowed charge power behaves strangely. We instead estimate this value @@ -202,7 +199,7 @@ void update_values_tesla_model_3_battery() { //This function maps all the value max_target_charge_power = 0; } else if (soc_vi > 950) { // When real SOC is between 95-99.99%, ramp the value between Max<->0 max_target_charge_power = MAXCHARGEPOWERALLOWED * (1 - (soc_vi - 950) / 50.0); - } else { // No limits, max charging power allowed + } else { // No limits, max charging power allowed max_target_charge_power = MAXCHARGEPOWERALLOWED; } diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.h b/Software/src/battery/TESLA-MODEL-3-BATTERY.h index 15d207c8..2775b56c 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.h +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.h @@ -9,6 +9,8 @@ 4030 // 403.0V,if battery voltage goes over this, charging is not possible (goes into forced discharge) #define ABSOLUTE_MIN_VOLTAGE 2450 // 245.0V if battery voltage goes under this, discharging further is disabled #define MAXCHARGEPOWERALLOWED 15000 // 15000W we use a define since the value supplied by Tesla is always 0 +#define MAXDISCHARGEPOWERALLOWED \ + 60000 // 60000W we need to cap this value to max 60kW, most inverters overflow otherwise // These parameters need to be mapped for the Inverter extern uint16_t SOC;