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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 <http://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/ultralcd.h"
  32. #include "../../../module/motion.h"
  33. #include "../../../module/stepper.h"
  34. // Save 130 bytes with non-duplication of PSTR
  35. inline void echo_not_entered(const char c) { SERIAL_CHAR(c); SERIAL_ECHOLNPGM(" not entered."); }
  36. /**
  37. * G29: Mesh-based Z probe, probes a grid and produces a
  38. * mesh to compensate for variable bed height
  39. *
  40. * Parameters With MESH_BED_LEVELING:
  41. *
  42. * S0 Report the current mesh values
  43. * S1 Start probing mesh points
  44. * S2 Probe the next mesh point
  45. * S3 In Jn Zn.nn Manually modify a single point
  46. * S4 Zn.nn Set z offset. Positive away from bed, negative closer to bed.
  47. * S5 Reset and disable mesh
  48. *
  49. */
  50. void GcodeSuite::G29() {
  51. static int mbl_probe_index = -1;
  52. #if HAS_SOFTWARE_ENDSTOPS
  53. static bool enable_soft_endstops;
  54. #endif
  55. MeshLevelingState state = (MeshLevelingState)parser.byteval('S', (int8_t)MeshReport);
  56. if (!WITHIN(state, 0, 5)) {
  57. SERIAL_ECHOLNPGM("S out of range (0-5).");
  58. return;
  59. }
  60. int8_t ix, iy;
  61. switch (state) {
  62. case MeshReport:
  63. SERIAL_ECHOPGM("Mesh Bed Leveling ");
  64. if (leveling_is_valid()) {
  65. serialprintln_onoff(planner.leveling_active);
  66. mbl.report_mesh();
  67. }
  68. else
  69. SERIAL_ECHOLNPGM("has no data.");
  70. break;
  71. case MeshStart:
  72. mbl.reset();
  73. mbl_probe_index = 0;
  74. if (!ui.wait_for_bl_move) {
  75. enqueue_and_echo_commands_P(PSTR("G28\nG29 S2"));
  76. return;
  77. }
  78. state = MeshNext;
  79. case MeshNext:
  80. if (mbl_probe_index < 0) {
  81. SERIAL_ECHOLNPGM("Start mesh probing with \"G29 S1\" first.");
  82. return;
  83. }
  84. // For each G29 S2...
  85. if (mbl_probe_index == 0) {
  86. #if HAS_SOFTWARE_ENDSTOPS
  87. // For the initial G29 S2 save software endstop state
  88. enable_soft_endstops = soft_endstops_enabled;
  89. #endif
  90. // Move close to the bed before the first point
  91. do_blocking_move_to_z(0);
  92. }
  93. else {
  94. // Save Z for the previous mesh position
  95. mbl.set_zigzag_z(mbl_probe_index - 1, current_position[Z_AXIS]);
  96. #if HAS_SOFTWARE_ENDSTOPS
  97. soft_endstops_enabled = enable_soft_endstops;
  98. #endif
  99. }
  100. // If there's another point to sample, move there with optional lift.
  101. if (mbl_probe_index < GRID_MAX_POINTS) {
  102. #if HAS_SOFTWARE_ENDSTOPS
  103. // Disable software endstops to allow manual adjustment
  104. // If G29 is not completed, they will not be re-enabled
  105. soft_endstops_enabled = false;
  106. #endif
  107. mbl.zigzag(mbl_probe_index++, ix, iy);
  108. _manual_goto_xy(mbl.index_to_xpos[ix], mbl.index_to_ypos[iy]);
  109. }
  110. else {
  111. // One last "return to the bed" (as originally coded) at completion
  112. current_position[Z_AXIS] = MANUAL_PROBE_HEIGHT;
  113. line_to_current_position();
  114. planner.synchronize();
  115. // After recording the last point, activate home and activate
  116. mbl_probe_index = -1;
  117. SERIAL_ECHOLNPGM("Mesh probing done.");
  118. BUZZ(100, 659);
  119. BUZZ(100, 698);
  120. gcode.home_all_axes();
  121. set_bed_leveling_enabled(true);
  122. #if ENABLED(MESH_G28_REST_ORIGIN)
  123. current_position[Z_AXIS] = 0;
  124. set_destination_from_current();
  125. buffer_line_to_destination(homing_feedrate(Z_AXIS));
  126. planner.synchronize();
  127. #endif
  128. #if ENABLED(LCD_BED_LEVELING)
  129. ui.wait_for_bl_move = false;
  130. #endif
  131. }
  132. break;
  133. case MeshSet:
  134. if (parser.seenval('I')) {
  135. ix = parser.value_int();
  136. if (!WITHIN(ix, 0, GRID_MAX_POINTS_X - 1)) {
  137. SERIAL_ECHOPAIR("I out of range (0-", int(GRID_MAX_POINTS_X - 1));
  138. SERIAL_ECHOLNPGM(")");
  139. return;
  140. }
  141. }
  142. else
  143. return echo_not_entered('J');
  144. if (parser.seenval('J')) {
  145. iy = parser.value_int();
  146. if (!WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1)) {
  147. SERIAL_ECHOPAIR("J out of range (0-", int(GRID_MAX_POINTS_Y - 1));
  148. SERIAL_ECHOLNPGM(")");
  149. return;
  150. }
  151. }
  152. else
  153. return echo_not_entered('J');
  154. if (parser.seenval('Z')) {
  155. mbl.z_values[ix][iy] = parser.value_linear_units();
  156. #if ENABLED(EXTENSIBLE_UI)
  157. ExtUI::onMeshUpdate(ix, iy, mbl.z_values[ix][iy]);
  158. #endif
  159. }
  160. else
  161. return echo_not_entered('Z');
  162. break;
  163. case MeshSetZOffset:
  164. if (parser.seenval('Z'))
  165. mbl.z_offset = parser.value_linear_units();
  166. else
  167. return echo_not_entered('Z');
  168. break;
  169. case MeshReset:
  170. reset_bed_level();
  171. break;
  172. } // switch(state)
  173. if (state == MeshNext) {
  174. SERIAL_ECHOPAIR("MBL G29 point ", MIN(mbl_probe_index, GRID_MAX_POINTS));
  175. SERIAL_ECHOLNPAIR(" of ", int(GRID_MAX_POINTS));
  176. }
  177. report_current_position();
  178. }
  179. #endif // MESH_BED_LEVELING