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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, LCD_BED_TRAMMING)
  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 BED_TRAMMING_Z_HOP
  34. #define BED_TRAMMING_Z_HOP 4.0
  35. #endif
  36. #ifndef BED_TRAMMING_HEIGHT
  37. #define BED_TRAMMING_HEIGHT 0.0
  38. #endif
  39. #if ENABLED(BED_TRAMMING_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 BED_TRAMMING_PROBE_TOLERANCE
  46. #define BED_TRAMMING_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(BED_TRAMMING_Z_HOP >= 0, "BED_TRAMMING_Z_HOP must be >= 0. Please update your configuration.");
  58. #ifndef BED_TRAMMING_LEVELING_ORDER
  59. #define BED_TRAMMING_LEVELING_ORDER { LF, RF, LB, RB } // Default
  60. //#define BED_TRAMMING_LEVELING_ORDER { LF, LB, RF } // 3 hard-coded points
  61. //#define BED_TRAMMING_LEVELING_ORDER { LF, RF } // 3-Point tramming - Rear
  62. //#define BED_TRAMMING_LEVELING_ORDER { LF, LB } // 3-Point tramming - Right
  63. //#define BED_TRAMMING_LEVELING_ORDER { RF, RB } // 3-Point tramming - Left
  64. //#define BED_TRAMMING_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[] = BED_TRAMMING_LEVELING_ORDER;
  71. constexpr bool level_corners_3_points = COUNT(lco) == 2;
  72. static_assert(level_corners_3_points || COUNT(lco) == 4, "BED_TRAMMING_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 BED_TRAMMING_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(BED_TRAMMING_INCLUDE_CENTER);
  77. constexpr int center_index = TERN(BED_TRAMMING_INCLUDE_CENTER, available_points - 1, -1);
  78. constexpr float inset_lfrb[4] = BED_TRAMMING_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(BED_TRAMMING_INCLUDE_CENTER) && ENABLED(BED_TRAMMING_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(BED_TRAMMING_INCLUDE_CENTER)
  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(BED_TRAMMING_INCLUDE_CENTER, bed_corner == center_index)) {
  119. current_position.set(X_CENTER, Y_CENTER);
  120. TERN_(BED_TRAMMING_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(BED_TRAMMING_USE_PROBE)
  136. #define VALIDATE_POINT(X, Y, STR) static_assert(Probe::build_time::can_reach((X), (Y)), \
  137. "BED_TRAMMING_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_F(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_lchar('/');
  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_F(MSG_BUTTON_DONE), GET_TEXT_F(MSG_BUTTON_SKIP)
  171. , []{ corner_probing_done = true; wait_for_probe = false; }
  172. , []{ wait_for_probe = false; }
  173. , GET_TEXT_F(MSG_BED_TRAMMING_RAISE)
  174. );
  175. }
  176. void _lcd_draw_level_prompt() {
  177. if (!ui.should_draw()) return;
  178. MenuItem_confirm::select_screen(
  179. GET_TEXT_F(TERN(HAS_LEVELING, MSG_BUTTON_LEVEL, MSG_BUTTON_DONE)),
  180. TERN(HAS_LEVELING, GET_TEXT_F(MSG_BUTTON_BACK), nullptr)
  181. , []{ queue.inject(TERN(HAS_LEVELING, F("G29N"), FPSTR(G28_STR))); ui.return_to_status(); }
  182. , TERN(HAS_LEVELING, ui.goto_previous_screen_no_defer, []{})
  183. , GET_TEXT_F(MSG_BED_TRAMMING_IN_RANGE)
  184. );
  185. }
  186. bool _lcd_level_bed_corners_probe(bool verify=false) {
  187. if (verify) do_blocking_move_to_z(current_position.z + BED_TRAMMING_Z_HOP); // do clearance if needed
  188. TERN_(BLTOUCH, if (!bltouch.high_speed_mode) bltouch.deploy()); // Deploy in LOW SPEED MODE on every probe action
  189. do_blocking_move_to_z(last_z - BED_TRAMMING_PROBE_TOLERANCE, MMM_TO_MMS(Z_PROBE_FEEDRATE_SLOW)); // Move down to lower tolerance
  190. if (TEST(endstops.trigger_state(), Z_MIN_PROBE)) { // check if probe triggered
  191. endstops.hit_on_purpose();
  192. set_current_from_steppers_for_axis(Z_AXIS);
  193. sync_plan_position();
  194. TERN_(BLTOUCH, if (!bltouch.high_speed_mode) bltouch.stow()); // Stow in LOW SPEED MODE on every trigger
  195. // Triggered outside tolerance range?
  196. if (ABS(current_position.z - last_z) > BED_TRAMMING_PROBE_TOLERANCE) {
  197. last_z = current_position.z; // Above tolerance. Set a new Z for subsequent corners.
  198. good_points = 0; // ...and start over
  199. }
  200. return true; // probe triggered
  201. }
  202. do_blocking_move_to_z(last_z); // go back to tolerance middle point before raise
  203. return false; // probe not triggered
  204. }
  205. bool _lcd_level_bed_corners_raise() {
  206. bool probe_triggered = false;
  207. corner_probing_done = false;
  208. wait_for_probe = true;
  209. ui.goto_screen(_lcd_draw_raise); // show raise screen
  210. ui.set_selection(true);
  211. while (wait_for_probe && !probe_triggered) { // loop while waiting to bed raise and probe trigger
  212. probe_triggered = PROBE_TRIGGERED();
  213. if (probe_triggered) {
  214. endstops.hit_on_purpose();
  215. TERN_(BED_TRAMMING_AUDIO_FEEDBACK, BUZZ(200, 600));
  216. }
  217. idle();
  218. }
  219. TERN_(BLTOUCH, if (!bltouch.high_speed_mode) bltouch.stow());
  220. ui.goto_screen(_lcd_draw_probing);
  221. return (probe_triggered);
  222. }
  223. void _lcd_test_corners() {
  224. bed_corner = TERN(BED_TRAMMING_INCLUDE_CENTER, center_index, 0);
  225. last_z = BED_TRAMMING_HEIGHT;
  226. endstops.enable_z_probe(true);
  227. good_points = 0;
  228. ui.goto_screen(_lcd_draw_probing);
  229. do {
  230. ui.refresh(LCDVIEW_REDRAW_NOW);
  231. _lcd_draw_probing(); // update screen with # of good points
  232. do_blocking_move_to_z(current_position.z + BED_TRAMMING_Z_HOP + TERN0(BLTOUCH, bltouch.z_extra_clearance())); // clearance
  233. _lcd_level_bed_corners_get_next_position(); // Select next corner coordinates
  234. current_position -= probe.offset_xy; // Account for probe offsets
  235. do_blocking_move_to_xy(current_position); // Goto corner
  236. TERN_(BLTOUCH, if (bltouch.high_speed_mode) bltouch.deploy()); // Deploy in HIGH SPEED MODE
  237. if (!_lcd_level_bed_corners_probe()) { // Probe down to tolerance
  238. if (_lcd_level_bed_corners_raise()) { // Prompt user to raise bed if needed
  239. #if ENABLED(BED_TRAMMING_VERIFY_RAISED) // Verify
  240. while (!_lcd_level_bed_corners_probe(true)) { // Loop while corner verified
  241. if (!_lcd_level_bed_corners_raise()) { // Prompt user to raise bed if needed
  242. if (corner_probing_done) return; // Done was selected
  243. break; // Skip was selected
  244. }
  245. }
  246. #endif
  247. }
  248. else if (corner_probing_done) // Done was selected
  249. return;
  250. }
  251. if (bed_corner != center_index) good_points++; // ignore center
  252. if (++bed_corner > 3) bed_corner = 0;
  253. } while (good_points < nr_edge_points); // loop until all points within tolerance
  254. #if ENABLED(BLTOUCH)
  255. if (bltouch.high_speed_mode) {
  256. // In HIGH SPEED MODE do clearance and stow at the very end
  257. do_blocking_move_to_z(current_position.z + BED_TRAMMING_Z_HOP);
  258. bltouch.stow();
  259. }
  260. #endif
  261. ui.goto_screen(_lcd_draw_level_prompt); // prompt for bed leveling
  262. ui.set_selection(true);
  263. }
  264. #else // !BED_TRAMMING_USE_PROBE
  265. static void _lcd_goto_next_corner() {
  266. line_to_z(BED_TRAMMING_Z_HOP);
  267. // Select next corner coordinates
  268. _lcd_level_bed_corners_get_next_position();
  269. line_to_current_position(manual_feedrate_mm_s.x);
  270. line_to_z(BED_TRAMMING_HEIGHT);
  271. if (++bed_corner >= available_points) bed_corner = 0;
  272. }
  273. #endif // !BED_TRAMMING_USE_PROBE
  274. static void _lcd_level_bed_corners_homing() {
  275. _lcd_draw_homing();
  276. if (!all_axes_homed()) return;
  277. #if ENABLED(BED_TRAMMING_USE_PROBE)
  278. _lcd_test_corners();
  279. if (corner_probing_done) ui.goto_previous_screen_no_defer();
  280. TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
  281. endstops.enable_z_probe(false);
  282. #else
  283. bed_corner = 0;
  284. ui.goto_screen([]{
  285. MenuItem_confirm::select_screen(
  286. GET_TEXT_F(MSG_BUTTON_NEXT), GET_TEXT_F(MSG_BUTTON_DONE)
  287. , _lcd_goto_next_corner
  288. , []{
  289. line_to_z(BED_TRAMMING_Z_HOP); // Raise Z off the bed when done
  290. TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
  291. ui.goto_previous_screen_no_defer();
  292. }
  293. , GET_TEXT_F(TERN(BED_TRAMMING_INCLUDE_CENTER, MSG_LEVEL_BED_NEXT_POINT, MSG_NEXT_CORNER))
  294. , (const char*)nullptr, F("?")
  295. );
  296. });
  297. ui.set_selection(true);
  298. _lcd_goto_next_corner();
  299. #endif
  300. }
  301. void _lcd_level_bed_corners() {
  302. ui.defer_status_screen();
  303. if (!all_axes_trusted()) {
  304. set_all_unhomed();
  305. queue.inject_P(G28_STR);
  306. }
  307. // Disable leveling so the planner won't mess with us
  308. #if HAS_LEVELING
  309. leveling_was_active = planner.leveling_active;
  310. set_bed_leveling_enabled(false);
  311. #endif
  312. ui.goto_screen(_lcd_level_bed_corners_homing);
  313. }
  314. #endif // HAS_MARLINUI_MENU && LCD_BED_TRAMMING