From f430141904f55269cba3297dfbc4522866c2fc92 Mon Sep 17 00:00:00 2001 From: bdring Date: Sun, 28 Apr 2019 10:44:57 -0500 Subject: [PATCH] Added limit switch debouncing. - limit Switch debouncing can be enabled in config.h - #127 --- Grbl_Esp32/config.h | 13 ++-- Grbl_Esp32/grbl_limits.cpp | 130 +++++++++++++++++++++---------------- Grbl_Esp32/grbl_limits.h | 5 +- 3 files changed, 82 insertions(+), 66 deletions(-) diff --git a/Grbl_Esp32/config.h b/Grbl_Esp32/config.h index 30b104cc..057affb7 100644 --- a/Grbl_Esp32/config.h +++ b/Grbl_Esp32/config.h @@ -561,14 +561,11 @@ Some features should not be changed. See notes below. // #define RX_BUFFER_SIZE 128 // (1-254) Uncomment to override defaults in serial.h // #define TX_BUFFER_SIZE 100 // (1-254) -// A simple software debouncing feature for hard limit switches. When enabled, the interrupt -// monitoring the hard limit switch pins will enable the Arduino's watchdog timer to re-check -// the limit pin state after a delay of about 32msec. This can help with CNC machines with -// problematic false triggering of their hard limit switches, but it WILL NOT fix issues with -// electrical interference on the signal cables from external sources. It's recommended to first -// use shielded signal cables with their shielding connected to ground (old USB/computer cables -// work well and are cheap to find) and wire in a low-pass circuit into each limit pin. -// #define ENABLE_SOFTWARE_DEBOUNCE // Default disabled. Uncomment to enable. +// A simple software debouncing feature for hard limit switches. When enabled, the limit +// switch interrupt unblock a waiting task which will recheck the limit switch pins after +// a short delay. Default disabled +//#define ENABLE_SOFTWARE_DEBOUNCE // Default disabled. Uncomment to enable. +#define DEBOUNCE_PERIOD 32 // in milliseconds default 32 microseconds // Configures the position after a probing cycle during Grbl's check mode. Disabled sets // the position to the probe target, when enabled sets the position to the start position. diff --git a/Grbl_Esp32/grbl_limits.cpp b/Grbl_Esp32/grbl_limits.cpp index 3fa5251e..250db8a9 100644 --- a/Grbl_Esp32/grbl_limits.cpp +++ b/Grbl_Esp32/grbl_limits.cpp @@ -27,7 +27,7 @@ #include "grbl.h" - +xQueueHandle limit_sw_queue; // used by limit switch debouncing // Homing axis search distance multiplier. Computed by this value times the cycle travel. #ifndef HOMING_AXIS_SEARCH_SCALAR @@ -39,25 +39,32 @@ void IRAM_ATTR isr_limit_switches() { - // Ignore limit switches if already in an alarm state or in-process of executing an alarm. + // Ignore limit switches if already in an alarm state or in-process of executing an alarm. // When in the alarm state, Grbl should have been reset or will force a reset, so any pending // moves in the planner and serial buffers are all cleared and newly sent blocks will be // locked out until a homing cycle or a kill lock command. Allows the user to disable the hard // limit setting if their limits are constantly triggering after a reset and move their axes. - if ( ( sys.state != STATE_ALARM) & (bit_isfalse(sys.state, STATE_HOMING)) ) { - if (!(sys_rt_exec_alarm)) { - #ifdef HARD_LIMIT_FORCE_STATE_CHECK - // Check limit pin state. - if (limits_get_state()) { - mc_reset(); // Initiate system kill. - system_set_exec_alarm(EXEC_ALARM_HARD_LIMIT); // Indicate hard limit critical event - } - #else - mc_reset(); // Initiate system kill. - system_set_exec_alarm(EXEC_ALARM_HARD_LIMIT); // Indicate hard limit critical event - #endif - } + if ( ( sys.state != STATE_ALARM) & (bit_isfalse(sys.state, STATE_HOMING)) ) { + if (!(sys_rt_exec_alarm)) { + + #ifdef ENABLE_SOFTWARE_DEBOUNCE + // we will start a task that will recheck the switches after a small delay + int evt; + xQueueSendFromISR(limit_sw_queue, &evt, NULL); + #else + #ifdef HARD_LIMIT_FORCE_STATE_CHECK + // Check limit pin state. + if (limits_get_state()) { + mc_reset(); // Initiate system kill. + system_set_exec_alarm(EXEC_ALARM_HARD_LIMIT); // Indicate hard limit critical event + } + #else + mc_reset(); // Initiate system kill. + system_set_exec_alarm(EXEC_ALARM_HARD_LIMIT); // Indicate hard limit critical event + #endif + #endif + } } } @@ -300,15 +307,17 @@ void limits_init() limits_disable(); } - -// TODO Debounce -/* - #ifdef ENABLE_SOFTWARE_DEBOUNCE - MCUSR &= ~(1<