Merge branch 'main' into bugfix/foxess-can-load

This commit is contained in:
Daniel Öster 2025-03-10 21:42:02 +02:00
commit 7b7f085160
7 changed files with 102 additions and 35 deletions

View file

@ -1,11 +1,9 @@
#include "DALY-BMS.h"
#include "../include.h"
#ifdef DALY_BMS
#include <cstdint>
#include "../datalayer/datalayer.h"
#include "../devboard/utils/events.h"
#include "../include.h"
#include "RENAULT-TWIZY.h"
#include "RJXZS-BMS.h"
#include "DALY-BMS.h"
/* Do not change code below unless you are sure what you are doing */
@ -33,7 +31,7 @@ void update_values_battery() {
// limit power when SoC is low or high
uint32_t adaptive_power_limit = 999999;
if (SOC < 2000)
adaptive_power_limit = ((uint32_t)SOC * POWER_PER_PERCENT) / 100;
adaptive_power_limit = ((uint32_t)(SOC + 100) * POWER_PER_PERCENT) / 100;
else if (SOC > 8000)
adaptive_power_limit = ((10000 - (uint32_t)SOC) * POWER_PER_PERCENT) / 100;
@ -42,9 +40,15 @@ void update_values_battery() {
if (SOC < 2000 && adaptive_power_limit < datalayer.battery.status.max_discharge_power_W)
datalayer.battery.status.max_discharge_power_W = adaptive_power_limit;
// always allow to charge at least a little bit
if (datalayer.battery.status.max_charge_power_W < POWER_PER_PERCENT)
datalayer.battery.status.max_charge_power_W = POWER_PER_PERCENT;
int32_t temperature_limit = POWER_PER_DEGREE_C * (int32_t)temperature_min_dC / 10 + POWER_AT_0_DEGREE_C;
if (temperature_limit <= 0 || temperature_min_dC < BATTERY_MINTEMPERATURE ||
temperature_max_dC > BATTERY_MAXTEMPERATURE)
temperature_limit = 0;
if (temperature_limit < datalayer.battery.status.max_discharge_power_W)
datalayer.battery.status.max_discharge_power_W = temperature_limit;
if (temperature_limit < datalayer.battery.status.max_charge_power_W)
datalayer.battery.status.max_charge_power_W = temperature_limit;
memcpy(datalayer.battery.status.cell_voltages_mV, cellvoltages_mV, sizeof(cellvoltages_mV));
datalayer.battery.status.cell_min_voltage_mV = cellvoltage_min_mV;

View file

@ -3,11 +3,13 @@
/* Tweak these according to your battery build */
#define CELL_COUNT 14
#define MAX_PACK_VOLTAGE_DV 588 //588 = 58.8V
#define MIN_PACK_VOLTAGE_DV 518 //518 = 51.8V
#define MAX_CELL_VOLTAGE_MV 4250 //Battery is put into emergency stop if one cell goes over this value
#define MIN_CELL_VOLTAGE_MV 2700 //Battery is put into emergency stop if one cell goes below this value
#define POWER_PER_PERCENT 50 // below 20% and above 80% limit power to 50W * SOC (i.e. 150W at 3%, 500W at 10%, ...)
#define MAX_PACK_VOLTAGE_DV 580 //580 = 58.0V
#define MIN_PACK_VOLTAGE_DV 460 //480 = 48.0V
#define MAX_CELL_VOLTAGE_MV 4200 //Battery is put into emergency stop if one cell goes over this value
#define MIN_CELL_VOLTAGE_MV 3200 //Battery is put into emergency stop if one cell goes below this value
#define POWER_PER_PERCENT 50 // below 20% and above 80% limit power to 50W * SOC (i.e. 150W at 3%, 500W at 10%, ...)
#define POWER_PER_DEGREE_C 60 // max power added/removed per degree above/below 0°C
#define POWER_AT_0_DEGREE_C 800 // power at 0°C
/* Do not modify any rows below*/
#define BATTERY_SELECTED

View file

@ -25,16 +25,30 @@ static String object_id_prefix = "";
static String device_name = "";
static String device_id = "";
static void publish_common_info(void);
static void publish_cell_voltages(void);
static void publish_events(void);
static bool publish_common_info(void);
static bool publish_cell_voltages(void);
static bool publish_events(void);
/** Publish global values and call callbacks for specific modules */
static void publish_values(void) {
mqtt_publish((topic_name + "/status").c_str(), "online", false);
publish_events();
publish_common_info();
publish_cell_voltages();
if (mqtt_publish((topic_name + "/status").c_str(), "online", false) == false) {
return;
}
if (publish_events() == false) {
return;
}
if (publish_common_info() == false) {
return;
}
#ifdef MQTT_PUBLISH_CELL_VOLTAGES
if (publish_cell_voltages() == false) {
return;
}
#endif
}
#ifdef HA_AUTODISCOVERY
@ -169,7 +183,7 @@ void set_battery_attributes(JsonDocument& doc, const DATALAYER_BATTERY_TYPE& bat
static std::vector<EventData> order_events;
static void publish_common_info(void) {
static bool publish_common_info(void) {
static JsonDocument doc;
static String state_topic = topic_name + "/info";
#ifdef HA_AUTODISCOVERY
@ -191,6 +205,8 @@ static void publish_common_info(void) {
serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateCommonInfoAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
ha_common_info_published = true;
} else {
return false;
}
doc.clear();
}
@ -211,18 +227,20 @@ static void publish_common_info(void) {
}
#endif // DOUBLE_BATTERY
serializeJson(doc, mqtt_msg);
if (!mqtt_publish(state_topic.c_str(), mqtt_msg, false)) {
if (mqtt_publish(state_topic.c_str(), mqtt_msg, false) == false) {
#ifdef DEBUG_LOG
logging.println("Common info MQTT msg could not be sent");
#endif // DEBUG_LOG
return false;
}
doc.clear();
#ifdef HA_AUTODISCOVERY
}
#endif // HA_AUTODISCOVERY
return true;
}
static void publish_cell_voltages(void) {
static bool publish_cell_voltages(void) {
static JsonDocument doc;
static String state_topic = topic_name + "/spec_data";
#ifdef DOUBLE_BATTERY
@ -244,6 +262,7 @@ static void publish_cell_voltages(void) {
serializeJson(doc, mqtt_msg, sizeof(mqtt_msg));
if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "").c_str(), mqtt_msg, true) == false) {
failed_to_publish = true;
return false;
}
}
doc.clear(); // clear after sending autoconfig
@ -260,6 +279,7 @@ static void publish_cell_voltages(void) {
serializeJson(doc, mqtt_msg, sizeof(mqtt_msg));
if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "_2_").c_str(), mqtt_msg, true) == false) {
failed_to_publish = true;
return false;
}
}
doc.clear(); // clear after sending autoconfig
@ -286,6 +306,7 @@ static void publish_cell_voltages(void) {
#ifdef DEBUG_LOG
logging.println("Cell voltage MQTT msg could not be sent");
#endif // DEBUG_LOG
return false;
}
doc.clear();
}
@ -306,13 +327,15 @@ static void publish_cell_voltages(void) {
#ifdef DEBUG_LOG
logging.println("Cell voltage MQTT msg could not be sent");
#endif // DEBUG_LOG
return false;
}
doc.clear();
}
#endif // DOUBLE_BATTERY
return true;
}
void publish_events() {
bool publish_events() {
static JsonDocument doc;
static String state_topic = topic_name + "/events";
#ifdef HA_AUTODISCOVERY
@ -331,6 +354,8 @@ void publish_events() {
serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateEventsAutoConfigTopic("event").c_str(), mqtt_msg, true)) {
ha_events_published = true;
} else {
return false;
}
doc.clear();
@ -368,6 +393,7 @@ void publish_events() {
#ifdef DEBUG_LOG
logging.println("Common info MQTT msg could not be sent");
#endif // DEBUG_LOG
return false;
} else {
set_event_MQTTpublished(event_handle);
}
@ -378,9 +404,10 @@ void publish_events() {
#ifdef HA_AUTODISCOVERY
}
#endif // HA_AUTODISCOVERY
return true;
}
static void publish_buttons_discovery(void) {
static bool publish_buttons_discovery(void) {
#ifdef HA_AUTODISCOVERY
if (ha_buttons_published == false) {
#ifdef DEBUG_LOG
@ -397,11 +424,14 @@ static void publish_buttons_discovery(void) {
serializeJson(doc, mqtt_msg);
if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
ha_buttons_published = true;
} else {
return false;
}
doc.clear();
}
}
#endif // HA_AUTODISCOVERY
return true;
}
static void subscribe() {
@ -511,6 +541,7 @@ void init_mqtt(void) {
mqtt_cfg.session.last_will.retain = true;
mqtt_cfg.session.last_will.msg = "offline";
mqtt_cfg.session.last_will.msg_len = strlen(mqtt_cfg.session.last_will.msg);
mqtt_cfg.network.timeout_ms = MQTT_TIMEOUT;
client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client);
}

View file

@ -25,8 +25,13 @@ String settings_processor(const String& var) {
"<h4 style='color: white;'>Password: ######## <span id='Password'></span> <button "
"onclick='editPassword()'>Edit</button></h4>";
#ifndef RS485_BATTERY_SELECTED
content += "<h4 style='color: white;'>Battery interface: <span id='Battery'>" +
String(getCANInterfaceName(can_config.battery)) + "</span></h4>";
#endif
#ifdef RS485_BATTERY_SELECTED
content += "<h4 style='color: white;'>Battery interface: RS485<span id='Battery'></span></h4>";
#endif
#ifdef DOUBLE_BATTERY
content += "<h4 style='color: white;'>Battery #2 interface: <span id='Battery'>" +

View file

@ -25,6 +25,7 @@
*/
#define TASK_CORE_PRIO 4
#define TASK_CONNECTIVITY_PRIO 3
#define TASK_MQTT_PRIO 2
#define TASK_MODBUS_PRIO 8
#define TASK_ACAN2515_PRIORITY 10
#define TASK_ACAN2517FD_PRIORITY 10