My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

G34_M422.cpp 21KB

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