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

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