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.

G29.cpp 31KB

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