Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ramped charge value for Tesla #100

Merged
merged 8 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 11 additions & 6 deletions Software/src/battery/TESLA-MODEL-3-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ 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 MIN_CELL_VOLTAGE_LFP 2800 //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 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
Expand Down Expand Up @@ -193,12 +193,17 @@ 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
Copy link
Collaborator

@lenvm lenvm Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoding the value 20 (representing 0.20%), shouldn’t this be based on MINPERCENTAGE?

Copy link
Collaborator

@lenvm lenvm Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also change these lines into one if, else if statement that defines the max_target_discharge_power, rather than two separate if statements.

Additionally let's define MAXDISCHARGEPOWERALLOWED as 60000.

Suggested code:

// Define the allowed discharge power
max_target_discharge_power = (max_discharge_current * volts);
// Cap the allowed discharge power if real SOC is lower than the minimum percentage, or discharge power is higher than the maximum discharge power allowed
if (soc_vi < MINPERCENTAGE) {  // When real SOC is lower than the minimum percentage, set allowed discharge power to 0
  max_target_discharge_power = 0;
else if (max_target_discharge_power > MAXDISCHARGEPOWERALLOWED) {
  max_target_discharge_power = MAXDISCHARGEPOWERALLOWED;
}

This also removes the need for the temporaryvariable.

Additionally I would like to suggest to define, in another pull request, a convention how SOC and other percentages are defined, either 20.0% as 200 (as is the case for MINPERCENTAGE and soc_vi), or 20.00% as 2000 (as is the case for SOC), such that we don't make conversion mistakes.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenvm fixed in e2d3c4f , I decided against using MINPERCENTAGE, and going for the simpler if SOC=0 check, since that is what other battery types use! Nice bonus with the temporaryvariable removed!

max_target_discharge_power = 0;
}

//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 = 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
Copy link
Collaborator

@lenvm lenvm Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of two separate if statements, I suggest using one large if, else if, else statement:

if (SOC == 10000) {  // When 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 {
  max_target_charge_power = MAXCHARGEPOWERALLOWED;
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement, fixed in 36deffd

max_target_charge_power = 0;
}

stat_batt_power = (volts * amps); //TODO: check if scaling is OK
Expand Down
3 changes: 2 additions & 1 deletion Software/src/battery/TESLA-MODEL-3-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down