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

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