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.

G34_M422.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  24. #include "../gcode.h"
  25. #include "../../module/delta.h"
  26. #include "../../module/motion.h"
  27. #include "../../module/stepper.h"
  28. #include "../../module/endstops.h"
  29. #if HOTENDS > 1
  30. #include "../../module/tool_change.h"
  31. #endif
  32. #if HAS_BED_PROBE
  33. #include "../../module/probe.h"
  34. #endif
  35. #if ENABLED(BLTOUCH)
  36. #include "../../feature/bltouch.h"
  37. #endif
  38. #if HAS_LEVELING
  39. #include "../../feature/bedlevel/bedlevel.h"
  40. #endif
  41. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  42. #include "../../core/debug_out.h"
  43. float z_auto_align_xpos[Z_STEPPER_COUNT] = Z_STEPPER_ALIGN_X,
  44. z_auto_align_ypos[Z_STEPPER_COUNT] = Z_STEPPER_ALIGN_Y;
  45. inline void set_all_z_lock(const bool lock) {
  46. stepper.set_z_lock(lock);
  47. stepper.set_z2_lock(lock);
  48. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  49. stepper.set_z3_lock(lock);
  50. #endif
  51. }
  52. /**
  53. * G34: Z-Stepper automatic alignment
  54. *
  55. * Parameters: I<iterations> T<accuracy> A<amplification>
  56. */
  57. void GcodeSuite::G34() {
  58. if (DEBUGGING(LEVELING)) {
  59. DEBUG_ECHOLNPGM(">>> G34");
  60. log_machine_info();
  61. }
  62. do { // break out on error
  63. if (!TEST(axis_known_position, X_AXIS) || !TEST(axis_known_position, Y_AXIS)) {
  64. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> XY homing required.");
  65. break;
  66. }
  67. const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS);
  68. if (!WITHIN(z_auto_align_iterations, 1, 30)) {
  69. SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30).");
  70. break;
  71. }
  72. const float z_auto_align_accuracy = parser.floatval('T', Z_STEPPER_ALIGN_ACC);
  73. if (!WITHIN(z_auto_align_accuracy, 0.01f, 1.0f)) {
  74. SERIAL_ECHOLNPGM("?(T)arget accuracy out of bounds (0.01-1.0).");
  75. break;
  76. }
  77. const float z_auto_align_amplification = parser.floatval('A', Z_STEPPER_ALIGN_AMP);
  78. if (!WITHIN(ABS(z_auto_align_amplification), 0.5f, 2.0f)) {
  79. SERIAL_ECHOLNPGM("?(A)mplification out of bounds (0.5-2.0).");
  80. break;
  81. }
  82. // Wait for planner moves to finish!
  83. planner.synchronize();
  84. // Disable the leveling matrix before auto-aligning
  85. #if HAS_LEVELING
  86. #if ENABLED(RESTORE_LEVELING_AFTER_G34)
  87. const bool leveling_was_active = planner.leveling_active;
  88. #endif
  89. set_bed_leveling_enabled(false);
  90. #endif
  91. #if ENABLED(CNC_WORKSPACE_PLANES)
  92. workspace_plane = PLANE_XY;
  93. #endif
  94. #if ENABLED(BLTOUCH)
  95. bltouch.reset();
  96. bltouch.stow();
  97. #endif
  98. // Always home with tool 0 active
  99. #if HOTENDS > 1
  100. const uint8_t old_tool_index = active_extruder;
  101. tool_change(0, 0, true);
  102. #endif
  103. #if HAS_DUPLICATION_MODE
  104. extruder_duplication_enabled = false;
  105. #endif
  106. // Before moving other axes raise Z, if needed. Never lower Z.
  107. if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
  108. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Raise Z (before moving to probe pos) to ", Z_CLEARANCE_BETWEEN_PROBES);
  109. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  110. }
  111. // Remember corrections to determine errors on each iteration
  112. float last_z_align_move[Z_STEPPER_COUNT] = ARRAY_N(Z_STEPPER_COUNT, 10000.0f, 10000.0f, 10000.0f),
  113. z_measured[Z_STEPPER_COUNT] = { 0 };
  114. bool err_break = false;
  115. for (uint8_t iteration = 0; iteration < z_auto_align_iterations; ++iteration) {
  116. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions.");
  117. // Reset minimum value
  118. float z_measured_min = 100000.0f;
  119. // For each iteration go through all probe positions (one per Z-Stepper)
  120. for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
  121. // Probe a Z height for each stepper
  122. z_measured[zstepper] = probe_pt(z_auto_align_xpos[zstepper], z_auto_align_ypos[zstepper], PROBE_PT_RAISE, false);
  123. // Stop on error
  124. if (isnan(z_measured[zstepper])) {
  125. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> PROBING FAILED!");
  126. err_break = true;
  127. break;
  128. }
  129. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " measured position is ", z_measured[zstepper]);
  130. // Remember the maximum position to calculate the correction
  131. z_measured_min = MIN(z_measured_min, z_measured[zstepper]);
  132. }
  133. if (err_break) break;
  134. // Remember the current z position to return to
  135. float z_original_position = current_position[Z_AXIS];
  136. // Iterations can stop early if all corrections are below required accuracy
  137. bool success_break = true;
  138. // Correct stepper offsets and re-iterate
  139. for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
  140. stepper.set_separate_multi_axis(true);
  141. set_all_z_lock(true); // Steppers will be enabled separately
  142. // Calculate current stepper move
  143. const float z_align_move = z_measured[zstepper] - z_measured_min,
  144. z_align_abs = ABS(z_align_move);
  145. // Check for lost accuracy compared to last move
  146. if (last_z_align_move[zstepper] < z_align_abs - 1.0) {
  147. // Stop here
  148. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> detected decreasing accuracy.");
  149. err_break = true;
  150. break;
  151. }
  152. else
  153. last_z_align_move[zstepper] = z_align_abs;
  154. // Only stop early if all measured points achieve accuracy target
  155. if (z_align_abs > z_auto_align_accuracy) success_break = false;
  156. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " corrected by ", z_align_move);
  157. switch (zstepper) {
  158. case 0: stepper.set_z_lock(false); break;
  159. case 1: stepper.set_z2_lock(false); break;
  160. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  161. case 2: stepper.set_z3_lock(false); break;
  162. #endif
  163. }
  164. // This will lose home position and require re-homing
  165. do_blocking_move_to_z(z_auto_align_amplification * z_align_move + current_position[Z_AXIS]);
  166. }
  167. if (err_break) break;
  168. // Move Z back to previous position
  169. set_all_z_lock(true);
  170. do_blocking_move_to_z(z_original_position);
  171. set_all_z_lock(false);
  172. stepper.set_separate_multi_axis(false);
  173. if (success_break) {
  174. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> achieved target accuracy.");
  175. break;
  176. }
  177. }
  178. if (err_break) break;
  179. // Restore the active tool after homing
  180. #if HOTENDS > 1
  181. tool_change(old_tool_index, 0, (
  182. #if ENABLED(PARKING_EXTRUDER)
  183. false // Fetch the previous toolhead
  184. #else
  185. true
  186. #endif
  187. ));
  188. #endif
  189. #if HAS_LEVELING
  190. #if ENABLED(RESTORE_LEVELING_AFTER_G34)
  191. set_bed_leveling_enabled(leveling_was_active);
  192. #endif
  193. #endif
  194. // After this operation the z position needs correction
  195. set_axis_is_not_at_home(Z_AXIS);
  196. gcode.G28(false);
  197. } while(0);
  198. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G34");
  199. }
  200. /**
  201. * M422: Z-Stepper automatic alignment parameter selection
  202. */
  203. void GcodeSuite::M422() {
  204. const int8_t zstepper = parser.intval('S') - 1;
  205. if (!WITHIN(zstepper, 0, Z_STEPPER_COUNT - 1)) {
  206. SERIAL_ECHOLNPGM("?(S) Z-Stepper index invalid.");
  207. return;
  208. }
  209. const float x_pos = parser.floatval('X', z_auto_align_xpos[zstepper]);
  210. if (!WITHIN(x_pos, X_MIN_POS, X_MAX_POS)) {
  211. SERIAL_ECHOLNPGM("?(X) out of bounds.");
  212. return;
  213. }
  214. const float y_pos = parser.floatval('Y', z_auto_align_ypos[zstepper]);
  215. if (!WITHIN(y_pos, Y_MIN_POS, Y_MAX_POS)) {
  216. SERIAL_ECHOLNPGM("?(Y) out of bounds.");
  217. return;
  218. }
  219. z_auto_align_xpos[zstepper] = x_pos;
  220. z_auto_align_ypos[zstepper] = y_pos;
  221. }
  222. #endif // Z_STEPPER_AUTO_ALIGN