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

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