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.

G29.cpp 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. /**
  23. * G29.cpp - Auto Bed Leveling
  24. */
  25. #include "../../../inc/MarlinConfig.h"
  26. #if HAS_ABL_NOT_UBL
  27. #include "../../gcode.h"
  28. #include "../../../feature/bedlevel/bedlevel.h"
  29. #include "../../../module/motion.h"
  30. #include "../../../module/planner.h"
  31. #include "../../../module/stepper.h"
  32. #include "../../../module/probe.h"
  33. #include "../../queue.h"
  34. #if ENABLED(PROBE_TEMP_COMPENSATION)
  35. #include "../../../feature/probe_temp_comp.h"
  36. #include "../../../module/temperature.h"
  37. #endif
  38. #if HAS_DISPLAY
  39. #include "../../../lcd/marlinui.h"
  40. #endif
  41. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  42. #include "../../../libs/least_squares_fit.h"
  43. #endif
  44. #if ABL_PLANAR
  45. #include "../../../libs/vector_3.h"
  46. #endif
  47. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  48. #include "../../../core/debug_out.h"
  49. #if ENABLED(EXTENSIBLE_UI)
  50. #include "../../../lcd/extui/ui_api.h"
  51. #endif
  52. #if ENABLED(DWIN_CREALITY_LCD)
  53. #include "../../../lcd/dwin/e3v2/dwin.h"
  54. #endif
  55. #if HAS_MULTI_HOTEND
  56. #include "../../../module/tool_change.h"
  57. #endif
  58. #if ABL_GRID
  59. #if ENABLED(PROBE_Y_FIRST)
  60. #define PR_OUTER_VAR meshCount.x
  61. #define PR_OUTER_END abl_grid_points.x
  62. #define PR_INNER_VAR meshCount.y
  63. #define PR_INNER_END abl_grid_points.y
  64. #else
  65. #define PR_OUTER_VAR meshCount.y
  66. #define PR_OUTER_END abl_grid_points.y
  67. #define PR_INNER_VAR meshCount.x
  68. #define PR_INNER_END abl_grid_points.x
  69. #endif
  70. #endif
  71. #define G29_RETURN(b) return TERN_(G29_RETRY_AND_RECOVER, b)
  72. /**
  73. * G29: Detailed Z probe, probes the bed at 3 or more points.
  74. * Will fail if the printer has not been homed with G28.
  75. *
  76. * Enhanced G29 Auto Bed Leveling Probe Routine
  77. *
  78. * O Auto-level only if needed
  79. *
  80. * D Dry-Run mode. Just evaluate the bed Topology - Don't apply
  81. * or alter the bed level data. Useful to check the topology
  82. * after a first run of G29.
  83. *
  84. * J Jettison current bed leveling data
  85. *
  86. * V Set the verbose level (0-4). Example: "G29 V3"
  87. *
  88. * Parameters With LINEAR leveling only:
  89. *
  90. * P Set the size of the grid that will be probed (P x P points).
  91. * Example: "G29 P4"
  92. *
  93. * X Set the X size of the grid that will be probed (X x Y points).
  94. * Example: "G29 X7 Y5"
  95. *
  96. * Y Set the Y size of the grid that will be probed (X x Y points).
  97. *
  98. * T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
  99. * This is useful for manual bed leveling and finding flaws in the bed (to
  100. * assist with part placement).
  101. * Not supported by non-linear delta printer bed leveling.
  102. *
  103. * Parameters With LINEAR and BILINEAR leveling only:
  104. *
  105. * S Set the XY travel speed between probe points (in units/min)
  106. *
  107. * H Set bounds to a centered square H x H units in size
  108. *
  109. * -or-
  110. *
  111. * F Set the Front limit of the probing grid
  112. * B Set the Back limit of the probing grid
  113. * L Set the Left limit of the probing grid
  114. * R Set the Right limit of the probing grid
  115. *
  116. * Parameters with DEBUG_LEVELING_FEATURE only:
  117. *
  118. * C Make a totally fake grid with no actual probing.
  119. * For use in testing when no probing is possible.
  120. *
  121. * Parameters with BILINEAR leveling only:
  122. *
  123. * Z Supply an additional Z probe offset
  124. *
  125. * Extra parameters with PROBE_MANUALLY:
  126. *
  127. * To do manual probing simply repeat G29 until the procedure is complete.
  128. * The first G29 accepts parameters. 'G29 Q' for status, 'G29 A' to abort.
  129. *
  130. * Q Query leveling and G29 state
  131. *
  132. * A Abort current leveling procedure
  133. *
  134. * Extra parameters with BILINEAR only:
  135. *
  136. * W Write a mesh point. (If G29 is idle.)
  137. * I X index for mesh point
  138. * J Y index for mesh point
  139. * X X for mesh point, overrides I
  140. * Y Y for mesh point, overrides J
  141. * Z Z for mesh point. Otherwise, raw current Z.
  142. *
  143. * Without PROBE_MANUALLY:
  144. *
  145. * E By default G29 will engage the Z probe, test the bed, then disengage.
  146. * Include "E" to engage/disengage the Z probe for each sample.
  147. * There's no extra effect if you have a fixed Z probe.
  148. */
  149. G29_TYPE GcodeSuite::G29() {
  150. reset_stepper_timeout();
  151. const bool seenQ = EITHER(DEBUG_LEVELING_FEATURE, PROBE_MANUALLY) && parser.seen('Q');
  152. // G29 Q is also available if debugging
  153. #if ENABLED(DEBUG_LEVELING_FEATURE)
  154. const uint8_t old_debug_flags = marlin_debug_flags;
  155. if (seenQ) marlin_debug_flags |= MARLIN_DEBUG_LEVELING;
  156. DEBUG_SECTION(log_G29, "G29", DEBUGGING(LEVELING));
  157. if (DEBUGGING(LEVELING)) log_machine_info();
  158. marlin_debug_flags = old_debug_flags;
  159. if (DISABLED(PROBE_MANUALLY) && seenQ) G29_RETURN(false);
  160. #endif
  161. const bool seenA = TERN0(PROBE_MANUALLY, parser.seen('A')),
  162. no_action = seenA || seenQ,
  163. faux = ENABLED(DEBUG_LEVELING_FEATURE) && DISABLED(PROBE_MANUALLY) ? parser.boolval('C') : no_action;
  164. if (!no_action && planner.leveling_active && parser.boolval('O')) { // Auto-level only if needed
  165. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Auto-level not needed, skip");
  166. G29_RETURN(false);
  167. }
  168. // Send 'N' to force homing before G29 (internal only)
  169. if (parser.seen('N'))
  170. process_subcommands_now_P(TERN(G28_L0_ENSURES_LEVELING_OFF, PSTR("G28L0"), G28_STR));
  171. // Don't allow auto-leveling without homing first
  172. if (homing_needed_error()) G29_RETURN(false);
  173. // Define local vars 'static' for manual probing, 'auto' otherwise
  174. #define ABL_VAR TERN_(PROBE_MANUALLY, static)
  175. ABL_VAR int verbose_level;
  176. ABL_VAR xy_pos_t probePos;
  177. ABL_VAR float measured_z;
  178. ABL_VAR bool dryrun, abl_should_enable;
  179. #if EITHER(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
  180. ABL_VAR int abl_probe_index;
  181. #endif
  182. #if ABL_GRID
  183. #if ENABLED(PROBE_MANUALLY)
  184. ABL_VAR xy_int8_t meshCount;
  185. #endif
  186. ABL_VAR xy_pos_t probe_position_lf, probe_position_rb;
  187. ABL_VAR xy_float_t gridSpacing = { 0, 0 };
  188. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  189. ABL_VAR bool do_topography_map;
  190. ABL_VAR xy_uint8_t abl_grid_points;
  191. #else // Bilinear
  192. constexpr xy_uint8_t abl_grid_points = { GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y };
  193. #endif
  194. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  195. ABL_VAR int abl_points;
  196. #else
  197. int constexpr abl_points = GRID_MAX_POINTS;
  198. #endif
  199. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  200. ABL_VAR float zoffset;
  201. #elif ENABLED(AUTO_BED_LEVELING_LINEAR)
  202. ABL_VAR int indexIntoAB[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  203. ABL_VAR float eqnAMatrix[(GRID_MAX_POINTS) * 3], // "A" matrix of the linear system of equations
  204. eqnBVector[GRID_MAX_POINTS], // "B" vector of Z points
  205. mean;
  206. #endif
  207. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  208. #if ENABLED(PROBE_MANUALLY)
  209. int constexpr abl_points = 3; // used to show total points
  210. #endif
  211. vector_3 points[3];
  212. probe.get_three_points(points);
  213. #endif // AUTO_BED_LEVELING_3POINT
  214. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  215. struct linear_fit_data lsf_results;
  216. #endif
  217. /**
  218. * On the initial G29 fetch command parameters.
  219. */
  220. if (!g29_in_progress) {
  221. TERN_(HAS_MULTI_HOTEND, if (active_extruder) tool_change(0));
  222. #if EITHER(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
  223. abl_probe_index = -1;
  224. #endif
  225. abl_should_enable = planner.leveling_active;
  226. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  227. const bool seen_w = parser.seen('W');
  228. if (seen_w) {
  229. if (!leveling_is_valid()) {
  230. SERIAL_ERROR_MSG("No bilinear grid");
  231. G29_RETURN(false);
  232. }
  233. const float rz = parser.seenval('Z') ? RAW_Z_POSITION(parser.value_linear_units()) : current_position.z;
  234. if (!WITHIN(rz, -10, 10)) {
  235. SERIAL_ERROR_MSG("Bad Z value");
  236. G29_RETURN(false);
  237. }
  238. const float rx = RAW_X_POSITION(parser.linearval('X', NAN)),
  239. ry = RAW_Y_POSITION(parser.linearval('Y', NAN));
  240. int8_t i = parser.byteval('I', -1), j = parser.byteval('J', -1);
  241. if (!isnan(rx) && !isnan(ry)) {
  242. // Get nearest i / j from rx / ry
  243. i = (rx - bilinear_start.x + 0.5 * gridSpacing.x) / gridSpacing.x;
  244. j = (ry - bilinear_start.y + 0.5 * gridSpacing.y) / gridSpacing.y;
  245. LIMIT(i, 0, GRID_MAX_POINTS_X - 1);
  246. LIMIT(j, 0, GRID_MAX_POINTS_Y - 1);
  247. }
  248. if (WITHIN(i, 0, GRID_MAX_POINTS_X - 1) && WITHIN(j, 0, GRID_MAX_POINTS_Y)) {
  249. set_bed_leveling_enabled(false);
  250. z_values[i][j] = rz;
  251. TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
  252. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(i, j, rz));
  253. set_bed_leveling_enabled(abl_should_enable);
  254. if (abl_should_enable) report_current_position();
  255. }
  256. G29_RETURN(false);
  257. } // parser.seen('W')
  258. #else
  259. constexpr bool seen_w = false;
  260. #endif
  261. // Jettison bed leveling data
  262. if (!seen_w && parser.seen('J')) {
  263. reset_bed_level();
  264. G29_RETURN(false);
  265. }
  266. verbose_level = parser.intval('V');
  267. if (!WITHIN(verbose_level, 0, 4)) {
  268. SERIAL_ECHOLNPGM("?(V)erbose level implausible (0-4).");
  269. G29_RETURN(false);
  270. }
  271. dryrun = parser.boolval('D') || TERN0(PROBE_MANUALLY, no_action);
  272. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  273. incremental_LSF_reset(&lsf_results);
  274. do_topography_map = verbose_level > 2 || parser.boolval('T');
  275. // X and Y specify points in each direction, overriding the default
  276. // These values may be saved with the completed mesh
  277. abl_grid_points.set(
  278. parser.byteval('X', GRID_MAX_POINTS_X),
  279. parser.byteval('Y', GRID_MAX_POINTS_Y)
  280. );
  281. if (parser.seenval('P')) abl_grid_points.x = abl_grid_points.y = parser.value_int();
  282. if (!WITHIN(abl_grid_points.x, 2, GRID_MAX_POINTS_X)) {
  283. SERIAL_ECHOLNPGM("?Probe points (X) implausible (2-" STRINGIFY(GRID_MAX_POINTS_X) ").");
  284. G29_RETURN(false);
  285. }
  286. if (!WITHIN(abl_grid_points.y, 2, GRID_MAX_POINTS_Y)) {
  287. SERIAL_ECHOLNPGM("?Probe points (Y) implausible (2-" STRINGIFY(GRID_MAX_POINTS_Y) ").");
  288. G29_RETURN(false);
  289. }
  290. abl_points = abl_grid_points.x * abl_grid_points.y;
  291. mean = 0;
  292. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  293. zoffset = parser.linearval('Z');
  294. #endif
  295. #if ABL_GRID
  296. xy_probe_feedrate_mm_s = MMM_TO_MMS(parser.linearval('S', XY_PROBE_SPEED));
  297. const float x_min = probe.min_x(), x_max = probe.max_x(),
  298. y_min = probe.min_y(), y_max = probe.max_y();
  299. if (parser.seen('H')) {
  300. const int16_t size = (int16_t)parser.value_linear_units();
  301. probe_position_lf.set(_MAX((X_CENTER) - size / 2, x_min), _MAX((Y_CENTER) - size / 2, y_min));
  302. probe_position_rb.set(_MIN(probe_position_lf.x + size, x_max), _MIN(probe_position_lf.y + size, y_max));
  303. }
  304. else {
  305. probe_position_lf.set(parser.linearval('L', x_min), parser.linearval('F', y_min));
  306. probe_position_rb.set(parser.linearval('R', x_max), parser.linearval('B', y_max));
  307. }
  308. if (!probe.good_bounds(probe_position_lf, probe_position_rb)) {
  309. if (DEBUGGING(LEVELING)) {
  310. DEBUG_ECHOLNPAIR("G29 L", probe_position_lf.x, " R", probe_position_rb.x,
  311. " F", probe_position_lf.y, " B", probe_position_rb.y);
  312. }
  313. SERIAL_ECHOLNPGM("? (L,R,F,B) out of bounds.");
  314. G29_RETURN(false);
  315. }
  316. // Probe at the points of a lattice grid
  317. gridSpacing.set((probe_position_rb.x - probe_position_lf.x) / (abl_grid_points.x - 1),
  318. (probe_position_rb.y - probe_position_lf.y) / (abl_grid_points.y - 1));
  319. #endif // ABL_GRID
  320. if (verbose_level > 0) {
  321. SERIAL_ECHOPGM("G29 Auto Bed Leveling");
  322. if (dryrun) SERIAL_ECHOPGM(" (DRYRUN)");
  323. SERIAL_EOL();
  324. }
  325. planner.synchronize();
  326. #if ENABLED(AUTO_BED_LEVELING_3POINT)
  327. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> 3-point Leveling");
  328. points[0].z = points[1].z = points[2].z = 0; // Probe at 3 arbitrary points
  329. #endif
  330. #if BOTH(AUTO_BED_LEVELING_BILINEAR, EXTENSIBLE_UI)
  331. ExtUI::onMeshLevelingStart();
  332. #endif
  333. if (!faux) {
  334. remember_feedrate_scaling_off();
  335. #if ENABLED(PREHEAT_BEFORE_LEVELING)
  336. if (!dryrun) probe.preheat_for_probing(LEVELING_NOZZLE_TEMP, LEVELING_BED_TEMP);
  337. #endif
  338. }
  339. // Disable auto bed leveling during G29.
  340. // Be formal so G29 can be done successively without G28.
  341. if (!no_action) set_bed_leveling_enabled(false);
  342. // Deploy certain probes before starting probing
  343. #if HAS_BED_PROBE
  344. if (ENABLED(BLTOUCH))
  345. do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE);
  346. else if (probe.deploy()) {
  347. set_bed_leveling_enabled(abl_should_enable);
  348. G29_RETURN(false);
  349. }
  350. #endif
  351. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  352. if (TERN1(PROBE_MANUALLY, !no_action)
  353. && (gridSpacing != bilinear_grid_spacing || probe_position_lf != bilinear_start)
  354. ) {
  355. // Reset grid to 0.0 or "not probed". (Also disables ABL)
  356. reset_bed_level();
  357. // Initialize a grid with the given dimensions
  358. bilinear_grid_spacing = gridSpacing;
  359. bilinear_start = probe_position_lf;
  360. // Can't re-enable (on error) until the new grid is written
  361. abl_should_enable = false;
  362. }
  363. #endif // AUTO_BED_LEVELING_BILINEAR
  364. } // !g29_in_progress
  365. #if ENABLED(PROBE_MANUALLY)
  366. // For manual probing, get the next index to probe now.
  367. // On the first probe this will be incremented to 0.
  368. if (!no_action) {
  369. ++abl_probe_index;
  370. g29_in_progress = true;
  371. }
  372. // Abort current G29 procedure, go back to idle state
  373. if (seenA && g29_in_progress) {
  374. SERIAL_ECHOLNPGM("Manual G29 aborted");
  375. SET_SOFT_ENDSTOP_LOOSE(false);
  376. set_bed_leveling_enabled(abl_should_enable);
  377. g29_in_progress = false;
  378. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  379. }
  380. // Query G29 status
  381. if (verbose_level || seenQ) {
  382. SERIAL_ECHOPGM("Manual G29 ");
  383. if (g29_in_progress) {
  384. SERIAL_ECHOPAIR("point ", _MIN(abl_probe_index + 1, abl_points));
  385. SERIAL_ECHOLNPAIR(" of ", abl_points);
  386. }
  387. else
  388. SERIAL_ECHOLNPGM("idle");
  389. }
  390. if (no_action) G29_RETURN(false);
  391. if (abl_probe_index == 0) {
  392. // For the initial G29 S2 save software endstop state
  393. SET_SOFT_ENDSTOP_LOOSE(true);
  394. // Move close to the bed before the first point
  395. do_blocking_move_to_z(0);
  396. }
  397. else {
  398. #if EITHER(AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_3POINT)
  399. const uint16_t index = abl_probe_index - 1;
  400. #endif
  401. // For G29 after adjusting Z.
  402. // Save the previous Z before going to the next point
  403. measured_z = current_position.z;
  404. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  405. mean += measured_z;
  406. eqnBVector[index] = measured_z;
  407. eqnAMatrix[index + 0 * abl_points] = probePos.x;
  408. eqnAMatrix[index + 1 * abl_points] = probePos.y;
  409. eqnAMatrix[index + 2 * abl_points] = 1;
  410. incremental_LSF(&lsf_results, probePos, measured_z);
  411. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  412. points[index].z = measured_z;
  413. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  414. const float newz = measured_z + zoffset;
  415. z_values[meshCount.x][meshCount.y] = newz;
  416. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(meshCount, newz));
  417. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR_P(PSTR("Save X"), meshCount.x, SP_Y_STR, meshCount.y, SP_Z_STR, measured_z + zoffset);
  418. #endif
  419. }
  420. //
  421. // If there's another point to sample, move there with optional lift.
  422. //
  423. #if ABL_GRID
  424. // Skip any unreachable points
  425. while (abl_probe_index < abl_points) {
  426. // Set meshCount.x, meshCount.y based on abl_probe_index, with zig-zag
  427. PR_OUTER_VAR = abl_probe_index / PR_INNER_END;
  428. PR_INNER_VAR = abl_probe_index - (PR_OUTER_VAR * PR_INNER_END);
  429. // Probe in reverse order for every other row/column
  430. const bool zig = (PR_OUTER_VAR & 1); // != ((PR_OUTER_END) & 1);
  431. if (zig) PR_INNER_VAR = (PR_INNER_END - 1) - PR_INNER_VAR;
  432. probePos = probe_position_lf + gridSpacing * meshCount.asFloat();
  433. TERN_(AUTO_BED_LEVELING_LINEAR, indexIntoAB[meshCount.x][meshCount.y] = abl_probe_index);
  434. // Keep looping till a reachable point is found
  435. if (position_is_reachable(probePos)) break;
  436. ++abl_probe_index;
  437. }
  438. // Is there a next point to move to?
  439. if (abl_probe_index < abl_points) {
  440. _manual_goto_xy(probePos); // Can be used here too!
  441. // Disable software endstops to allow manual adjustment
  442. // If G29 is not completed, they will not be re-enabled
  443. SET_SOFT_ENDSTOP_LOOSE(true);
  444. G29_RETURN(false);
  445. }
  446. else {
  447. // Leveling done! Fall through to G29 finishing code below
  448. SERIAL_ECHOLNPGM("Grid probing done.");
  449. // Re-enable software endstops, if needed
  450. SET_SOFT_ENDSTOP_LOOSE(false);
  451. }
  452. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  453. // Probe at 3 arbitrary points
  454. if (abl_probe_index < abl_points) {
  455. probePos = points[abl_probe_index];
  456. _manual_goto_xy(probePos);
  457. // Disable software endstops to allow manual adjustment
  458. // If G29 is not completed, they will not be re-enabled
  459. SET_SOFT_ENDSTOP_LOOSE(true);
  460. G29_RETURN(false);
  461. }
  462. else {
  463. SERIAL_ECHOLNPGM("3-point probing done.");
  464. // Re-enable software endstops, if needed
  465. SET_SOFT_ENDSTOP_LOOSE(false);
  466. if (!dryrun) {
  467. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  468. if (planeNormal.z < 0) planeNormal *= -1;
  469. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  470. // Can't re-enable (on error) until the new grid is written
  471. abl_should_enable = false;
  472. }
  473. }
  474. #endif // AUTO_BED_LEVELING_3POINT
  475. #else // !PROBE_MANUALLY
  476. {
  477. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  478. measured_z = 0;
  479. #if ABL_GRID
  480. bool zig = PR_OUTER_END & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION
  481. measured_z = 0;
  482. xy_int8_t meshCount;
  483. // Outer loop is X with PROBE_Y_FIRST enabled
  484. // Outer loop is Y with PROBE_Y_FIRST disabled
  485. for (PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_END && !isnan(measured_z); PR_OUTER_VAR++) {
  486. int8_t inStart, inStop, inInc;
  487. if (zig) { // Zig away from origin
  488. inStart = 0; // Left or front
  489. inStop = PR_INNER_END; // Right or back
  490. inInc = 1; // Zig right
  491. }
  492. else { // Zag towards origin
  493. inStart = PR_INNER_END - 1; // Right or back
  494. inStop = -1; // Left or front
  495. inInc = -1; // Zag left
  496. }
  497. zig ^= true; // zag
  498. // An index to print current state
  499. uint8_t pt_index = (PR_OUTER_VAR) * (PR_INNER_END) + 1;
  500. // Inner loop is Y with PROBE_Y_FIRST enabled
  501. // Inner loop is X with PROBE_Y_FIRST disabled
  502. for (PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; pt_index++, PR_INNER_VAR += inInc) {
  503. probePos = probe_position_lf + gridSpacing * meshCount.asFloat();
  504. TERN_(AUTO_BED_LEVELING_LINEAR, indexIntoAB[meshCount.x][meshCount.y] = ++abl_probe_index); // 0...
  505. // Avoid probing outside the round or hexagonal area
  506. if (TERN0(IS_KINEMATIC, !probe.can_reach(probePos))) continue;
  507. if (verbose_level) SERIAL_ECHOLNPAIR("Probing mesh point ", int(pt_index), "/", abl_points, ".");
  508. TERN_(HAS_DISPLAY, ui.status_printf_P(0, PSTR(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_MESH), int(pt_index), int(abl_points)));
  509. measured_z = faux ? 0.001f * random(-100, 101) : probe.probe_at_point(probePos, raise_after, verbose_level);
  510. if (isnan(measured_z)) {
  511. set_bed_leveling_enabled(abl_should_enable);
  512. break; // Breaks out of both loops
  513. }
  514. #if ENABLED(PROBE_TEMP_COMPENSATION)
  515. temp_comp.compensate_measurement(TSI_BED, thermalManager.degBed(), measured_z);
  516. temp_comp.compensate_measurement(TSI_PROBE, thermalManager.degProbe(), measured_z);
  517. TERN_(USE_TEMP_EXT_COMPENSATION, temp_comp.compensate_measurement(TSI_EXT, thermalManager.degHotend(), measured_z));
  518. #endif
  519. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  520. mean += measured_z;
  521. eqnBVector[abl_probe_index] = measured_z;
  522. eqnAMatrix[abl_probe_index + 0 * abl_points] = probePos.x;
  523. eqnAMatrix[abl_probe_index + 1 * abl_points] = probePos.y;
  524. eqnAMatrix[abl_probe_index + 2 * abl_points] = 1;
  525. incremental_LSF(&lsf_results, probePos, measured_z);
  526. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  527. const float z = measured_z + zoffset;
  528. z_values[meshCount.x][meshCount.y] = z;
  529. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(meshCount, z));
  530. #endif
  531. abl_should_enable = false;
  532. idle_no_sleep();
  533. } // inner
  534. } // outer
  535. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  536. // Probe at 3 arbitrary points
  537. LOOP_L_N(i, 3) {
  538. if (verbose_level) SERIAL_ECHOLNPAIR("Probing point ", int(i + 1), "/3.");
  539. TERN_(HAS_DISPLAY, ui.status_printf_P(0, PSTR(S_FMT " %i/3"), GET_TEXT(MSG_PROBING_MESH), int(i + 1)));
  540. // Retain the last probe position
  541. probePos = points[i];
  542. measured_z = faux ? 0.001 * random(-100, 101) : probe.probe_at_point(probePos, raise_after, verbose_level);
  543. if (isnan(measured_z)) {
  544. set_bed_leveling_enabled(abl_should_enable);
  545. break;
  546. }
  547. points[i].z = measured_z;
  548. }
  549. if (!dryrun && !isnan(measured_z)) {
  550. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  551. if (planeNormal.z < 0) planeNormal *= -1;
  552. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  553. // Can't re-enable (on error) until the new grid is written
  554. abl_should_enable = false;
  555. }
  556. #endif // AUTO_BED_LEVELING_3POINT
  557. TERN_(HAS_DISPLAY, ui.reset_status());
  558. // Stow the probe. No raise for FIX_MOUNTED_PROBE.
  559. if (probe.stow()) {
  560. set_bed_leveling_enabled(abl_should_enable);
  561. measured_z = NAN;
  562. }
  563. }
  564. #endif // !PROBE_MANUALLY
  565. //
  566. // G29 Finishing Code
  567. //
  568. // Unless this is a dry run, auto bed leveling will
  569. // definitely be enabled after this point.
  570. //
  571. // If code above wants to continue leveling, it should
  572. // return or loop before this point.
  573. //
  574. if (DEBUGGING(LEVELING)) DEBUG_POS("> probing complete", current_position);
  575. #if ENABLED(PROBE_MANUALLY)
  576. g29_in_progress = false;
  577. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  578. #endif
  579. // Calculate leveling, print reports, correct the position
  580. if (!isnan(measured_z)) {
  581. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  582. if (!dryrun) extrapolate_unprobed_bed_level();
  583. print_bilinear_leveling_grid();
  584. refresh_bed_level();
  585. TERN_(ABL_BILINEAR_SUBDIVISION, print_bilinear_leveling_grid_virt());
  586. #elif ENABLED(AUTO_BED_LEVELING_LINEAR)
  587. // For LINEAR leveling calculate matrix, print reports, correct the position
  588. /**
  589. * solve the plane equation ax + by + d = z
  590. * A is the matrix with rows [x y 1] for all the probed points
  591. * B is the vector of the Z positions
  592. * the normal vector to the plane is formed by the coefficients of the
  593. * plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  594. * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  595. */
  596. struct { float a, b, d; } plane_equation_coefficients;
  597. finish_incremental_LSF(&lsf_results);
  598. plane_equation_coefficients.a = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below
  599. plane_equation_coefficients.b = -lsf_results.B; // but that is not yet tested.
  600. plane_equation_coefficients.d = -lsf_results.D;
  601. mean /= abl_points;
  602. if (verbose_level) {
  603. SERIAL_ECHOPAIR_F("Eqn coefficients: a: ", plane_equation_coefficients.a, 8);
  604. SERIAL_ECHOPAIR_F(" b: ", plane_equation_coefficients.b, 8);
  605. SERIAL_ECHOPAIR_F(" d: ", plane_equation_coefficients.d, 8);
  606. if (verbose_level > 2)
  607. SERIAL_ECHOPAIR_F("\nMean of sampled points: ", mean, 8);
  608. SERIAL_EOL();
  609. }
  610. // Create the matrix but don't correct the position yet
  611. if (!dryrun)
  612. planner.bed_level_matrix = matrix_3x3::create_look_at(
  613. vector_3(-plane_equation_coefficients.a, -plane_equation_coefficients.b, 1) // We can eliminate the '-' here and up above
  614. );
  615. // Show the Topography map if enabled
  616. if (do_topography_map) {
  617. float min_diff = 999;
  618. auto print_topo_map = [&](PGM_P const title, const bool get_min) {
  619. serialprintPGM(title);
  620. for (int8_t yy = abl_grid_points.y - 1; yy >= 0; yy--) {
  621. LOOP_L_N(xx, abl_grid_points.x) {
  622. const int ind = indexIntoAB[xx][yy];
  623. xyz_float_t tmp = { eqnAMatrix[ind + 0 * abl_points],
  624. eqnAMatrix[ind + 1 * abl_points], 0 };
  625. apply_rotation_xyz(planner.bed_level_matrix, tmp);
  626. if (get_min) NOMORE(min_diff, eqnBVector[ind] - tmp.z);
  627. const float subval = get_min ? mean : tmp.z + min_diff,
  628. diff = eqnBVector[ind] - subval;
  629. SERIAL_CHAR(' '); if (diff >= 0.0) SERIAL_CHAR('+'); // Include + for column alignment
  630. SERIAL_ECHO_F(diff, 5);
  631. } // xx
  632. SERIAL_EOL();
  633. } // yy
  634. SERIAL_EOL();
  635. };
  636. print_topo_map(PSTR("\nBed Height Topography:\n"
  637. " +--- BACK --+\n"
  638. " | |\n"
  639. " L | (+) | R\n"
  640. " E | | I\n"
  641. " F | (-) N (+) | G\n"
  642. " T | | H\n"
  643. " | (-) | T\n"
  644. " | |\n"
  645. " O-- FRONT --+\n"
  646. " (0,0)\n"), true);
  647. if (verbose_level > 3)
  648. print_topo_map(PSTR("\nCorrected Bed Height vs. Bed Topology:\n"), false);
  649. } //do_topography_map
  650. #endif // AUTO_BED_LEVELING_LINEAR
  651. #if ABL_PLANAR
  652. // For LINEAR and 3POINT leveling correct the current position
  653. if (verbose_level > 0)
  654. planner.bed_level_matrix.debug(PSTR("\n\nBed Level Correction Matrix:"));
  655. if (!dryrun) {
  656. //
  657. // Correct the current XYZ position based on the tilted plane.
  658. //
  659. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position);
  660. xyze_pos_t converted = current_position;
  661. planner.force_unapply_leveling(converted); // use conversion machinery
  662. // Use the last measured distance to the bed, if possible
  663. if ( NEAR(current_position.x, probePos.x - probe.offset_xy.x)
  664. && NEAR(current_position.y, probePos.y - probe.offset_xy.y)
  665. ) {
  666. const float simple_z = current_position.z - measured_z;
  667. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probed Z", simple_z, " Matrix Z", converted.z, " Discrepancy ", simple_z - converted.z);
  668. converted.z = simple_z;
  669. }
  670. // The rotated XY and corrected Z are now current_position
  671. current_position = converted;
  672. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
  673. }
  674. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  675. if (!dryrun) {
  676. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("G29 uncorrected Z:", current_position.z);
  677. // Unapply the offset because it is going to be immediately applied
  678. // and cause compensation movement in Z
  679. const float fade_scaling_factor = TERN(ENABLE_LEVELING_FADE_HEIGHT, planner.fade_scaling_factor_for_z(current_position.z), 1);
  680. current_position.z -= fade_scaling_factor * bilinear_z_offset(current_position);
  681. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(" corrected Z:", current_position.z);
  682. }
  683. #endif // ABL_PLANAR
  684. // Auto Bed Leveling is complete! Enable if possible.
  685. planner.leveling_active = dryrun ? abl_should_enable : true;
  686. } // !isnan(measured_z)
  687. // Restore state after probing
  688. if (!faux) restore_feedrate_and_scaling();
  689. // Sync the planner from the current_position
  690. if (planner.leveling_active) sync_plan_position();
  691. #if HAS_BED_PROBE
  692. probe.move_z_after_probing();
  693. #endif
  694. #ifdef Z_PROBE_END_SCRIPT
  695. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
  696. planner.synchronize();
  697. process_subcommands_now_P(PSTR(Z_PROBE_END_SCRIPT));
  698. #endif
  699. #if ENABLED(DWIN_CREALITY_LCD)
  700. DWIN_CompletedLeveling();
  701. #endif
  702. report_current_position();
  703. G29_RETURN(isnan(measured_z));
  704. }
  705. #endif // HAS_ABL_NOT_UBL