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

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