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

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