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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #if ENABLED(PREHEAT_BEFORE_LEVELING)
  43. #ifndef LEVELING_NOZZLE_TEMP
  44. #define LEVELING_NOZZLE_TEMP 0
  45. #endif
  46. #ifndef LEVELING_BED_TEMP
  47. #define LEVELING_BED_TEMP 0
  48. #endif
  49. #endif
  50. class Probe {
  51. public:
  52. #if ENABLED(SENSORLESS_PROBING)
  53. typedef struct { bool x:1, y:1, z:1; } sense_bool_t;
  54. static sense_bool_t test_sensitivity;
  55. #endif
  56. #if HAS_BED_PROBE
  57. static xyz_pos_t offset;
  58. #if EITHER(PREHEAT_BEFORE_PROBING, PREHEAT_BEFORE_LEVELING)
  59. static void preheat_for_probing(const celsius_t hotend_temp, const celsius_t bed_temp);
  60. #endif
  61. static bool set_deployed(const bool deploy);
  62. #if IS_KINEMATIC
  63. #if HAS_PROBE_XY_OFFSET
  64. // Return true if the both nozzle and the probe can reach the given point.
  65. // Note: This won't work on SCARA since the probe offset rotates with the arm.
  66. static bool can_reach(const_float_t rx, const_float_t ry) {
  67. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y) // The nozzle can go where it needs to go?
  68. && position_is_reachable(rx, ry, ABS(PROBING_MARGIN)); // Can the nozzle also go near there?
  69. }
  70. #else
  71. static bool can_reach(const_float_t rx, const_float_t ry) {
  72. return position_is_reachable(rx, ry, PROBING_MARGIN);
  73. }
  74. #endif
  75. #else
  76. /**
  77. * Return whether the given position is within the bed, and whether the nozzle
  78. * can reach the position required to put the probe at the given position.
  79. *
  80. * Example: For a probe offset of -10,+10, then for the probe to reach 0,0 the
  81. * nozzle must be be able to reach +10,-10.
  82. */
  83. static bool can_reach(const_float_t rx, const_float_t ry) {
  84. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y)
  85. && COORDINATE_OKAY(rx, min_x() - fslop, max_x() + fslop)
  86. && COORDINATE_OKAY(ry, min_y() - fslop, max_y() + fslop);
  87. }
  88. #endif
  89. static void move_z_after_probing() {
  90. #ifdef Z_AFTER_PROBING
  91. do_z_clearance(Z_AFTER_PROBING, true); // Move down still permitted
  92. #endif
  93. }
  94. 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);
  95. 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) {
  96. return probe_at_point(pos.x, pos.y, raise_after, verbose_level, probe_relative, sanity_check);
  97. }
  98. #else
  99. static constexpr xyz_pos_t offset = xyz_pos_t(LINEAR_AXIS_ARRAY(0, 0, 0, 0, 0, 0)); // See #16767
  100. static bool set_deployed(const bool) { return false; }
  101. static bool can_reach(const_float_t rx, const_float_t ry) { return position_is_reachable(rx, ry); }
  102. #endif
  103. static void move_z_after_homing() {
  104. #ifdef Z_AFTER_HOMING
  105. do_z_clearance(Z_AFTER_HOMING, true);
  106. #elif BOTH(Z_AFTER_PROBING, HAS_BED_PROBE)
  107. move_z_after_probing();
  108. #endif
  109. }
  110. static bool can_reach(const xy_pos_t &pos) { return can_reach(pos.x, pos.y); }
  111. static bool good_bounds(const xy_pos_t &lf, const xy_pos_t &rb) {
  112. return (
  113. #if IS_KINEMATIC
  114. can_reach(lf.x, 0) && can_reach(rb.x, 0) && can_reach(0, lf.y) && can_reach(0, rb.y)
  115. #else
  116. can_reach(lf) && can_reach(rb)
  117. #endif
  118. );
  119. }
  120. // Use offset_xy for read only access
  121. // More optimal the XY offset is known to always be zero.
  122. #if HAS_PROBE_XY_OFFSET
  123. static const xy_pos_t &offset_xy;
  124. #else
  125. static constexpr xy_pos_t offset_xy = xy_pos_t({ 0, 0 }); // See #16767
  126. #endif
  127. static bool deploy() { return set_deployed(true); }
  128. static bool stow() { return set_deployed(false); }
  129. #if HAS_BED_PROBE || HAS_LEVELING
  130. #if IS_KINEMATIC
  131. static constexpr float printable_radius = (
  132. TERN_(DELTA, DELTA_PRINTABLE_RADIUS)
  133. TERN_(IS_SCARA, SCARA_PRINTABLE_RADIUS)
  134. );
  135. static constexpr float probe_radius(const xy_pos_t &probe_offset_xy = offset_xy) {
  136. return printable_radius - _MAX(PROBING_MARGIN, HYPOT(probe_offset_xy.x, probe_offset_xy.y));
  137. }
  138. #endif
  139. static constexpr float _min_x(const xy_pos_t &probe_offset_xy = offset_xy) {
  140. return TERN(IS_KINEMATIC,
  141. (X_CENTER) - probe_radius(probe_offset_xy),
  142. _MAX((X_MIN_BED) + (PROBING_MARGIN_LEFT), (X_MIN_POS) + probe_offset_xy.x)
  143. );
  144. }
  145. static constexpr float _max_x(const xy_pos_t &probe_offset_xy = offset_xy) {
  146. return TERN(IS_KINEMATIC,
  147. (X_CENTER) + probe_radius(probe_offset_xy),
  148. _MIN((X_MAX_BED) - (PROBING_MARGIN_RIGHT), (X_MAX_POS) + probe_offset_xy.x)
  149. );
  150. }
  151. static constexpr float _min_y(const xy_pos_t &probe_offset_xy = offset_xy) {
  152. return TERN(IS_KINEMATIC,
  153. (Y_CENTER) - probe_radius(probe_offset_xy),
  154. _MAX((Y_MIN_BED) + (PROBING_MARGIN_FRONT), (Y_MIN_POS) + probe_offset_xy.y)
  155. );
  156. }
  157. static constexpr float _max_y(const xy_pos_t &probe_offset_xy = offset_xy) {
  158. return TERN(IS_KINEMATIC,
  159. (Y_CENTER) + probe_radius(probe_offset_xy),
  160. _MIN((Y_MAX_BED) - (PROBING_MARGIN_BACK), (Y_MAX_POS) + probe_offset_xy.y)
  161. );
  162. }
  163. static float min_x() { return _min_x() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.x)); }
  164. static float max_x() { return _max_x() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.x)); }
  165. static float min_y() { return _min_y() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.y)); }
  166. static float max_y() { return _max_y() TERN_(NOZZLE_AS_PROBE, TERN_(HAS_HOME_OFFSET, - home_offset.y)); }
  167. // constexpr helpers used in build-time static_asserts, relying on default probe offsets.
  168. class build_time {
  169. static constexpr xyz_pos_t default_probe_xyz_offset =
  170. #if HAS_BED_PROBE
  171. NOZZLE_TO_PROBE_OFFSET
  172. #else
  173. { 0 }
  174. #endif
  175. ;
  176. static constexpr xy_pos_t default_probe_xy_offset = { default_probe_xyz_offset.x, default_probe_xyz_offset.y };
  177. public:
  178. static constexpr bool can_reach(float x, float y) {
  179. #if IS_KINEMATIC
  180. return HYPOT2(x, y) <= sq(probe_radius(default_probe_xy_offset));
  181. #else
  182. return COORDINATE_OKAY(x, _min_x(default_probe_xy_offset) - fslop, _max_x(default_probe_xy_offset) + fslop)
  183. && COORDINATE_OKAY(y, _min_y(default_probe_xy_offset) - fslop, _max_y(default_probe_xy_offset) + fslop);
  184. #endif
  185. }
  186. static constexpr bool can_reach(const xy_pos_t &point) { return can_reach(point.x, point.y); }
  187. };
  188. #if NEEDS_THREE_PROBE_POINTS
  189. // Retrieve three points to probe the bed. Any type exposing set(X,Y) may be used.
  190. template <typename T>
  191. static void get_three_points(T points[3]) {
  192. #if HAS_FIXED_3POINT
  193. #define VALIDATE_PROBE_PT(N) static_assert(Probe::build_time::can_reach(xy_pos_t{PROBE_PT_##N##_X, PROBE_PT_##N##_Y}), \
  194. "PROBE_PT_" STRINGIFY(N) "_(X|Y) is unreachable using default NOZZLE_TO_PROBE_OFFSET and PROBING_MARGIN");
  195. VALIDATE_PROBE_PT(1); VALIDATE_PROBE_PT(2); VALIDATE_PROBE_PT(3);
  196. points[0] = xy_float_t({ PROBE_PT_1_X, PROBE_PT_1_Y });
  197. points[1] = xy_float_t({ PROBE_PT_2_X, PROBE_PT_2_Y });
  198. points[2] = xy_float_t({ PROBE_PT_3_X, PROBE_PT_3_Y });
  199. #else
  200. #if IS_KINEMATIC
  201. constexpr float SIN0 = 0.0, SIN120 = 0.866025, SIN240 = -0.866025,
  202. COS0 = 1.0, COS120 = -0.5 , COS240 = -0.5;
  203. points[0] = xy_float_t({ (X_CENTER) + probe_radius() * COS0, (Y_CENTER) + probe_radius() * SIN0 });
  204. points[1] = xy_float_t({ (X_CENTER) + probe_radius() * COS120, (Y_CENTER) + probe_radius() * SIN120 });
  205. points[2] = xy_float_t({ (X_CENTER) + probe_radius() * COS240, (Y_CENTER) + probe_radius() * SIN240 });
  206. #else
  207. points[0] = xy_float_t({ min_x(), min_y() });
  208. points[1] = xy_float_t({ max_x(), min_y() });
  209. points[2] = xy_float_t({ (min_x() + max_x()) / 2, max_y() });
  210. #endif
  211. #endif
  212. }
  213. #endif
  214. #endif // HAS_BED_PROBE
  215. #if HAS_Z_SERVO_PROBE
  216. static void servo_probe_init();
  217. #endif
  218. #if HAS_QUIET_PROBING
  219. static void set_probing_paused(const bool p);
  220. #endif
  221. #if ENABLED(PROBE_TARE)
  222. static void tare_init();
  223. static bool tare();
  224. #endif
  225. // Basic functions for Sensorless Homing and Probing
  226. #if EITHER(SENSORLESS_HOMING, SENSORLESS_PROBING)
  227. static void enable_stallguard_diag1();
  228. static void disable_stallguard_diag1();
  229. static void set_homing_current(const bool onoff);
  230. #endif
  231. private:
  232. static bool probe_down_to_z(const_float_t z, const_feedRate_t fr_mm_s);
  233. static void do_z_raise(const float z_raise);
  234. static float run_z_probe(const bool sanity_check=true);
  235. };
  236. extern Probe probe;