My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

G29.cpp 32KB

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