Skip to content

Commit

Permalink
Merge pull request #898 from MG-EV-Hackers/trip-consumption
Browse files Browse the repository at this point in the history
Trip consumption
  • Loading branch information
dexterbg committed May 29, 2023
2 parents ab78fab + 66d7809 commit 646049a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 137 deletions.
28 changes: 21 additions & 7 deletions vehicle/OVMS.V3/components/vehicle_mgev/src/mg_poll_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/
static const char *TAG = "v-mgev-bms";

#include "vehicle_mgev.h"

Expand Down Expand Up @@ -153,11 +154,24 @@ void OvmsVehicleMgEv::IncomingBmsPoll(
StandardMetrics.ms_v_charge_state->SetValue("topoff");
}
}

// Save SOC for display
StandardMetrics.ms_v_bat_soc->SetValue(scaledSoc);
// Calculate Estimated Range
float batTemp = StandardMetrics.ms_v_bat_temp->AsFloat();
float effSoh = StandardMetrics.ms_v_bat_soh->AsFloat();
// Get average trip consumption weighted by current trip consumption (25%)
float kmPerKwh = (m_avg_consumption->AsFloat(0, KPkWh) * 3.0 + m_trip_consumption->AsFloat(0,KPkWh))/4;

if(kmPerKwh < 4.648) kmPerKwh = 4.648; //21.5 kWh/100km
if(kmPerKwh > 7.728) kmPerKwh = 7.728; //13 kWh/100km
if(batTemp > 20) batTemp = 20;
// Set full range accounting for battery State of Health (SoH)
StdMetrics.ms_v_bat_range_full->SetValue(WLTP_RANGE * effSoh * 0.01f, Kilometers);
// Set battery capacity reduced by SOC and SOH
float batteryCapacity = 42.5f * (scaledSoc * 0.01f) * (effSoh * 0.01f);
StandardMetrics.ms_v_bat_range_est->SetValue(batteryCapacity * (kmPerKwh * (1-((20 - batTemp) * 1.3f) * 0.01f)));
// Ideal range set to SoC percentage of WLTP Range
StandardMetrics.ms_v_bat_range_ideal->SetValue(WLTP_RANGE * (scaledSoc / 100));
StandardMetrics.ms_v_bat_range_ideal->SetValue(StdMetrics.ms_v_bat_range_full->AsFloat(0, Kilometers) * (scaledSoc * 0.01f));
}
break;
case batteryErrorPid:
Expand All @@ -178,7 +192,7 @@ void OvmsVehicleMgEv::IncomingBmsPoll(
StandardMetrics.ms_v_bat_soh->SetValue(value / 100.0);
break;
case bmsRangePid:
StandardMetrics.ms_v_bat_range_est->SetValue(value / 10.0);
//StandardMetrics.ms_v_bat_range_est->SetValue(value / 10.0);
break;
case bmsMaxCellVoltagePid:
m_bms_max_cell_voltage->SetValue(value / 1000.0);
Expand Down Expand Up @@ -248,10 +262,10 @@ void OvmsVehicleMgEv::SetBmsStatus(uint8_t status)

float OvmsVehicleMgEv::calculateSoc(uint16_t value)
{
int BMSVersion = MyConfig.GetParamValueInt("xmg", "bms.version", DEFAULT_BMS_VERSION);
float lowerlimit = BMSDoDLimits[BMSVersion].Lower*10;
float upperlimit = BMSDoDLimits[BMSVersion].Upper*10;

int BMSVersion = MyConfig.GetParamValueInt("xmg", "bmsval", DEFAULT_BMS_VERSION);
float lowerlimit = BMSDoDLimits[BMSVersion].Lower;
float upperlimit = BMSDoDLimits[BMSVersion].Upper;
ESP_LOGD(TAG, "BMS Limits: Lower = %f Upper = %f",lowerlimit,upperlimit);
// Calculate SOC from upper and lower limits
return (value - lowerlimit) * 100.0f / (upperlimit - lowerlimit);
}
36 changes: 27 additions & 9 deletions vehicle/OVMS.V3/components/vehicle_mgev/src/mg_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,28 @@ void OvmsVehicleMgEv::WebDeInit()
void OvmsVehicleMgEv::WebCfgFeatures(PageEntry_t &p, PageContext_t &c)
{
std::string error;
std::string bmstype;
int bmsval;

//When we have more versions, need to change this to int and change from checkbox to select
bool updatedbms = DEFAULT_BMS_VERSION == 1 ? true : false;
//bool updatedbms = DEFAULT_BMS_VERSION == 1 ? true : false;

if (c.method == "POST") {
updatedbms = (c.getvar("updatedbms") == "yes");
//updatedbms = (c.getvar("updatedbms") == "yes");
bmstype = c.getvar("bmstype");
bmsval = atoi(bmstype.c_str());


if (error == "") {
// store:
//"Updated" BMS is version 1 (corresponding to BMSDoDLimits array element). "Original" BMS is version 0 (corresponding to BMSDoDLimits array element)
MyConfig.SetParamValueInt("xmg", "bms.version", updatedbms ? 1 : 0);
//MyConfig.SetParamValueInt("xmg", "bms.version", updatedbms ? 1 : 0);
MyConfig.SetParamValueInt("xmg", "bmsval", bmsval);
MyConfig.SetParamValue("xmg", "bmstype", bmstype);


c.head(200);
c.alert("success", "<p class=\"lead\">MG ZS EV / MG5 feature configuration saved.</p>");
c.alert("success", "<p class=\"lead\">MG ZS EV feature configuration saved.</p>");
MyWebServer.OutputHome(p, c);
c.done();
return;
Expand All @@ -110,7 +119,7 @@ void OvmsVehicleMgEv::WebCfgFeatures(PageEntry_t &p, PageContext_t &c)
c.alert("danger", error.c_str());
} else {
// read configuration:
switch (MyConfig.GetParamValueInt("xmg", "bms.version", DEFAULT_BMS_VERSION))
/*switch (MyConfig.GetParamValueInt("xmg", "bms.version", DEFAULT_BMS_VERSION))
{
case 0:
//"Updated" BMS is version 0 (corresponding to BMSDoDLimits array element)
Expand All @@ -121,16 +130,25 @@ void OvmsVehicleMgEv::WebCfgFeatures(PageEntry_t &p, PageContext_t &c)
updatedbms = true;
break;
}
*/
bmsval = MyConfig.GetParamValueInt("xmg", "bmsval",0);
bmstype = MyConfig.GetParamValue("xmg", "bmstype", "0");

c.head(200);
}
// generate form:
c.panel_start("primary", "MG ZS EV / MG5 feature configuration");
c.panel_start("primary", "MG ZS EV feature configuration");
c.form_start(p.uri);

c.fieldset_start("General");
c.fieldset_start("BMS Firmware Status");
//When we have more versions, need to change this to select and updatedbms to int
c.input_checkbox("Updated BMS Firmware", "updatedbms", updatedbms,
"<p>Select this if you have BMS Firmware later than Jan 2021</p>");
//c.input_checkbox("Updated BMS Firmware", "updatedbms", updatedbms, "<p>Select this if you have BMS Firmware later than Jan 2021</p>");
c.input_radio_start("BMS Type", "bmstype");
c.input_radio_option("bmstype", "Original BMS Firmware", "0", bmsval == 0);
c.input_radio_option("bmstype", "Updated BMS Firmware", "1", bmsval == 1);
c.input_radio_option("bmstype", "Select this if SOC not 100% at Full Charge after BMS Update", "2", bmsval == 2);
c.input_radio_end("");

c.fieldset_end();
c.print("<hr>");
c.input_button("default", "Save");
Expand Down

0 comments on commit 646049a

Please sign in to comment.