Merge branch 'main' into bugfix/precharge-osc

This commit is contained in:
Daniel Öster 2025-08-24 15:09:10 +03:00
commit 26eed03397
13 changed files with 188 additions and 156 deletions

View file

@ -70,7 +70,7 @@ class KiaHyundai64Battery : public CanBattery {
uint16_t CellVoltMin_mV = 3700;
uint16_t allowedDischargePower = 0;
uint16_t allowedChargePower = 0;
uint16_t batteryVoltage = 0;
uint16_t batteryVoltage = 3700;
uint16_t inverterVoltageFrameHigh = 0;
uint16_t inverterVoltage = 0;
uint16_t cellvoltages_mv[98];

View file

@ -55,7 +55,7 @@ const int OFF = 0;
#define OFF 1
#endif //NC_CONTACTORS
#define MAX_ALLOWED_FAULT_TICKS 1000
#define MAX_ALLOWED_FAULT_TICKS 1000 //1000 = 10 seconds
#define NEGATIVE_CONTACTOR_TIME_MS \
500 // Time after negative contactor is turned on, to start precharge (not actual precharge time!)
#define PRECHARGE_COMPLETED_TIME_MS \
@ -192,7 +192,7 @@ void handle_contactors() {
set(negPin, OFF, PWM_OFF_DUTY);
set(posPin, OFF, PWM_OFF_DUTY);
set_event(EVENT_ERROR_OPEN_CONTACTOR, 0);
datalayer.system.status.contactors_engaged = false;
datalayer.system.status.contactors_engaged = 2;
return; // A fault scenario latches the contactor control. It is not possible to recover without a powercycle (and investigation why fault occured)
}
@ -201,10 +201,9 @@ void handle_contactors() {
set(prechargePin, OFF);
set(negPin, OFF, PWM_OFF_DUTY);
set(posPin, OFF, PWM_OFF_DUTY);
datalayer.system.status.contactors_engaged = false;
datalayer.system.status.contactors_engaged = 0;
if (datalayer.system.status.battery_allows_contactor_closing &&
datalayer.system.status.inverter_allows_contactor_closing &&
if (datalayer.system.status.inverter_allows_contactor_closing &&
!datalayer.system.settings.equipment_stop_active) {
contactorStatus = START_PRECHARGE;
}
@ -263,7 +262,7 @@ void handle_contactors() {
set(posPin, ON, PWM_HOLD_DUTY);
dbg_contactors("PRECHARGE_OFF");
contactorStatus = COMPLETED;
datalayer.system.status.contactors_engaged = true;
datalayer.system.status.contactors_engaged = 1;
}
break;
default:

View file

@ -310,8 +310,8 @@ struct DATALAYER_SYSTEM_STATUS_TYPE {
/** True if the inverter allows for the contactors to close */
bool inverter_allows_contactor_closing = true;
/** True if the contactor controlled by battery-emulator is closed */
bool contactors_engaged = false;
/** 0 if starting up, 1 if contactors engaged, 2 if the contactors controlled by battery-emulator is opened */
uint8_t contactors_engaged = 0;
/** True if the contactor controlled by battery-emulator is closed. Determined by check_interconnect_available(); if voltage is OK */
bool contactors_battery2_engaged = false;

View file

@ -38,7 +38,16 @@ class StarkHal : public Esp32Hal {
virtual gpio_num_t CAN_SE_PIN() { return GPIO_NUM_NC; }
// CANFD_ADDON defines for MCP2517
virtual gpio_num_t MCP2517_SCK() { return GPIO_NUM_17; }
// Stark CMR v1 has GPIO pin 16 for SCK, CMR v2 has GPIO pin 17. Only diff between the two boards
bool isStarkVersion1() {
size_t flashSize = ESP.getFlashChipSize();
if (flashSize == 4 * 1024 * 1024) {
return true;
} else { //v2
return false;
}
}
virtual gpio_num_t MCP2517_SCK() { return isStarkVersion1() ? GPIO_NUM_16 : GPIO_NUM_17; }
virtual gpio_num_t MCP2517_SDI() { return GPIO_NUM_5; }
virtual gpio_num_t MCP2517_SDO() { return GPIO_NUM_34; }
virtual gpio_num_t MCP2517_CS() { return GPIO_NUM_18; }

View file

@ -141,7 +141,9 @@ SensorConfig batterySensorConfigTemplate[] = {
{"balancing_active_cells", "Balancing Active Cells", "", "", "", always}};
SensorConfig globalSensorConfigTemplate[] = {{"bms_status", "BMS Status", "", "", "", always},
{"pause_status", "Pause Status", "", "", "", always}};
{"pause_status", "Pause Status", "", "", "", always},
{"event_level", "Event Level", "", "", "", always},
{"emulator_status", "Emulator Status", "", "", "", always}};
static std::list<SensorConfig> sensorConfigs;
@ -311,6 +313,10 @@ static bool publish_common_info(void) {
set_battery_attributes(doc, datalayer.battery2, "_2", battery2->supports_charged_energy());
}
}
doc["event_level"] = get_event_level_string(get_event_level());
doc["emulator_status"] = get_emulator_status_string(get_emulator_status());
serializeJson(doc, mqtt_msg);
if (mqtt_publish(state_topic.c_str(), mqtt_msg, false) == false) {
#ifdef DEBUG_LOG

View file

@ -14,6 +14,7 @@ typedef struct {
static EVENT_TYPE events;
static const char* EVENTS_ENUM_TYPE_STRING[] = {EVENTS_ENUM_TYPE(GENERATE_STRING)};
static const char* EVENTS_LEVEL_TYPE_STRING[] = {EVENTS_LEVEL_TYPE(GENERATE_STRING)};
static const char* EMULATOR_STATUS_STRING[] = {EMULATOR_STATUS(GENERATE_STRING)};
/* Local function prototypes */
static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched);
@ -270,8 +271,8 @@ String get_event_message_string(EVENTS_ENUM_TYPE event) {
case EVENT_INTERFACE_MISSING:
return "Configuration trying to use CAN interface not baked into the software. Recompile software!";
case EVENT_ERROR_OPEN_CONTACTOR:
return "Too much time spent in error state. Opening contactors, not safe to continue charging. "
"Check other error code for reason!";
return "Too much time spent in error state. Opening contactors, not safe to continue. "
"Check other active ERROR code for reason. Reboot emulator after problem is solved!";
case EVENT_MODBUS_INVERTER_MISSING:
return "Modbus inverter has not sent any data. Inspect communication wiring!";
case EVENT_NO_ENABLE_DETECTED:
@ -393,6 +394,11 @@ const char* get_event_level_string(EVENTS_ENUM_TYPE event) {
return EVENTS_LEVEL_TYPE_STRING[events.entries[event].level] + 12;
}
const char* get_event_level_string(EVENTS_LEVEL_TYPE event_level) {
// Return the event level but skip "EVENT_LEVEL_TYPE_" that should always be first
return EVENTS_LEVEL_TYPE_STRING[event_level] + 17;
}
const EVENTS_STRUCT_TYPE* get_event_pointer(EVENTS_ENUM_TYPE event) {
return &events.entries[event];
}
@ -401,6 +407,27 @@ EVENTS_LEVEL_TYPE get_event_level(void) {
return events.level;
}
EMULATOR_STATUS get_emulator_status() {
switch (events.level) {
case EVENT_LEVEL_DEBUG:
case EVENT_LEVEL_INFO:
return EMULATOR_STATUS::STATUS_OK;
case EVENT_LEVEL_WARNING:
return EMULATOR_STATUS::STATUS_WARNING;
case EVENT_LEVEL_UPDATE:
return EMULATOR_STATUS::STATUS_UPDATING;
case EVENT_LEVEL_ERROR:
return EMULATOR_STATUS::STATUS_ERROR;
default:
return EMULATOR_STATUS::STATUS_OK;
}
}
const char* get_emulator_status_string(EMULATOR_STATUS status) {
// Return the status string but skip "STATUS_" that should always be first
return EMULATOR_STATUS_STRING[status] + 7;
}
/* Local functions */
static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) {

View file

@ -126,6 +126,14 @@ typedef enum { EVENTS_ENUM_TYPE(GENERATE_ENUM) } EVENTS_ENUM_TYPE;
typedef enum { EVENTS_LEVEL_TYPE(GENERATE_ENUM) } EVENTS_LEVEL_TYPE;
#define EMULATOR_STATUS(XX) \
XX(STATUS_OK) \
XX(STATUS_WARNING) \
XX(STATUS_ERROR) \
XX(STATUS_UPDATING)
typedef enum { EMULATOR_STATUS(GENERATE_ENUM) } EMULATOR_STATUS;
typedef enum {
EVENT_STATE_PENDING = 0,
EVENT_STATE_INACTIVE,
@ -151,8 +159,11 @@ struct EventData {
const char* get_event_enum_string(EVENTS_ENUM_TYPE event);
String get_event_message_string(EVENTS_ENUM_TYPE event);
const char* get_event_level_string(EVENTS_ENUM_TYPE event);
const char* get_event_level_string(EVENTS_LEVEL_TYPE event_level);
EVENTS_LEVEL_TYPE get_event_level(void);
EMULATOR_STATUS get_emulator_status();
const char* get_emulator_status_string(EMULATOR_STATUS status);
void init_events(void);
void set_event_latched(EVENTS_ENUM_TYPE event, uint8_t data);

View file

@ -32,10 +32,6 @@ void led_exe(void) {
led->exe();
}
led_color led_get_color() {
return led->color;
}
void LED::exe(void) {
// Update brightness
@ -53,27 +49,21 @@ void LED::exe(void) {
}
// Set color
switch (get_event_level()) {
case EVENT_LEVEL_INFO:
color = led_color::GREEN;
switch (get_emulator_status()) {
case EMULATOR_STATUS::STATUS_OK:
pixels.setPixelColor(COLOR_GREEN(brightness)); // Green pulsing LED
break;
case EVENT_LEVEL_WARNING:
color = led_color::YELLOW;
case EMULATOR_STATUS::STATUS_WARNING:
pixels.setPixelColor(COLOR_YELLOW(brightness)); // Yellow pulsing LED
break;
case EVENT_LEVEL_DEBUG:
case EVENT_LEVEL_UPDATE:
color = led_color::BLUE;
pixels.setPixelColor(COLOR_BLUE(brightness)); // Blue pulsing LED
break;
case EVENT_LEVEL_ERROR:
color = led_color::RED;
case EMULATOR_STATUS::STATUS_ERROR:
pixels.setPixelColor(COLOR_RED(esp32hal->LED_MAX_BRIGHTNESS())); // Red LED full brightness
break;
default:
case EMULATOR_STATUS::STATUS_UPDATING:
pixels.setPixelColor(COLOR_BLUE(brightness)); // Blue pulsing LED
break;
}
pixels.show(); // This sends the updated pixel color to the hardware.
}

View file

@ -6,8 +6,6 @@
class LED {
public:
led_color color = led_color::GREEN;
LED(gpio_num_t pin, uint8_t maxBrightness)
: pixels(pin), max_brightness(maxBrightness), brightness(maxBrightness), mode(led_mode_enum::CLASSIC) {}
@ -31,6 +29,5 @@ class LED {
bool led_init(void);
void led_exe(void);
led_color led_get_color(void);
#endif // LED_H_

View file

@ -21,7 +21,6 @@ enum class comm_interface {
Highest
};
enum led_color { GREEN, YELLOW, RED, BLUE };
enum led_mode_enum { CLASSIC, FLOW, HEARTBEAT };
enum PrechargeState {
AUTO_PRECHARGE_IDLE,

View file

@ -836,6 +836,28 @@ String processor(const String& var) {
content += "button:hover { background-color: #3A4A52; }";
content += "h2 { font-size: 1.2em; margin: 0.3em 0 0.5em 0; }";
content += "h4 { margin: 0.6em 0; line-height: 1.2; }";
//content += ".tooltip { position: relative; display: inline-block; }";
content += ".tooltip .tooltiptext {";
content += " visibility: hidden;";
content += " width: 200px;";
content += " background-color: #3A4A52;"; // Matching your button hover color
content += " color: white;";
content += " text-align: center;";
content += " border-radius: 6px;";
content += " padding: 8px;";
content += " position: absolute;";
content += " z-index: 1;";
content += " bottom: 125%;";
content += " left: 50%;";
content += " margin-left: -100px;";
content += " opacity: 0;";
content += " transition: opacity 0.3s;";
content += " font-size: 0.9em;";
content += " font-weight: normal;";
content += " line-height: 1.4;";
content += "}";
content += ".tooltip:hover .tooltiptext { visibility: visible; opacity: 1; }";
content += ".tooltip-icon { color: #505E67; cursor: help; }"; // Matching your button color
content += "</style>";
// Compact header
@ -943,21 +965,18 @@ String processor(const String& var) {
content += "<div style='background-color: ";
}
switch (led_get_color()) {
case led_color::GREEN:
switch (get_emulator_status()) {
case EMULATOR_STATUS::STATUS_OK:
content += "#2D3F2F;";
break;
case led_color::YELLOW:
case EMULATOR_STATUS::STATUS_WARNING:
content += "#F5CC00;";
break;
case led_color::BLUE:
content += "#2B35AF;"; // Blue in test mode
break;
case led_color::RED:
case EMULATOR_STATUS::STATUS_ERROR:
content += "#A70107;";
break;
default: // Some new color, make background green
content += "#2D3F2F;";
case EMULATOR_STATUS::STATUS_UPDATING:
content += "#2B35AF;"; // Blue in test mode
break;
}
@ -1115,58 +1134,6 @@ String processor(const String& var) {
}
}
content += "<h4>Battery allows contactor closing: ";
if (datalayer.system.status.battery_allows_contactor_closing == true) {
content += "<span>&#10003;</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
}
content += " Inverter allows contactor closing: ";
if (datalayer.system.status.inverter_allows_contactor_closing == true) {
content += "<span>&#10003;</span></h4>";
} else {
content += "<span style='color: red;'>&#10005;</span></h4>";
}
if (emulator_pause_status == NORMAL)
content += "<h4>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
else
content += "<h4 style='color: red;'>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
if (contactor_control_enabled) {
content += "<h4>Contactors controlled by emulator, state: ";
if (datalayer.system.status.contactors_engaged) {
content += "<span style='color: green;'>ON</span>";
} else {
content += "<span style='color: red;'>OFF</span>";
}
content += "</h4>";
if (contactor_control_enabled_double_battery) {
if (pwm_contactor_control) {
content += "<h4>Cont. Neg.: ";
if (datalayer.system.status.contactors_battery2_engaged) {
content += "<span style='color: green;'>Economized</span>";
content += " Cont. Pos.: ";
content += "<span style='color: green;'>Economized</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
content += " Cont. Pos.: ";
content += "<span style='color: red;'>&#10005;</span>";
}
} else if (
esp32hal->SECOND_BATTERY_CONTACTORS_PIN() !=
GPIO_NUM_NC) { // No PWM_CONTACTOR_CONTROL , we can read the pin and see feedback. Helpful if channel overloaded
content += "<h4>Cont. Neg.: ";
if (digitalRead(esp32hal->SECOND_BATTERY_CONTACTORS_PIN()) == HIGH) {
content += "<span style='color: green;'>&#10003;</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
}
} //no PWM_CONTACTOR_CONTROL
content += "</h4>";
}
}
// Close the block
content += "</div>";
@ -1264,71 +1231,85 @@ String processor(const String& var) {
} else { // > 0
content += "<h4>Battery charging!</h4>";
}
content += "</div>";
content += "</div>";
}
}
// Block for Contactor status and component request status
// Start a new block with gray background color
content += "<div style='background-color: #333; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
content += "<h4>Automatic contactor closing allowed:</h4>";
content += "<h4>Battery: ";
if (datalayer.system.status.battery2_allowed_contactor_closing == true) {
content += "<span>&#10003;</span>";
if (emulator_pause_status == NORMAL) {
content += "<h4>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
content += "<h4 style='color: red;'>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
}
content += " Inverter: ";
content += "<h4>Emulator allows contactor closing: ";
if (datalayer.battery.status.bms_status == FAULT) {
content += "<span style='color: red;'>&#10005;</span>";
} else {
content += "<span>&#10003;</span>";
}
content += " Inverter allows contactor closing: ";
if (datalayer.system.status.inverter_allows_contactor_closing == true) {
content += "<span>&#10003;</span></h4>";
} else {
content += "<span style='color: red;'>&#10005;</span></h4>";
}
if (battery2) {
content += "<h4>Secondary battery allowed to join ";
if (datalayer.system.status.battery2_allowed_contactor_closing == true) {
content += "<span>&#10003;</span>";
} else {
content += "<span style='color: red;'>&#10005; (voltage mismatch)</span>";
}
}
if (emulator_pause_status == NORMAL)
content += "<h4>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
else
content += "<h4 style='color: red;'>Power status: " + String(get_emulator_pause_status().c_str()) + " </h4>";
if (contactor_control_enabled) {
content += "<h4>Contactors controlled by emulator, state: ";
if (!contactor_control_enabled) {
content += "<div class=\"tooltip\">";
content += "<h4>Contactors not fully controlled via emulator <span style=\"color:orange\">[?]</span></h4>";
content +=
"<span class=\"tooltiptext\">This means you are either running CAN controlled contactors OR manually "
"powering the contactors. Battery-Emulator will have limited amount of control over the contactors!</span>";
content += "</div>";
} else { //contactor_control_enabled TRUE
content += "<div class=\"tooltip\"><h4>Contactors controlled by emulator, state: ";
if (datalayer.system.status.contactors_engaged == 0) {
content += "<span style='color: green;'>PRECHARGE</span>";
} else if (datalayer.system.status.contactors_engaged == 1) {
content += "<span style='color: green;'>ON</span>";
} else if (datalayer.system.status.contactors_engaged == 2) {
content += "<span style='color: red;'>OFF</span>";
content += "<span class=\"tooltip-icon\"> [!]</span>";
content +=
"<span class=\"tooltiptext\">Emulator spent too much time in critical FAULT event. Investigate event "
"causing this via Events page. Reboot required to resume operation!</span>";
}
content += "</h4></div>";
if (contactor_control_enabled_double_battery && battery2) {
content += "<h4>Secondary battery contactor, state: ";
if (pwm_contactor_control) {
if (datalayer.system.status.contactors_battery2_engaged) {
content += "<span style='color: green;'>Economized</span>";
} else {
content += "<span style='color: red;'>OFF</span>";
}
} else if (
esp32hal->SECOND_BATTERY_CONTACTORS_PIN() !=
GPIO_NUM_NC) { // No PWM_CONTACTOR_CONTROL , we can read the pin and see feedback. Helpful if channel overloaded
if (digitalRead(esp32hal->SECOND_BATTERY_CONTACTORS_PIN()) == HIGH) {
content += "<span style='color: green;'>ON</span>";
} else {
content += "<span style='color: red;'>OFF</span>";
}
content += "</h4>";
if (contactor_control_enabled_double_battery) {
content += "<h4>Cont. Neg.: ";
if (pwm_contactor_control) {
if (datalayer.system.status.contactors_battery2_engaged) {
content += "<span style='color: green;'>Economized</span>";
content += " Cont. Pos.: ";
content += "<span style='color: green;'>Economized</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
content += " Cont. Pos.: ";
content += "<span style='color: red;'>&#10005;</span>";
}
} else { // No PWM_CONTACTOR_CONTROL , we can read the pin and see feedback. Helpful if channel overloaded
#if defined(SECOND_POSITIVE_CONTACTOR_PIN) && defined(SECOND_NEGATIVE_CONTACTOR_PIN)
if (digitalRead(SECOND_NEGATIVE_CONTACTOR_PIN) == HIGH) {
content += "<span style='color: green;'>&#10003;</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
}
content += " Cont. Pos.: ";
if (digitalRead(SECOND_POSITIVE_CONTACTOR_PIN) == HIGH) {
content += "<span style='color: green;'>&#10003;</span>";
} else {
content += "<span style='color: red;'>&#10005;</span>";
}
#endif
}
} //no PWM_CONTACTOR_CONTROL
content += "</h4>";
}
}
// Close the block
content += "</div>";
content += "</div>";
}
}
if (charger) {
// Start a new block with orange background color

View file

@ -14,3 +14,5 @@ void pinMode(uint8_t pin, uint8_t mode) {}
int max(int a, int b) {
return (a > b) ? a : b;
}
ESPClass ESP;

View file

@ -24,4 +24,15 @@ void delay(unsigned long ms);
void delayMicroseconds(unsigned long us);
int max(int a, int b);
class ESPClass {
public:
size_t getFlashChipSize() {
// This is a placeholder for the actual implementation
// that retrieves the flash chip size.
return 4 * 1024 * 1024; // Example: returning 4MB
}
};
extern ESPClass ESP;
#endif