NC logic for button. 2 button behaviors. some suggestions implemented

This commit is contained in:
amarofarinha 2024-10-02 16:20:10 +01:00
parent 8344bf5179
commit 173acab23a
5 changed files with 51 additions and 25 deletions

View file

@ -138,10 +138,11 @@ unsigned long timeSpentInFaultedMode = 0;
#ifdef EQUIPMENT_STOP_BUTTON #ifdef EQUIPMENT_STOP_BUTTON
volatile unsigned long equipment_button_press_time = 0; // Time when button is pressed volatile unsigned long equipment_button_press_time = 0; // Time when button is pressed
const unsigned long equipment_button_long_press_duration = 5000; // 5 seconds for long press const unsigned long equipment_button_long_press_duration = 5000; // 5 seconds for long press
int equipment_button_lastState = LOW; // the previous state from the input pin int equipment_button_lastState = HIGH; // the previous state from the input pin NC
int equipment_button_currentState; // the current reading from the input pin int equipment_button_currentState; // the current reading from the input pin
unsigned long equipment_button_pressedTime = 0; unsigned long equipment_button_pressedTime = 0;
unsigned long equipment_button_releasedTime = 0; unsigned long equipment_button_releasedTime = 0;
bool first_run_after_boot = true;
#endif #endif
TaskHandle_t main_loop_task; TaskHandle_t main_loop_task;
TaskHandle_t connectivity_loop_task; TaskHandle_t connectivity_loop_task;
@ -352,9 +353,8 @@ void init_stored_settings() {
static uint32_t temp = 0; static uint32_t temp = 0;
settings.begin("batterySettings", false); settings.begin("batterySettings", false);
//allways get the emergency stop status // Always get the emergency stop status
temp = settings.getBool("EMERGENGY_STOP", false); datalayer.system.settings.equipment_stop_active = settings.getBool("EMERGENCY_STOP", false);
datalayer.system.settings.equipment_stop_active = temp;
if (datalayer.system.settings.equipment_stop_active) { if (datalayer.system.settings.equipment_stop_active) {
set_event(EVENT_EMERGENCY_STOP, 1); set_event(EVENT_EMERGENCY_STOP, 1);
} }
@ -363,9 +363,7 @@ void init_stored_settings() {
settings.clear(); // If this clear function is executed, no settings will be read from storage settings.clear(); // If this clear function is executed, no settings will be read from storage
//always save the emergency stop status //always save the emergency stop status
if (datalayer.system.settings.equipment_stop_active) { settings.putBool("EMERGENCY_STOP", datalayer.system.settings.equipment_stop_active);
settings.putBool("EMERGENGY_STOP", datalayer.system.settings.equipment_stop_active);
}
#endif #endif
@ -564,12 +562,24 @@ void init_battery() {
#ifdef EQUIPMENT_STOP_BUTTON #ifdef EQUIPMENT_STOP_BUTTON
void monitor_equipment_stop_button() { void monitor_equipment_stop_button() {
//NC Logic
// read the state of the switch/button: // read the state of the switch/button:
equipment_button_currentState = digitalRead(EQUIPMENT_STOP_PIN); equipment_button_currentState = digitalRead(EQUIPMENT_STOP_PIN);
if (equipment_button_lastState == LOW && equipment_button_currentState == HIGH) // button is pressed if (equipment_stop_behavior == TOGGLE_SWITCH) {
if (equipment_button_lastState != equipment_button_currentState || first_run_after_boot) {
if (!equipment_button_currentState) {
// Changed to ON initiating equipment stop.
setBatteryPause(true, true, true);
} else {
// Changed to OFF ending equipment stop.
setBatteryPause(false, false, false);
}
}
} else if (equipment_stop_behavior == PERSISTENT_ACTIVATION_SWITCH) {
if (equipment_button_lastState == HIGH && equipment_button_currentState == LOW) { // button is pressed
equipment_button_pressedTime = millis(); equipment_button_pressedTime = millis();
else if (equipment_button_lastState == HIGH && equipment_button_currentState == LOW) { // button is released } else if (equipment_button_lastState == LOW && equipment_button_currentState == HIGH) { // button is released
equipment_button_releasedTime = millis(); equipment_button_releasedTime = millis();
long pressDuration = equipment_button_releasedTime - equipment_button_pressedTime; long pressDuration = equipment_button_releasedTime - equipment_button_pressedTime;
@ -582,14 +592,18 @@ void monitor_equipment_stop_button() {
setBatteryPause(false, false, false); setBatteryPause(false, false, false);
} }
} }
}
// save the the last state // save the the last state
equipment_button_lastState = equipment_button_currentState; equipment_button_lastState = equipment_button_currentState;
if (first_run_after_boot) {
first_run_after_boot = false;
}
} }
void init_equipment_stop_button() { void init_equipment_stop_button() {
//using external pullup resistors NC
//using external pulldown resistors
pinMode(EQUIPMENT_STOP_PIN, INPUT); pinMode(EQUIPMENT_STOP_PIN, INPUT);
} }
@ -901,7 +915,7 @@ void init_serialDataLink() {
void store_settings_emergency_stop() { void store_settings_emergency_stop() {
settings.begin("batterySettings", false); settings.begin("batterySettings", false);
settings.putBool("EMERGENGY_STOP", datalayer.system.settings.equipment_stop_active); settings.putBool("EMERGENCY_STOP", datalayer.system.settings.equipment_stop_active);
settings.end(); settings.end();
} }

View file

@ -48,6 +48,13 @@ const char* mqtt_password = "REDACTED"; // Set NULL for no password
#endif // USE_MQTT #endif // USE_MQTT
#endif // WIFI #endif // WIFI
#ifdef EQUIPMENT_STOP_BUTTON
// Equipment stop button behavior. Use NC button for safety reasons.
//TOGGLE_SWITCH - activated while pressed, deactivated when released, button state is reflected in the emulator state
//PERSISTENT_ACTIVATION_SWITCH - short press to activate, long press to deactivate, state is persisted between reboots
volatile STOP_BUTTON_BEHAVIOR equipment_stop_behavior = TOGGLE_SWITCH;
#endif
/* Charger settings (Optional, when using generator charging) */ /* Charger settings (Optional, when using generator charging) */
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack
volatile float CHARGER_MAX_HV = 420; // Max permissible output (VDC) of charger volatile float CHARGER_MAX_HV = 420; // Max permissible output (VDC) of charger

View file

@ -65,7 +65,7 @@
#define MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled. #define MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled.
#define LOAD_SAVED_SETTINGS_ON_BOOT //Enable this line to read settings stored via the webserver on boot (overrides Wifi/battery settings set below) #define LOAD_SAVED_SETTINGS_ON_BOOT //Enable this line to read settings stored via the webserver on boot (overrides Wifi/battery settings set below)
//#define FUNCTION_TIME_MEASUREMENT // Enable this to record execution times and present them in the web UI (WARNING, raises CPU load, do not use for production) //#define FUNCTION_TIME_MEASUREMENT // Enable this to record execution times and present them in the web UI (WARNING, raises CPU load, do not use for production)
//#define EQUIPMENT_STOP_BUTTON // Enable this to have a emergengy/equipment stop button connected to board //#define EQUIPMENT_STOP_BUTTON // Enable this to allow an emergency/equipment stop button connected to the Battery-Emulator to disengage the battery
/* MQTT options */ /* MQTT options */
// #define MQTT // Enable this line to enable MQTT // #define MQTT // Enable this line to enable MQTT
@ -120,6 +120,11 @@ extern volatile float CHARGER_END_A;
extern bool charger_HV_enabled; extern bool charger_HV_enabled;
extern bool charger_aux12V_enabled; extern bool charger_aux12V_enabled;
#ifdef EQUIPMENT_STOP_BUTTON
typedef enum { TOGGLE_SWITCH = 0, PERSISTENT_ACTIVATION_SWITCH = 1 } STOP_BUTTON_BEHAVIOR;
extern volatile STOP_BUTTON_BEHAVIOR equipment_stop_behavior;
#endif
#ifdef WIFICONFIG #ifdef WIFICONFIG
extern IPAddress local_IP; extern IPAddress local_IP;
// Set your Gateway IP address // Set your Gateway IP address

View file

@ -198,7 +198,7 @@ void update_machineryprotection() {
//battery pause status begin //battery pause status begin
void setBatteryPause(bool pause_battery, bool pause_CAN, bool emergency_stop) { void setBatteryPause(bool pause_battery, bool pause_CAN, bool emergency_stop) {
//fist handle emergency stop / resume // First handle emergency stop / resume
if (emergency_stop && !datalayer.system.settings.equipment_stop_active) { if (emergency_stop && !datalayer.system.settings.equipment_stop_active) {
datalayer.system.settings.equipment_stop_active = true; datalayer.system.settings.equipment_stop_active = true;
store_settings_emergency_stop(); store_settings_emergency_stop();

View file

@ -167,7 +167,7 @@ void init_webserver() {
}); });
// Route for emergency stop/resume // Route for emergency stop/resume
server.on("/estop", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/emergencyStop", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password)) if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication(); return request->requestAuthentication();
if (request->hasParam("stop")) { if (request->hasParam("stop")) {
@ -908,7 +908,7 @@ String processor(const String& var) {
content += content +=
"var xhr=new " "var xhr=new "
"XMLHttpRequest();xhr.onload=function() { " "XMLHttpRequest();xhr.onload=function() { "
"window.location.reload();};xhr.open('GET','/estop?stop='+stop,true);xhr.send();"; "window.location.reload();};xhr.open('GET','/emergencyStop?stop='+stop,true);xhr.send();";
content += "}"; content += "}";
content += "</script>"; content += "</script>";