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.

G29.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * G29.cpp - Mesh Bed Leveling
  24. */
  25. #include "../../../inc/MarlinConfig.h"
  26. #if ENABLED(MESH_BED_LEVELING)
  27. #include "../../../feature/bedlevel/bedlevel.h"
  28. #include "../../gcode.h"
  29. #include "../../queue.h"
  30. #include "../../../libs/buzzer.h"
  31. #include "../../../lcd/marlinui.h"
  32. #include "../../../module/motion.h"
  33. #include "../../../module/stepper.h"
  34. #if ENABLED(EXTENSIBLE_UI)
  35. #include "../../../lcd/extui/ui_api.h"
  36. #endif
  37. // Save 130 bytes with non-duplication of PSTR
  38. inline void echo_not_entered(const char c) { SERIAL_CHAR(c); SERIAL_ECHOLNPGM(" not entered."); }
  39. /**
  40. * G29: Mesh-based Z probe, probes a grid and produces a
  41. * mesh to compensate for variable bed height
  42. *
  43. * Parameters With MESH_BED_LEVELING:
  44. *
  45. * S0 Report the current mesh values
  46. * S1 Start probing mesh points
  47. * S2 Probe the next mesh point
  48. * S3 In Jn Zn.nn Manually modify a single point
  49. * S4 Zn.nn Set z offset. Positive away from bed, negative closer to bed.
  50. * S5 Reset and disable mesh
  51. */
  52. void GcodeSuite::G29() {
  53. static int mbl_probe_index = -1;
  54. MeshLevelingState state = (MeshLevelingState)parser.byteval('S', (int8_t)MeshReport);
  55. if (!WITHIN(state, 0, 5)) {
  56. SERIAL_ECHOLNPGM("S out of range (0-5).");
  57. return;
  58. }
  59. int8_t ix, iy;
  60. switch (state) {
  61. case MeshReport:
  62. SERIAL_ECHOPGM("Mesh Bed Leveling ");
  63. if (leveling_is_valid()) {
  64. serialprintln_onoff(planner.leveling_active);
  65. mbl.report_mesh();
  66. }
  67. else
  68. SERIAL_ECHOLNPGM("has no data.");
  69. break;
  70. case MeshStart:
  71. mbl.reset();
  72. mbl_probe_index = 0;
  73. if (!ui.wait_for_move) {
  74. queue.inject_P(parser.seen('N') ? PSTR("G28" TERN(G28_L0_ENSURES_LEVELING_OFF, "L0", "") "\nG29S2") : PSTR("G29S2"));
  75. return;
  76. }
  77. state = MeshNext;
  78. case MeshNext:
  79. if (mbl_probe_index < 0) {
  80. SERIAL_ECHOLNPGM("Start mesh probing with \"G29 S1\" first.");
  81. return;
  82. }
  83. // For each G29 S2...
  84. if (mbl_probe_index == 0) {
  85. // Move close to the bed before the first point
  86. do_blocking_move_to_z(MANUAL_PROBE_START_Z);
  87. }
  88. else {
  89. // Save Z for the previous mesh position
  90. mbl.set_zigzag_z(mbl_probe_index - 1, current_position.z);
  91. SET_SOFT_ENDSTOP_LOOSE(false);
  92. }
  93. // If there's another point to sample, move there with optional lift.
  94. if (mbl_probe_index < (GRID_MAX_POINTS)) {
  95. // Disable software endstops to allow manual adjustment
  96. // If G29 is left hanging without completion they won't be re-enabled!
  97. SET_SOFT_ENDSTOP_LOOSE(true);
  98. mbl.zigzag(mbl_probe_index++, ix, iy);
  99. _manual_goto_xy({ mbl.index_to_xpos[ix], mbl.index_to_ypos[iy] });
  100. }
  101. else {
  102. // One last "return to the bed" (as originally coded) at completion
  103. current_position.z = MANUAL_PROBE_HEIGHT;
  104. line_to_current_position();
  105. planner.synchronize();
  106. // After recording the last point, activate home and activate
  107. mbl_probe_index = -1;
  108. SERIAL_ECHOLNPGM("Mesh probing done.");
  109. TERN_(HAS_STATUS_MESSAGE, ui.set_status(GET_TEXT(MSG_MESH_DONE)));
  110. BUZZ(100, 659);
  111. BUZZ(100, 698);
  112. home_all_axes();
  113. set_bed_leveling_enabled(true);
  114. #if ENABLED(MESH_G28_REST_ORIGIN)
  115. current_position.z = 0;
  116. line_to_current_position(homing_feedrate(Z_AXIS));
  117. planner.synchronize();
  118. #endif
  119. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  120. }
  121. break;
  122. case MeshSet:
  123. if (parser.seenval('I')) {
  124. ix = parser.value_int();
  125. if (!WITHIN(ix, 0, (GRID_MAX_POINTS_X) - 1)) {
  126. SERIAL_ECHOLNPAIR("I out of range (0-", (GRID_MAX_POINTS_X) - 1, ")");
  127. return;
  128. }
  129. }
  130. else
  131. return echo_not_entered('J');
  132. if (parser.seenval('J')) {
  133. iy = parser.value_int();
  134. if (!WITHIN(iy, 0, (GRID_MAX_POINTS_Y) - 1)) {
  135. SERIAL_ECHOLNPAIR("J out of range (0-", (GRID_MAX_POINTS_Y) - 1, ")");
  136. return;
  137. }
  138. }
  139. else
  140. return echo_not_entered('J');
  141. if (parser.seenval('Z')) {
  142. mbl.z_values[ix][iy] = parser.value_linear_units();
  143. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(ix, iy, mbl.z_values[ix][iy]));
  144. }
  145. else
  146. return echo_not_entered('Z');
  147. break;
  148. case MeshSetZOffset:
  149. if (parser.seenval('Z'))
  150. mbl.z_offset = parser.value_linear_units();
  151. else
  152. return echo_not_entered('Z');
  153. break;
  154. case MeshReset:
  155. reset_bed_level();
  156. break;
  157. } // switch(state)
  158. if (state == MeshNext) {
  159. SERIAL_ECHOLNPAIR("MBL G29 point ", _MIN(mbl_probe_index, GRID_MAX_POINTS), " of ", GRID_MAX_POINTS);
  160. if (mbl_probe_index > 0) TERN_(HAS_STATUS_MESSAGE, ui.status_printf_P(0, PSTR(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_MESH), _MIN(mbl_probe_index, GRID_MAX_POINTS), int(GRID_MAX_POINTS)));
  161. }
  162. report_current_position();
  163. }
  164. #endif // MESH_BED_LEVELING