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

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