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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/planner.h"
  26. #include "../../module/stepper.h"
  27. #include "../../module/motion.h"
  28. #include "../../module/probe.h"
  29. #if HOTENDS > 1
  30. #include "../../module/tool_change.h"
  31. #endif
  32. #if HAS_LEVELING
  33. #include "../../feature/bedlevel/bedlevel.h"
  34. #endif
  35. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  36. #include "../../libs/least_squares_fit.h"
  37. #endif
  38. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  39. #include "../../core/debug_out.h"
  40. //
  41. // Sanity check G34 / M422 settings
  42. //
  43. constexpr xy_pos_t test_z_stepper_align_xy[] = Z_STEPPER_ALIGN_XY;
  44. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  45. static_assert(COUNT(test_z_stepper_align_xy) >= Z_STEPPER_COUNT,
  46. "Z_STEPPER_ALIGN_XY requires at least three {X,Y} entries (Z, Z2, Z3, ...)."
  47. );
  48. constexpr float test_z_stepper_align_stepper_xy[][XY] = Z_STEPPER_ALIGN_STEPPER_XY;
  49. static_assert(
  50. COUNT(test_z_stepper_align_stepper_xy) == Z_STEPPER_COUNT,
  51. "Z_STEPPER_ALIGN_STEPPER_XY requires three {X,Y} entries (one per Z stepper)."
  52. );
  53. #else
  54. static_assert(COUNT(test_z_stepper_align_xy) == Z_STEPPER_COUNT,
  55. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  56. "Z_STEPPER_ALIGN_XY requires three {X,Y} entries (Z, Z2, and Z3)."
  57. #else
  58. "Z_STEPPER_ALIGN_XY requires two {X,Y} entries (Z and Z2)."
  59. #endif
  60. );
  61. #endif
  62. constexpr xyz_pos_t dpo = NOZZLE_TO_PROBE_OFFSET;
  63. #define LTEST(N) (test_z_stepper_align_xy[N].x >= _MAX(X_MIN_BED + MIN_PROBE_EDGE_LEFT, X_MIN_POS + dpo.x) - 0.00001f)
  64. #define RTEST(N) (test_z_stepper_align_xy[N].x <= _MIN(X_MAX_BED - MIN_PROBE_EDGE_RIGHT, X_MAX_POS + dpo.x) + 0.00001f)
  65. #define FTEST(N) (test_z_stepper_align_xy[N].y >= _MAX(Y_MIN_BED + MIN_PROBE_EDGE_FRONT, Y_MIN_POS + dpo.y) - 0.00001f)
  66. #define BTEST(N) (test_z_stepper_align_xy[N].y <= _MIN(Y_MAX_BED - MIN_PROBE_EDGE_BACK, Y_MAX_POS + dpo.y) + 0.00001f)
  67. static_assert(LTEST(0) && RTEST(0), "The 1st Z_STEPPER_ALIGN_XY X is unreachable with the default probe X offset.");
  68. static_assert(FTEST(0) && BTEST(0), "The 1st Z_STEPPER_ALIGN_XY Y is unreachable with the default probe Y offset.");
  69. static_assert(LTEST(1) && RTEST(1), "The 2nd Z_STEPPER_ALIGN_XY X is unreachable with the default probe X offset.");
  70. static_assert(FTEST(1) && BTEST(1), "The 2nd Z_STEPPER_ALIGN_XY Y is unreachable with the default probe Y offset.");
  71. static_assert(LTEST(2) && RTEST(2), "The 3rd Z_STEPPER_ALIGN_XY X is unreachable with the default probe X offset.");
  72. static_assert(FTEST(2) && BTEST(2), "The 3rd Z_STEPPER_ALIGN_XY Y is unreachable with the default probe Y offset.");
  73. //
  74. // G34 / M422 shared data
  75. //
  76. static xy_pos_t z_stepper_align_pos[] = Z_STEPPER_ALIGN_XY;
  77. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  78. static xy_pos_t z_stepper_align_stepper_pos[] = Z_STEPPER_ALIGN_STEPPER_XY;
  79. #endif
  80. #define G34_PROBE_COUNT COUNT(z_stepper_align_pos)
  81. inline void set_all_z_lock(const bool lock) {
  82. stepper.set_z_lock(lock);
  83. stepper.set_z2_lock(lock);
  84. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  85. stepper.set_z3_lock(lock);
  86. #endif
  87. }
  88. /**
  89. * G34: Z-Stepper automatic alignment
  90. *
  91. * I<iterations>
  92. * T<accuracy>
  93. * A<amplification>
  94. */
  95. void GcodeSuite::G34() {
  96. if (DEBUGGING(LEVELING)) {
  97. DEBUG_ECHOLNPGM(">>> G34");
  98. log_machine_info();
  99. }
  100. do { // break out on error
  101. const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS);
  102. if (!WITHIN(z_auto_align_iterations, 1, 30)) {
  103. SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30).");
  104. break;
  105. }
  106. const float z_auto_align_accuracy = parser.floatval('T', Z_STEPPER_ALIGN_ACC);
  107. if (!WITHIN(z_auto_align_accuracy, 0.01f, 1.0f)) {
  108. SERIAL_ECHOLNPGM("?(T)arget accuracy out of bounds (0.01-1.0).");
  109. break;
  110. }
  111. const float z_auto_align_amplification =
  112. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  113. Z_STEPPER_ALIGN_AMP;
  114. #else
  115. parser.floatval('A', Z_STEPPER_ALIGN_AMP);
  116. if (!WITHIN(ABS(z_auto_align_amplification), 0.5f, 2.0f)) {
  117. SERIAL_ECHOLNPGM("?(A)mplification out of bounds (0.5-2.0).");
  118. break;
  119. }
  120. #endif
  121. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  122. // Wait for planner moves to finish!
  123. planner.synchronize();
  124. // Disable the leveling matrix before auto-aligning
  125. #if HAS_LEVELING
  126. #if ENABLED(RESTORE_LEVELING_AFTER_G34)
  127. const bool leveling_was_active = planner.leveling_active;
  128. #endif
  129. set_bed_leveling_enabled(false);
  130. #endif
  131. #if ENABLED(CNC_WORKSPACE_PLANES)
  132. workspace_plane = PLANE_XY;
  133. #endif
  134. // Always home with tool 0 active
  135. #if HOTENDS > 1
  136. const uint8_t old_tool_index = active_extruder;
  137. tool_change(0, true);
  138. #endif
  139. #if HAS_DUPLICATION_MODE
  140. extruder_duplication_enabled = false;
  141. #endif
  142. #if BOTH(BLTOUCH, BLTOUCH_HS_MODE)
  143. // In BLTOUCH HS mode, the probe travels in a deployed state.
  144. // Users of G34 might have a badly misaligned bed, so raise Z by the
  145. // length of the deployed pin (BLTOUCH stroke < 7mm)
  146. #define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES + 7.0f
  147. #else
  148. #define Z_BASIC_CLEARANCE Z_CLEARANCE_BETWEEN_PROBES
  149. #endif
  150. // Compute a worst-case clearance height to probe from. After the first
  151. // iteration this will be re-calculated based on the actual bed position
  152. float z_probe = Z_BASIC_CLEARANCE + (G34_MAX_GRADE) * 0.01f * (
  153. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  154. SQRT(_MAX(HYPOT2(z_stepper_align_pos[0].x - z_stepper_align_pos[0].y, z_stepper_align_pos[1].x - z_stepper_align_pos[1].y),
  155. HYPOT2(z_stepper_align_pos[1].x - z_stepper_align_pos[1].y, z_stepper_align_pos[2].x - z_stepper_align_pos[2].y),
  156. HYPOT2(z_stepper_align_pos[2].x - z_stepper_align_pos[2].y, z_stepper_align_pos[0].x - z_stepper_align_pos[0].y)))
  157. #else
  158. HYPOT(z_stepper_align_pos[0].x - z_stepper_align_pos[0].y, z_stepper_align_pos[1].x - z_stepper_align_pos[1].y)
  159. #endif
  160. );
  161. // Home before the alignment procedure
  162. if (!all_axes_known()) home_all_axes();
  163. // Move the Z coordinate realm towards the positive - dirty trick
  164. current_position.z -= z_probe * 0.5f;
  165. float last_z_align_move[Z_STEPPER_COUNT] = ARRAY_N(Z_STEPPER_COUNT, 10000.0f, 10000.0f, 10000.0f),
  166. z_measured[G34_PROBE_COUNT] = { 0 },
  167. z_maxdiff = 0.0f,
  168. amplification = z_auto_align_amplification;
  169. uint8_t iteration;
  170. bool err_break = false;
  171. for (iteration = 0; iteration < z_auto_align_iterations; ++iteration) {
  172. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions.");
  173. SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1));
  174. // Initialize minimum value
  175. float z_measured_min = 100000.0f,
  176. z_measured_max = -100000.0f;
  177. // Probe all positions (one per Z-Stepper)
  178. for (uint8_t i = 0; i < G34_PROBE_COUNT; ++i) {
  179. // iteration odd/even --> downward / upward stepper sequence
  180. const uint8_t iprobe = (iteration & 1) ? G34_PROBE_COUNT - 1 - i : i;
  181. // Safe clearance even on an incline
  182. if (iteration == 0 || i > 0) do_blocking_move_to_z(z_probe);
  183. // Probe a Z height for each stepper.
  184. const float z_probed_height = probe_at_point(z_stepper_align_pos[iprobe], raise_after, 0, true);
  185. if (isnan(z_probed_height)) {
  186. SERIAL_ECHOLNPGM("Probing failed.");
  187. err_break = true;
  188. break;
  189. }
  190. // Add height to each value, to provide a more useful target height for
  191. // the next iteration of probing. This allows adjustments to be made away from the bed.
  192. z_measured[iprobe] = z_probed_height + Z_CLEARANCE_BETWEEN_PROBES;
  193. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(iprobe + 1), " measured position is ", z_measured[iprobe]);
  194. // Remember the minimum measurement to calculate the correction later on
  195. z_measured_min = _MIN(z_measured_min, z_measured[iprobe]);
  196. z_measured_max = _MAX(z_measured_max, z_measured[iprobe]);
  197. } // for (i)
  198. if (err_break) break;
  199. // Adapt the next probe clearance height based on the new measurements.
  200. // Safe_height = lowest distance to bed (= highest measurement) plus highest measured misalignment.
  201. z_maxdiff = z_measured_max - z_measured_min;
  202. z_probe = Z_BASIC_CLEARANCE + z_measured_max + z_maxdiff;
  203. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  204. // Replace the initial values in z_measured with calculated heights at
  205. // each stepper position. This allows the adjustment algorithm to be
  206. // shared between both possible probing mechanisms.
  207. // This must be done after the next z_probe height is calculated, so that
  208. // the height is calculated from actual print area positions, and not
  209. // extrapolated motor movements.
  210. // Compute the least-squares fit for all probed points.
  211. // Calculate the Z position of each stepper and store it in z_measured.
  212. // This allows the actual adjustment logic to be shared by both algorithms.
  213. linear_fit_data lfd;
  214. incremental_LSF_reset(&lfd);
  215. for (uint8_t i = 0; i < G34_PROBE_COUNT; ++i) {
  216. SERIAL_ECHOLNPAIR("PROBEPT_", int(i + 1), ": ", z_measured[i]);
  217. incremental_LSF(&lfd, z_stepper_align_pos[i], z_measured[i]);
  218. }
  219. finish_incremental_LSF(&lfd);
  220. z_measured_min = 100000.0f;
  221. for (uint8_t i = 0; i < Z_STEPPER_COUNT; ++i) {
  222. z_measured[i] = -(lfd.A * z_stepper_align_stepper_pos[i].x + lfd.B * z_stepper_align_stepper_pos[i].y);
  223. z_measured_min = _MIN(z_measured_min, z_measured[i]);
  224. }
  225. SERIAL_ECHOLNPAIR("CALCULATED STEPPER POSITIONS: Z1=", z_measured[0], " Z2=", z_measured[1], " Z3=", z_measured[2]);
  226. #endif
  227. SERIAL_ECHOLNPAIR("\n"
  228. "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1])
  229. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  230. , " Z2-Z3=", ABS(z_measured[1] - z_measured[2])
  231. , " Z3-Z1=", ABS(z_measured[2] - z_measured[0])
  232. #endif
  233. );
  234. // The following correction actions are to be enabled for select Z-steppers only
  235. stepper.set_separate_multi_axis(true);
  236. bool success_break = true;
  237. // Correct the individual stepper offsets
  238. for (uint8_t zstepper = 0; zstepper < Z_STEPPER_COUNT; ++zstepper) {
  239. // Calculate current stepper move
  240. const float z_align_move = z_measured[zstepper] - z_measured_min,
  241. z_align_abs = ABS(z_align_move);
  242. #if DISABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  243. // Optimize one iteration's correction based on the first measurements
  244. if (z_align_abs > 0.0f) amplification = iteration == 1 ? _MIN(last_z_align_move[zstepper] / z_align_abs, 2.0f) : z_auto_align_amplification;
  245. #endif
  246. // Check for less accuracy compared to last move
  247. if (last_z_align_move[zstepper] < z_align_abs - 1.0) {
  248. SERIAL_ECHOLNPGM("Decreasing accuracy detected.");
  249. err_break = true;
  250. break;
  251. }
  252. // Remember the alignment for the next iteration
  253. last_z_align_move[zstepper] = z_align_abs;
  254. // Stop early if all measured points achieve accuracy target
  255. if (z_align_abs > z_auto_align_accuracy) success_break = false;
  256. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " corrected by ", z_align_move);
  257. // Lock all steppers except one
  258. set_all_z_lock(true);
  259. switch (zstepper) {
  260. case 0: stepper.set_z_lock(false); break;
  261. case 1: stepper.set_z2_lock(false); break;
  262. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  263. case 2: stepper.set_z3_lock(false); break;
  264. #endif
  265. }
  266. // Do a move to correct part of the misalignment for the current stepper
  267. do_blocking_move_to_z(amplification * z_align_move + current_position.z);
  268. } // for (zstepper)
  269. // Back to normal stepper operations
  270. set_all_z_lock(false);
  271. stepper.set_separate_multi_axis(false);
  272. if (err_break) break;
  273. if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); break; }
  274. } // for (iteration)
  275. if (err_break) { SERIAL_ECHOLNPGM("G34 aborted."); break; }
  276. SERIAL_ECHOLNPAIR("Did ", int(iteration + (iteration != z_auto_align_iterations)), " iterations of ", int(z_auto_align_iterations));
  277. SERIAL_ECHOLNPAIR_F("Accuracy: ", z_maxdiff);
  278. // Restore the active tool after homing
  279. #if HOTENDS > 1
  280. tool_change(old_tool_index, (
  281. #if ENABLED(PARKING_EXTRUDER)
  282. false // Fetch the previous toolhead
  283. #else
  284. true
  285. #endif
  286. ));
  287. #endif
  288. #if HAS_LEVELING && ENABLED(RESTORE_LEVELING_AFTER_G34)
  289. set_bed_leveling_enabled(leveling_was_active);
  290. #endif
  291. // After this operation the z position needs correction
  292. set_axis_is_not_at_home(Z_AXIS);
  293. // Stow the probe, as the last call to probe_at_point(...) left
  294. // the probe deployed if it was successful.
  295. STOW_PROBE();
  296. // Home Z after the alignment procedure
  297. process_subcommands_now_P(PSTR("G28 Z"));
  298. }while(0);
  299. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G34");
  300. }
  301. /**
  302. * M422: Set a Z-Stepper automatic alignment XY point.
  303. * Use repeatedly to set multiple points.
  304. *
  305. * S<index> : Index of the probe point to set
  306. *
  307. * With Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS:
  308. * W<index> : Index of the Z stepper position to set
  309. * The W and S parameters may not be combined.
  310. *
  311. * S and W require an X and/or Y parameter
  312. * X<pos> : X position to set (Unchanged if omitted)
  313. * Y<pos> : Y position to set (Unchanged if omitted)
  314. */
  315. void GcodeSuite::M422() {
  316. if (!parser.seen_any()) {
  317. for (uint8_t i = 0; i < G34_PROBE_COUNT; ++i)
  318. SERIAL_ECHOLNPAIR_P(PSTR("M422 S"), i + 1, SP_X_STR, z_stepper_align_pos[i].x, SP_Y_STR, z_stepper_align_pos[i].y);
  319. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  320. for (uint8_t i = 0; i < Z_STEPPER_COUNT; ++i)
  321. SERIAL_ECHOLNPAIR_P(PSTR("M422 W"), i + 1, SP_X_STR, z_stepper_align_stepper_pos[i].x, SP_Y_STR, z_stepper_align_stepper_pos[i].y);
  322. #endif
  323. return;
  324. }
  325. const bool is_probe_point = parser.seen('S');
  326. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  327. if (is_probe_point && parser.seen('W')) {
  328. SERIAL_ECHOLNPGM("?(S) and (W) may not be combined.");
  329. return;
  330. }
  331. #endif
  332. xy_pos_t *pos_dest = (
  333. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  334. !is_probe_point ? z_stepper_align_stepper_pos :
  335. #endif
  336. z_stepper_align_pos
  337. );
  338. if (!is_probe_point
  339. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  340. && !parser.seen('W')
  341. #endif
  342. ) {
  343. SERIAL_ECHOLNPGM(
  344. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  345. "?(S) or (W) is required."
  346. #else
  347. "?(S) is required."
  348. #endif
  349. );
  350. return;
  351. }
  352. // Get the Probe Position Index or Z Stepper Index
  353. int8_t position_index;
  354. if (is_probe_point) {
  355. position_index = parser.intval('S') - 1;
  356. if (!WITHIN(position_index, 0, int8_t(G34_PROBE_COUNT) - 1)) {
  357. SERIAL_ECHOLNPGM("?(S) Z-ProbePosition index invalid.");
  358. return;
  359. }
  360. }
  361. else {
  362. #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  363. position_index = parser.intval('W') - 1;
  364. if (!WITHIN(position_index, 0, Z_STEPPER_COUNT - 1)) {
  365. SERIAL_ECHOLNPGM("?(W) Z-Stepper index invalid.");
  366. return;
  367. }
  368. #endif
  369. }
  370. const xy_pos_t pos = {
  371. parser.floatval('X', pos_dest[position_index].x),
  372. parser.floatval('Y', pos_dest[position_index].y)
  373. };
  374. if (is_probe_point) {
  375. if (!position_is_reachable_by_probe(pos.x, Y_CENTER)) {
  376. SERIAL_ECHOLNPGM("?(X) out of bounds.");
  377. return;
  378. }
  379. if (!position_is_reachable_by_probe(pos)) {
  380. SERIAL_ECHOLNPGM("?(Y) out of bounds.");
  381. return;
  382. }
  383. }
  384. pos_dest[position_index] = pos;
  385. }
  386. #endif // Z_STEPPER_AUTO_ALIGN