Skip to content

Commit

Permalink
Feature: BMS initiated emergency charging
Browse files Browse the repository at this point in the history
This change logically connects the AC-Charger with the BMS to add BMS
initiated emergency charging and respecting BMS current limits.
  • Loading branch information
MalteSchm authored and schlimmchen committed Apr 2, 2024
1 parent da96273 commit 8abf614
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 17 deletions.
10 changes: 8 additions & 2 deletions include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Arduino.h"
#include "JkBmsDataPoints.h"
#include "VeDirectShuntController.h"
#include <cfloat>

// mandatory interface for all kinds of batteries
class BatteryStats {
Expand Down Expand Up @@ -37,7 +38,10 @@ class BatteryStats {

// returns true if the battery reached a critically low voltage/SoC,
// such that it is in need of charging to prevent degredation.
virtual bool needsCharging() const { return false; }
virtual bool getImmediateChargingRequest() const { return false; };

virtual float getChargeCurrent() const { return 0; };
virtual float getChargeCurrentLimitation() const { return FLT_MAX; };

protected:
virtual void mqttPublish() const;
Expand Down Expand Up @@ -71,7 +75,9 @@ class PylontechBatteryStats : public BatteryStats {
public:
void getLiveViewData(JsonVariant& root) const final;
void mqttPublish() const final;
bool needsCharging() const final { return _chargeImmediately; }
bool getImmediateChargingRequest() const { return _chargeImmediately; } ;
float getChargeCurrent() const { return _current; } ;
float getChargeCurrentLimitation() const { return _chargeCurrentLimitation; } ;

private:
void setManufacturer(String&& m) { _manufacturer = std::move(m); }
Expand Down
1 change: 1 addition & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ struct CONFIG_T {
bool Enabled;
uint32_t CAN_Controller_Frequency;
bool Auto_Power_Enabled;
bool Emergency_Charge_Enabled;
float Auto_Power_Voltage_Limit;
float Auto_Power_Enable_Voltage_Limit;
float Auto_Power_Lower_Power_Limit;
Expand Down
1 change: 1 addition & 0 deletions include/Huawei_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class HuaweiCanClass {

uint8_t _autoPowerEnabledCounter = 0;
bool _autoPowerEnabled = false;
bool _batteryEmergencyCharging = false;
};

extern HuaweiCanClass HuaweiCan;
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ bool ConfigurationClass::write()
huawei["enabled"] = config.Huawei.Enabled;
huawei["can_controller_frequency"] = config.Huawei.CAN_Controller_Frequency;
huawei["auto_power_enabled"] = config.Huawei.Auto_Power_Enabled;
huawei["emergency_charge_enabled"] = config.Huawei.Emergency_Charge_Enabled;
huawei["voltage_limit"] = config.Huawei.Auto_Power_Voltage_Limit;
huawei["enable_voltage_limit"] = config.Huawei.Auto_Power_Enable_Voltage_Limit;
huawei["lower_power_limit"] = config.Huawei.Auto_Power_Lower_Power_Limit;
Expand Down Expand Up @@ -464,6 +465,7 @@ bool ConfigurationClass::read()
config.Huawei.Enabled = huawei["enabled"] | HUAWEI_ENABLED;
config.Huawei.CAN_Controller_Frequency = huawei["can_controller_frequency"] | HUAWEI_CAN_CONTROLLER_FREQUENCY;
config.Huawei.Auto_Power_Enabled = huawei["auto_power_enabled"] | false;
config.Huawei.Emergency_Charge_Enabled = huawei["emergency_charge_enabled"] | false;
config.Huawei.Auto_Power_Voltage_Limit = huawei["voltage_limit"] | HUAWEI_AUTO_POWER_VOLTAGE_LIMIT;
config.Huawei.Auto_Power_Enable_Voltage_Limit = huawei["enable_voltage_limit"] | HUAWEI_AUTO_POWER_ENABLE_VOLTAGE_LIMIT;
config.Huawei.Auto_Power_Lower_Power_Limit = huawei["lower_power_limit"] | HUAWEI_AUTO_POWER_LOWER_POWER_LIMIT;
Expand Down
52 changes: 44 additions & 8 deletions src/Huawei_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*
* Copyright (C) 2023 Malte Schmidt and others
*/
#include "Battery.h"
#include "Huawei_can.h"
#include "MessageOutput.h"
#include "PowerMeter.h"
Expand All @@ -13,6 +14,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <algorithm>
#include <math.h>

HuaweiCanClass HuaweiCan;
Expand Down Expand Up @@ -298,18 +300,45 @@ void HuaweiCanClass::loop()
digitalWrite(_huaweiPower, 1);
}

// ***********************
// Automatic power control
// ***********************

if (_mode == HUAWEI_MODE_AUTO_INT ) {
if (_mode == HUAWEI_MODE_AUTO_INT || _batteryEmergencyCharging) {

// Set voltage limit in periodic intervals
// Set voltage limit in periodic intervals if we're in auto mode or if emergency battery charge is requested.
if ( _nextAutoModePeriodicIntMillis < millis()) {
MessageOutput.printf("[HuaweiCanClass::loop] Periodically setting voltage limit: %f \r\n", config.Huawei.Auto_Power_Voltage_Limit);
_setValue(config.Huawei.Auto_Power_Voltage_Limit, HUAWEI_ONLINE_VOLTAGE);
_nextAutoModePeriodicIntMillis = millis() + 60000;
}
}
// ***********************
// Emergency charge
// ***********************
auto stats = Battery.getStats();
if (config.Huawei.Emergency_Charge_Enabled && stats->getImmediateChargingRequest()) {
_batteryEmergencyCharging = true;

// Set output current
float efficiency = (_rp.efficiency > 0.5 ? _rp.efficiency : 1.0);
float outputCurrent = efficiency * (config.Huawei.Auto_Power_Upper_Power_Limit / _rp.output_voltage);
MessageOutput.printf("[HuaweiCanClass::loop] Emergency Charge Output current %f \r\n", outputCurrent);
_setValue(outputCurrent, HUAWEI_ONLINE_CURRENT);
return;
}

if (_batteryEmergencyCharging && !stats->getImmediateChargingRequest()) {
// Battery request has changed. Set current to 0, wait for PSU to respond and then clear state
_setValue(0, HUAWEI_ONLINE_CURRENT);
if (_rp.output_current < 1) {
_batteryEmergencyCharging = false;
}
return;
}

// ***********************
// Automatic power control
// ***********************

if (_mode == HUAWEI_MODE_AUTO_INT ) {

// Check if we should run automatic power calculation at all.
// We may have set a value recently and still wait for output stabilization
Expand Down Expand Up @@ -377,10 +406,16 @@ void HuaweiCanClass::loop()
newPowerLimit = config.Huawei.Auto_Power_Upper_Power_Limit;
}

// Set the actual output limit
// Calculate output current
float efficiency = (_rp.efficiency > 0.5 ? _rp.efficiency : 1.0);
float outputCurrent = efficiency * (newPowerLimit / _rp.output_voltage);
MessageOutput.printf("[HuaweiCanClass::loop] Output current %f \r\n", outputCurrent);
float calculatedCurrent = efficiency * (newPowerLimit / _rp.output_voltage);

// Limit output current to value requested by BMS
float permissableCurrent = stats->getChargeCurrentLimitation() - (stats->getChargeCurrent() - _rp.output_current); // BMS current limit - current from other sources
float outputCurrent = std::min(calculatedCurrent, permissableCurrent);
outputCurrent= outputCurrent > 0 ? outputCurrent : 0;

MessageOutput.printf("[HuaweiCanClass::loop] Setting output current to %.2fA. This is the lower value of calculated %.2fA and BMS permissable %.2fA currents\r\n", outputCurrent, calculatedCurrent, permissableCurrent);
_autoPowerEnabled = true;
_setValue(outputCurrent, HUAWEI_ONLINE_CURRENT);

Expand Down Expand Up @@ -415,6 +450,7 @@ void HuaweiCanClass::_setValue(float in, uint8_t parameterType)

if (in < 0) {
MessageOutput.printf("[HuaweiCanClass::_setValue] Error: Tried to set voltage/current to negative value %f \r\n", in);
return;
}

// Start PSU if needed
Expand Down
4 changes: 3 additions & 1 deletion src/WebApi_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ void WebApiHuaweiClass::onAdminGet(AsyncWebServerRequest* request)
root["enabled"] = config.Huawei.Enabled;
root["can_controller_frequency"] = config.Huawei.CAN_Controller_Frequency;
root["auto_power_enabled"] = config.Huawei.Auto_Power_Enabled;
root["emergency_charge_enabled"] = config.Huawei.Emergency_Charge_Enabled;
root["voltage_limit"] = static_cast<int>(config.Huawei.Auto_Power_Voltage_Limit * 100) / 100.0;
root["enable_voltage_limit"] = static_cast<int>(config.Huawei.Auto_Power_Enable_Voltage_Limit * 100) / 100.0;
root["lower_power_limit"] = config.Huawei.Auto_Power_Lower_Power_Limit;
Expand Down Expand Up @@ -239,6 +240,7 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
if (!(root.containsKey("enabled")) ||
!(root.containsKey("can_controller_frequency")) ||
!(root.containsKey("auto_power_enabled")) ||
!(root.containsKey("emergency_charge_enabled")) ||
!(root.containsKey("voltage_limit")) ||
!(root.containsKey("lower_power_limit")) ||
!(root.containsKey("upper_power_limit"))) {
Expand All @@ -253,11 +255,11 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
config.Huawei.Enabled = root["enabled"].as<bool>();
config.Huawei.CAN_Controller_Frequency = root["can_controller_frequency"].as<uint32_t>();
config.Huawei.Auto_Power_Enabled = root["auto_power_enabled"].as<bool>();
config.Huawei.Emergency_Charge_Enabled = root["emergency_charge_enabled"].as<bool>();
config.Huawei.Auto_Power_Voltage_Limit = root["voltage_limit"].as<float>();
config.Huawei.Auto_Power_Enable_Voltage_Limit = root["enable_voltage_limit"].as<float>();
config.Huawei.Auto_Power_Lower_Power_Limit = root["lower_power_limit"].as<float>();
config.Huawei.Auto_Power_Upper_Power_Limit = root["upper_power_limit"].as<float>();

WebApi.writeConfig(retMsg);

response->setLength();
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,13 @@
"Limits": "Limits",
"VoltageLimit": "Ladespannungslimit",
"enableVoltageLimit": "Start Spannungslimit",
"stopVoltageLimitHint": "Maximal Spannung des Ladegeräts. Entspricht der geünschten Ladeschlussspannung der Batterie. Verwendet für die Automatische Leistungssteuerung und beim Notfallladen",
"enableVoltageLimitHint": "Die automatische Leistungssteuerung wird deaktiviert wenn die Ausgangsspannung über diesem Wert liegt und wenn gleichzeitig die Ausgangsleistung unter die minimale Leistung fällt.\nDie automatische Leistungssteuerung wird re-aktiveiert wenn die Batteriespannung unter diesen Wert fällt.",
"maxPowerLimitHint": "Maximale Ausgangsleistung. Verwendet für die Automatische Leistungssteuerung und beim Notfallladen",
"lowerPowerLimit": "Minimale Leistung",
"upperPowerLimit": "Maximale Leistung",
"Seconds": "@:base.Seconds"
"Seconds": "@:base.Seconds",
"EnableEmergencyCharge": "Notfallladen: Batterie wird mit maximaler Leistung geladen wenn durch das Batterie BMS angefordert"
},
"battery": {
"battery": "Batterie",
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,13 @@
"Limits": "Limits",
"VoltageLimit": "Charge Voltage limit",
"enableVoltageLimit": "Re-enable voltage limit",
"stopVoltageLimitHint": "Maximum charger voltage. Equals battery charge voltage limit. Used for automatic power control and when emergency charging",
"enableVoltageLimitHint": "Automatic power control is disabled if the output voltage is higher then this value and if the output power drops below the minimum output power limit (set below).\nAutomatic power control is re-enabled if the battery voltage drops below the value set in this field.",
"maxPowerLimitHint": "Maximum output power. Used for automatic power control and when emergency charging",
"lowerPowerLimit": "Minimum output power",
"upperPowerLimit": "Maximum output power",
"Seconds": "@:base.Seconds"
"Seconds": "@:base.Seconds",
"EnableEmergencyCharge": "Emergency charge. Battery charged with maximum power if requested by Battery BMS"
},
"battery": {
"battery": "Battery",
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,13 @@
"Limits": "Limits",
"VoltageLimit": "Charge Voltage limit",
"enableVoltageLimit": "Re-enable voltage limit",
"stopVoltageLimitHint": "Maximum charger voltage. Equals battery charge voltage limit. Used for automatic power control and when emergency charging",
"enableVoltageLimitHint": "Automatic power control is disabled if the output voltage is higher then this value and if the output power drops below the minimum output power limit (set below).\nAutomatic power control is re-enabled if the battery voltage drops below the value set in this field.",
"maxPowerLimitHint": "Maximum output power. Used for automatic power control and when emergency charging",
"lowerPowerLimit": "Minimum output power",
"upperPowerLimit": "Maximum output power",
"Seconds": "@:base.Seconds"
"Seconds": "@:base.Seconds",
"EnableEmergencyCharge": "Emergency charge. Battery charged with maximum power if requested by Battery BMS"
},
"battery": {
"battery": "Battery",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/AcChargerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface AcChargerConfig {
enable_voltage_limit: number;
lower_power_limit: number;
upper_power_limit: number;
emergency_charge_enabled: boolean;
}
15 changes: 12 additions & 3 deletions webapp/src/views/AcChargerAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@
v-model="acChargerConfigList.auto_power_enabled"
type="checkbox" wide/>

<InputElement v-show="acChargerConfigList.enabled"
:label="$t('acchargeradmin.EnableEmergencyCharge')"
v-model="acChargerConfigList.emergency_charge_enabled"
type="checkbox" wide/>

<CardElement :text="$t('acchargeradmin.Limits')" textVariant="text-bg-primary" add-space
v-show="acChargerConfigList.auto_power_enabled">
v-show="acChargerConfigList.auto_power_enabled || acChargerConfigList.emergency_charge_enabled">
<div class="row mb-3">
<label for="voltageLimit" class="col-sm-2 col-form-label">{{ $t('acchargeradmin.VoltageLimit') }}:</label>
<label for="voltageLimit" class="col-sm-2 col-form-label">{{ $t('acchargeradmin.VoltageLimit') }}:
<BIconInfoCircle v-tooltip :title="$t('acchargeradmin.stopVoltageLimitHint')" />
</label>
<div class="col-sm-10">
<div class="input-group">
<input type="number" step="0.01" class="form-control" id="voltageLimit"
Expand Down Expand Up @@ -60,7 +67,9 @@
<span class="input-group-text" id="lowerPowerLimitDescription">W</span>
</div>
</div>
<label for="upperPowerLimit" class="col-sm-2 col-form-label">{{ $t('acchargeradmin.upperPowerLimit') }}:</label>
<label for="upperPowerLimit" class="col-sm-2 col-form-label">{{ $t('acchargeradmin.upperPowerLimit') }}:
<BIconInfoCircle v-tooltip :title="$t('acchargeradmin.maxPowerLimitHint')" />
</label>
<div class="col-sm-10">
<div class="input-group">
<input type="number" class="form-control" id="upperPowerLimit"
Expand Down

0 comments on commit 8abf614

Please sign in to comment.