Prusa MINI Firmware overview
endstops.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 /**
25  * endstops.h - manages endstops
26  */
27 
28 #include "../inc/MarlinConfig.h"
29 #include <stdint.h>
30 
31 enum EndstopEnum : char {
38 };
39 
40 class Endstops {
41  public:
42  #if HAS_EXTRA_ENDSTOPS
43  typedef uint16_t esbits_t;
44  #if ENABLED(X_DUAL_ENDSTOPS)
45  static float x2_endstop_adj;
46  #endif
47  #if ENABLED(Y_DUAL_ENDSTOPS)
48  static float y2_endstop_adj;
49  #endif
50  #if Z_MULTI_ENDSTOPS
51  static float z2_endstop_adj;
52  #endif
53  #if ENABLED(Z_TRIPLE_ENDSTOPS)
54  static float z3_endstop_adj;
55  #endif
56  #else
57  typedef uint8_t esbits_t;
58  #endif
59 
60  private:
61  static bool enabled, enabled_globally;
62  static esbits_t live_state;
63  static volatile uint8_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
64 
65  #if ENDSTOP_NOISE_THRESHOLD
66  static esbits_t validated_live_state;
67  static uint8_t endstop_poll_count; // Countdown from threshold for polling
68  #endif
69 
70  public:
71  Endstops() {};
72 
73  /**
74  * Initialize the endstop pins
75  */
76  static void init();
77 
78  /**
79  * Are endstops or the probe set to abort the move?
80  */
81  FORCE_INLINE static bool abort_enabled() {
82  return (enabled
83  #if HAS_BED_PROBE
84  || z_probe_enabled
85  #endif
86  );
87  }
88 
89  static inline bool global_enabled() { return enabled_globally; }
90 
91  /**
92  * Periodic call to poll endstops if required. Called from temperature ISR
93  */
94  static void poll();
95 
96  /**
97  * Update endstops bits from the pins. Apply filtering to get a verified state.
98  * If abort_enabled() and moving towards a triggered switch, abort the current move.
99  * Called from ISR contexts.
100  */
101  static void update();
102 
103  /**
104  * Get Endstop hit state.
105  */
106  FORCE_INLINE static uint8_t trigger_state() { return hit_state; }
107 
108  /**
109  * Get current endstops state
110  */
112  return
113  #if ENDSTOP_NOISE_THRESHOLD
114  validated_live_state
115  #else
116  live_state
117  #endif
118  ;
119  }
120 
121  /**
122  * Report endstop hits to serial. Called from loop().
123  */
124  static void event_handler();
125 
126  /**
127  * Report endstop positions in response to M119
128  */
129  static void M119();
130 
131  // Enable / disable endstop checking globally
132  static void enable_globally(const bool onoff=true);
133 
134  // Enable / disable endstop checking
135  static void enable(const bool onoff=true);
136 
137  // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
138  static void not_homing();
139 
140  #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
141  // If the last move failed to trigger an endstop, call kill
142  static void validate_homing_move();
143  #else
145  #endif
146 
147  // Clear endstops (i.e., they were hit intentionally) to suppress the report
148  FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
149 
150  // Enable / disable endstop z-probe checking
151  #if HAS_BED_PROBE
152  static volatile bool z_probe_enabled;
153  static void enable_z_probe(const bool onoff=true);
154  #endif
155 
156  static void resync();
157 
158  // Debugging of endstops
159  #if ENABLED(PINS_DEBUGGING)
160  static bool monitor_flag;
161  static void monitor();
162  static void run_monitor();
163  #endif
164 
165  #if ENABLED(SPI_ENDSTOPS)
166  typedef struct {
167  union {
168  bool any;
169  struct { bool x:1, y:1, z:1; };
170  };
171  } tmc_spi_homing_t;
172  static tmc_spi_homing_t tmc_spi_homing;
173  static void clear_endstop_state();
174  static bool tmc_spi_homing_check();
175  #endif
176 };
177 
178 extern Endstops endstops;
179 
180 /**
181  * A class to save and change the endstop state,
182  * then restore it when it goes out of scope.
183  */
185  bool saved;
186 
187  public:
188  TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
189  endstops.enable_globally(enable);
190  }
192 };
GET_TEXT
#define GET_TEXT(MSG)
Definition: multi_language.h:72
SERIAL_CHAR
#define SERIAL_CHAR(x)
Definition: serial.h:69
endstops.h
Z2_MIN
Definition: endstops.h:36
Endstops::resync
static void resync()
Definition: endstops.cpp:318
_ENDSTOP
#define _ENDSTOP(AXIS, MINMAX)
Definition: endstops.cpp:491
_O2
#define _O2
Definition: macros.h:45
temperature.h
PROCESS_ENDSTOP
#define PROCESS_ENDSTOP(AXIS, MINMAX)
Z_AXIS_HEAD
#define Z_AXIS_HEAD
_ENDSTOP_HIT_ECHO
#define _ENDSTOP_HIT_ECHO(A, C)
MSG_Z_PROBE
#define MSG_Z_PROBE
Definition: language.h:198
Endstops::abort_enabled
static FORCE_INLINE bool abort_enabled()
Definition: endstops.h:81
Endstops::Endstops
Endstops()
Definition: endstops.h:71
Y_AXIS_HEAD
#define Y_AXIS_HEAD
SET_INPUT_PULLDOWN
#define SET_INPUT_PULLDOWN(IO)
set pin as input with pulldown wrapper
Definition: fastio.h:95
stepper.h
Stepper::axis_is_moving
static FORCE_INLINE bool axis_is_moving(const AxisEnum axis)
Definition: stepper.h:381
Endstops::state
static FORCE_INLINE esbits_t state()
Definition: endstops.h:111
printcounter.h
X_AXIS
Definition: types.h:37
stepper
Stepper stepper
Definition: stepper.cpp:82
MIN
#define MIN(a, b)
Definition: usbd_def.h:265
print_job_timer
Stopwatch print_job_timer
Definition: printcounter.cpp:63
Z2_MAX
Definition: endstops.h:36
MSG_ENDSTOPS_HIT
#define MSG_ENDSTOPS_HIT
Definition: language.h:241
extDigitalRead
#define extDigitalRead(IO)
Definition: fastio.h:92
Endstops::validate_homing_move
static FORCE_INLINE void validate_homing_move()
Definition: endstops.h:144
Stepper::motor_direction
static FORCE_INLINE bool motor_direction(const AxisEnum axis)
Definition: stepper.h:378
PGM_P
#define PGM_P
Definition: pgmspace.h:30
X_MIN
Definition: endstops.h:32
X_MAX
Definition: endstops.h:33
i
uint8_t i
Definition: screen_test_graph.c:72
Y_MAX
Definition: endstops.h:33
Y
Definition: L6470_Marlin.h:30
FIL_RUNOUT2_PIN
#define FIL_RUNOUT2_PIN
Definition: pins_GT2560_V3.h:83
Endstops::enable
static void enable(const bool onoff=true)
Definition: endstops.cpp:291
Y2_MIN
Definition: endstops.h:35
kill
void kill(PGM_P const lcd_error, PGM_P const lcd_component, const bool steppers_off)
Definition: Marlin.cpp:718
X_MIN_PIN
#define X_MIN_PIN
Definition: pins_ESP32.h:45
Endstops
Definition: endstops.h:40
BLTouch::_reset_SW_mode
static FORCE_INLINE void _reset_SW_mode()
Definition: bltouch.h:82
PROCESS_TRIPLE_ENDSTOP
#define PROCESS_TRIPLE_ENDSTOP(AXIS1, AXIS2, AXIS3, MINMAX)
SERIAL_ECHO_START
#define SERIAL_ECHO_START()
Definition: serial.h:179
Stopwatch::stop
static bool stop()
Stop the stopwatch.
Definition: stopwatch.cpp:36
MSG_ENDSTOP_HIT
#define MSG_ENDSTOP_HIT
Definition: language.h:215
Planner::endstop_triggered
static void endstop_triggered(const AxisEnum axis)
Definition: planner.cpp:1508
Z3
Definition: L6470_Marlin.h:30
_ENDSTOP_HIT
#define _ENDSTOP_HIT(AXIS, MINMAX)
Endstops::poll
static void poll()
Definition: endstops.cpp:272
MAX
#define MAX(a, b)
Definition: usbd_def.h:266
Z
Definition: L6470_Marlin.h:30
BLTouch::_set_SW_mode
static FORCE_INLINE void _set_SW_mode()
Definition: bltouch.h:81
SET_INPUT_PULLUP
#define SET_INPUT_PULLUP(IO)
Definition: fastio.h:100
Y2_MAX
Definition: endstops.h:35
pin_t
int8_t pin_t
Definition: HAL.h:65
Endstops::not_homing
static void not_homing()
Definition: endstops.cpp:297
UPDATE_ENDSTOP_BIT
#define UPDATE_ENDSTOP_BIT(AXIS, MINMAX)
bltouch
BLTouch bltouch
Z3_MAX
Definition: endstops.h:37
FORCE_INLINE
#define FORCE_INLINE
Definition: macros.h:40
PSTR
#define PSTR(str)
Definition: pgmspace.h:31
SET_INPUT
#define SET_INPUT(IO)
Definition: fastio.h:99
Z_MIN_PROBE
Definition: endstops.h:32
X2
Definition: L6470_Marlin.h:30
print_es_state
static void print_es_state(const bool is_hit, PGM_P const label=nullptr)
Definition: endstops.cpp:391
X2_MAX
Definition: endstops.h:34
Y_MIN_PIN
#define Y_MIN_PIN
Definition: pins_ESP32.h:46
X_MIN_TEST
#define X_MIN_TEST
Endstops::M119
static void M119()
Definition: endstops.cpp:398
Y_MAX_PIN
#define Y_MAX_PIN
Definition: pins_RAMPS_LINUX.h:78
ENDSTOP_HIT_TEST_Y
#define ENDSTOP_HIT_TEST_Y()
EndstopEnum
EndstopEnum
Definition: endstops.h:31
SERIAL_ECHOPGM
#define SERIAL_ECHOPGM(S)
Definition: serial.h:173
Z_MIN_PROBE_ENDSTOP_INVERTING
#define Z_MIN_PROBE_ENDSTOP_INVERTING
Definition: Configuration_A3ides_2209_MINI.h:611
Endstops::event_handler
static void event_handler()
Definition: endstops.cpp:341
Endstops::init
static void init()
Definition: endstops.cpp:94
ENDSTOP_HIT_TEST_Z
#define ENDSTOP_HIT_TEST_Z()
TemporaryGlobalEndstopsState::TemporaryGlobalEndstopsState
TemporaryGlobalEndstopsState(const bool enable)
Definition: endstops.h:188
LED_PIN
#define LED_PIN
Definition: pins_RAMPS_LINUX.h:208
TemporaryGlobalEndstopsState
Definition: endstops.h:184
NUM_RUNOUT_SENSORS
#define NUM_RUNOUT_SENSORS
Definition: pins_FORMBOT_TREX3.h:132
Temperature::disable_all_heaters
static void disable_all_heaters()
Definition: temperature.cpp:1926
Endstops::z2_endstop_adj
static float z2_endstop_adj
Definition: endstops.h:51
uint8_t
const uint8_t[]
Definition: 404_html.c:3
ES_REPORT
#define ES_REPORT(S)
ui
MarlinUI ui
_BV
#define _BV(bit)
Definition: wiring_constants.h:99
Z2
Definition: L6470_Marlin.h:30
MSG_FILAMENT_RUNOUT_SENSOR
#define MSG_FILAMENT_RUNOUT_SENSOR
Definition: language.h:199
X_MAX_TEST
#define X_MAX_TEST
FIL_RUNOUT_PIN
#define FIL_RUNOUT_PIN
Definition: pins_RAMPS_LINUX.h:216
HAS_BED_PROBE
#define HAS_BED_PROBE
Definition: Conditionals_LCD.h:500
Y_AXIS
Definition: types.h:38
CBI
#define CBI(A, B)
Definition: macros.h:89
X_MAX_PIN
#define X_MAX_PIN
Definition: pins_RAMPS_LINUX.h:75
Z_MIN_PROBE_PIN
#define Z_MIN_PROBE_PIN
Definition: pins_RAMPS_LINUX.h:86
Z_AXIS
Definition: types.h:39
TemporaryGlobalEndstopsState::~TemporaryGlobalEndstopsState
~TemporaryGlobalEndstopsState()
Definition: endstops.h:191
Z_MIN_PIN
#define Z_MIN_PIN
Definition: pins_ESP32.h:47
Z_MIN
Definition: endstops.h:32
MSG_ENDSTOP_OPEN
#define MSG_ENDSTOP_OPEN
Definition: language.h:216
COPY_LIVE_STATE
#define COPY_LIVE_STATE(SRC_BIT, DST_BIT)
Endstops::enable_globally
static void enable_globally(const bool onoff=true)
Definition: endstops.cpp:285
SERIAL_EOL
#define SERIAL_EOL()
Definition: serial.h:181
ENDSTOP_HIT_TEST_X
#define ENDSTOP_HIT_TEST_X()
quickstop_stepper
void quickstop_stepper()
Definition: Marlin.cpp:272
X2_MIN
Definition: endstops.h:34
joystick
Joystick joystick
TEST
#define TEST(n, b)
Definition: macros.h:81
Endstops::global_enabled
static bool global_enabled()
Definition: endstops.h:89
safe_delay
void safe_delay(millis_t ms)
Definition: utility.cpp:28
X_AXIS_HEAD
#define X_AXIS_HEAD
Endstops::update
static void update()
Definition: endstops.cpp:496
PROCESS_DUAL_ENDSTOP
#define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX)
Z_MAX
Definition: endstops.h:33
Z3_MIN
Definition: endstops.h:37
serialprintPGM
void serialprintPGM(PGM_P str)
Definition: serial.cpp:35
SBI
#define SBI(A, B)
Definition: macros.h:85
S_FMT
#define S_FMT
Definition: macros.h:68
SERIAL_ECHOLNPGM
#define SERIAL_ECHOLNPGM(S)
Definition: serial.h:174
Endstops::esbits_t
uint16_t esbits_t
Definition: endstops.h:43
X
Definition: L6470_Marlin.h:30
analogWrite
void analogWrite(uint32_t ulPin, uint32_t ulValue)
Definition: wiring_analog.c:12
TEST_ENDSTOP
#define TEST_ENDSTOP(ENDSTOP)
READ
#define READ(IO)
Definition: fastio.h:95
Y2
Definition: L6470_Marlin.h:30
endstops
Endstops endstops
Definition: endstops.cpp:51
Y_MIN
Definition: endstops.h:32
thermalManager
Temperature thermalManager
Definition: temperature.cpp:89
endstops
Endstops endstops
Definition: endstops.cpp:51
Endstops::hit_on_purpose
static FORCE_INLINE void hit_on_purpose()
Definition: endstops.h:148
millis_t
uint32_t millis_t
Definition: millis_t.h:26
MSG_M119_REPORT
#define MSG_M119_REPORT
Definition: language.h:212
Endstops::trigger_state
static FORCE_INLINE uint8_t trigger_state()
Definition: endstops.h:106
setup_endstop_interrupts
void setup_endstop_interrupts()
Definition: endstop_interrupts.h:105
ENABLED
#define ENABLED(V...)
Definition: macros.h:177
planner
Planner planner
Definition: planner.cpp:111
Z_MAX_PIN
#define Z_MAX_PIN
Definition: pins_RAMPS_LINUX.h:80