My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

G34_M422.cpp 8.0KB

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