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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 HAS_LEVELING
  36. #include "../../feature/bedlevel/bedlevel.h"
  37. #endif
  38. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  39. #include "../../core/debug_out.h"
  40. float z_auto_align_xpos[Z_STEPPER_COUNT] = Z_STEPPER_ALIGN_X,
  41. z_auto_align_ypos[Z_STEPPER_COUNT] = Z_STEPPER_ALIGN_Y;
  42. inline void set_all_z_lock(const bool lock) {
  43. stepper.set_z_lock(lock);
  44. stepper.set_z2_lock(lock);
  45. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  46. stepper.set_z3_lock(lock);
  47. #endif
  48. }
  49. /**
  50. * G34: Z-Stepper automatic alignment
  51. *
  52. * Parameters: I<iterations> T<accuracy> A<amplification>
  53. */
  54. void GcodeSuite::G34() {
  55. if (DEBUGGING(LEVELING)) {
  56. DEBUG_ECHOLNPGM(">>> G34");
  57. log_machine_info();
  58. }
  59. do { // break out on error
  60. const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS);
  61. if (!WITHIN(z_auto_align_iterations, 1, 30)) {
  62. SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30).");
  63. break;
  64. }
  65. const float z_auto_align_accuracy = parser.floatval('T', Z_STEPPER_ALIGN_ACC);
  66. if (!WITHIN(z_auto_align_accuracy, 0.01f, 1.0f)) {
  67. SERIAL_ECHOLNPGM("?(T)arget accuracy out of bounds (0.01-1.0).");
  68. break;
  69. }
  70. const float z_auto_align_amplification = parser.floatval('A', Z_STEPPER_ALIGN_AMP);
  71. if (!WITHIN(ABS(z_auto_align_amplification), 0.5f, 2.0f)) {
  72. SERIAL_ECHOLNPGM("?(A)mplification out of bounds (0.5-2.0).");
  73. break;
  74. }
  75. // Wait for planner moves to finish!
  76. planner.synchronize();
  77. // Disable the leveling matrix before auto-aligning
  78. #if HAS_LEVELING
  79. #if ENABLED(RESTORE_LEVELING_AFTER_G34)
  80. const bool leveling_was_active = planner.leveling_active;
  81. #endif
  82. set_bed_leveling_enabled(false);
  83. #endif
  84. #if ENABLED(CNC_WORKSPACE_PLANES)
  85. workspace_plane = PLANE_XY;
  86. #endif
  87. // Always home with tool 0 active
  88. #if HOTENDS > 1
  89. const uint8_t old_tool_index = active_extruder;
  90. tool_change(0, true);
  91. #endif
  92. #if HAS_DUPLICATION_MODE
  93. extruder_duplication_enabled = false;
  94. #endif
  95. #if BOTH(BLTOUCH, BLTOUCH_HS_MODE)
  96. // In BLTOUCH HS mode, the probe travels in a deployed state.
  97. // Users of G34 might have a badly misaligned bed, so raise Z by the
  98. // length of the deployed pin (BLTOUCH stroke < 7mm)
  99. #define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES + 7.0f
  100. #else
  101. #define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES
  102. #endif
  103. float z_probe = Z_BASIC_CLEARANCE + (G34_MAX_GRADE) * 0.01f * (
  104. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  105. SQRT(_MAX(HYPOT2(z_auto_align_xpos[0] - z_auto_align_ypos[0], z_auto_align_xpos[1] - z_auto_align_ypos[1]),
  106. HYPOT2(z_auto_align_xpos[1] - z_auto_align_ypos[1], z_auto_align_xpos[2] - z_auto_align_ypos[2]),
  107. HYPOT2(z_auto_align_xpos[2] - z_auto_align_ypos[2], z_auto_align_xpos[0] - z_auto_align_ypos[0])))
  108. #else
  109. HYPOT(z_auto_align_xpos[0] - z_auto_align_ypos[0], z_auto_align_xpos[1] - z_auto_align_ypos[1])
  110. #endif
  111. );
  112. // Home before the alignment procedure
  113. if (homing_needed()) home_all_axes();
  114. // Move the Z coordinate realm towards the positive - dirty trick
  115. current_position[Z_AXIS] -= z_probe * 0.5;
  116. float last_z_align_move[Z_STEPPER_COUNT] = ARRAY_N(Z_STEPPER_COUNT, 10000.0f, 10000.0f, 10000.0f),
  117. z_measured[Z_STEPPER_COUNT] = { 0 },
  118. z_maxdiff = 0.0f,
  119. amplification = z_auto_align_amplification;
  120. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  121. uint8_t iteration;
  122. bool err_break = false;
  123. for (iteration = 0; iteration < z_auto_align_iterations; ++iteration) {
  124. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions.");
  125. SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1));
  126. // Initialize minimum value
  127. float z_measured_min = 100000.0f;
  128. // Probe all positions (one per Z-Stepper)
  129. for (uint8_t izstepper = 0; izstepper < Z_STEPPER_COUNT; ++izstepper) {
  130. // iteration odd/even --> downward / upward stepper sequence
  131. const uint8_t zstepper = (iteration & 1) ? Z_STEPPER_COUNT - 1 - izstepper : izstepper;
  132. // Safe clearance even on an incline
  133. if (iteration == 0 || izstepper > 0) do_blocking_move_to_z(z_probe);
  134. // Probe a Z height for each stepper.
  135. const float z_probed_height = probe_pt(z_auto_align_xpos[zstepper], z_auto_align_ypos[zstepper], raise_after, 0, true);
  136. if (isnan(z_probed_height)) {
  137. SERIAL_ECHOLNPGM("Probing failed.");
  138. err_break = true;
  139. break;
  140. }
  141. // Add height to each value, to provide a more useful target height for
  142. // the next iteration of probing. This allows adjustments to be made away from the bed.
  143. z_measured[zstepper] = z_probed_height + Z_CLEARANCE_BETWEEN_PROBES;
  144. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " measured position is ", z_measured[zstepper]);
  145. // Remember the minimum measurement to calculate the correction later on
  146. z_measured_min = _MIN(z_measured_min, z_measured[zstepper]);
  147. } // for (zstepper)
  148. if (err_break) break;
  149. // Adapt the next probe clearance height based on the new measurements.
  150. // Safe_height = lowest distance to bed (= highest measurement) plus highest measured misalignment.
  151. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  152. z_maxdiff = _MAX(ABS(z_measured[0] - z_measured[1]), ABS(z_measured[1] - z_measured[2]), ABS(z_measured[2] - z_measured[0]));
  153. z_probe = Z_BASIC_CLEARANCE + _MAX(z_measured[0], z_measured[1], z_measured[2]) + z_maxdiff;
  154. #else
  155. z_maxdiff = ABS(z_measured[0] - z_measured[1]);
  156. z_probe = Z_BASIC_CLEARANCE + _MAX(z_measured[0], z_measured[1]) + z_maxdiff;
  157. #endif
  158. SERIAL_ECHOPAIR("\n"
  159. "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1])
  160. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  161. , " Z2-Z3=", ABS(z_measured[1] - z_measured[2])
  162. , " Z3-Z1=", ABS(z_measured[2] - z_measured[0])
  163. #endif
  164. );
  165. SERIAL_EOL();
  166. SERIAL_EOL();
  167. // The following correction actions are to be enabled for select Z-steppers only
  168. stepper.set_separate_multi_axis(true);
  169. bool success_break = true;
  170. // Correct the individual stepper offsets
  171. for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
  172. // Calculate current stepper move
  173. const float z_align_move = z_measured[zstepper] - z_measured_min,
  174. z_align_abs = ABS(z_align_move);
  175. // Optimize one iterations correction based on the first measurements
  176. if (z_align_abs > 0.0f) amplification = iteration == 1 ? _MIN(last_z_align_move[zstepper] / z_align_abs, 2.0f) : z_auto_align_amplification;
  177. // Check for less accuracy compared to last move
  178. if (last_z_align_move[zstepper] < z_align_abs - 1.0) {
  179. SERIAL_ECHOLNPGM("Decreasing accuracy detected.");
  180. err_break = true;
  181. break;
  182. }
  183. // Remember the alignment for the next iteration
  184. last_z_align_move[zstepper] = z_align_abs;
  185. // Stop early if all measured points achieve accuracy target
  186. if (z_align_abs > z_auto_align_accuracy) success_break = false;
  187. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " corrected by ", z_align_move);
  188. // Lock all steppers except one
  189. set_all_z_lock(true);
  190. switch (zstepper) {
  191. case 0: stepper.set_z_lock(false); break;
  192. case 1: stepper.set_z2_lock(false); break;
  193. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  194. case 2: stepper.set_z3_lock(false); break;
  195. #endif
  196. }
  197. // Do a move to correct part of the misalignment for the current stepper
  198. do_blocking_move_to_z(amplification * z_align_move + current_position[Z_AXIS]);
  199. } // for (zstepper)
  200. // Back to normal stepper operations
  201. set_all_z_lock(false);
  202. stepper.set_separate_multi_axis(false);
  203. if (err_break) break;
  204. if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); break; }
  205. } // for (iteration)
  206. if (err_break) { SERIAL_ECHOLNPGM("G34 aborted."); break; }
  207. SERIAL_ECHOLNPAIR("Did ", int(iteration + (iteration != z_auto_align_iterations)), " iterations of ", int(z_auto_align_iterations));
  208. SERIAL_ECHOLNPAIR_F("Accuracy: ", z_maxdiff);
  209. SERIAL_EOL();
  210. // Restore the active tool after homing
  211. #if HOTENDS > 1
  212. tool_change(old_tool_index, (
  213. #if ENABLED(PARKING_EXTRUDER)
  214. false // Fetch the previous toolhead
  215. #else
  216. true
  217. #endif
  218. ));
  219. #endif
  220. #if HAS_LEVELING && ENABLED(RESTORE_LEVELING_AFTER_G34)
  221. set_bed_leveling_enabled(leveling_was_active);
  222. #endif
  223. // After this operation the z position needs correction
  224. set_axis_is_not_at_home(Z_AXIS);
  225. // Stow the probe, as the last call to probe_pt(...) left
  226. // the probe deployed if it was successful.
  227. STOW_PROBE();
  228. // Home Z after the alignment procedure
  229. process_subcommands_now_P(PSTR("G28 Z"));
  230. }while(0);
  231. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G34");
  232. }
  233. /**
  234. * M422: Z-Stepper automatic alignment parameter selection
  235. */
  236. void GcodeSuite::M422() {
  237. const int8_t zstepper = parser.intval('S') - 1;
  238. if (!WITHIN(zstepper, 0, Z_STEPPER_COUNT - 1)) {
  239. SERIAL_ECHOLNPGM("?(S) Z-Stepper index invalid.");
  240. return;
  241. }
  242. const float x_pos = parser.floatval('X', z_auto_align_xpos[zstepper]);
  243. if (!WITHIN(x_pos, X_MIN_POS, X_MAX_POS)) {
  244. SERIAL_ECHOLNPGM("?(X) out of bounds.");
  245. return;
  246. }
  247. const float y_pos = parser.floatval('Y', z_auto_align_ypos[zstepper]);
  248. if (!WITHIN(y_pos, Y_MIN_POS, Y_MAX_POS)) {
  249. SERIAL_ECHOLNPGM("?(Y) out of bounds.");
  250. return;
  251. }
  252. z_auto_align_xpos[zstepper] = x_pos;
  253. z_auto_align_ypos[zstepper] = y_pos;
  254. }
  255. #endif // Z_STEPPER_AUTO_ALIGN