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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfigPre.h"
  23. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  24. #include "../../feature/z_stepper_align.h"
  25. #include "../gcode.h"
  26. #include "../../module/motion.h"
  27. #include "../../module/stepper.h"
  28. #include "../../module/planner.h"
  29. #include "../../module/probe.h"
  30. #include "../../lcd/marlinui.h" // for LCD_MESSAGE
  31. #if HAS_LEVELING
  32. #include "../../feature/bedlevel/bedlevel.h"
  33. #endif
  34. #if HAS_MULTI_HOTEND
  35. #include "../../module/tool_change.h"
  36. #endif
  37. #if HAS_Z_STEPPER_ALIGN_STEPPER_XY
  38. #include "../../libs/least_squares_fit.h"
  39. #endif
  40. #if ENABLED(BLTOUCH)
  41. #include "../../feature/bltouch.h"
  42. #endif
  43. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  44. #include "../../core/debug_out.h"
  45. #if NUM_Z_STEPPERS >= 3
  46. #define TRIPLE_Z 1
  47. #if NUM_Z_STEPPERS >= 4
  48. #define QUAD_Z 1
  49. #endif
  50. #endif
  51. /**
  52. * G34: Z-Stepper automatic alignment
  53. *
  54. * Manual stepper lock controls (reset by G28):
  55. * L Unlock all steppers
  56. * Z<1-4> Z stepper to lock / unlock
  57. * S<state> 0=UNLOCKED 1=LOCKED. If omitted, assume LOCKED.
  58. *
  59. * Examples:
  60. * G34 Z1 ; Lock Z1
  61. * G34 L Z2 ; Unlock all, then lock Z2
  62. * G34 Z2 S0 ; Unlock Z2
  63. *
  64. * With Z_STEPPER_AUTO_ALIGN:
  65. * I<iterations> Number of tests. If omitted, Z_STEPPER_ALIGN_ITERATIONS.
  66. * T<accuracy> Target Accuracy factor. If omitted, Z_STEPPER_ALIGN_ACC.
  67. * A<amplification> Provide an Amplification value. If omitted, Z_STEPPER_ALIGN_AMP.
  68. * R Flag to recalculate points based on current probe offsets
  69. */
  70. void GcodeSuite::G34() {
  71. DEBUG_SECTION(log_G34, "G34", DEBUGGING(LEVELING));
  72. if (DEBUGGING(LEVELING)) log_machine_info();
  73. planner.synchronize(); // Prevent damage
  74. const bool seenL = parser.seen('L');
  75. if (seenL) stepper.set_all_z_lock(false);
  76. const bool seenZ = parser.seenval('Z');
  77. if (seenZ) {
  78. const bool state = parser.boolval('S', true);
  79. switch (parser.intval('Z')) {
  80. case 1: stepper.set_z1_lock(state); break;
  81. case 2: stepper.set_z2_lock(state); break;
  82. #if TRIPLE_Z
  83. case 3: stepper.set_z3_lock(state); break;
  84. #if QUAD_Z
  85. case 4: stepper.set_z4_lock(state); break;
  86. #endif
  87. #endif
  88. }
  89. }
  90. if (seenL || seenZ) {
  91. stepper.set_separate_multi_axis(seenZ);
  92. return;
  93. }
  94. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  95. do { // break out on error
  96. const int8_t z_auto_align_iterations = parser.intval('I', Z_STEPPER_ALIGN_ITERATIONS);
  97. if (!WITHIN(z_auto_align_iterations, 1, 30)) {
  98. SERIAL_ECHOLNPGM("?(I)teration out of bounds (1-30).");
  99. break;
  100. }
  101. const float z_auto_align_accuracy = parser.floatval('T', Z_STEPPER_ALIGN_ACC);
  102. if (!WITHIN(z_auto_align_accuracy, 0.01f, 1.0f)) {
  103. SERIAL_ECHOLNPGM("?(T)arget accuracy out of bounds (0.01-1.0).");
  104. break;
  105. }
  106. const float z_auto_align_amplification = TERN(HAS_Z_STEPPER_ALIGN_STEPPER_XY, Z_STEPPER_ALIGN_AMP, parser.floatval('A', Z_STEPPER_ALIGN_AMP));
  107. if (!WITHIN(ABS(z_auto_align_amplification), 0.5f, 2.0f)) {
  108. SERIAL_ECHOLNPGM("?(A)mplification out of bounds (0.5-2.0).");
  109. break;
  110. }
  111. if (parser.seen('R')) z_stepper_align.reset_to_default();
  112. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  113. // Disable the leveling matrix before auto-aligning
  114. #if HAS_LEVELING
  115. #if ENABLED(RESTORE_LEVELING_AFTER_G34)
  116. const bool leveling_was_active = planner.leveling_active;
  117. #endif
  118. set_bed_leveling_enabled(false);
  119. #endif
  120. TERN_(CNC_WORKSPACE_PLANES, workspace_plane = PLANE_XY);
  121. // Always home with tool 0 active
  122. #if HAS_MULTI_HOTEND
  123. const uint8_t old_tool_index = active_extruder;
  124. tool_change(0, true);
  125. #endif
  126. TERN_(HAS_DUPLICATION_MODE, set_duplication_enabled(false));
  127. // In BLTOUCH HS mode, the probe travels in a deployed state.
  128. // Users of G34 might have a badly misaligned bed, so raise Z by the
  129. // length of the deployed pin (BLTOUCH stroke < 7mm)
  130. #define Z_BASIC_CLEARANCE (Z_CLEARANCE_BETWEEN_PROBES + TERN0(BLTOUCH, bltouch.z_extra_clearance()))
  131. // Compute a worst-case clearance height to probe from. After the first
  132. // iteration this will be re-calculated based on the actual bed position
  133. auto magnitude2 = [&](const uint8_t i, const uint8_t j) {
  134. const xy_pos_t diff = z_stepper_align.xy[i] - z_stepper_align.xy[j];
  135. return HYPOT2(diff.x, diff.y);
  136. };
  137. float z_probe = Z_BASIC_CLEARANCE + (G34_MAX_GRADE) * 0.01f * SQRT(_MAX(0, magnitude2(0, 1)
  138. #if TRIPLE_Z
  139. , magnitude2(2, 1), magnitude2(2, 0)
  140. #if QUAD_Z
  141. , magnitude2(3, 2), magnitude2(3, 1), magnitude2(3, 0)
  142. #endif
  143. #endif
  144. ));
  145. // Home before the alignment procedure
  146. home_if_needed();
  147. // Move the Z coordinate realm towards the positive - dirty trick
  148. current_position.z += z_probe * 0.5f;
  149. sync_plan_position();
  150. // Now, the Z origin lies below the build plate. That allows to probe deeper, before run_z_probe throws an error.
  151. // This hack is un-done at the end of G34 - either by re-homing, or by using the probed heights of the last iteration.
  152. #if !HAS_Z_STEPPER_ALIGN_STEPPER_XY
  153. float last_z_align_move[NUM_Z_STEPPERS] = ARRAY_N_1(NUM_Z_STEPPERS, 10000.0f);
  154. #else
  155. float last_z_align_level_indicator = 10000.0f;
  156. #endif
  157. float z_measured[NUM_Z_STEPPERS] = { 0 },
  158. z_maxdiff = 0.0f,
  159. amplification = z_auto_align_amplification;
  160. #if !HAS_Z_STEPPER_ALIGN_STEPPER_XY
  161. bool adjustment_reverse = false;
  162. #endif
  163. #if HAS_STATUS_MESSAGE
  164. PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION);
  165. const uint8_t iter_str_len = strlen_P(msg_iteration);
  166. #endif
  167. // Final z and iteration values will be used after breaking the loop
  168. float z_measured_min;
  169. uint8_t iteration = 0;
  170. bool err_break = false; // To break out of nested loops
  171. while (iteration < z_auto_align_iterations) {
  172. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions.");
  173. const int iter = iteration + 1;
  174. SERIAL_ECHOLNPGM("\nG34 Iteration: ", iter);
  175. #if HAS_STATUS_MESSAGE
  176. char str[iter_str_len + 2 + 1];
  177. sprintf_P(str, msg_iteration, iter);
  178. ui.set_status(str);
  179. #endif
  180. // Initialize minimum value
  181. z_measured_min = 100000.0f;
  182. float z_measured_max = -100000.0f;
  183. // Probe all positions (one per Z-Stepper)
  184. LOOP_L_N(i, NUM_Z_STEPPERS) {
  185. // iteration odd/even --> downward / upward stepper sequence
  186. const uint8_t iprobe = (iteration & 1) ? NUM_Z_STEPPERS - 1 - i : i;
  187. // Safe clearance even on an incline
  188. if ((iteration == 0 || i > 0) && z_probe > current_position.z) do_blocking_move_to_z(z_probe);
  189. xy_pos_t &ppos = z_stepper_align.xy[iprobe];
  190. if (DEBUGGING(LEVELING))
  191. DEBUG_ECHOLNPGM_P(PSTR("Probing X"), ppos.x, SP_Y_STR, ppos.y);
  192. // Probe a Z height for each stepper.
  193. // Probing sanity check is disabled, as it would trigger even in normal cases because
  194. // current_position.z has been manually altered in the "dirty trick" above.
  195. const float z_probed_height = probe.probe_at_point(DIFF_TERN(HAS_HOME_OFFSET, ppos, xy_pos_t(home_offset)), raise_after, 0, true, false);
  196. if (isnan(z_probed_height)) {
  197. SERIAL_ECHOLNPGM("Probing failed");
  198. LCD_MESSAGE(MSG_LCD_PROBING_FAILED);
  199. err_break = true;
  200. break;
  201. }
  202. // Add height to each value, to provide a more useful target height for
  203. // the next iteration of probing. This allows adjustments to be made away from the bed.
  204. z_measured[iprobe] = z_probed_height + Z_CLEARANCE_BETWEEN_PROBES;
  205. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Z", iprobe + 1, " measured position is ", z_measured[iprobe]);
  206. // Remember the minimum measurement to calculate the correction later on
  207. z_measured_min = _MIN(z_measured_min, z_measured[iprobe]);
  208. z_measured_max = _MAX(z_measured_max, z_measured[iprobe]);
  209. } // for (i)
  210. if (err_break) break;
  211. // Adapt the next probe clearance height based on the new measurements.
  212. // Safe_height = lowest distance to bed (= highest measurement) plus highest measured misalignment.
  213. z_maxdiff = z_measured_max - z_measured_min;
  214. z_probe = Z_BASIC_CLEARANCE + z_measured_max + z_maxdiff;
  215. #if HAS_Z_STEPPER_ALIGN_STEPPER_XY
  216. // Replace the initial values in z_measured with calculated heights at
  217. // each stepper position. This allows the adjustment algorithm to be
  218. // shared between both possible probing mechanisms.
  219. // This must be done after the next z_probe height is calculated, so that
  220. // the height is calculated from actual print area positions, and not
  221. // extrapolated motor movements.
  222. // Compute the least-squares fit for all probed points.
  223. // Calculate the Z position of each stepper and store it in z_measured.
  224. // This allows the actual adjustment logic to be shared by both algorithms.
  225. linear_fit_data lfd;
  226. incremental_LSF_reset(&lfd);
  227. LOOP_L_N(i, NUM_Z_STEPPERS) {
  228. SERIAL_ECHOLNPGM("PROBEPT_", i, ": ", z_measured[i]);
  229. incremental_LSF(&lfd, z_stepper_align.xy[i], z_measured[i]);
  230. }
  231. finish_incremental_LSF(&lfd);
  232. z_measured_min = 100000.0f;
  233. LOOP_L_N(i, NUM_Z_STEPPERS) {
  234. z_measured[i] = -(lfd.A * z_stepper_align.stepper_xy[i].x + lfd.B * z_stepper_align.stepper_xy[i].y + lfd.D);
  235. z_measured_min = _MIN(z_measured_min, z_measured[i]);
  236. }
  237. SERIAL_ECHOLNPGM(
  238. LIST_N(DOUBLE(NUM_Z_STEPPERS),
  239. "Calculated Z1=", z_measured[0],
  240. " Z2=", z_measured[1],
  241. " Z3=", z_measured[2],
  242. " Z4=", z_measured[3]
  243. )
  244. );
  245. #endif
  246. SERIAL_ECHOLNPGM("\n"
  247. "Z2-Z1=", ABS(z_measured[1] - z_measured[0])
  248. #if TRIPLE_Z
  249. , " Z3-Z2=", ABS(z_measured[2] - z_measured[1])
  250. , " Z3-Z1=", ABS(z_measured[2] - z_measured[0])
  251. #if QUAD_Z
  252. , " Z4-Z3=", ABS(z_measured[3] - z_measured[2])
  253. , " Z4-Z2=", ABS(z_measured[3] - z_measured[1])
  254. , " Z4-Z1=", ABS(z_measured[3] - z_measured[0])
  255. #endif
  256. #endif
  257. );
  258. #if HAS_STATUS_MESSAGE
  259. char fstr1[10];
  260. char msg[6 + (6 + 5) * NUM_Z_STEPPERS + 1]
  261. #if TRIPLE_Z
  262. , fstr2[10], fstr3[10]
  263. #if QUAD_Z
  264. , fstr4[10], fstr5[10], fstr6[10]
  265. #endif
  266. #endif
  267. ;
  268. sprintf_P(msg,
  269. PSTR("1:2=%s" TERN_(TRIPLE_Z, " 3-2=%s 3-1=%s") TERN_(QUAD_Z, " 4-3=%s 4-2=%s 4-1=%s")),
  270. dtostrf(ABS(z_measured[1] - z_measured[0]), 1, 3, fstr1)
  271. OPTARG(TRIPLE_Z,
  272. dtostrf(ABS(z_measured[2] - z_measured[1]), 1, 3, fstr2),
  273. dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr3))
  274. OPTARG(QUAD_Z,
  275. dtostrf(ABS(z_measured[3] - z_measured[2]), 1, 3, fstr4),
  276. dtostrf(ABS(z_measured[3] - z_measured[1]), 1, 3, fstr5),
  277. dtostrf(ABS(z_measured[3] - z_measured[0]), 1, 3, fstr6))
  278. );
  279. ui.set_status(msg);
  280. #endif
  281. auto decreasing_accuracy = [](const_float_t v1, const_float_t v2) {
  282. if (v1 < v2 * 0.7f) {
  283. SERIAL_ECHOLNPGM("Decreasing Accuracy Detected.");
  284. LCD_MESSAGE(MSG_DECREASING_ACCURACY);
  285. return true;
  286. }
  287. return false;
  288. };
  289. #if HAS_Z_STEPPER_ALIGN_STEPPER_XY
  290. // Check if the applied corrections go in the correct direction.
  291. // Calculate the sum of the absolute deviations from the mean of the probe measurements.
  292. // Compare to the last iteration to ensure it's getting better.
  293. // Calculate mean value as a reference
  294. float z_measured_mean = 0.0f;
  295. LOOP_L_N(zstepper, NUM_Z_STEPPERS) z_measured_mean += z_measured[zstepper];
  296. z_measured_mean /= NUM_Z_STEPPERS;
  297. // Calculate the sum of the absolute deviations from the mean value
  298. float z_align_level_indicator = 0.0f;
  299. LOOP_L_N(zstepper, NUM_Z_STEPPERS)
  300. z_align_level_indicator += ABS(z_measured[zstepper] - z_measured_mean);
  301. // If it's getting worse, stop and throw an error
  302. err_break = decreasing_accuracy(last_z_align_level_indicator, z_align_level_indicator);
  303. if (err_break) break;
  304. last_z_align_level_indicator = z_align_level_indicator;
  305. #endif
  306. // The following correction actions are to be enabled for select Z-steppers only
  307. stepper.set_separate_multi_axis(true);
  308. bool success_break = true;
  309. // Correct the individual stepper offsets
  310. LOOP_L_N(zstepper, NUM_Z_STEPPERS) {
  311. // Calculate current stepper move
  312. float z_align_move = z_measured[zstepper] - z_measured_min;
  313. const float z_align_abs = ABS(z_align_move);
  314. #if !HAS_Z_STEPPER_ALIGN_STEPPER_XY
  315. // Optimize one iteration's correction based on the first measurements
  316. if (z_align_abs) amplification = (iteration == 1) ? _MIN(last_z_align_move[zstepper] / z_align_abs, 2.0f) : z_auto_align_amplification;
  317. // Check for less accuracy compared to last move
  318. if (decreasing_accuracy(last_z_align_move[zstepper], z_align_abs)) {
  319. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Z", zstepper + 1, " last_z_align_move = ", last_z_align_move[zstepper]);
  320. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Z", zstepper + 1, " z_align_abs = ", z_align_abs);
  321. adjustment_reverse = !adjustment_reverse;
  322. }
  323. // Remember the alignment for the next iteration, but only if steppers move,
  324. // otherwise it would be just zero (in case this stepper was at z_measured_min already)
  325. if (z_align_abs > 0) last_z_align_move[zstepper] = z_align_abs;
  326. #endif
  327. // Stop early if all measured points achieve accuracy target
  328. if (z_align_abs > z_auto_align_accuracy) success_break = false;
  329. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Z", zstepper + 1, " corrected by ", z_align_move);
  330. // Lock all steppers except one
  331. stepper.set_all_z_lock(true, zstepper);
  332. #if !HAS_Z_STEPPER_ALIGN_STEPPER_XY
  333. // Decreasing accuracy was detected so move was inverted.
  334. // Will match reversed Z steppers on dual steppers. Triple will need more work to map.
  335. if (adjustment_reverse) {
  336. z_align_move = -z_align_move;
  337. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Z", zstepper + 1, " correction reversed to ", z_align_move);
  338. }
  339. #endif
  340. // Do a move to correct part of the misalignment for the current stepper
  341. do_blocking_move_to_z(amplification * z_align_move + current_position.z);
  342. } // for (zstepper)
  343. // Back to normal stepper operations
  344. stepper.set_all_z_lock(false);
  345. stepper.set_separate_multi_axis(false);
  346. if (err_break) break;
  347. if (success_break) {
  348. SERIAL_ECHOLNPGM("Target accuracy achieved.");
  349. LCD_MESSAGE(MSG_ACCURACY_ACHIEVED);
  350. break;
  351. }
  352. iteration++;
  353. } // while (iteration < z_auto_align_iterations)
  354. if (err_break)
  355. SERIAL_ECHOLNPGM("G34 aborted.");
  356. else {
  357. SERIAL_ECHOLNPGM("Did ", iteration + (iteration != z_auto_align_iterations), " of ", z_auto_align_iterations);
  358. SERIAL_ECHOLNPAIR_F("Accuracy: ", z_maxdiff);
  359. }
  360. // Stow the probe because the last call to probe.probe_at_point(...)
  361. // leaves the probe deployed when it's successful.
  362. IF_DISABLED(TOUCH_MI_PROBE, probe.stow());
  363. #if ENABLED(HOME_AFTER_G34)
  364. // After this operation the z position needs correction
  365. set_axis_never_homed(Z_AXIS);
  366. // Home Z after the alignment procedure
  367. process_subcommands_now(F("G28Z"));
  368. #else
  369. // Use the probed height from the last iteration to determine the Z height.
  370. // z_measured_min is used, because all steppers are aligned to z_measured_min.
  371. // Ideally, this would be equal to the 'z_probe * 0.5f' which was added earlier.
  372. current_position.z -= z_measured_min - (float)Z_CLEARANCE_BETWEEN_PROBES;
  373. sync_plan_position();
  374. #endif
  375. // Restore the active tool after homing
  376. TERN_(HAS_MULTI_HOTEND, tool_change(old_tool_index, DISABLED(PARKING_EXTRUDER))); // Fetch previous tool for parking extruder
  377. #if BOTH(HAS_LEVELING, RESTORE_LEVELING_AFTER_G34)
  378. set_bed_leveling_enabled(leveling_was_active);
  379. #endif
  380. }while(0);
  381. #endif // Z_STEPPER_AUTO_ALIGN
  382. }
  383. #endif // Z_MULTI_ENDSTOPS || Z_STEPPER_AUTO_ALIGN
  384. #if ENABLED(Z_STEPPER_AUTO_ALIGN)
  385. /**
  386. * M422: Set a Z-Stepper automatic alignment XY point.
  387. * Use repeatedly to set multiple points.
  388. *
  389. * S<index> : Index of the probe point to set
  390. *
  391. * With Z_STEPPER_ALIGN_STEPPER_XY:
  392. * W<index> : Index of the Z stepper position to set
  393. * The W and S parameters may not be combined.
  394. *
  395. * S and W require an X and/or Y parameter
  396. * X<pos> : X position to set (Unchanged if omitted)
  397. * Y<pos> : Y position to set (Unchanged if omitted)
  398. *
  399. * R : Recalculate points based on current probe offsets
  400. */
  401. void GcodeSuite::M422() {
  402. if (!parser.seen_any()) return M422_report();
  403. if (parser.seen('R')) {
  404. z_stepper_align.reset_to_default();
  405. return;
  406. }
  407. const bool is_probe_point = parser.seen_test('S');
  408. if (TERN0(HAS_Z_STEPPER_ALIGN_STEPPER_XY, is_probe_point && parser.seen_test('W'))) {
  409. SERIAL_ECHOLNPGM("?(S) and (W) may not be combined.");
  410. return;
  411. }
  412. xy_pos_t * const pos_dest = (
  413. TERN_(HAS_Z_STEPPER_ALIGN_STEPPER_XY, !is_probe_point ? z_stepper_align.stepper_xy :)
  414. z_stepper_align.xy
  415. );
  416. if (!is_probe_point && TERN1(HAS_Z_STEPPER_ALIGN_STEPPER_XY, !parser.seen_test('W'))) {
  417. SERIAL_ECHOLNPGM("?(S)" TERN_(HAS_Z_STEPPER_ALIGN_STEPPER_XY, " or (W)") " is required.");
  418. return;
  419. }
  420. // Get the Probe Position Index or Z Stepper Index
  421. int8_t position_index = 1;
  422. FSTR_P err_string = F("?(S) Probe-position");
  423. if (is_probe_point)
  424. position_index = parser.intval('S');
  425. else {
  426. #if HAS_Z_STEPPER_ALIGN_STEPPER_XY
  427. err_string = F("?(W) Z-stepper");
  428. position_index = parser.intval('W');
  429. #endif
  430. }
  431. if (!WITHIN(position_index, 1, NUM_Z_STEPPERS)) {
  432. SERIAL_ECHOF(err_string);
  433. SERIAL_ECHOLNPGM(" index invalid (1.." STRINGIFY(NUM_Z_STEPPERS) ").");
  434. return;
  435. }
  436. --position_index;
  437. const xy_pos_t pos = {
  438. parser.floatval('X', pos_dest[position_index].x),
  439. parser.floatval('Y', pos_dest[position_index].y)
  440. };
  441. if (is_probe_point) {
  442. if (!probe.can_reach(pos.x, Y_CENTER)) {
  443. SERIAL_ECHOLNPGM("?(X) out of bounds.");
  444. return;
  445. }
  446. if (!probe.can_reach(pos)) {
  447. SERIAL_ECHOLNPGM("?(Y) out of bounds.");
  448. return;
  449. }
  450. }
  451. pos_dest[position_index] = pos;
  452. }
  453. void GcodeSuite::M422_report(const bool forReplay/*=true*/) {
  454. report_heading(forReplay, F(STR_Z_AUTO_ALIGN));
  455. LOOP_L_N(i, NUM_Z_STEPPERS) {
  456. report_echo_start(forReplay);
  457. SERIAL_ECHOLNPGM_P(
  458. PSTR(" M422 S"), i + 1,
  459. SP_X_STR, z_stepper_align.xy[i].x,
  460. SP_Y_STR, z_stepper_align.xy[i].y
  461. );
  462. }
  463. #if HAS_Z_STEPPER_ALIGN_STEPPER_XY
  464. LOOP_L_N(i, NUM_Z_STEPPERS) {
  465. report_echo_start(forReplay);
  466. SERIAL_ECHOLNPGM_P(
  467. PSTR(" M422 W"), i + 1,
  468. SP_X_STR, z_stepper_align.stepper_xy[i].x,
  469. SP_Y_STR, z_stepper_align.stepper_xy[i].y
  470. );
  471. }
  472. #endif
  473. }
  474. #endif // Z_STEPPER_AUTO_ALIGN