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

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