Initial Implementation of Core Emergency Stop Feature on Battery Emulator

This commit is contained in:
amarofarinha 2024-10-01 19:12:22 +01:00
parent af9886ad33
commit 81cd6780a3
7 changed files with 84 additions and 5 deletions

View file

@ -334,10 +334,24 @@ void init_serial() {
}
void init_stored_settings() {
static uint32_t temp = 0;
settings.begin("batterySettings", false);
//allways get the emergency stop status
temp = settings.getBool("EMERGENGY_STOP", false);
datalayer.system.settings.equipment_stop_active = temp;
if (datalayer.system.settings.equipment_stop_active) {
set_event(EVENT_EMERGENCY_STOP, 1);
}
#ifndef LOAD_SAVED_SETTINGS_ON_BOOT
settings.clear(); // If this clear function is executed, no settings will be read from storage
//always save the emergency stop status
if (datalayer.system.settings.equipment_stop_active) {
settings.putBool("EMERGENGY_STOP", datalayer.system.settings.equipment_stop_active);
}
#endif
#ifdef WIFI
@ -356,7 +370,6 @@ void init_stored_settings() {
}
#endif
static uint32_t temp = 0;
temp = settings.getUInt("BATTERY_WH_MAX", false);
if (temp != 0) {
datalayer.battery.info.total_capacity_Wh = temp;
@ -658,9 +671,14 @@ void handle_contactors() {
timeSpentInFaultedMode = 0;
}
if (timeSpentInFaultedMode > MAX_ALLOWED_FAULT_TICKS) {
//handle contactor control SHUTDOWN_REQUESTED vs DISCONNECTED
if (timeSpentInFaultedMode > MAX_ALLOWED_FAULT_TICKS ||
(datalayer.system.settings.equipment_stop_active && contactorStatus != SHUTDOWN_REQUESTED)) {
contactorStatus = SHUTDOWN_REQUESTED;
}
if (contactorStatus == SHUTDOWN_REQUESTED && !datalayer.system.settings.equipment_stop_active) {
contactorStatus = DISCONNECTED;
}
if (contactorStatus == SHUTDOWN_REQUESTED) {
digitalWrite(PRECHARGE_PIN, LOW);
#ifndef PWM_CONTACTOR_CONTROL
@ -832,6 +850,12 @@ void init_serialDataLink() {
#endif
}
void store_settings_emergency_stop() {
settings.begin("batterySettings", false);
settings.putBool("EMERGENGY_STOP", datalayer.system.settings.equipment_stop_active);
settings.end();
}
void storeSettings() {
settings.begin("batterySettings", false);
#ifdef WIFI

View file

@ -172,6 +172,7 @@ typedef struct {
} DATALAYER_SYSTEM_STATUS_TYPE;
typedef struct {
bool equipment_stop_active = false;
} DATALAYER_SYSTEM_SETTINGS_TYPE;
typedef struct {

View file

@ -196,7 +196,18 @@ void update_machineryprotection() {
}
//battery pause status begin
void setBatteryPause(bool pause_battery, bool pause_CAN) {
void setBatteryPause(bool pause_battery, bool pause_CAN, bool emergency_stop) {
//fist handle emergency stop / resume
if (emergency_stop && !datalayer.system.settings.equipment_stop_active) {
datalayer.system.settings.equipment_stop_active = true;
store_settings_emergency_stop();
set_event(EVENT_EMERGENCY_STOP, 1);
} else if (!emergency_stop && datalayer.system.settings.equipment_stop_active) {
datalayer.system.settings.equipment_stop_active = false;
store_settings_emergency_stop();
clear_event(EVENT_EMERGENCY_STOP);
}
emulator_pause_CAN_send_ON = pause_CAN;

View file

@ -15,10 +15,12 @@ extern battery_pause_status emulator_pause_status;
extern bool allowed_to_send_CAN;
//battery pause status end
extern void store_settings_emergency_stop();
void update_machineryprotection();
//battery pause status begin
void setBatteryPause(bool pause_battery, bool pause_CAN);
void setBatteryPause(bool pause_battery, bool pause_CAN, bool emergency_stop = false);
void emulator_pause_state_send_CAN_battery();
std::string get_emulator_pause_status();
//battery pause status end

View file

@ -213,6 +213,7 @@ void init_events(void) {
events.entries[EVENT_WIFI_DISCONNECT].level = EVENT_LEVEL_INFO;
events.entries[EVENT_MQTT_CONNECT].level = EVENT_LEVEL_INFO;
events.entries[EVENT_MQTT_DISCONNECT].level = EVENT_LEVEL_INFO;
events.entries[EVENT_EMERGENCY_STOP].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_EEPROM_WRITE].log = false; // Don't log the logger...
@ -411,6 +412,8 @@ const char* get_event_message_string(EVENTS_ENUM_TYPE event) {
return "Info: MQTT connected.";
case EVENT_MQTT_DISCONNECT:
return "Info: MQTT disconnected.";
case EVENT_EMERGENCY_STOP:
return "ERROR: EMERGENCY STOP ACTIVATED!!!";
default:
return "";
}

View file

@ -101,6 +101,7 @@
XX(EVENT_WIFI_DISCONNECT) \
XX(EVENT_MQTT_CONNECT) \
XX(EVENT_MQTT_DISCONNECT) \
XX(EVENT_EMERGENCY_STOP) \
XX(EVENT_NOF_EVENTS)
typedef enum { EVENTS_ENUM_TYPE(GENERATE_ENUM) } EVENTS_ENUM_TYPE;

View file

@ -166,6 +166,23 @@ void init_webserver() {
}
});
// Route for emergency stop/resume
server.on("/estop", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
if (request->hasParam("stop")) {
String valueStr = request->getParam("stop")->value();
if (valueStr == "true" || valueStr == "1") {
setBatteryPause(true, true, true);
} else {
setBatteryPause(false, false, false);
}
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");
}
});
// Route for editing SOCMin
server.on("/updateSocMin", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
@ -844,6 +861,21 @@ String processor(const String& var) {
content += "<button onclick='askReboot()'>Reboot Emulator</button>";
if (WEBSERVER_AUTH_REQUIRED)
content += "<button onclick='logout()'>Logout</button>";
if (!datalayer.system.settings.equipment_stop_active)
content +=
"<br/><br/><button style=\"background:red;color:white;cursor:pointer;\""
" onclick=\""
"if(confirm('This action will open contactors on the battery and stop all CAN communications. Are you "
"sure?')) { estop(true); }\""
">EQUIPMENT STOP</button><br/>";
else
content +=
"<br/><br/><button style=\"background:green;color:white;cursor:pointer;\""
"20px;font-size:16px;font-weight:bold;cursor:pointer;border-radius:5px; margin:10px;"
" onclick=\""
"if(confirm('This action will restore the battery state. Are you sure?')) { estop(false); }\""
">RESUME FROM EQUIPMENT STOP</button><br/>";
content += "<script>";
content += "function OTA() { window.location.href = '/update'; }";
content += "function Cellmon() { window.location.href = '/cellmonitor'; }";
@ -872,7 +904,12 @@ String processor(const String& var) {
"XMLHttpRequest();xhr.onload=function() { "
"window.location.reload();};xhr.open('GET','/pause?p='+pause,true);xhr.send();";
content += "}";
content += "function estop(stop){";
content +=
"var xhr=new "
"XMLHttpRequest();xhr.onload=function() { "
"window.location.reload();};xhr.open('GET','/estop?stop='+stop,true);xhr.send();";
content += "}";
content += "</script>";
//Script for refreshing page