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.

menu_bed_corners.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. //
  23. // Level Bed Corners menu
  24. //
  25. #include "../../inc/MarlinConfigPre.h"
  26. #if BOTH(HAS_LCD_MENU, LEVEL_BED_CORNERS)
  27. #include "menu_item.h"
  28. #include "../../module/motion.h"
  29. #include "../../module/planner.h"
  30. #if HAS_LEVELING
  31. #include "../../feature/bedlevel/bedlevel.h"
  32. #endif
  33. #ifndef LEVEL_CORNERS_Z_HOP
  34. #define LEVEL_CORNERS_Z_HOP 4.0
  35. #endif
  36. #ifndef LEVEL_CORNERS_HEIGHT
  37. #define LEVEL_CORNERS_HEIGHT 0.0
  38. #endif
  39. #if ENABLED(LEVEL_CORNERS_USE_PROBE)
  40. #include "../../module/probe.h"
  41. #include "../../module/endstops.h"
  42. #if ENABLED(BLTOUCH)
  43. #include "../../feature/bltouch.h"
  44. #endif
  45. #ifndef LEVEL_CORNERS_PROBE_TOLERANCE
  46. #define LEVEL_CORNERS_PROBE_TOLERANCE 0.2
  47. #endif
  48. float last_z;
  49. int good_points;
  50. bool corner_probing_done, wait_for_probe;
  51. #if HAS_MARLINUI_U8GLIB
  52. #include "../dogm/marlinui_DOGM.h"
  53. #endif
  54. #define GOOD_POINTS_TO_STR(N) ui8tostr2(N)
  55. #define LAST_Z_TO_STR(N) ftostr53_63(N) //ftostr42_52(N)
  56. #endif
  57. static_assert(LEVEL_CORNERS_Z_HOP >= 0, "LEVEL_CORNERS_Z_HOP must be >= 0. Please update your configuration.");
  58. #if HAS_LEVELING
  59. static bool leveling_was_active = false;
  60. #endif
  61. #ifndef LEVEL_CORNERS_LEVELING_ORDER
  62. #define LEVEL_CORNERS_LEVELING_ORDER { LF, RF, LB, RB } // Default
  63. //#define LEVEL_CORNERS_LEVELING_ORDER { LF, LB, RF } // 3 hard-coded points
  64. //#define LEVEL_CORNERS_LEVELING_ORDER { LF, RF } // 3-Point tramming - Rear
  65. //#define LEVEL_CORNERS_LEVELING_ORDER { LF, LB } // 3-Point tramming - Right
  66. //#define LEVEL_CORNERS_LEVELING_ORDER { RF, RB } // 3-Point tramming - Left
  67. //#define LEVEL_CORNERS_LEVELING_ORDER { LB, RB } // 3-Point tramming - Front
  68. #endif
  69. #define LF 1
  70. #define RF 2
  71. #define RB 3
  72. #define LB 4
  73. constexpr int lco[] = LEVEL_CORNERS_LEVELING_ORDER;
  74. constexpr bool level_corners_3_points = COUNT(lco) == 2;
  75. static_assert(level_corners_3_points || COUNT(lco) == 4, "LEVEL_CORNERS_LEVELING_ORDER must have exactly 2 or 4 corners.");
  76. constexpr int lcodiff = abs(lco[0] - lco[1]);
  77. static_assert(COUNT(lco) == 4 || lcodiff == 1 || lcodiff == 3, "The first two LEVEL_CORNERS_LEVELING_ORDER corners must be on the same edge.");
  78. constexpr int nr_edge_points = level_corners_3_points ? 3 : 4;
  79. constexpr int available_points = nr_edge_points + ENABLED(LEVEL_CENTER_TOO);
  80. constexpr int center_index = TERN(LEVEL_CENTER_TOO, available_points - 1, -1);
  81. constexpr float inset_lfrb[4] = LEVEL_CORNERS_INSET_LFRB;
  82. constexpr xy_pos_t lf { (X_MIN_BED) + inset_lfrb[0], (Y_MIN_BED) + inset_lfrb[1] },
  83. rb { (X_MAX_BED) - inset_lfrb[2], (Y_MAX_BED) - inset_lfrb[3] };
  84. static int8_t bed_corner;
  85. /**
  86. * Select next corner coordinates
  87. */
  88. static inline void _lcd_level_bed_corners_get_next_position() {
  89. if (level_corners_3_points) {
  90. if (bed_corner >= available_points) bed_corner = 0; // Above max position -> move back to first corner
  91. switch (bed_corner) {
  92. case 0 ... 1:
  93. // First two corners set explicitly by the configuration
  94. current_position = lf; // Left front
  95. switch (lco[bed_corner]) {
  96. case RF: current_position.x = rb.x; break; // Right Front
  97. case RB: current_position = rb; break; // Right Back
  98. case LB: current_position.y = rb.y; break; // Left Back
  99. }
  100. break;
  101. case 2:
  102. // Determine which edge to probe for 3rd point
  103. current_position.set(lf.x + (rb.x - lf.x) / 2, lf.y + (rb.y - lf.y) / 2);
  104. if ((lco[0] == LB && lco[1] == RB) || (lco[0] == RB && lco[1] == LB)) current_position.y = lf.y; // Front Center
  105. if ((lco[0] == LF && lco[1] == LB) || (lco[0] == LB && lco[1] == LF)) current_position.x = rb.x; // Center Right
  106. if ((lco[0] == RF && lco[1] == RB) || (lco[0] == RB && lco[1] == RF)) current_position.x = lf.x; // Left Center
  107. if ((lco[0] == LF && lco[1] == RF) || (lco[0] == RF && lco[1] == LF)) current_position.y = rb.y; // Center Back
  108. #if DISABLED(LEVEL_CENTER_TOO) && ENABLED(LEVEL_CORNERS_USE_PROBE)
  109. bed_corner++; // Must increment the count to ensure it resets the loop if the 3rd point is out of tolerance
  110. #endif
  111. break;
  112. #if ENABLED(LEVEL_CENTER_TOO)
  113. case 3:
  114. current_position.set(X_CENTER, Y_CENTER);
  115. break;
  116. #endif
  117. }
  118. }
  119. else {
  120. // Four-Corner Bed Tramming with optional center
  121. if (TERN0(LEVEL_CENTER_TOO, bed_corner == center_index)) {
  122. current_position.set(X_CENTER, Y_CENTER);
  123. TERN_(LEVEL_CORNERS_USE_PROBE, good_points--); // Decrement to allow one additional probe point
  124. }
  125. else {
  126. current_position = lf; // Left front
  127. switch (lco[bed_corner]) {
  128. case RF: current_position.x = rb.x; break; // Right front
  129. case RB: current_position = rb; break; // Right rear
  130. case LB: current_position.y = rb.y; break; // Left rear
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Level corners, starting in the front-left corner.
  137. */
  138. #if ENABLED(LEVEL_CORNERS_USE_PROBE)
  139. #define VALIDATE_POINT(X, Y, STR) static_assert(Probe::build_time::can_reach((X), (Y)), \
  140. "LEVEL_CORNERS_INSET_LFRB " STR " inset is not reachable with the default NOZZLE_TO_PROBE offset and PROBING_MARGIN.")
  141. VALIDATE_POINT(lf.x, Y_CENTER, "left"); VALIDATE_POINT(X_CENTER, lf.y, "front");
  142. VALIDATE_POINT(rb.x, Y_CENTER, "right"); VALIDATE_POINT(X_CENTER, rb.y, "back");
  143. #ifndef PAGE_CONTAINS
  144. #define PAGE_CONTAINS(...) true
  145. #endif
  146. void _lcd_draw_probing() {
  147. if (!ui.should_draw()) return;
  148. TERN_(HAS_MARLINUI_U8GLIB, ui.set_font(FONT_MENU)); // Set up the font for extra info
  149. MenuItem_static::draw(0, GET_TEXT(MSG_PROBING_MESH), SS_INVERT); // "Probing Mesh" heading
  150. uint8_t cy = LCD_HEIGHT - 1, y = LCD_ROW_Y(cy);
  151. // Display # of good points found vs total needed
  152. if (PAGE_CONTAINS(y - (MENU_FONT_HEIGHT), y)) {
  153. SETCURSOR(0, cy);
  154. lcd_put_u8str_P(GET_TEXT(MSG_LEVEL_CORNERS_GOOD_POINTS));
  155. lcd_put_u8str(GOOD_POINTS_TO_STR(good_points));
  156. lcd_put_wchar('/');
  157. lcd_put_u8str(GOOD_POINTS_TO_STR(nr_edge_points));
  158. }
  159. --cy;
  160. y -= MENU_FONT_HEIGHT;
  161. // Display the Last Z value
  162. if (PAGE_CONTAINS(y - (MENU_FONT_HEIGHT), y)) {
  163. SETCURSOR(0, cy);
  164. lcd_put_u8str_P(GET_TEXT(MSG_LEVEL_CORNERS_LAST_Z));
  165. lcd_put_u8str(LAST_Z_TO_STR(last_z));
  166. }
  167. }
  168. void _lcd_draw_raise() {
  169. if (!ui.should_draw()) return;
  170. MenuItem_confirm::select_screen(
  171. GET_TEXT(MSG_BUTTON_DONE), GET_TEXT(MSG_BUTTON_SKIP)
  172. , []{ corner_probing_done = true; wait_for_probe = false; }
  173. , []{ wait_for_probe = false; }
  174. , GET_TEXT(MSG_LEVEL_CORNERS_RAISE)
  175. , (const char*)nullptr, NUL_STR
  176. );
  177. }
  178. void _lcd_draw_level_prompt() {
  179. if (!ui.should_draw()) return;
  180. MenuItem_confirm::confirm_screen(
  181. []{ queue.inject_P(TERN(HAS_LEVELING, PSTR("G29N"), G28_STR));
  182. ui.return_to_status();
  183. }
  184. , []{ ui.goto_previous_screen_no_defer(); }
  185. , GET_TEXT(MSG_LEVEL_CORNERS_IN_RANGE)
  186. , (const char*)nullptr, PSTR("?")
  187. );
  188. }
  189. bool _lcd_level_bed_corners_probe(bool verify=false) {
  190. if (verify) do_blocking_move_to_z(current_position.z + LEVEL_CORNERS_Z_HOP); // do clearance if needed
  191. TERN_(BLTOUCH_SLOW_MODE, bltouch.deploy()); // Deploy in LOW SPEED MODE on every probe action
  192. do_blocking_move_to_z(last_z - LEVEL_CORNERS_PROBE_TOLERANCE, MMM_TO_MMS(Z_PROBE_SPEED_SLOW)); // Move down to lower tolerance
  193. if (TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE))) { // check if probe triggered
  194. endstops.hit_on_purpose();
  195. set_current_from_steppers_for_axis(Z_AXIS);
  196. sync_plan_position();
  197. TERN_(BLTOUCH_SLOW_MODE, bltouch.stow()); // Stow in LOW SPEED MODE on every trigger
  198. // Triggered outside tolerance range?
  199. if (ABS(current_position.z - last_z) > LEVEL_CORNERS_PROBE_TOLERANCE) {
  200. last_z = current_position.z; // Above tolerance. Set a new Z for subsequent corners.
  201. good_points = 0; // ...and start over
  202. }
  203. return true; // probe triggered
  204. }
  205. do_blocking_move_to_z(last_z); // go back to tolerance middle point before raise
  206. return false; // probe not triggered
  207. }
  208. bool _lcd_level_bed_corners_raise() {
  209. bool probe_triggered = false;
  210. corner_probing_done = false;
  211. wait_for_probe = true;
  212. ui.goto_screen(_lcd_draw_raise); // show raise screen
  213. ui.set_selection(true);
  214. while (wait_for_probe && !probe_triggered) { // loop while waiting to bed raise and probe trigger
  215. probe_triggered = PROBE_TRIGGERED();
  216. if (probe_triggered) {
  217. endstops.hit_on_purpose();
  218. TERN_(LEVEL_CORNERS_AUDIO_FEEDBACK, ui.buzz(200, 600));
  219. }
  220. idle();
  221. }
  222. TERN_(BLTOUCH_SLOW_MODE, bltouch.stow());
  223. ui.goto_screen(_lcd_draw_probing);
  224. return (probe_triggered);
  225. }
  226. void _lcd_test_corners() {
  227. bed_corner = TERN(LEVEL_CENTER_TOO, center_index, 0);
  228. last_z = LEVEL_CORNERS_HEIGHT;
  229. endstops.enable_z_probe(true);
  230. good_points = 0;
  231. ui.goto_screen(_lcd_draw_probing);
  232. do {
  233. ui.refresh(LCDVIEW_REDRAW_NOW);
  234. _lcd_draw_probing(); // update screen with # of good points
  235. do_blocking_move_to_z(current_position.z + LEVEL_CORNERS_Z_HOP); // clearance
  236. _lcd_level_bed_corners_get_next_position(); // Select next corner coordinates
  237. current_position -= probe.offset_xy; // Account for probe offsets
  238. do_blocking_move_to_xy(current_position); // Goto corner
  239. if (!_lcd_level_bed_corners_probe()) { // Probe down to tolerance
  240. if (_lcd_level_bed_corners_raise()) { // Prompt user to raise bed if needed
  241. #if ENABLED(LEVEL_CORNERS_VERIFY_RAISED) // Verify
  242. while (!_lcd_level_bed_corners_probe(true)) { // Loop while corner verified
  243. if (!_lcd_level_bed_corners_raise()) { // Prompt user to raise bed if needed
  244. if (corner_probing_done) return; // Done was selected
  245. break; // Skip was selected
  246. }
  247. }
  248. #endif
  249. }
  250. else if (corner_probing_done) // Done was selected
  251. return;
  252. }
  253. if (bed_corner != center_index) good_points++; // ignore center
  254. if (++bed_corner > 3) bed_corner = 0;
  255. } while (good_points < nr_edge_points); // loop until all points within tolerance
  256. ui.goto_screen(_lcd_draw_level_prompt); // prompt for bed leveling
  257. ui.set_selection(true);
  258. }
  259. #else // !LEVEL_CORNERS_USE_PROBE
  260. static inline void _lcd_goto_next_corner() {
  261. line_to_z(LEVEL_CORNERS_Z_HOP);
  262. // Select next corner coordinates
  263. _lcd_level_bed_corners_get_next_position();
  264. line_to_current_position(manual_feedrate_mm_s.x);
  265. line_to_z(LEVEL_CORNERS_HEIGHT);
  266. if (++bed_corner >= available_points) bed_corner = 0;
  267. }
  268. #endif // !LEVEL_CORNERS_USE_PROBE
  269. static inline void _lcd_level_bed_corners_homing() {
  270. _lcd_draw_homing();
  271. if (!all_axes_homed()) return;
  272. #if ENABLED(LEVEL_CORNERS_USE_PROBE)
  273. _lcd_test_corners();
  274. if (corner_probing_done) ui.goto_previous_screen_no_defer();
  275. TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
  276. endstops.enable_z_probe(false);
  277. #else
  278. bed_corner = 0;
  279. ui.goto_screen([]{
  280. MenuItem_confirm::select_screen(
  281. GET_TEXT(MSG_BUTTON_NEXT), GET_TEXT(MSG_BUTTON_DONE)
  282. , _lcd_goto_next_corner
  283. , []{
  284. line_to_z(LEVEL_CORNERS_Z_HOP); // Raise Z off the bed when done
  285. TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
  286. ui.goto_previous_screen_no_defer();
  287. }
  288. , GET_TEXT(TERN(LEVEL_CENTER_TOO, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
  289. , (const char*)nullptr, PSTR("?")
  290. );
  291. });
  292. ui.set_selection(true);
  293. _lcd_goto_next_corner();
  294. #endif
  295. }
  296. void _lcd_level_bed_corners() {
  297. ui.defer_status_screen();
  298. if (!all_axes_trusted()) {
  299. set_all_unhomed();
  300. queue.inject_P(G28_STR);
  301. }
  302. // Disable leveling so the planner won't mess with us
  303. #if HAS_LEVELING
  304. leveling_was_active = planner.leveling_active;
  305. set_bed_leveling_enabled(false);
  306. #endif
  307. ui.goto_screen(_lcd_level_bed_corners_homing);
  308. }
  309. #endif // HAS_LCD_MENU && LEVEL_BED_CORNERS