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.

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