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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 abl_points;
  191. #elif ENABLED(PROBE_MANUALLY) // Bilinear
  192. int constexpr abl_points = 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 abl_points = 3; // used to show total points
  205. #endif
  206. // Probe at 3 arbitrary points
  207. ABL_VAR vector_3 points[3] = {
  208. vector_3(PROBE_PT_1_X, PROBE_PT_1_Y, 0),
  209. vector_3(PROBE_PT_2_X, PROBE_PT_2_Y, 0),
  210. vector_3(PROBE_PT_3_X, 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. abl_points = 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, abl_points));
  383. SERIAL_PROTOCOLLNPAIR(" of ", abl_points);
  384. }
  385. else
  386. SERIAL_PROTOCOLLNPGM("idle");
  387. }
  388. if (no_action) return;
  389. if (abl_probe_index == 0) {
  390. // For the initial G29 S2 save software endstop state
  391. #if HAS_SOFTWARE_ENDSTOPS
  392. enable_soft_endstops = soft_endstops_enabled;
  393. #endif
  394. // Move close to the bed before the first point
  395. do_blocking_move_to_z(Z_MIN_POS);
  396. }
  397. else {
  398. #if ENABLED(AUTO_BED_LEVELING_LINEAR) || ENABLED(AUTO_BED_LEVELING_3POINT)
  399. const uint16_t index = abl_probe_index - 1;
  400. #endif
  401. // For G29 after adjusting Z.
  402. // Save the previous Z before going to the next point
  403. measured_z = current_position[Z_AXIS];
  404. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  405. mean += measured_z;
  406. eqnBVector[index] = measured_z;
  407. eqnAMatrix[index + 0 * abl_points] = xProbe;
  408. eqnAMatrix[index + 1 * abl_points] = yProbe;
  409. eqnAMatrix[index + 2 * abl_points] = 1;
  410. incremental_LSF(&lsf_results, xProbe, yProbe, measured_z);
  411. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  412. points[index].z = measured_z;
  413. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  414. z_values[xCount][yCount] = measured_z + zoffset;
  415. #if ENABLED(DEBUG_LEVELING_FEATURE)
  416. if (DEBUGGING(LEVELING)) {
  417. SERIAL_PROTOCOLPAIR("Save X", xCount);
  418. SERIAL_PROTOCOLPAIR(" Y", yCount);
  419. SERIAL_PROTOCOLLNPAIR(" Z", measured_z + zoffset);
  420. }
  421. #endif
  422. #endif
  423. }
  424. //
  425. // If there's another point to sample, move there with optional lift.
  426. //
  427. #if ABL_GRID
  428. // Skip any unreachable points
  429. while (abl_probe_index < abl_points) {
  430. // Set xCount, yCount based on abl_probe_index, with zig-zag
  431. PR_OUTER_VAR = abl_probe_index / PR_INNER_END;
  432. PR_INNER_VAR = abl_probe_index - (PR_OUTER_VAR * PR_INNER_END);
  433. // Probe in reverse order for every other row/column
  434. bool zig = (PR_OUTER_VAR & 1); // != ((PR_OUTER_END) & 1);
  435. if (zig) PR_INNER_VAR = (PR_INNER_END - 1) - PR_INNER_VAR;
  436. const float xBase = xCount * xGridSpacing + left_probe_bed_position,
  437. yBase = yCount * yGridSpacing + front_probe_bed_position;
  438. xProbe = FLOOR(xBase + (xBase < 0 ? 0 : 0.5));
  439. yProbe = FLOOR(yBase + (yBase < 0 ? 0 : 0.5));
  440. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  441. indexIntoAB[xCount][yCount] = abl_probe_index;
  442. #endif
  443. // Keep looping till a reachable point is found
  444. if (position_is_reachable(xProbe, yProbe)) break;
  445. ++abl_probe_index;
  446. }
  447. // Is there a next point to move to?
  448. if (abl_probe_index < abl_points) {
  449. _manual_goto_xy(xProbe, yProbe); // Can be used here too!
  450. #if HAS_SOFTWARE_ENDSTOPS
  451. // Disable software endstops to allow manual adjustment
  452. // If G29 is not completed, they will not be re-enabled
  453. soft_endstops_enabled = false;
  454. #endif
  455. return;
  456. }
  457. else {
  458. // Leveling done! Fall through to G29 finishing code below
  459. SERIAL_PROTOCOLLNPGM("Grid probing done.");
  460. // Re-enable software endstops, if needed
  461. #if HAS_SOFTWARE_ENDSTOPS
  462. soft_endstops_enabled = enable_soft_endstops;
  463. #endif
  464. }
  465. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  466. // Probe at 3 arbitrary points
  467. if (abl_probe_index < abl_points) {
  468. xProbe = points[abl_probe_index].x;
  469. yProbe = points[abl_probe_index].y;
  470. _manual_goto_xy(xProbe, yProbe);
  471. #if HAS_SOFTWARE_ENDSTOPS
  472. // Disable software endstops to allow manual adjustment
  473. // If G29 is not completed, they will not be re-enabled
  474. soft_endstops_enabled = false;
  475. #endif
  476. return;
  477. }
  478. else {
  479. SERIAL_PROTOCOLLNPGM("3-point probing done.");
  480. // Re-enable software endstops, if needed
  481. #if HAS_SOFTWARE_ENDSTOPS
  482. soft_endstops_enabled = enable_soft_endstops;
  483. #endif
  484. if (!dryrun) {
  485. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  486. if (planeNormal.z < 0) {
  487. planeNormal.x *= -1;
  488. planeNormal.y *= -1;
  489. planeNormal.z *= -1;
  490. }
  491. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  492. // Can't re-enable (on error) until the new grid is written
  493. abl_should_enable = false;
  494. }
  495. }
  496. #endif // AUTO_BED_LEVELING_3POINT
  497. #else // !PROBE_MANUALLY
  498. {
  499. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  500. measured_z = 0;
  501. #if ABL_GRID
  502. bool zig = PR_OUTER_END & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION
  503. measured_z = 0;
  504. // Outer loop is Y with PROBE_Y_FIRST disabled
  505. for (uint8_t PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_END && !isnan(measured_z); PR_OUTER_VAR++) {
  506. int8_t inStart, inStop, inInc;
  507. if (zig) { // away from origin
  508. inStart = 0;
  509. inStop = PR_INNER_END;
  510. inInc = 1;
  511. }
  512. else { // towards origin
  513. inStart = PR_INNER_END - 1;
  514. inStop = -1;
  515. inInc = -1;
  516. }
  517. zig ^= true; // zag
  518. // Inner loop is Y with PROBE_Y_FIRST enabled
  519. for (int8_t PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; PR_INNER_VAR += inInc) {
  520. float xBase = left_probe_bed_position + xGridSpacing * xCount,
  521. yBase = front_probe_bed_position + yGridSpacing * yCount;
  522. xProbe = FLOOR(xBase + (xBase < 0 ? 0 : 0.5));
  523. yProbe = FLOOR(yBase + (yBase < 0 ? 0 : 0.5));
  524. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  525. indexIntoAB[xCount][yCount] = ++abl_probe_index; // 0...
  526. #endif
  527. #if IS_KINEMATIC
  528. // Avoid probing outside the round or hexagonal area
  529. if (!position_is_reachable_by_probe(xProbe, yProbe)) continue;
  530. #endif
  531. measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, raise_after, verbose_level);
  532. if (isnan(measured_z)) {
  533. set_bed_leveling_enabled(abl_should_enable);
  534. break;
  535. }
  536. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  537. mean += measured_z;
  538. eqnBVector[abl_probe_index] = measured_z;
  539. eqnAMatrix[abl_probe_index + 0 * abl_points] = xProbe;
  540. eqnAMatrix[abl_probe_index + 1 * abl_points] = yProbe;
  541. eqnAMatrix[abl_probe_index + 2 * abl_points] = 1;
  542. incremental_LSF(&lsf_results, xProbe, yProbe, measured_z);
  543. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  544. z_values[xCount][yCount] = measured_z + zoffset;
  545. #endif
  546. abl_should_enable = false;
  547. idle();
  548. } // inner
  549. } // outer
  550. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  551. // Probe at 3 arbitrary points
  552. for (uint8_t i = 0; i < 3; ++i) {
  553. // Retain the last probe position
  554. xProbe = points[i].x;
  555. yProbe = points[i].y;
  556. measured_z = faux ? 0.001 * random(-100, 101) : probe_pt(xProbe, yProbe, raise_after, verbose_level);
  557. if (isnan(measured_z)) {
  558. set_bed_leveling_enabled(abl_should_enable);
  559. break;
  560. }
  561. points[i].z = measured_z;
  562. }
  563. if (!dryrun && !isnan(measured_z)) {
  564. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  565. if (planeNormal.z < 0) {
  566. planeNormal.x *= -1;
  567. planeNormal.y *= -1;
  568. planeNormal.z *= -1;
  569. }
  570. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  571. // Can't re-enable (on error) until the new grid is written
  572. abl_should_enable = false;
  573. }
  574. #endif // AUTO_BED_LEVELING_3POINT
  575. // Stow the probe. No raise for FIX_MOUNTED_PROBE.
  576. if (STOW_PROBE()) {
  577. set_bed_leveling_enabled(abl_should_enable);
  578. measured_z = NAN;
  579. }
  580. }
  581. #endif // !PROBE_MANUALLY
  582. //
  583. // G29 Finishing Code
  584. //
  585. // Unless this is a dry run, auto bed leveling will
  586. // definitely be enabled after this point.
  587. //
  588. // If code above wants to continue leveling, it should
  589. // return or loop before this point.
  590. //
  591. #if ENABLED(DEBUG_LEVELING_FEATURE)
  592. if (DEBUGGING(LEVELING)) DEBUG_POS("> probing complete", current_position);
  593. #endif
  594. #if ENABLED(PROBE_MANUALLY)
  595. g29_in_progress = false;
  596. #if ENABLED(LCD_BED_LEVELING)
  597. lcd_wait_for_move = false;
  598. #endif
  599. #endif
  600. // Calculate leveling, print reports, correct the position
  601. if (!isnan(measured_z)) {
  602. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  603. if (!dryrun) extrapolate_unprobed_bed_level();
  604. print_bilinear_leveling_grid();
  605. refresh_bed_level();
  606. #if ENABLED(ABL_BILINEAR_SUBDIVISION)
  607. print_bilinear_leveling_grid_virt();
  608. #endif
  609. #elif ENABLED(AUTO_BED_LEVELING_LINEAR)
  610. // For LINEAR leveling calculate matrix, print reports, correct the position
  611. /**
  612. * solve the plane equation ax + by + d = z
  613. * A is the matrix with rows [x y 1] for all the probed points
  614. * B is the vector of the Z positions
  615. * the normal vector to the plane is formed by the coefficients of the
  616. * plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  617. * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  618. */
  619. float plane_equation_coefficients[3];
  620. finish_incremental_LSF(&lsf_results);
  621. plane_equation_coefficients[0] = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below
  622. plane_equation_coefficients[1] = -lsf_results.B; // but that is not yet tested.
  623. plane_equation_coefficients[2] = -lsf_results.D;
  624. mean /= abl_points;
  625. if (verbose_level) {
  626. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  627. SERIAL_PROTOCOL_F(plane_equation_coefficients[0], 8);
  628. SERIAL_PROTOCOLPGM(" b: ");
  629. SERIAL_PROTOCOL_F(plane_equation_coefficients[1], 8);
  630. SERIAL_PROTOCOLPGM(" d: ");
  631. SERIAL_PROTOCOL_F(plane_equation_coefficients[2], 8);
  632. SERIAL_EOL();
  633. if (verbose_level > 2) {
  634. SERIAL_PROTOCOLPGM("Mean of sampled points: ");
  635. SERIAL_PROTOCOL_F(mean, 8);
  636. SERIAL_EOL();
  637. }
  638. }
  639. // Create the matrix but don't correct the position yet
  640. if (!dryrun)
  641. planner.bed_level_matrix = matrix_3x3::create_look_at(
  642. vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1) // We can eliminate the '-' here and up above
  643. );
  644. // Show the Topography map if enabled
  645. if (do_topography_map) {
  646. SERIAL_PROTOCOLLNPGM("\nBed Height Topography:\n"
  647. " +--- BACK --+\n"
  648. " | |\n"
  649. " L | (+) | R\n"
  650. " E | | I\n"
  651. " F | (-) N (+) | G\n"
  652. " T | | H\n"
  653. " | (-) | T\n"
  654. " | |\n"
  655. " O-- FRONT --+\n"
  656. " (0,0)");
  657. float min_diff = 999;
  658. for (int8_t yy = abl_grid_points_y - 1; yy >= 0; yy--) {
  659. for (uint8_t xx = 0; xx < abl_grid_points_x; xx++) {
  660. int ind = indexIntoAB[xx][yy];
  661. float diff = eqnBVector[ind] - mean,
  662. x_tmp = eqnAMatrix[ind + 0 * abl_points],
  663. y_tmp = eqnAMatrix[ind + 1 * abl_points],
  664. z_tmp = 0;
  665. apply_rotation_xyz(planner.bed_level_matrix, x_tmp, y_tmp, z_tmp);
  666. NOMORE(min_diff, eqnBVector[ind] - z_tmp);
  667. if (diff >= 0.0)
  668. SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment
  669. else
  670. SERIAL_PROTOCOLCHAR(' ');
  671. SERIAL_PROTOCOL_F(diff, 5);
  672. } // xx
  673. SERIAL_EOL();
  674. } // yy
  675. SERIAL_EOL();
  676. if (verbose_level > 3) {
  677. SERIAL_PROTOCOLLNPGM("\nCorrected Bed Height vs. Bed Topology:");
  678. for (int8_t yy = abl_grid_points_y - 1; yy >= 0; yy--) {
  679. for (uint8_t xx = 0; xx < abl_grid_points_x; xx++) {
  680. int ind = indexIntoAB[xx][yy];
  681. float x_tmp = eqnAMatrix[ind + 0 * abl_points],
  682. y_tmp = eqnAMatrix[ind + 1 * abl_points],
  683. z_tmp = 0;
  684. apply_rotation_xyz(planner.bed_level_matrix, x_tmp, y_tmp, z_tmp);
  685. float diff = eqnBVector[ind] - z_tmp - min_diff;
  686. if (diff >= 0.0)
  687. SERIAL_PROTOCOLPGM(" +");
  688. // Include + for column alignment
  689. else
  690. SERIAL_PROTOCOLCHAR(' ');
  691. SERIAL_PROTOCOL_F(diff, 5);
  692. } // xx
  693. SERIAL_EOL();
  694. } // yy
  695. SERIAL_EOL();
  696. }
  697. } //do_topography_map
  698. #endif // AUTO_BED_LEVELING_LINEAR
  699. #if ABL_PLANAR
  700. // For LINEAR and 3POINT leveling correct the current position
  701. if (verbose_level > 0)
  702. planner.bed_level_matrix.debug(PSTR("\n\nBed Level Correction Matrix:"));
  703. if (!dryrun) {
  704. //
  705. // Correct the current XYZ position based on the tilted plane.
  706. //
  707. #if ENABLED(DEBUG_LEVELING_FEATURE)
  708. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position);
  709. #endif
  710. float converted[XYZ];
  711. COPY(converted, current_position);
  712. planner.leveling_active = true;
  713. planner.unapply_leveling(converted); // use conversion machinery
  714. planner.leveling_active = false;
  715. // Use the last measured distance to the bed, if possible
  716. if ( NEAR(current_position[X_AXIS], xProbe - (X_PROBE_OFFSET_FROM_EXTRUDER))
  717. && NEAR(current_position[Y_AXIS], yProbe - (Y_PROBE_OFFSET_FROM_EXTRUDER))
  718. ) {
  719. const float simple_z = current_position[Z_AXIS] - measured_z;
  720. #if ENABLED(DEBUG_LEVELING_FEATURE)
  721. if (DEBUGGING(LEVELING)) {
  722. SERIAL_ECHOPAIR("Z from Probe:", simple_z);
  723. SERIAL_ECHOPAIR(" Matrix:", converted[Z_AXIS]);
  724. SERIAL_ECHOLNPAIR(" Discrepancy:", simple_z - converted[Z_AXIS]);
  725. }
  726. #endif
  727. converted[Z_AXIS] = simple_z;
  728. }
  729. // The rotated XY and corrected Z are now current_position
  730. COPY(current_position, converted);
  731. #if ENABLED(DEBUG_LEVELING_FEATURE)
  732. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
  733. #endif
  734. }
  735. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  736. if (!dryrun) {
  737. #if ENABLED(DEBUG_LEVELING_FEATURE)
  738. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("G29 uncorrected Z:", current_position[Z_AXIS]);
  739. #endif
  740. // Unapply the offset because it is going to be immediately applied
  741. // and cause compensation movement in Z
  742. current_position[Z_AXIS] -= bilinear_z_offset(current_position);
  743. #if ENABLED(DEBUG_LEVELING_FEATURE)
  744. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR(" corrected Z:", current_position[Z_AXIS]);
  745. #endif
  746. }
  747. #endif // ABL_PLANAR
  748. #ifdef Z_PROBE_END_SCRIPT
  749. #if ENABLED(DEBUG_LEVELING_FEATURE)
  750. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
  751. #endif
  752. enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
  753. stepper.synchronize();
  754. #endif
  755. // Auto Bed Leveling is complete! Enable if possible.
  756. planner.leveling_active = dryrun ? abl_should_enable : true;
  757. } // !isnan(measured_z)
  758. // Restore state after probing
  759. if (!faux) clean_up_after_endstop_or_probe_move();
  760. #if ENABLED(DEBUG_LEVELING_FEATURE)
  761. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< G29");
  762. #endif
  763. KEEPALIVE_STATE(IN_HANDLER);
  764. if (planner.leveling_active)
  765. SYNC_PLAN_POSITION_KINEMATIC();
  766. #if HAS_BED_PROBE && Z_AFTER_PROBING
  767. move_z_after_probing();
  768. #endif
  769. report_current_position();
  770. }
  771. #endif // OLDSCHOOL_ABL