Add feature to changing SOC method in UI on BYD Atto3

This commit is contained in:
freddanastrom 2025-06-07 10:15:55 +02:00
parent c450de308f
commit c6363f9467
5 changed files with 44 additions and 13 deletions

View file

@ -151,7 +151,12 @@ void BydAttoBattery::
datalayer_battery->status.voltage_dV = BMS_voltage * 10; datalayer_battery->status.voltage_dV = BMS_voltage * 10;
} }
#ifdef USE_ESTIMATED_SOC if (SOC_method == MEASURED) {
// Pack is not crashed, we can use periodically transmitted SOC
datalayer_battery->status.real_soc = battery_highprecision_SOC * 10;
}
else
{
// When the battery is crashed hard, it locks itself and SOC becomes unavailable. // When the battery is crashed hard, it locks itself and SOC becomes unavailable.
// We instead estimate the SOC% based on the battery voltage. // We instead estimate the SOC% based on the battery voltage.
// This is a bad solution, you wont be able to use 100% of the battery // This is a bad solution, you wont be able to use 100% of the battery
@ -161,11 +166,7 @@ void BydAttoBattery::
if (battery_type == STANDARD_RANGE) { if (battery_type == STANDARD_RANGE) {
datalayer_battery->status.real_soc = estimateSOCstandard(datalayer_battery->status.voltage_dV); datalayer_battery->status.real_soc = estimateSOCstandard(datalayer_battery->status.voltage_dV);
} }
SOC_method = ESTIMATED; }
#else // Pack is not crashed, we can use periodically transmitted SOC
datalayer_battery->status.real_soc = battery_highprecision_SOC * 10;
SOC_method = MEASURED;
#endif
datalayer_battery->status.current_dA = -BMS_current; datalayer_battery->status.current_dA = -BMS_current;
@ -292,6 +293,12 @@ void BydAttoBattery::
stateMachineClearCrash = STARTED; stateMachineClearCrash = STARTED;
datalayer_bydatto->UserRequestCrashReset = false; datalayer_bydatto->UserRequestCrashReset = false;
} }
// Change SOC method if requested from webserver datalayer
if (datalayer_bydatto->ChangeSOCMethod) {
SOC_method = !SOC_method; // Toggle the SOC method
datalayer_bydatto->ChangeSOCMethod = false;
}
} }
} }
@ -691,6 +698,11 @@ void BydAttoBattery::setup(void) { // Performs one time setup at startup
datalayer_battery->info.max_cell_voltage_mV = MAX_CELL_VOLTAGE_MV; datalayer_battery->info.max_cell_voltage_mV = MAX_CELL_VOLTAGE_MV;
datalayer_battery->info.min_cell_voltage_mV = MIN_CELL_VOLTAGE_MV; datalayer_battery->info.min_cell_voltage_mV = MIN_CELL_VOLTAGE_MV;
datalayer_battery->info.max_cell_voltage_deviation_mV = MAX_CELL_DEVIATION_MV; datalayer_battery->info.max_cell_voltage_deviation_mV = MAX_CELL_DEVIATION_MV;
#ifdef USE_ESTIMATED_SOC // Initial setup for selected SOC method
SOC_method = ESTIMATED;
#else
SOC_method = MEASURED;
#endif
} }
#endif #endif

View file

@ -57,6 +57,13 @@ class BydAttoBattery : public CanBattery {
void reset_crash() { datalayer_bydatto->UserRequestCrashReset = true; } void reset_crash() { datalayer_bydatto->UserRequestCrashReset = true; }
#ifndef USE_ESTIMATED_SOC
// Changing SOC method in UI is only enabled if we initially use measured SOC
bool supports_change_SOC_method() { return true; }
#endif
void change_SOC_method() { datalayer_bydatto->ChangeSOCMethod = true; }
BatteryHtmlRenderer& get_status_renderer() { return renderer; } BatteryHtmlRenderer& get_status_renderer() { return renderer; }
private: private:
@ -81,6 +88,7 @@ class BydAttoBattery : public CanBattery {
unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
unsigned long previousMillis200 = 0; // will store last time a 200ms CAN Message was send unsigned long previousMillis200 = 0; // will store last time a 200ms CAN Message was send
bool SOC_method = false; bool SOC_method = false;
bool ChangeSOCMethod = false;
uint8_t counter_50ms = 0; uint8_t counter_50ms = 0;
uint8_t counter_100ms = 0; uint8_t counter_100ms = 0;
uint8_t frame6_counter = 0xB; uint8_t frame6_counter = 0xB;

View file

@ -29,6 +29,7 @@ class Battery {
virtual bool supports_set_fake_voltage() { return false; } virtual bool supports_set_fake_voltage() { return false; }
virtual bool supports_manual_balancing() { return false; } virtual bool supports_manual_balancing() { return false; }
virtual bool supports_real_BMS_status() { return false; } virtual bool supports_real_BMS_status() { return false; }
virtual bool supports_change_SOC_method() { return false; }
virtual void clear_isolation() {} virtual void clear_isolation() {}
virtual void reset_BMS() {} virtual void reset_BMS() {}
@ -40,6 +41,7 @@ class Battery {
virtual void reset_BECM() {} virtual void reset_BECM() {}
virtual void request_open_contactors() {} virtual void request_open_contactors() {}
virtual void request_close_contactors() {} virtual void request_close_contactors() {}
virtual void change_SOC_method() {}
virtual void set_fake_voltage(float v) {} virtual void set_fake_voltage(float v) {}
virtual float get_voltage() { static_cast<float>(datalayer.battery.status.voltage_dV) / 10.0; } virtual float get_voltage() { static_cast<float>(datalayer.battery.status.voltage_dV) / 10.0; }

View file

@ -171,6 +171,9 @@ typedef struct {
/** User requesting crash reset via WebUI*/ /** User requesting crash reset via WebUI*/
bool UserRequestCrashReset = false; bool UserRequestCrashReset = false;
/** bool */ /** bool */
/** User changing SOC method via WebUI*/
bool ChangeSOCMethod = false;
/** bool */
/** Which SOC method currently used. 0 = Estimated, 1 = Measured */ /** Which SOC method currently used. 0 = Estimated, 1 = Measured */
bool SOC_method = 0; bool SOC_method = 0;
/** uint16_t */ /** uint16_t */

View file

@ -56,6 +56,12 @@ std::vector<BatteryCommand> battery_commands = {
[](Battery* b) { [](Battery* b) {
b->reset_SOH(); b->reset_SOH();
}}, }},
{"changeSOC", "Change SOC method",
"change SOC method? This will toggle between ESTIMATED and MEASURED SOC methods.",
[](Battery* b) { return b && b->supports_change_SOC_method(); },
[](Battery* b) {
b->change_SOC_method();
}},
}; };
String advanced_battery_processor(const String& var) { String advanced_battery_processor(const String& var) {