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_leveling.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // Bed Leveling Menus
  24. //
  25. #include "../../inc/MarlinConfigPre.h"
  26. #if ENABLED(LCD_BED_LEVELING)
  27. #include "menu_item.h"
  28. #include "../../module/planner.h"
  29. #include "../../feature/bedlevel/bedlevel.h"
  30. #if HAS_BED_PROBE && DISABLED(BABYSTEP_ZPROBE_OFFSET)
  31. #include "../../module/probe.h"
  32. #endif
  33. #if HAS_GRAPHICAL_TFT
  34. #include "../tft/tft.h"
  35. #if ENABLED(TOUCH_SCREEN)
  36. #include "../tft/touch.h"
  37. #endif
  38. #endif
  39. #if EITHER(PROBE_MANUALLY, MESH_BED_LEVELING)
  40. #include "../../module/motion.h"
  41. #include "../../gcode/queue.h"
  42. //
  43. // Motion > Level Bed handlers
  44. //
  45. static uint8_t manual_probe_index;
  46. // LCD probed points are from defaults
  47. constexpr uint8_t total_probe_points = TERN(AUTO_BED_LEVELING_3POINT, 3, GRID_MAX_POINTS);
  48. //
  49. // Bed leveling is done. Wait for G29 to complete.
  50. // A flag is used so that this can release control
  51. // and allow the command queue to be processed.
  52. //
  53. // When G29 finishes the last move:
  54. // - Raise Z to the "Z after probing" height
  55. // - Don't return until done.
  56. //
  57. // ** This blocks the command queue! **
  58. //
  59. void _lcd_level_bed_done() {
  60. if (!ui.wait_for_move) {
  61. #if Z_AFTER_PROBING > 0 && DISABLED(MESH_BED_LEVELING)
  62. // Display "Done" screen and wait for moves to complete
  63. line_to_z(Z_AFTER_PROBING);
  64. ui.synchronize(GET_TEXT(MSG_LEVEL_BED_DONE));
  65. #endif
  66. ui.goto_previous_screen_no_defer();
  67. ui.completion_feedback();
  68. }
  69. if (ui.should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, GET_TEXT(MSG_LEVEL_BED_DONE));
  70. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  71. }
  72. void _lcd_level_goto_next_point();
  73. //
  74. // Step 7: Get the Z coordinate, click goes to the next point or exits
  75. //
  76. void _lcd_level_bed_get_z() {
  77. if (ui.use_click()) {
  78. //
  79. // Save the current Z position and move
  80. //
  81. // If done...
  82. if (++manual_probe_index >= total_probe_points) {
  83. //
  84. // The last G29 records the point and enables bed leveling
  85. //
  86. ui.wait_for_move = true;
  87. ui.goto_screen(_lcd_level_bed_done);
  88. #if ENABLED(MESH_BED_LEVELING)
  89. queue.inject(F("G29S2"));
  90. #elif ENABLED(PROBE_MANUALLY)
  91. queue.inject(F("G29V1"));
  92. #endif
  93. }
  94. else
  95. _lcd_level_goto_next_point();
  96. return;
  97. }
  98. //
  99. // Encoder knob or keypad buttons adjust the Z position
  100. //
  101. if (ui.encoderPosition) {
  102. const float z = current_position.z + float(int32_t(ui.encoderPosition)) * (MESH_EDIT_Z_STEP);
  103. line_to_z(constrain(z, -(LCD_PROBE_Z_RANGE) * 0.5f, (LCD_PROBE_Z_RANGE) * 0.5f));
  104. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  105. ui.encoderPosition = 0;
  106. }
  107. //
  108. // Draw on first display, then only on Z change
  109. //
  110. if (ui.should_draw()) {
  111. const float v = current_position.z;
  112. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_MOVE_Z), ftostr43sign(v + (v < 0 ? -0.0001f : 0.0001f), '+'));
  113. }
  114. }
  115. //
  116. // Step 6: Display "Next point: 1 / 9" while waiting for move to finish
  117. //
  118. void _lcd_level_bed_moving() {
  119. if (ui.should_draw()) {
  120. char msg[10];
  121. sprintf_P(msg, PSTR("%i / %u"), int(manual_probe_index + 1), total_probe_points);
  122. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_LEVEL_BED_NEXT_POINT), msg);
  123. }
  124. ui.refresh(LCDVIEW_CALL_NO_REDRAW);
  125. if (!ui.wait_for_move) ui.goto_screen(_lcd_level_bed_get_z);
  126. }
  127. //
  128. // Step 5: Initiate a move to the next point
  129. //
  130. void _lcd_level_goto_next_point() {
  131. ui.goto_screen(_lcd_level_bed_moving);
  132. // G29 Records Z, moves, and signals when it pauses
  133. ui.wait_for_move = true;
  134. #if ENABLED(MESH_BED_LEVELING)
  135. queue.inject(manual_probe_index ? F("G29S2") : F("G29S1"));
  136. #elif ENABLED(PROBE_MANUALLY)
  137. queue.inject(F("G29V1"));
  138. #endif
  139. }
  140. //
  141. // Step 4: Display "Click to Begin", wait for click
  142. // Move to the first probe position
  143. //
  144. void _lcd_level_bed_homing_done() {
  145. if (ui.should_draw()) {
  146. MenuItem_static::draw(1, GET_TEXT(MSG_LEVEL_BED_WAITING));
  147. // Color UI needs a control to detect a touch
  148. #if BOTH(TOUCH_SCREEN, HAS_GRAPHICAL_TFT)
  149. touch.add_control(CLICK, 0, 0, TFT_WIDTH, TFT_HEIGHT);
  150. #endif
  151. }
  152. if (ui.use_click()) {
  153. manual_probe_index = 0;
  154. _lcd_level_goto_next_point();
  155. }
  156. }
  157. //
  158. // Step 3: Display "Homing XYZ" - Wait for homing to finish
  159. //
  160. void _lcd_level_bed_homing() {
  161. _lcd_draw_homing();
  162. if (all_axes_homed()) ui.goto_screen(_lcd_level_bed_homing_done);
  163. }
  164. #if ENABLED(PROBE_MANUALLY)
  165. extern bool g29_in_progress;
  166. #endif
  167. //
  168. // Step 2: Continue Bed Leveling...
  169. //
  170. void _lcd_level_bed_continue() {
  171. ui.defer_status_screen();
  172. set_all_unhomed();
  173. ui.goto_screen(_lcd_level_bed_homing);
  174. queue.inject_P(G28_STR);
  175. }
  176. #endif // PROBE_MANUALLY || MESH_BED_LEVELING
  177. #if ENABLED(MESH_EDIT_MENU)
  178. inline void refresh_planner() {
  179. set_current_from_steppers_for_axis(ALL_AXES_ENUM);
  180. sync_plan_position();
  181. }
  182. void menu_edit_mesh() {
  183. static uint8_t xind, yind; // =0
  184. START_MENU();
  185. BACK_ITEM(MSG_BED_LEVELING);
  186. EDIT_ITEM(uint8, MSG_MESH_X, &xind, 0, (GRID_MAX_POINTS_X) - 1);
  187. EDIT_ITEM(uint8, MSG_MESH_Y, &yind, 0, (GRID_MAX_POINTS_Y) - 1);
  188. EDIT_ITEM_FAST(float43, MSG_MESH_EDIT_Z, &Z_VALUES(xind, yind), -(LCD_PROBE_Z_RANGE) * 0.5, (LCD_PROBE_Z_RANGE) * 0.5, refresh_planner);
  189. END_MENU();
  190. }
  191. #endif // MESH_EDIT_MENU
  192. /**
  193. * Step 1: Bed Level entry-point
  194. *
  195. * << Motion
  196. * Auto Home (if homing needed)
  197. * Leveling On/Off (if data exists, and homed)
  198. * Fade Height: --- (Req: ENABLE_LEVELING_FADE_HEIGHT)
  199. * Mesh Z Offset: --- (Req: MESH_BED_LEVELING)
  200. * Z Probe Offset: --- (Req: HAS_BED_PROBE, Opt: BABYSTEP_ZPROBE_OFFSET)
  201. * Level Bed >
  202. * Level Corners > (if homed)
  203. * Load Settings (Req: EEPROM_SETTINGS)
  204. * Save Settings (Req: EEPROM_SETTINGS)
  205. */
  206. void menu_bed_leveling() {
  207. const bool is_homed = all_axes_trusted(),
  208. is_valid = leveling_is_valid();
  209. START_MENU();
  210. BACK_ITEM(MSG_MOTION);
  211. // Auto Home if not using manual probing
  212. #if NONE(PROBE_MANUALLY, MESH_BED_LEVELING)
  213. if (!is_homed) GCODES_ITEM(MSG_AUTO_HOME, G28_STR);
  214. #endif
  215. // Level Bed
  216. #if EITHER(PROBE_MANUALLY, MESH_BED_LEVELING)
  217. // Manual leveling uses a guided procedure
  218. SUBMENU(MSG_LEVEL_BED, _lcd_level_bed_continue);
  219. #else
  220. // Automatic leveling can just run the G-code
  221. GCODES_ITEM(MSG_LEVEL_BED, is_homed ? PSTR("G29") : PSTR("G29N"));
  222. #endif
  223. #if ENABLED(MESH_EDIT_MENU)
  224. if (is_valid) SUBMENU(MSG_EDIT_MESH, menu_edit_mesh);
  225. #endif
  226. // Homed and leveling is valid? Then leveling can be toggled.
  227. if (is_homed && is_valid) {
  228. bool show_state = planner.leveling_active;
  229. EDIT_ITEM(bool, MSG_BED_LEVELING, &show_state, _lcd_toggle_bed_leveling);
  230. }
  231. // Z Fade Height
  232. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  233. // Shadow for editing the fade height
  234. editable.decimal = planner.z_fade_height;
  235. EDIT_ITEM_FAST(float3, MSG_Z_FADE_HEIGHT, &editable.decimal, 0, 100, []{ set_z_fade_height(editable.decimal); });
  236. #endif
  237. //
  238. // Mesh Bed Leveling Z-Offset
  239. //
  240. #if ENABLED(MESH_BED_LEVELING)
  241. #if WITHIN(Z_PROBE_OFFSET_RANGE_MIN, -9, 9)
  242. #define LCD_Z_OFFSET_TYPE float43 // Values from -9.000 to +9.000
  243. #else
  244. #define LCD_Z_OFFSET_TYPE float42_52 // Values from -99.99 to 99.99
  245. #endif
  246. EDIT_ITEM(LCD_Z_OFFSET_TYPE, MSG_BED_Z, &mbl.z_offset, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX);
  247. #endif
  248. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  249. SUBMENU(MSG_ZPROBE_ZOFFSET, lcd_babystep_zoffset);
  250. #elif HAS_BED_PROBE
  251. EDIT_ITEM(LCD_Z_OFFSET_TYPE, MSG_ZPROBE_ZOFFSET, &probe.offset.z, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX);
  252. #endif
  253. #if ENABLED(LEVEL_BED_CORNERS)
  254. SUBMENU(MSG_BED_TRAMMING, _lcd_level_bed_corners);
  255. #endif
  256. #if ENABLED(EEPROM_SETTINGS)
  257. ACTION_ITEM(MSG_LOAD_EEPROM, ui.load_settings);
  258. ACTION_ITEM(MSG_STORE_EEPROM, ui.store_settings);
  259. #endif
  260. END_MENU();
  261. }
  262. #endif // LCD_BED_LEVELING