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) 2016 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. void echo_not_entered() { SERIAL_PROTOCOLLNPGM(" 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 Produce a mesh report
  43. * S1 Start probing mesh points
  44. * S2 Probe the next mesh point
  45. * S3 Xn Yn 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. * The S0 report the points as below
  50. *
  51. * +----> X-axis 1-n
  52. * |
  53. * |
  54. * v Y-axis 1-n
  55. *
  56. */
  57. void GcodeSuite::G29() {
  58. static int mbl_probe_index = -1;
  59. #if HAS_SOFTWARE_ENDSTOPS
  60. static bool enable_soft_endstops;
  61. #endif
  62. const MeshLevelingState state = (MeshLevelingState)parser.byteval('S', (int8_t)MeshReport);
  63. if (!WITHIN(state, 0, 5)) {
  64. SERIAL_PROTOCOLLNPGM("S out of range (0-5).");
  65. return;
  66. }
  67. int8_t px, py;
  68. switch (state) {
  69. case MeshReport:
  70. if (leveling_is_valid()) {
  71. SERIAL_PROTOCOLLNPAIR("State: ", planner.leveling_active ? MSG_ON : MSG_OFF);
  72. mbl_mesh_report();
  73. }
  74. else
  75. SERIAL_PROTOCOLLNPGM("Mesh bed leveling has no data.");
  76. break;
  77. case MeshStart:
  78. mbl.reset();
  79. mbl_probe_index = 0;
  80. enqueue_and_echo_commands_P(lcd_wait_for_move ? PSTR("G29 S2") : PSTR("G28\nG29 S2"));
  81. break;
  82. case MeshNext:
  83. if (mbl_probe_index < 0) {
  84. SERIAL_PROTOCOLLNPGM("Start mesh probing with \"G29 S1\" first.");
  85. return;
  86. }
  87. // For each G29 S2...
  88. if (mbl_probe_index == 0) {
  89. #if HAS_SOFTWARE_ENDSTOPS
  90. // For the initial G29 S2 save software endstop state
  91. enable_soft_endstops = soft_endstops_enabled;
  92. #endif
  93. }
  94. else {
  95. // For G29 S2 after adjusting Z.
  96. mbl.set_zigzag_z(mbl_probe_index - 1, current_position[Z_AXIS]);
  97. #if HAS_SOFTWARE_ENDSTOPS
  98. soft_endstops_enabled = enable_soft_endstops;
  99. #endif
  100. }
  101. // If there's another point to sample, move there with optional lift.
  102. if (mbl_probe_index < GRID_MAX_POINTS) {
  103. mbl.zigzag(mbl_probe_index, px, py);
  104. _manual_goto_xy(mbl.index_to_xpos[px], mbl.index_to_ypos[py]);
  105. #if HAS_SOFTWARE_ENDSTOPS
  106. // Disable software endstops to allow manual adjustment
  107. // If G29 is not completed, they will not be re-enabled
  108. soft_endstops_enabled = false;
  109. #endif
  110. mbl_probe_index++;
  111. }
  112. else {
  113. // One last "return to the bed" (as originally coded) at completion
  114. current_position[Z_AXIS] = Z_MIN_POS + MANUAL_PROBE_HEIGHT;
  115. line_to_current_position();
  116. stepper.synchronize();
  117. // After recording the last point, activate home and activate
  118. mbl_probe_index = -1;
  119. SERIAL_PROTOCOLLNPGM("Mesh probing done.");
  120. BUZZ(100, 659);
  121. BUZZ(100, 698);
  122. mbl.has_mesh = true;
  123. gcode.home_all_axes();
  124. set_bed_leveling_enabled(true);
  125. #if ENABLED(MESH_G28_REST_ORIGIN)
  126. current_position[Z_AXIS] = Z_MIN_POS;
  127. set_destination_from_current();
  128. buffer_line_to_destination(homing_feedrate(Z_AXIS));
  129. stepper.synchronize();
  130. #endif
  131. }
  132. break;
  133. case MeshSet:
  134. if (parser.seenval('X')) {
  135. px = parser.value_int() - 1;
  136. if (!WITHIN(px, 0, GRID_MAX_POINTS_X - 1)) {
  137. SERIAL_PROTOCOLLNPGM("X out of range (1-" STRINGIFY(GRID_MAX_POINTS_X) ").");
  138. return;
  139. }
  140. }
  141. else {
  142. SERIAL_CHAR('X'); echo_not_entered();
  143. return;
  144. }
  145. if (parser.seenval('Y')) {
  146. py = parser.value_int() - 1;
  147. if (!WITHIN(py, 0, GRID_MAX_POINTS_Y - 1)) {
  148. SERIAL_PROTOCOLLNPGM("Y out of range (1-" STRINGIFY(GRID_MAX_POINTS_Y) ").");
  149. return;
  150. }
  151. }
  152. else {
  153. SERIAL_CHAR('Y'); echo_not_entered();
  154. return;
  155. }
  156. if (parser.seenval('Z'))
  157. mbl.z_values[px][py] = parser.value_linear_units();
  158. else {
  159. SERIAL_CHAR('Z'); echo_not_entered();
  160. return;
  161. }
  162. break;
  163. case MeshSetZOffset:
  164. if (parser.seenval('Z'))
  165. mbl.z_offset = parser.value_linear_units();
  166. else {
  167. SERIAL_CHAR('Z'); echo_not_entered();
  168. return;
  169. }
  170. break;
  171. case MeshReset:
  172. reset_bed_level();
  173. break;
  174. } // switch(state)
  175. if (state == MeshStart || state == MeshNext) {
  176. SERIAL_PROTOCOLPAIR("MBL G29 point ", min(mbl_probe_index, GRID_MAX_POINTS));
  177. SERIAL_PROTOCOLLNPAIR(" of ", int(GRID_MAX_POINTS));
  178. }
  179. report_current_position();
  180. }
  181. #endif // MESH_BED_LEVELING