2016-04-27 14:15:20 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-04-27 14:15:20 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-04-27 14:15:20 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 08:25:55 +00:00
|
|
|
#pragma once
|
2016-04-27 14:15:20 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-10 11:56:04 +00:00
|
|
|
* endstops.h - manages endstops
|
2016-04-27 14:15:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-18 10:55:09 +00:00
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-03-07 04:35:22 +00:00
|
|
|
enum EndstopEnum : char {
|
2019-07-24 06:52:36 +00:00
|
|
|
X_MIN, Y_MIN, Z_MIN, Z_MIN_PROBE,
|
|
|
|
X_MAX, Y_MAX, Z_MAX,
|
|
|
|
X2_MIN, X2_MAX,
|
|
|
|
Y2_MIN, Y2_MAX,
|
|
|
|
Z2_MIN, Z2_MAX,
|
2020-01-20 05:35:07 +00:00
|
|
|
Z3_MIN, Z3_MAX,
|
|
|
|
Z4_MIN, Z4_MAX
|
2017-09-18 10:55:09 +00:00
|
|
|
};
|
2016-04-27 14:15:20 +00:00
|
|
|
|
2020-07-12 18:16:56 +00:00
|
|
|
#define X_ENDSTOP (X_HOME_DIR < 0 ? X_MIN : X_MAX)
|
|
|
|
#define Y_ENDSTOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
|
|
|
|
#define Z_ENDSTOP (Z_HOME_DIR < 0 ? TERN(HOMING_Z_WITH_PROBE, Z_MIN, Z_MIN_PROBE) : Z_MAX)
|
|
|
|
|
2016-04-27 14:15:20 +00:00
|
|
|
class Endstops {
|
|
|
|
public:
|
2019-02-06 10:59:22 +00:00
|
|
|
#if HAS_EXTRA_ENDSTOPS
|
2017-09-18 10:55:09 +00:00
|
|
|
typedef uint16_t esbits_t;
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(X_DUAL_ENDSTOPS, static float x2_endstop_adj);
|
|
|
|
TERN_(Y_DUAL_ENDSTOPS, static float y2_endstop_adj);
|
|
|
|
TERN_(Z_MULTI_ENDSTOPS, static float z2_endstop_adj);
|
2020-01-20 05:35:07 +00:00
|
|
|
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
|
2018-06-19 16:55:49 +00:00
|
|
|
static float z3_endstop_adj;
|
2018-03-10 11:56:04 +00:00
|
|
|
#endif
|
2020-01-20 05:35:07 +00:00
|
|
|
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
|
|
|
|
static float z4_endstop_adj;
|
|
|
|
#endif
|
2016-04-27 14:15:20 +00:00
|
|
|
#else
|
2018-05-21 20:51:38 +00:00
|
|
|
typedef uint8_t esbits_t;
|
2016-04-27 14:15:20 +00:00
|
|
|
#endif
|
2017-09-18 10:55:09 +00:00
|
|
|
|
2018-05-21 20:51:38 +00:00
|
|
|
private:
|
2018-09-24 20:56:01 +00:00
|
|
|
static bool enabled, enabled_globally;
|
2018-05-21 20:51:38 +00:00
|
|
|
static esbits_t live_state;
|
|
|
|
static volatile uint8_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
|
2018-06-22 01:14:16 +00:00
|
|
|
|
2018-09-25 01:59:12 +00:00
|
|
|
#if ENDSTOP_NOISE_THRESHOLD
|
2018-06-22 01:14:16 +00:00
|
|
|
static esbits_t validated_live_state;
|
2018-05-21 20:51:38 +00:00
|
|
|
static uint8_t endstop_poll_count; // Countdown from threshold for polling
|
|
|
|
#endif
|
2016-07-19 13:31:09 +00:00
|
|
|
|
2018-05-21 20:51:38 +00:00
|
|
|
public:
|
2018-05-16 07:08:43 +00:00
|
|
|
Endstops() {};
|
2016-04-27 14:15:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the endstop pins
|
|
|
|
*/
|
2017-09-06 11:28:32 +00:00
|
|
|
static void init();
|
2016-04-27 14:15:20 +00:00
|
|
|
|
2018-05-16 07:08:43 +00:00
|
|
|
/**
|
2018-06-22 01:14:16 +00:00
|
|
|
* Are endstops or the probe set to abort the move?
|
2018-05-16 07:08:43 +00:00
|
|
|
*/
|
2018-06-22 01:14:16 +00:00
|
|
|
FORCE_INLINE static bool abort_enabled() {
|
2020-04-22 21:35:03 +00:00
|
|
|
return enabled || TERN0(HAS_BED_PROBE, z_probe_enabled);
|
2018-06-22 01:14:16 +00:00
|
|
|
}
|
2018-05-16 07:08:43 +00:00
|
|
|
|
2019-02-06 23:20:17 +00:00
|
|
|
static inline bool global_enabled() { return enabled_globally; }
|
|
|
|
|
2018-05-16 07:08:43 +00:00
|
|
|
/**
|
|
|
|
* Periodic call to poll endstops if required. Called from temperature ISR
|
|
|
|
*/
|
|
|
|
static void poll();
|
|
|
|
|
2016-04-27 14:15:20 +00:00
|
|
|
/**
|
2018-06-22 01:14:16 +00:00
|
|
|
* Update endstops bits from the pins. Apply filtering to get a verified state.
|
|
|
|
* If abort_enabled() and moving towards a triggered switch, abort the current move.
|
|
|
|
* Called from ISR contexts.
|
2016-04-27 14:15:20 +00:00
|
|
|
*/
|
2016-05-26 18:01:20 +00:00
|
|
|
static void update();
|
2016-04-27 14:15:20 +00:00
|
|
|
|
2018-05-21 20:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get Endstop hit state.
|
|
|
|
*/
|
|
|
|
FORCE_INLINE static uint8_t trigger_state() { return hit_state; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current endstops state
|
|
|
|
*/
|
2018-06-02 00:02:22 +00:00
|
|
|
FORCE_INLINE static esbits_t state() {
|
|
|
|
return
|
2018-09-25 01:59:12 +00:00
|
|
|
#if ENDSTOP_NOISE_THRESHOLD
|
2018-06-02 00:02:22 +00:00
|
|
|
validated_live_state
|
|
|
|
#else
|
|
|
|
live_state
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2018-05-21 20:51:38 +00:00
|
|
|
|
2016-04-27 14:15:20 +00:00
|
|
|
/**
|
2018-05-25 00:28:15 +00:00
|
|
|
* Report endstop hits to serial. Called from loop().
|
2016-04-27 14:15:20 +00:00
|
|
|
*/
|
2018-07-11 22:33:26 +00:00
|
|
|
static void event_handler();
|
2016-04-27 14:15:20 +00:00
|
|
|
|
2016-04-27 21:46:24 +00:00
|
|
|
/**
|
2019-10-30 20:07:24 +00:00
|
|
|
* Report endstop states in response to M119
|
2016-04-27 21:46:24 +00:00
|
|
|
*/
|
2019-10-30 20:07:24 +00:00
|
|
|
static void report_states();
|
2016-04-27 21:46:24 +00:00
|
|
|
|
2016-04-27 14:15:20 +00:00
|
|
|
// Enable / disable endstop checking globally
|
2018-05-16 07:08:43 +00:00
|
|
|
static void enable_globally(const bool onoff=true);
|
2016-04-27 14:15:20 +00:00
|
|
|
|
|
|
|
// Enable / disable endstop checking
|
2018-05-16 07:08:43 +00:00
|
|
|
static void enable(const bool onoff=true);
|
2016-04-27 14:15:20 +00:00
|
|
|
|
|
|
|
// Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
|
2018-05-16 07:08:43 +00:00
|
|
|
static void not_homing();
|
2016-04-27 14:15:20 +00:00
|
|
|
|
2018-08-07 04:11:37 +00:00
|
|
|
#if ENABLED(VALIDATE_HOMING_ENDSTOPS)
|
|
|
|
// If the last move failed to trigger an endstop, call kill
|
|
|
|
static void validate_homing_move();
|
|
|
|
#else
|
|
|
|
FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
|
|
|
|
#endif
|
2018-07-01 02:54:07 +00:00
|
|
|
|
2016-04-27 14:15:20 +00:00
|
|
|
// Clear endstops (i.e., they were hit intentionally) to suppress the report
|
2018-05-25 00:28:15 +00:00
|
|
|
FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
|
2016-04-27 14:15:20 +00:00
|
|
|
|
|
|
|
// Enable / disable endstop z-probe checking
|
2016-05-04 03:15:18 +00:00
|
|
|
#if HAS_BED_PROBE
|
2016-05-26 18:01:20 +00:00
|
|
|
static volatile bool z_probe_enabled;
|
2018-06-13 06:22:02 +00:00
|
|
|
static void enable_z_probe(const bool onoff=true);
|
2016-04-27 14:15:20 +00:00
|
|
|
#endif
|
2016-04-28 09:15:53 +00:00
|
|
|
|
2018-11-13 10:31:58 +00:00
|
|
|
static void resync();
|
|
|
|
|
2017-09-18 10:55:09 +00:00
|
|
|
// Debugging of endstops
|
|
|
|
#if ENABLED(PINS_DEBUGGING)
|
|
|
|
static bool monitor_flag;
|
|
|
|
static void monitor();
|
2018-05-16 07:08:43 +00:00
|
|
|
static void run_monitor();
|
2017-09-18 10:55:09 +00:00
|
|
|
#endif
|
2019-08-05 03:22:58 +00:00
|
|
|
|
|
|
|
#if ENABLED(SPI_ENDSTOPS)
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
bool any;
|
|
|
|
struct { bool x:1, y:1, z:1; };
|
|
|
|
};
|
|
|
|
} tmc_spi_homing_t;
|
|
|
|
static tmc_spi_homing_t tmc_spi_homing;
|
|
|
|
static void clear_endstop_state();
|
|
|
|
static bool tmc_spi_homing_check();
|
|
|
|
#endif
|
2016-04-27 14:15:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Endstops endstops;
|
2019-02-06 23:20:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A class to save and change the endstop state,
|
|
|
|
* then restore it when it goes out of scope.
|
|
|
|
*/
|
|
|
|
class TemporaryGlobalEndstopsState {
|
|
|
|
bool saved;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
|
|
|
|
endstops.enable_globally(enable);
|
|
|
|
}
|
|
|
|
~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
|
|
|
|
};
|