My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

probe.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * module/probe.h - Move, deploy, enable, etc.
  25. */
  26. #include "../inc/MarlinConfig.h"
  27. #include "motion.h"
  28. #if HAS_BED_PROBE
  29. enum ProbePtRaise : uint8_t {
  30. PROBE_PT_NONE, // No raise or stow after run_z_probe
  31. PROBE_PT_STOW, // Do a complete stow after run_z_probe
  32. PROBE_PT_LAST_STOW, // Stow for sure, even in BLTouch HS mode
  33. PROBE_PT_RAISE, // Raise to "between" clearance after run_z_probe
  34. PROBE_PT_BIG_RAISE // Raise to big clearance after run_z_probe
  35. };
  36. #endif
  37. #if USES_Z_MIN_PROBE_PIN
  38. #define PROBE_TRIGGERED() (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
  39. #else
  40. #define PROBE_TRIGGERED() (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
  41. #endif
  42. #ifdef Z_AFTER_HOMING
  43. #define Z_POST_CLEARANCE Z_AFTER_HOMING
  44. #elif defined(Z_HOMING_HEIGHT)
  45. #define Z_POST_CLEARANCE Z_HOMING_HEIGHT
  46. #else
  47. #define Z_POST_CLEARANCE 10
  48. #endif
  49. #if ENABLED(PREHEAT_BEFORE_LEVELING)
  50. #ifndef LEVELING_NOZZLE_TEMP
  51. #define LEVELING_NOZZLE_TEMP 0
  52. #endif
  53. #ifndef LEVELING_BED_TEMP
  54. #define LEVELING_BED_TEMP 0
  55. #endif
  56. #endif
  57. class Probe {
  58. public:
  59. #if ENABLED(SENSORLESS_PROBING)
  60. typedef struct { bool x:1, y:1, z:1; } sense_bool_t;
  61. static sense_bool_t test_sensitivity;
  62. #endif
  63. #if HAS_BED_PROBE
  64. static xyz_pos_t offset;
  65. #if EITHER(PREHEAT_BEFORE_PROBING, PREHEAT_BEFORE_LEVELING)
  66. static void preheat_for_probing(const celsius_t hotend_temp, const celsius_t bed_temp);
  67. #endif
  68. static void probe_error_stop();
  69. static bool set_deployed(const bool deploy);
  70. #if IS_KINEMATIC
  71. #if HAS_PROBE_XY_OFFSET
  72. // Return true if the both nozzle and the probe can reach the given point.
  73. // Note: This won't work on SCARA since the probe offset rotates with the arm.
  74. static bool can_reach(const_float_t rx, const_float_t ry, const bool probe_relative=true) {
  75. if (probe_relative) {
  76. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y) // The nozzle can go where it needs to go?
  77. && position_is_reachable(rx, ry, PROBING_MARGIN); // Can the probe also go near there?
  78. }
  79. else {
  80. return position_is_reachable(rx, ry)
  81. && position_is_reachable(rx + offset_xy.x, ry + offset_xy.y, PROBING_MARGIN);
  82. }
  83. }
  84. #else
  85. static bool can_reach(const_float_t rx, const_float_t ry, const bool=true) {
  86. return position_is_reachable(rx, ry)
  87. && position_is_reachable(rx, ry, PROBING_MARGIN);
  88. }
  89. #endif
  90. #else
  91. /**
  92. * Return whether the given position is within the bed, and whether the nozzle
  93. * can reach the position required to put the probe at the given position.
  94. *
  95. * Example: For a probe offset of -10,+10, then for the probe to reach 0,0 the
  96. * nozzle must be be able to reach +10,-10.
  97. */
  98. static bool can_reach(const_float_t rx, const_float_t ry, const bool probe_relative=true) {
  99. if (probe_relative) {
  100. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y)
  101. && COORDINATE_OKAY(rx, min_x() - fslop, max_x() + fslop)
  102. && COORDINATE_OKAY(ry, min_y() - fslop, max_y() + fslop);
  103. }
  104. else {
  105. return position_is_reachable(rx, ry)
  106. && COORDINATE_OKAY(rx + offset_xy.x, min_x() - fslop, max_x() + fslop)
  107. && COORDINATE_OKAY(ry + offset_xy.y, min_y() - fslop, max_y() + fslop);
  108. }
  109. }
  110. #endif
  111. static void move_z_after_probing() {
  112. #ifdef Z_AFTER_PROBING
  113. do_z_clearance(Z_AFTER_PROBING, true); // Move down still permitted
  114. #endif
  115. }
  116. static float probe_at_point(const_float_t rx, const_float_t ry, const ProbePtRaise raise_after=PROBE_PT_NONE, const uint8_t verbose_level=0, const bool probe_relative=true, const bool sanity_check=true);
  117. static float probe_at_point(const xy_pos_t &pos, const ProbePtRaise raise_after=PROBE_PT_NONE, const uint8_t verbose_level=0, const bool probe_relative=true, const bool sanity_check=true) {
  118. return probe_at_point(pos.x, pos.y, raise_after, verbose_level, probe_relative, sanity_check);
  119. }
  120. #else
  121. static constexpr xyz_pos_t offset = xyz_pos_t(NUM_AXIS_ARRAY(0, 0, 0, 0, 0, 0)); // See #16767
  122. static bool set_deployed(const bool) { return false; }
  123. static bool can_reach(const_float_t rx, const_float_t ry, const bool=true) { return position_is_reachable(rx, ry); }
  124. #endif
  125. static void move_z_after_homing() {
  126. #ifdef Z_AFTER_HOMING
  127. do_z_clearance(Z_AFTER_HOMING, true);
  128. #elif BOTH(Z_AFTER_PROBING, HAS_BED_PROBE)
  129. move_z_after_probing();
  130. #endif
  131. }
  132. static bool can_reach(const xy_pos_t &pos, const bool probe_relative=true) { return can_reach(pos.x, pos.y, probe_relative); }
  133. static bool good_bounds(const xy_pos_t &lf, const xy_pos_t &rb) {
  134. return (
  135. #if IS_KINEMATIC
  136. can_reach(lf.x, 0) && can_reach(rb.x, 0) && can_reach(0, lf.y) && can_reach(0, rb.y)
  137. #else
  138. can_reach(lf) && can_reach(rb)
  139. #endif
  140. );
  141. }
  142. // Use offset_xy for read only access
  143. // More optimal the XY offset is known to always be zero.
  144. #if HAS_PROBE_XY_OFFSET
  145. static const xy_pos_t &offset_xy;
  146. #else
  147. static constexpr xy_pos_t offset_xy = xy_pos_t({ 0, 0 }); // See #16767
  148. #endif
  149. static bool deploy() { return set_deployed(true); }
  150. static bool stow() { return set_deployed(false); }
  151. #if HAS_BED_PROBE || HAS_LEVELING
  152. #if IS_KINEMATIC
  153. static constexpr float printable_radius = (
  154. TERN_(DELTA, DELTA_PRINTABLE_RADIUS)
  155. TERN_(IS_SCARA, SCARA_PRINTABLE_RADIUS)
  156. );
  157. static constexpr float probe_radius(const xy_pos_t &probe_offset_xy=offset_xy) {
  158. return printable_radius - _MAX(PROBING_MARGIN, HYPOT(probe_offset_xy.x, probe_offset_xy.y));
  159. }
  160. #endif
  161. /**
  162. * The nozzle is only able to move within the physical bounds of the machine.
  163. * If the PROBE has an OFFSET Marlin may need to apply additional limits so
  164. * the probe can be prevented from going to unreachable points.
  165. *
  166. * e.g., If the PROBE is to the LEFT of the NOZZLE, it will be limited in how
  167. * close it can get the RIGHT edge of the bed (unless the nozzle is able move
  168. * far enough past the right edge).
  169. */
  170. static constexpr float _min_x(const xy_pos_t &probe_offset_xy=offset_xy) {
  171. return TERN(IS_KINEMATIC,
  172. (X_CENTER) - probe_radius(probe_offset_xy),
  173. _MAX((X_MIN_BED) + (PROBING_MARGIN_LEFT), (X_MIN_POS) + probe_offset_xy.x)
  174. );
  175. }
  176. static constexpr float _max_x(const xy_pos_t &probe_offset_xy=offset_xy) {
  177. return TERN(IS_KINEMATIC,
  178. (X_CENTER) + probe_radius(probe_offset_xy),
  179. _MIN((X_MAX_BED) - (PROBING_MARGIN_RIGHT), (X_MAX_POS) + probe_offset_xy.x)
  180. );
  181. }
  182. static constexpr float _min_y(const xy_pos_t &probe_offset_xy=offset_xy) {
  183. return TERN(IS_KINEMATIC,
  184. (Y_CENTER) - probe_radius(probe_offset_xy),
  185. _MAX((Y_MIN_BED) + (PROBING_MARGIN_FRONT), (Y_MIN_POS) + probe_offset_xy.y)
  186. );
  187. }
  188. static constexpr float _max_y(const xy_pos_t &probe_offset_xy=offset_xy) {
  189. return TERN(IS_KINEMATIC,
  190. (Y_CENTER) + probe_radius(probe_offset_xy),
  191. _MIN((Y_MAX_BED) - (PROBING_MARGIN_BACK), (Y_MAX_POS) + probe_offset_xy.y)
  192. );
  193. }
  194. static float min_x() { return _min_x() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.x)); }
  195. static float max_x() { return _max_x() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.x)); }
  196. static float min_y() { return _min_y() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.y)); }
  197. static float max_y() { return _max_y() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.y)); }
  198. // constexpr helpers used in build-time static_asserts, relying on default probe offsets.
  199. class build_time {
  200. static constexpr xyz_pos_t default_probe_xyz_offset = xyz_pos_t(
  201. #if HAS_BED_PROBE
  202. NOZZLE_TO_PROBE_OFFSET
  203. #else
  204. { 0 }
  205. #endif
  206. );
  207. static constexpr xy_pos_t default_probe_xy_offset = xy_pos_t({ default_probe_xyz_offset.x, default_probe_xyz_offset.y });
  208. public:
  209. static constexpr bool can_reach(float x, float y) {
  210. #if IS_KINEMATIC
  211. return HYPOT2(x, y) <= sq(probe_radius(default_probe_xy_offset));
  212. #else
  213. return COORDINATE_OKAY(x, _min_x(default_probe_xy_offset) - fslop, _max_x(default_probe_xy_offset) + fslop)
  214. && COORDINATE_OKAY(y, _min_y(default_probe_xy_offset) - fslop, _max_y(default_probe_xy_offset) + fslop);
  215. #endif
  216. }
  217. static constexpr bool can_reach(const xy_pos_t &point) { return can_reach(point.x, point.y); }
  218. };
  219. #if NEEDS_THREE_PROBE_POINTS
  220. // Retrieve three points to probe the bed. Any type exposing set(X,Y) may be used.
  221. template <typename T>
  222. static void get_three_points(T points[3]) {
  223. #if HAS_FIXED_3POINT
  224. #define VALIDATE_PROBE_PT(N) static_assert(Probe::build_time::can_reach(xy_pos_t{PROBE_PT_##N##_X, PROBE_PT_##N##_Y}), \
  225. "PROBE_PT_" STRINGIFY(N) "_(X|Y) is unreachable using default NOZZLE_TO_PROBE_OFFSET and PROBING_MARGIN");
  226. VALIDATE_PROBE_PT(1); VALIDATE_PROBE_PT(2); VALIDATE_PROBE_PT(3);
  227. points[0] = xy_float_t({ PROBE_PT_1_X, PROBE_PT_1_Y });
  228. points[1] = xy_float_t({ PROBE_PT_2_X, PROBE_PT_2_Y });
  229. points[2] = xy_float_t({ PROBE_PT_3_X, PROBE_PT_3_Y });
  230. #else
  231. #if IS_KINEMATIC
  232. constexpr float SIN0 = 0.0, SIN120 = 0.866025, SIN240 = -0.866025,
  233. COS0 = 1.0, COS120 = -0.5 , COS240 = -0.5;
  234. points[0] = xy_float_t({ (X_CENTER) + probe_radius() * COS0, (Y_CENTER) + probe_radius() * SIN0 });
  235. points[1] = xy_float_t({ (X_CENTER) + probe_radius() * COS120, (Y_CENTER) + probe_radius() * SIN120 });
  236. points[2] = xy_float_t({ (X_CENTER) + probe_radius() * COS240, (Y_CENTER) + probe_radius() * SIN240 });
  237. #else
  238. points[0] = xy_float_t({ min_x(), min_y() });
  239. points[1] = xy_float_t({ max_x(), min_y() });
  240. points[2] = xy_float_t({ (min_x() + max_x()) / 2, max_y() });
  241. #endif
  242. #endif
  243. }
  244. #endif
  245. #endif // HAS_BED_PROBE
  246. #if HAS_Z_SERVO_PROBE
  247. static void servo_probe_init();
  248. #endif
  249. #if HAS_QUIET_PROBING
  250. static void set_probing_paused(const bool p);
  251. #endif
  252. #if ENABLED(PROBE_TARE)
  253. static void tare_init();
  254. static bool tare();
  255. #endif
  256. // Basic functions for Sensorless Homing and Probing
  257. #if USE_SENSORLESS
  258. static void enable_stallguard_diag1();
  259. static void disable_stallguard_diag1();
  260. static void set_homing_current(const bool onoff);
  261. #endif
  262. private:
  263. static bool probe_down_to_z(const_float_t z, const_feedRate_t fr_mm_s);
  264. static void do_z_raise(const float z_raise);
  265. static float run_z_probe(const bool sanity_check=true);
  266. };
  267. extern Probe probe;