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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_RAISE, // Raise to "between" clearance after run_z_probe
  33. PROBE_PT_BIG_RAISE // Raise to big clearance after run_z_probe
  34. };
  35. #endif
  36. class Probe {
  37. public:
  38. #if HAS_BED_PROBE
  39. static xyz_pos_t offset;
  40. static bool set_deployed(const bool deploy);
  41. #if IS_KINEMATIC
  42. #if HAS_PROBE_XY_OFFSET
  43. // Return true if the both nozzle and the probe can reach the given point.
  44. // Note: This won't work on SCARA since the probe offset rotates with the arm.
  45. static inline bool can_reach(const float &rx, const float &ry) {
  46. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y) // The nozzle can go where it needs to go?
  47. && position_is_reachable(rx, ry, ABS(PROBING_MARGIN)); // Can the nozzle also go near there?
  48. }
  49. #else
  50. FORCE_INLINE static bool can_reach(const float &rx, const float &ry) {
  51. return position_is_reachable(rx, ry, PROBING_MARGIN);
  52. }
  53. #endif
  54. #else
  55. /**
  56. * Return whether the given position is within the bed, and whether the nozzle
  57. * can reach the position required to put the probe at the given position.
  58. *
  59. * Example: For a probe offset of -10,+10, then for the probe to reach 0,0 the
  60. * nozzle must be be able to reach +10,-10.
  61. */
  62. static inline bool can_reach(const float &rx, const float &ry) {
  63. return position_is_reachable(rx - offset_xy.x, ry - offset_xy.y)
  64. && WITHIN(rx, min_x() - fslop, max_x() + fslop)
  65. && WITHIN(ry, min_y() - fslop, max_y() + fslop);
  66. }
  67. #endif
  68. static inline void move_z_after_probing() {
  69. #ifdef Z_AFTER_PROBING
  70. do_z_clearance(Z_AFTER_PROBING, true, true, true); // Move down still permitted
  71. #endif
  72. }
  73. static float probe_at_point(const float &rx, const float &ry, const ProbePtRaise raise_after=PROBE_PT_NONE, const uint8_t verbose_level=0, const bool probe_relative=true, const bool sanity_check=true);
  74. static inline 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) {
  75. return probe_at_point(pos.x, pos.y, raise_after, verbose_level, probe_relative, sanity_check);
  76. }
  77. #else
  78. static constexpr xyz_pos_t offset = xyz_pos_t({ 0, 0, 0 }); // See #16767
  79. static bool set_deployed(const bool) { return false; }
  80. FORCE_INLINE static bool can_reach(const float &rx, const float &ry) { return position_is_reachable(rx, ry); }
  81. #endif
  82. static inline void move_z_after_homing() {
  83. #ifdef Z_AFTER_HOMING
  84. do_z_clearance(Z_AFTER_HOMING, true, true, true);
  85. #elif BOTH(Z_AFTER_PROBING,HAS_BED_PROBE)
  86. move_z_after_probing();
  87. #endif
  88. }
  89. FORCE_INLINE static bool can_reach(const xy_pos_t &pos) { return can_reach(pos.x, pos.y); }
  90. FORCE_INLINE static bool good_bounds(const xy_pos_t &lf, const xy_pos_t &rb) {
  91. return (
  92. #if IS_KINEMATIC
  93. can_reach(lf.x, 0) && can_reach(rb.x, 0) && can_reach(0, lf.y) && can_reach(0, rb.y)
  94. #else
  95. can_reach(lf) && can_reach(rb)
  96. #endif
  97. );
  98. }
  99. // Use offset_xy for read only access
  100. // More optimal the XY offset is known to always be zero.
  101. #if HAS_PROBE_XY_OFFSET
  102. static const xyz_pos_t &offset_xy;
  103. #else
  104. static constexpr xy_pos_t offset_xy = xy_pos_t({ 0, 0 }); // See #16767
  105. #endif
  106. static inline bool deploy() { return set_deployed(true); }
  107. static inline bool stow() { return set_deployed(false); }
  108. #if HAS_BED_PROBE || HAS_LEVELING
  109. #if IS_KINEMATIC
  110. static constexpr float printable_radius = (
  111. TERN_(DELTA, DELTA_PRINTABLE_RADIUS)
  112. TERN_(IS_SCARA, SCARA_PRINTABLE_RADIUS)
  113. );
  114. static inline float probe_radius() {
  115. return printable_radius - _MAX(PROBING_MARGIN, HYPOT(offset_xy.x, offset_xy.y));
  116. }
  117. #endif
  118. static inline float min_x() {
  119. return TERN(IS_KINEMATIC,
  120. (X_CENTER) - probe_radius(),
  121. _MAX((X_MIN_BED) + (PROBING_MARGIN_LEFT), (X_MIN_POS) + offset_xy.x)
  122. ) - TERN0(NOZZLE_AS_PROBE, TERN0(HAS_HOME_OFFSET, home_offset.x));
  123. }
  124. static inline float max_x() {
  125. return TERN(IS_KINEMATIC,
  126. (X_CENTER) + probe_radius(),
  127. _MIN((X_MAX_BED) - (PROBING_MARGIN_RIGHT), (X_MAX_POS) + offset_xy.x)
  128. ) - TERN0(NOZZLE_AS_PROBE, TERN0(HAS_HOME_OFFSET, home_offset.x));
  129. }
  130. static inline float min_y() {
  131. return TERN(IS_KINEMATIC,
  132. (Y_CENTER) - probe_radius(),
  133. _MAX((Y_MIN_BED) + (PROBING_MARGIN_FRONT), (Y_MIN_POS) + offset_xy.y)
  134. ) - TERN0(NOZZLE_AS_PROBE, TERN0(HAS_HOME_OFFSET, home_offset.y));
  135. }
  136. static inline float max_y() {
  137. return TERN(IS_KINEMATIC,
  138. (Y_CENTER) + probe_radius(),
  139. _MIN((Y_MAX_BED) - (PROBING_MARGIN_BACK), (Y_MAX_POS) + offset_xy.y)
  140. ) - TERN0(NOZZLE_AS_PROBE, TERN0(HAS_HOME_OFFSET, home_offset.y));
  141. }
  142. #if NEEDS_THREE_PROBE_POINTS
  143. // Retrieve three points to probe the bed. Any type exposing set(X,Y) may be used.
  144. template <typename T>
  145. static inline void get_three_points(T points[3]) {
  146. #if HAS_FIXED_3POINT
  147. points[0].set(PROBE_PT_1_X, PROBE_PT_1_Y);
  148. points[1].set(PROBE_PT_2_X, PROBE_PT_2_Y);
  149. points[2].set(PROBE_PT_3_X, PROBE_PT_3_Y);
  150. #else
  151. #if IS_KINEMATIC
  152. constexpr float SIN0 = 0.0, SIN120 = 0.866025, SIN240 = -0.866025,
  153. COS0 = 1.0, COS120 = -0.5 , COS240 = -0.5;
  154. points[0].set((X_CENTER) + probe_radius() * COS0, (Y_CENTER) + probe_radius() * SIN0);
  155. points[1].set((X_CENTER) + probe_radius() * COS120, (Y_CENTER) + probe_radius() * SIN120);
  156. points[2].set((X_CENTER) + probe_radius() * COS240, (Y_CENTER) + probe_radius() * SIN240);
  157. #else
  158. points[0].set(min_x(), min_y());
  159. points[1].set(max_x(), min_y());
  160. points[2].set((min_x() + max_x()) / 2, max_y());
  161. #endif
  162. #endif
  163. }
  164. #endif
  165. #endif // HAS_BED_PROBE
  166. #if HAS_Z_SERVO_PROBE
  167. static void servo_probe_init();
  168. #endif
  169. #if QUIET_PROBING
  170. static void set_probing_paused(const bool p);
  171. #endif
  172. private:
  173. static bool probe_down_to_z(const float z, const feedRate_t fr_mm_s);
  174. static void do_z_raise(const float z_raise);
  175. static float run_z_probe(const bool sanity_check=true);
  176. };
  177. extern Probe probe;