Commit f4301419 authored by bdring's avatar bdring
Browse files

Added limit switch debouncing.

- limit Switch debouncing can be enabled in config.h
- #127
parent 228e7983
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -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.
// 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.
+74 −56
Original line number Diff line number Diff line
@@ -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
@@ -47,6 +47,12 @@ void IRAM_ATTR isr_limit_switches()

	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()) {
@@ -57,6 +63,7 @@ void IRAM_ATTR isr_limit_switches()
				  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();
  }
  
	// setup task used for debouncing
	limit_sw_queue = xQueueCreate(10, sizeof( int ));
	
	xTaskCreate(limitCheckTask, 
				"limitCheckTask", 
				2048, 
				NULL, 
				5, // priority 
				NULL);
								
								
// TODO Debounce
/*
  #ifdef ENABLE_SOFTWARE_DEBOUNCE
    MCUSR &= ~(1<<WDRF);
    WDTCSR |= (1<<WDCE) | (1<<WDE);
    WDTCSR = (1<<WDP0); // Set time-out at ~32msec.
  #endif
 */ 
}


@@ -332,9 +341,11 @@ uint8_t limits_get_state()
	#ifdef X_LIMIT_PIN
		pin += digitalRead(X_LIMIT_PIN);
	#endif
	
	#ifdef Y_LIMIT_PIN
		pin += (digitalRead(Y_LIMIT_PIN) << Y_AXIS);
	#endif
	
	#ifdef Z_LIMIT_PIN
		pin += (digitalRead(Z_LIMIT_PIN) << Z_AXIS);
	#endif
@@ -343,8 +354,7 @@ uint8_t limits_get_state()
		pin ^= INVERT_LIMIT_PIN_MASK;
	#endif	
  
  if (bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) 
	{ 
	if (bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) { 
		pin ^= LIMIT_MASK;
	}
  
@@ -356,16 +366,8 @@ uint8_t limits_get_state()
	}
	
	return(limit_state);	
  
	
}







// Performs a soft limit check. Called from mc_line() only. Assumes the machine has been homed,
// the workspace volume is in all negative space, and the system is in normal operation.
// NOTE: Used by jogging to limit travel within soft-limit volume.
@@ -383,6 +385,7 @@ void limits_soft_check(float *target)
			if (sys.abort) { return; }
		} while ( sys.state != STATE_IDLE );
    }
	
    mc_reset(); // Issue system reset and ensure spindle and coolant are shutdown.
    system_set_exec_alarm(EXEC_ALARM_SOFT_LIMIT); // Indicate soft limit critical event
    protocol_execute_realtime(); // Execute to enter critical event loop and system abort
@@ -390,6 +393,21 @@ void limits_soft_check(float *target)
  }
}

// this is the task
void limitCheckTask(void *pvParameters)
{	
	while(true) {
		int evt;
		xQueueReceive(limit_sw_queue, &evt, portMAX_DELAY); // block until receive queue
		vTaskDelay( DEBOUNCE_PERIOD / portTICK_PERIOD_MS ); // delay a while
		
		if (limits_get_state()) {
			mc_reset(); // Initiate system kill.
			system_set_exec_alarm(EXEC_ALARM_HARD_LIMIT); // Indicate hard limit critical event
		}		
	}
}

// return true if the axis is defined as a squared axis
// Squaring: is used on gantry type axes that have two motors
// Each motor with touch off its own switch to square the axis
+3 −2
Original line number Diff line number Diff line
@@ -32,8 +32,6 @@
#define SQUARING_MODE_A			1  // A motor runs
#define SQUARING_MODE_B			2  // B motor runs



// Initialize the limits module
void limits_init();

@@ -53,4 +51,7 @@ void isr_limit_switches();

bool axis_is_squared(uint8_t axis_mask);

// A task that runs after a limit switch interrupt.
void limitCheckTask(void *pvParameters); 

#endif
 No newline at end of file