mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 02:39:57 +02:00
NC logic for button. 2 button behaviors. some suggestions implemented
This commit is contained in:
parent
8344bf5179
commit
173acab23a
5 changed files with 51 additions and 25 deletions
|
@ -138,10 +138,11 @@ unsigned long timeSpentInFaultedMode = 0;
|
|||
#ifdef EQUIPMENT_STOP_BUTTON
|
||||
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
|
||||
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
|
||||
unsigned long equipment_button_pressedTime = 0;
|
||||
unsigned long equipment_button_releasedTime = 0;
|
||||
bool first_run_after_boot = true;
|
||||
#endif
|
||||
TaskHandle_t main_loop_task;
|
||||
TaskHandle_t connectivity_loop_task;
|
||||
|
@ -352,9 +353,8 @@ 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;
|
||||
// Always get the emergency stop status
|
||||
datalayer.system.settings.equipment_stop_active = settings.getBool("EMERGENCY_STOP", false);
|
||||
if (datalayer.system.settings.equipment_stop_active) {
|
||||
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
|
||||
|
||||
//always save the emergency stop status
|
||||
if (datalayer.system.settings.equipment_stop_active) {
|
||||
settings.putBool("EMERGENGY_STOP", datalayer.system.settings.equipment_stop_active);
|
||||
}
|
||||
settings.putBool("EMERGENCY_STOP", datalayer.system.settings.equipment_stop_active);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -564,12 +562,24 @@ void init_battery() {
|
|||
#ifdef EQUIPMENT_STOP_BUTTON
|
||||
|
||||
void monitor_equipment_stop_button() {
|
||||
//NC Logic
|
||||
// read the state of the switch/button:
|
||||
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();
|
||||
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();
|
||||
|
||||
long pressDuration = equipment_button_releasedTime - equipment_button_pressedTime;
|
||||
|
@ -582,14 +592,18 @@ void monitor_equipment_stop_button() {
|
|||
setBatteryPause(false, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// save the the last state
|
||||
equipment_button_lastState = equipment_button_currentState;
|
||||
|
||||
if (first_run_after_boot) {
|
||||
first_run_after_boot = false;
|
||||
}
|
||||
}
|
||||
|
||||
void init_equipment_stop_button() {
|
||||
|
||||
//using external pulldown resistors
|
||||
//using external pullup resistors NC
|
||||
pinMode(EQUIPMENT_STOP_PIN, INPUT);
|
||||
}
|
||||
|
||||
|
@ -901,7 +915,7 @@ void init_serialDataLink() {
|
|||
|
||||
void store_settings_emergency_stop() {
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,13 @@ const char* mqtt_password = "REDACTED"; // Set NULL for no password
|
|||
#endif // USE_MQTT
|
||||
#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) */
|
||||
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
|
||||
|
|
|
@ -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 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 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 */
|
||||
// #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_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
|
||||
extern IPAddress local_IP;
|
||||
// Set your Gateway IP address
|
||||
|
|
|
@ -198,7 +198,7 @@ void update_machineryprotection() {
|
|||
//battery pause status begin
|
||||
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) {
|
||||
datalayer.system.settings.equipment_stop_active = true;
|
||||
store_settings_emergency_stop();
|
||||
|
|
|
@ -167,7 +167,7 @@ void init_webserver() {
|
|||
});
|
||||
|
||||
// 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))
|
||||
return request->requestAuthentication();
|
||||
if (request->hasParam("stop")) {
|
||||
|
@ -908,7 +908,7 @@ String processor(const String& var) {
|
|||
content +=
|
||||
"var xhr=new "
|
||||
"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 += "</script>";
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue