mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 10:49:42 +02:00
Merge branch 'main' into bugfix/foxess-can-load
This commit is contained in:
commit
7b7f085160
7 changed files with 102 additions and 35 deletions
|
@ -70,9 +70,13 @@ MyTimer loop_task_timer_10s(INTERVAL_10_S);
|
|||
|
||||
MyTimer check_pause_2s(INTERVAL_2_S);
|
||||
|
||||
int64_t mqtt_task_time_us;
|
||||
MyTimer mqtt_task_timer_10s(INTERVAL_10_S);
|
||||
|
||||
TaskHandle_t main_loop_task;
|
||||
TaskHandle_t connectivity_loop_task;
|
||||
TaskHandle_t logging_loop_task;
|
||||
TaskHandle_t mqtt_loop_task;
|
||||
|
||||
Logging logging;
|
||||
|
||||
|
@ -99,6 +103,11 @@ void setup() {
|
|||
TASK_CONNECTIVITY_PRIO, &logging_loop_task, WIFI_CORE);
|
||||
#endif
|
||||
|
||||
#ifdef MQTT
|
||||
xTaskCreatePinnedToCore((TaskFunction_t)&mqtt_loop, "mqtt_loop", 4096, &mqtt_task_time_us, TASK_MQTT_PRIO,
|
||||
&mqtt_loop_task, WIFI_CORE);
|
||||
#endif
|
||||
|
||||
init_CAN();
|
||||
|
||||
init_contactors();
|
||||
|
@ -180,9 +189,6 @@ void connectivity_loop(void* task_time_us) {
|
|||
#ifdef MDNSRESPONDER
|
||||
init_mDNS();
|
||||
#endif
|
||||
#ifdef MQTT
|
||||
init_mqtt();
|
||||
#endif
|
||||
|
||||
while (true) {
|
||||
START_TIME_MEASUREMENT(wifi);
|
||||
|
@ -191,15 +197,9 @@ void connectivity_loop(void* task_time_us) {
|
|||
ota_monitor();
|
||||
#endif
|
||||
END_TIME_MEASUREMENT_MAX(wifi, datalayer.system.status.wifi_task_10s_max_us);
|
||||
#ifdef MQTT
|
||||
START_TIME_MEASUREMENT(mqtt);
|
||||
mqtt_loop();
|
||||
END_TIME_MEASUREMENT_MAX(mqtt, datalayer.system.status.mqtt_task_10s_max_us);
|
||||
#endif
|
||||
|
||||
#ifdef FUNCTION_TIME_MEASUREMENT
|
||||
if (connectivity_task_timer_10s.elapsed()) {
|
||||
datalayer.system.status.mqtt_task_10s_max_us = 0;
|
||||
datalayer.system.status.wifi_task_10s_max_us = 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -209,6 +209,28 @@ void connectivity_loop(void* task_time_us) {
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef MQTT
|
||||
void mqtt_loop(void* task_time_us) {
|
||||
esp_task_wdt_add(NULL); // Register this task with WDT
|
||||
|
||||
init_mqtt();
|
||||
|
||||
while (true) {
|
||||
START_TIME_MEASUREMENT(mqtt);
|
||||
mqtt_loop();
|
||||
END_TIME_MEASUREMENT_MAX(mqtt, datalayer.system.status.mqtt_task_10s_max_us);
|
||||
|
||||
#ifdef FUNCTION_TIME_MEASUREMENT
|
||||
if (mqtt_task_timer_10s.elapsed()) {
|
||||
datalayer.system.status.mqtt_task_10s_max_us = 0;
|
||||
}
|
||||
#endif
|
||||
esp_task_wdt_reset(); // Reset watchdog
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void core_loop(void* task_time_us) {
|
||||
esp_task_wdt_add(NULL); // Register this task with WDT
|
||||
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||
|
|
|
@ -122,6 +122,8 @@
|
|||
/* MQTT options */
|
||||
// #define MQTT // Enable this line to enable MQTT
|
||||
#define MQTT_QOS 0 // MQTT Quality of Service (0, 1, or 2)
|
||||
#define MQTT_PUBLISH_CELL_VOLTAGES // Enable this line to publish cell voltages to MQTT
|
||||
#define MQTT_TIMEOUT 2000 // MQTT timeout in milliseconds
|
||||
#define MQTT_MANUAL_TOPIC_OBJECT_NAME
|
||||
// Enable MQTT_MANUAL_TOPIC_OBJECT_NAME to use custom MQTT topic, object ID prefix, and device name.
|
||||
// WARNING: If this is not defined, the previous default naming format 'battery-emulator_esp32-XXXXXX' (based on hardware ID) will be used.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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'>" +
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue