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

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