My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

G29.cpp 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://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(PROBE_TEMP_COMPENSATION)
  35. #include "../../../feature/probe_temp_comp.h"
  36. #include "../../../module/temperature.h"
  37. #endif
  38. #if HAS_STATUS_MESSAGE
  39. #include "../../../lcd/marlinui.h"
  40. #endif
  41. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  42. #include "../../../libs/least_squares_fit.h"
  43. #endif
  44. #if ABL_PLANAR
  45. #include "../../../libs/vector_3.h"
  46. #endif
  47. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  48. #include "../../../core/debug_out.h"
  49. #if ENABLED(EXTENSIBLE_UI)
  50. #include "../../../lcd/extui/ui_api.h"
  51. #endif
  52. #if ENABLED(DWIN_CREALITY_LCD)
  53. #include "../../../lcd/dwin/e3v2/dwin.h"
  54. #endif
  55. #if HAS_MULTI_HOTEND
  56. #include "../../../module/tool_change.h"
  57. #endif
  58. #if ABL_USES_GRID
  59. #if ENABLED(PROBE_Y_FIRST)
  60. #define PR_OUTER_VAR abl.meshCount.x
  61. #define PR_OUTER_SIZE abl.grid_points.x
  62. #define PR_INNER_VAR abl.meshCount.y
  63. #define PR_INNER_SIZE abl.grid_points.y
  64. #else
  65. #define PR_OUTER_VAR abl.meshCount.y
  66. #define PR_OUTER_SIZE abl.grid_points.y
  67. #define PR_INNER_VAR abl.meshCount.x
  68. #define PR_INNER_SIZE abl.grid_points.x
  69. #endif
  70. #endif
  71. #define G29_RETURN(b) return TERN_(G29_RETRY_AND_RECOVER, b)
  72. // For manual probing values persist over multiple G29
  73. class G29_State {
  74. public:
  75. int verbose_level;
  76. xy_pos_t probePos;
  77. float measured_z;
  78. bool dryrun,
  79. reenable;
  80. #if EITHER(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
  81. int abl_probe_index;
  82. #endif
  83. #if ABL_USES_GRID
  84. xy_int8_t meshCount;
  85. xy_pos_t probe_position_lf,
  86. probe_position_rb;
  87. xy_float_t gridSpacing; // = { 0.0f, 0.0f }
  88. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  89. bool topography_map;
  90. xy_uint8_t grid_points;
  91. #else // Bilinear
  92. static constexpr xy_uint8_t grid_points = { GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y };
  93. #endif
  94. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  95. int abl_points;
  96. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  97. static constexpr int abl_points = 3;
  98. #else
  99. static constexpr int abl_points = GRID_MAX_POINTS;
  100. #endif
  101. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  102. float Z_offset;
  103. #endif
  104. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  105. int indexIntoAB[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  106. float eqnAMatrix[(GRID_MAX_POINTS) * 3], // "A" matrix of the linear system of equations
  107. eqnBVector[GRID_MAX_POINTS], // "B" vector of Z points
  108. mean;
  109. #endif
  110. #endif
  111. };
  112. #if ABL_USES_GRID && EITHER(AUTO_BED_LEVELING_3POINT, AUTO_BED_LEVELING_BILINEAR)
  113. constexpr xy_uint8_t G29_State::grid_points;
  114. constexpr int G29_State::abl_points;
  115. #endif
  116. /**
  117. * G29: Detailed Z probe, probes the bed at 3 or more points.
  118. * Will fail if the printer has not been homed with G28.
  119. *
  120. * Enhanced G29 Auto Bed Leveling Probe Routine
  121. *
  122. * O Auto-level only if needed
  123. *
  124. * D Dry-Run mode. Just evaluate the bed Topology - Don't apply
  125. * or alter the bed level data. Useful to check the topology
  126. * after a first run of G29.
  127. *
  128. * J Jettison current bed leveling data
  129. *
  130. * V Set the verbose level (0-4). Example: "G29 V3"
  131. *
  132. * Parameters With LINEAR leveling only:
  133. *
  134. * P Set the size of the grid that will be probed (P x P points).
  135. * Example: "G29 P4"
  136. *
  137. * X Set the X size of the grid that will be probed (X x Y points).
  138. * Example: "G29 X7 Y5"
  139. *
  140. * Y Set the Y size of the grid that will be probed (X x Y points).
  141. *
  142. * T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
  143. * This is useful for manual bed leveling and finding flaws in the bed (to
  144. * assist with part placement).
  145. * Not supported by non-linear delta printer bed leveling.
  146. *
  147. * Parameters With LINEAR and BILINEAR leveling only:
  148. *
  149. * S Set the XY travel speed between probe points (in units/min)
  150. *
  151. * H Set bounds to a centered square H x H units in size
  152. *
  153. * -or-
  154. *
  155. * F Set the Front limit of the probing grid
  156. * B Set the Back limit of the probing grid
  157. * L Set the Left limit of the probing grid
  158. * R Set the Right limit of the probing grid
  159. *
  160. * Parameters with DEBUG_LEVELING_FEATURE only:
  161. *
  162. * C Make a totally fake grid with no actual probing.
  163. * For use in testing when no probing is possible.
  164. *
  165. * Parameters with BILINEAR leveling only:
  166. *
  167. * Z Supply an additional Z probe offset
  168. *
  169. * Extra parameters with PROBE_MANUALLY:
  170. *
  171. * To do manual probing simply repeat G29 until the procedure is complete.
  172. * The first G29 accepts parameters. 'G29 Q' for status, 'G29 A' to abort.
  173. *
  174. * Q Query leveling and G29 state
  175. *
  176. * A Abort current leveling procedure
  177. *
  178. * Extra parameters with BILINEAR only:
  179. *
  180. * W Write a mesh point. (If G29 is idle.)
  181. * I X index for mesh point
  182. * J Y index for mesh point
  183. * X X for mesh point, overrides I
  184. * Y Y for mesh point, overrides J
  185. * Z Z for mesh point. Otherwise, raw current Z.
  186. *
  187. * Without PROBE_MANUALLY:
  188. *
  189. * E By default G29 will engage the Z probe, test the bed, then disengage.
  190. * Include "E" to engage/disengage the Z probe for each sample.
  191. * There's no extra effect if you have a fixed Z probe.
  192. */
  193. G29_TYPE GcodeSuite::G29() {
  194. TERN_(PROBE_MANUALLY, static) G29_State abl;
  195. reset_stepper_timeout();
  196. const bool seenQ = EITHER(DEBUG_LEVELING_FEATURE, PROBE_MANUALLY) && parser.seen('Q');
  197. // G29 Q is also available if debugging
  198. #if ENABLED(DEBUG_LEVELING_FEATURE)
  199. const uint8_t old_debug_flags = marlin_debug_flags;
  200. if (seenQ) marlin_debug_flags |= MARLIN_DEBUG_LEVELING;
  201. DEBUG_SECTION(log_G29, "G29", DEBUGGING(LEVELING));
  202. if (DEBUGGING(LEVELING)) log_machine_info();
  203. marlin_debug_flags = old_debug_flags;
  204. if (DISABLED(PROBE_MANUALLY) && seenQ) G29_RETURN(false);
  205. #endif
  206. const bool seenA = TERN0(PROBE_MANUALLY, parser.seen('A')),
  207. no_action = seenA || seenQ,
  208. faux = ENABLED(DEBUG_LEVELING_FEATURE) && DISABLED(PROBE_MANUALLY) ? parser.boolval('C') : no_action;
  209. if (!no_action && planner.leveling_active && parser.boolval('O')) { // Auto-level only if needed
  210. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Auto-level not needed, skip");
  211. G29_RETURN(false);
  212. }
  213. // Send 'N' to force homing before G29 (internal only)
  214. if (parser.seen('N'))
  215. process_subcommands_now_P(TERN(G28_L0_ENSURES_LEVELING_OFF, PSTR("G28L0"), G28_STR));
  216. // Don't allow auto-leveling without homing first
  217. if (homing_needed_error()) G29_RETURN(false);
  218. #if ENABLED(AUTO_BED_LEVELING_3POINT)
  219. vector_3 points[3];
  220. probe.get_three_points(points);
  221. #endif
  222. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  223. struct linear_fit_data lsf_results;
  224. #endif
  225. /**
  226. * On the initial G29 fetch command parameters.
  227. */
  228. if (!g29_in_progress) {
  229. TERN_(HAS_MULTI_HOTEND, if (active_extruder) tool_change(0));
  230. #if EITHER(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
  231. abl.abl_probe_index = -1;
  232. #endif
  233. abl.reenable = planner.leveling_active;
  234. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  235. const bool seen_w = parser.seen('W');
  236. if (seen_w) {
  237. if (!leveling_is_valid()) {
  238. SERIAL_ERROR_MSG("No bilinear grid");
  239. G29_RETURN(false);
  240. }
  241. const float rz = parser.seenval('Z') ? RAW_Z_POSITION(parser.value_linear_units()) : current_position.z;
  242. if (!WITHIN(rz, -10, 10)) {
  243. SERIAL_ERROR_MSG("Bad Z value");
  244. G29_RETURN(false);
  245. }
  246. const float rx = RAW_X_POSITION(parser.linearval('X', NAN)),
  247. ry = RAW_Y_POSITION(parser.linearval('Y', NAN));
  248. int8_t i = parser.byteval('I', -1), j = parser.byteval('J', -1);
  249. if (!isnan(rx) && !isnan(ry)) {
  250. // Get nearest i / j from rx / ry
  251. i = (rx - bilinear_start.x + 0.5 * abl.gridSpacing.x) / abl.gridSpacing.x;
  252. j = (ry - bilinear_start.y + 0.5 * abl.gridSpacing.y) / abl.gridSpacing.y;
  253. LIMIT(i, 0, GRID_MAX_POINTS_X - 1);
  254. LIMIT(j, 0, GRID_MAX_POINTS_Y - 1);
  255. }
  256. if (WITHIN(i, 0, GRID_MAX_POINTS_X - 1) && WITHIN(j, 0, GRID_MAX_POINTS_Y)) {
  257. set_bed_leveling_enabled(false);
  258. z_values[i][j] = rz;
  259. TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
  260. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(i, j, rz));
  261. set_bed_leveling_enabled(abl.reenable);
  262. if (abl.reenable) report_current_position();
  263. }
  264. G29_RETURN(false);
  265. } // parser.seen('W')
  266. #else
  267. constexpr bool seen_w = false;
  268. #endif
  269. // Jettison bed leveling data
  270. if (!seen_w && parser.seen('J')) {
  271. reset_bed_level();
  272. G29_RETURN(false);
  273. }
  274. abl.verbose_level = parser.intval('V');
  275. if (!WITHIN(abl.verbose_level, 0, 4)) {
  276. SERIAL_ECHOLNPGM("?(V)erbose level implausible (0-4).");
  277. G29_RETURN(false);
  278. }
  279. abl.dryrun = parser.boolval('D') || TERN0(PROBE_MANUALLY, no_action);
  280. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  281. incremental_LSF_reset(&lsf_results);
  282. abl.topography_map = abl.verbose_level > 2 || parser.boolval('T');
  283. // X and Y specify points in each direction, overriding the default
  284. // These values may be saved with the completed mesh
  285. abl.grid_points.set(
  286. parser.byteval('X', GRID_MAX_POINTS_X),
  287. parser.byteval('Y', GRID_MAX_POINTS_Y)
  288. );
  289. if (parser.seenval('P')) abl.grid_points.x = abl.grid_points.y = parser.value_int();
  290. if (!WITHIN(abl.grid_points.x, 2, GRID_MAX_POINTS_X)) {
  291. SERIAL_ECHOLNPGM("?Probe points (X) implausible (2-" STRINGIFY(GRID_MAX_POINTS_X) ").");
  292. G29_RETURN(false);
  293. }
  294. if (!WITHIN(abl.grid_points.y, 2, GRID_MAX_POINTS_Y)) {
  295. SERIAL_ECHOLNPGM("?Probe points (Y) implausible (2-" STRINGIFY(GRID_MAX_POINTS_Y) ").");
  296. G29_RETURN(false);
  297. }
  298. abl.abl_points = abl.grid_points.x * abl.grid_points.y;
  299. abl.mean = 0;
  300. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  301. abl.Z_offset = parser.linearval('Z');
  302. #endif
  303. #if ABL_USES_GRID
  304. xy_probe_feedrate_mm_s = MMM_TO_MMS(parser.linearval('S', XY_PROBE_FEEDRATE));
  305. const float x_min = probe.min_x(), x_max = probe.max_x(),
  306. y_min = probe.min_y(), y_max = probe.max_y();
  307. if (parser.seen('H')) {
  308. const int16_t size = (int16_t)parser.value_linear_units();
  309. abl.probe_position_lf.set(_MAX((X_CENTER) - size / 2, x_min), _MAX((Y_CENTER) - size / 2, y_min));
  310. abl.probe_position_rb.set(_MIN(abl.probe_position_lf.x + size, x_max), _MIN(abl.probe_position_lf.y + size, y_max));
  311. }
  312. else {
  313. abl.probe_position_lf.set(parser.linearval('L', x_min), parser.linearval('F', y_min));
  314. abl.probe_position_rb.set(parser.linearval('R', x_max), parser.linearval('B', y_max));
  315. }
  316. if (!probe.good_bounds(abl.probe_position_lf, abl.probe_position_rb)) {
  317. if (DEBUGGING(LEVELING)) {
  318. DEBUG_ECHOLNPAIR("G29 L", abl.probe_position_lf.x, " R", abl.probe_position_rb.x,
  319. " F", abl.probe_position_lf.y, " B", abl.probe_position_rb.y);
  320. }
  321. SERIAL_ECHOLNPGM("? (L,R,F,B) out of bounds.");
  322. G29_RETURN(false);
  323. }
  324. // Probe at the points of a lattice grid
  325. abl.gridSpacing.set((abl.probe_position_rb.x - abl.probe_position_lf.x) / (abl.grid_points.x - 1),
  326. (abl.probe_position_rb.y - abl.probe_position_lf.y) / (abl.grid_points.y - 1));
  327. #endif // ABL_USES_GRID
  328. if (abl.verbose_level > 0) {
  329. SERIAL_ECHOPGM("G29 Auto Bed Leveling");
  330. if (abl.dryrun) SERIAL_ECHOPGM(" (DRYRUN)");
  331. SERIAL_EOL();
  332. }
  333. planner.synchronize();
  334. #if ENABLED(AUTO_BED_LEVELING_3POINT)
  335. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> 3-point Leveling");
  336. points[0].z = points[1].z = points[2].z = 0; // Probe at 3 arbitrary points
  337. #endif
  338. #if BOTH(AUTO_BED_LEVELING_BILINEAR, EXTENSIBLE_UI)
  339. ExtUI::onMeshLevelingStart();
  340. #endif
  341. if (!faux) {
  342. remember_feedrate_scaling_off();
  343. #if ENABLED(PREHEAT_BEFORE_LEVELING)
  344. if (!abl.dryrun) probe.preheat_for_probing(LEVELING_NOZZLE_TEMP, LEVELING_BED_TEMP);
  345. #endif
  346. }
  347. // Disable auto bed leveling during G29.
  348. // Be formal so G29 can be done successively without G28.
  349. if (!no_action) set_bed_leveling_enabled(false);
  350. // Deploy certain probes before starting probing
  351. #if HAS_BED_PROBE
  352. if (ENABLED(BLTOUCH))
  353. do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE);
  354. else if (probe.deploy()) {
  355. set_bed_leveling_enabled(abl.reenable);
  356. G29_RETURN(false);
  357. }
  358. #endif
  359. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  360. if (TERN1(PROBE_MANUALLY, !no_action)
  361. && (abl.gridSpacing != bilinear_grid_spacing || abl.probe_position_lf != bilinear_start)
  362. ) {
  363. // Reset grid to 0.0 or "not probed". (Also disables ABL)
  364. reset_bed_level();
  365. // Initialize a grid with the given dimensions
  366. bilinear_grid_spacing = abl.gridSpacing;
  367. bilinear_start = abl.probe_position_lf;
  368. // Can't re-enable (on error) until the new grid is written
  369. abl.reenable = false;
  370. }
  371. #endif // AUTO_BED_LEVELING_BILINEAR
  372. } // !g29_in_progress
  373. #if ENABLED(PROBE_MANUALLY)
  374. // For manual probing, get the next index to probe now.
  375. // On the first probe this will be incremented to 0.
  376. if (!no_action) {
  377. ++abl.abl_probe_index;
  378. g29_in_progress = true;
  379. }
  380. // Abort current G29 procedure, go back to idle state
  381. if (seenA && g29_in_progress) {
  382. SERIAL_ECHOLNPGM("Manual G29 aborted");
  383. SET_SOFT_ENDSTOP_LOOSE(false);
  384. set_bed_leveling_enabled(abl.reenable);
  385. g29_in_progress = false;
  386. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  387. }
  388. // Query G29 status
  389. if (abl.verbose_level || seenQ) {
  390. SERIAL_ECHOPGM("Manual G29 ");
  391. if (g29_in_progress) {
  392. SERIAL_ECHOPAIR("point ", _MIN(abl.abl_probe_index + 1, abl.abl_points));
  393. SERIAL_ECHOLNPAIR(" of ", abl.abl_points);
  394. }
  395. else
  396. SERIAL_ECHOLNPGM("idle");
  397. }
  398. if (no_action) G29_RETURN(false);
  399. if (abl.abl_probe_index == 0) {
  400. // For the initial G29 S2 save software endstop state
  401. SET_SOFT_ENDSTOP_LOOSE(true);
  402. // Move close to the bed before the first point
  403. do_blocking_move_to_z(0);
  404. }
  405. else {
  406. #if EITHER(AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_3POINT)
  407. const uint16_t index = abl.abl_probe_index - 1;
  408. #endif
  409. // For G29 after adjusting Z.
  410. // Save the previous Z before going to the next point
  411. abl.measured_z = current_position.z;
  412. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  413. abl.mean += abl.measured_z;
  414. abl.eqnBVector[index] = abl.measured_z;
  415. abl.eqnAMatrix[index + 0 * abl.abl_points] = abl.probePos.x;
  416. abl.eqnAMatrix[index + 1 * abl.abl_points] = abl.probePos.y;
  417. abl.eqnAMatrix[index + 2 * abl.abl_points] = 1;
  418. incremental_LSF(&lsf_results, abl.probePos, abl.measured_z);
  419. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  420. points[index].z = abl.measured_z;
  421. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  422. const float newz = abl.measured_z + abl.Z_offset;
  423. z_values[abl.meshCount.x][abl.meshCount.y] = newz;
  424. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(abl.meshCount, newz));
  425. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR_P(PSTR("Save X"), abl.meshCount.x, SP_Y_STR, abl.meshCount.y, SP_Z_STR, abl.measured_z + abl.Z_offset);
  426. #endif
  427. }
  428. //
  429. // If there's another point to sample, move there with optional lift.
  430. //
  431. #if ABL_USES_GRID
  432. // Skip any unreachable points
  433. while (abl.abl_probe_index < abl.abl_points) {
  434. // Set abl.meshCount.x, abl.meshCount.y based on abl.abl_probe_index, with zig-zag
  435. PR_OUTER_VAR = abl.abl_probe_index / PR_INNER_SIZE;
  436. PR_INNER_VAR = abl.abl_probe_index - (PR_OUTER_VAR * PR_INNER_SIZE);
  437. // Probe in reverse order for every other row/column
  438. const bool zig = (PR_OUTER_VAR & 1); // != ((PR_OUTER_SIZE) & 1);
  439. if (zig) PR_INNER_VAR = (PR_INNER_SIZE - 1) - PR_INNER_VAR;
  440. abl.probePos = abl.probe_position_lf + abl.gridSpacing * abl.meshCount.asFloat();
  441. TERN_(AUTO_BED_LEVELING_LINEAR, abl.indexIntoAB[abl.meshCount.x][abl.meshCount.y] = abl.abl_probe_index);
  442. // Keep looping till a reachable point is found
  443. if (position_is_reachable(abl.probePos)) break;
  444. ++abl.abl_probe_index;
  445. }
  446. // Is there a next point to move to?
  447. if (abl.abl_probe_index < abl.abl_points) {
  448. _manual_goto_xy(abl.probePos); // Can be used here too!
  449. // Disable software endstops to allow manual adjustment
  450. // If G29 is not completed, they will not be re-enabled
  451. SET_SOFT_ENDSTOP_LOOSE(true);
  452. G29_RETURN(false);
  453. }
  454. else {
  455. // Leveling done! Fall through to G29 finishing code below
  456. SERIAL_ECHOLNPGM("Grid probing done.");
  457. // Re-enable software endstops, if needed
  458. SET_SOFT_ENDSTOP_LOOSE(false);
  459. }
  460. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  461. // Probe at 3 arbitrary points
  462. if (abl.abl_probe_index < abl.abl_points) {
  463. abl.probePos = points[abl.abl_probe_index];
  464. _manual_goto_xy(abl.probePos);
  465. // Disable software endstops to allow manual adjustment
  466. // If G29 is not completed, they will not be re-enabled
  467. SET_SOFT_ENDSTOP_LOOSE(true);
  468. G29_RETURN(false);
  469. }
  470. else {
  471. SERIAL_ECHOLNPGM("3-point probing done.");
  472. // Re-enable software endstops, if needed
  473. SET_SOFT_ENDSTOP_LOOSE(false);
  474. if (!abl.dryrun) {
  475. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  476. if (planeNormal.z < 0) planeNormal *= -1;
  477. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  478. // Can't re-enable (on error) until the new grid is written
  479. abl.reenable = false;
  480. }
  481. }
  482. #endif // AUTO_BED_LEVELING_3POINT
  483. #else // !PROBE_MANUALLY
  484. {
  485. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  486. abl.measured_z = 0;
  487. #if ABL_USES_GRID
  488. bool zig = PR_OUTER_SIZE & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION
  489. abl.measured_z = 0;
  490. // Outer loop is X with PROBE_Y_FIRST enabled
  491. // Outer loop is Y with PROBE_Y_FIRST disabled
  492. for (PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_SIZE && !isnan(abl.measured_z); PR_OUTER_VAR++) {
  493. int8_t inStart, inStop, inInc;
  494. if (zig) { // Zig away from origin
  495. inStart = 0; // Left or front
  496. inStop = PR_INNER_SIZE; // Right or back
  497. inInc = 1; // Zig right
  498. }
  499. else { // Zag towards origin
  500. inStart = PR_INNER_SIZE - 1; // Right or back
  501. inStop = -1; // Left or front
  502. inInc = -1; // Zag left
  503. }
  504. zig ^= true; // zag
  505. // An index to print current state
  506. uint8_t pt_index = (PR_OUTER_VAR) * (PR_INNER_SIZE) + 1;
  507. // Inner loop is Y with PROBE_Y_FIRST enabled
  508. // Inner loop is X with PROBE_Y_FIRST disabled
  509. for (PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; pt_index++, PR_INNER_VAR += inInc) {
  510. abl.probePos = abl.probe_position_lf + abl.gridSpacing * abl.meshCount.asFloat();
  511. TERN_(AUTO_BED_LEVELING_LINEAR, abl.indexIntoAB[abl.meshCount.x][abl.meshCount.y] = ++abl.abl_probe_index); // 0...
  512. // Avoid probing outside the round or hexagonal area
  513. if (TERN0(IS_KINEMATIC, !probe.can_reach(abl.probePos))) continue;
  514. if (abl.verbose_level) SERIAL_ECHOLNPAIR("Probing mesh point ", pt_index, "/", abl.abl_points, ".");
  515. TERN_(HAS_STATUS_MESSAGE, ui.status_printf_P(0, PSTR(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_MESH), int(pt_index), int(abl.abl_points)));
  516. abl.measured_z = faux ? 0.001f * random(-100, 101) : probe.probe_at_point(abl.probePos, raise_after, abl.verbose_level);
  517. if (isnan(abl.measured_z)) {
  518. set_bed_leveling_enabled(abl.reenable);
  519. break; // Breaks out of both loops
  520. }
  521. #if ENABLED(PROBE_TEMP_COMPENSATION)
  522. temp_comp.compensate_measurement(TSI_BED, thermalManager.degBed(), abl.measured_z);
  523. temp_comp.compensate_measurement(TSI_PROBE, thermalManager.degProbe(), abl.measured_z);
  524. TERN_(USE_TEMP_EXT_COMPENSATION, temp_comp.compensate_measurement(TSI_EXT, thermalManager.degHotend(), abl.measured_z));
  525. #endif
  526. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  527. abl.mean += abl.measured_z;
  528. abl.eqnBVector[abl.abl_probe_index] = abl.measured_z;
  529. abl.eqnAMatrix[abl.abl_probe_index + 0 * abl.abl_points] = abl.probePos.x;
  530. abl.eqnAMatrix[abl.abl_probe_index + 1 * abl.abl_points] = abl.probePos.y;
  531. abl.eqnAMatrix[abl.abl_probe_index + 2 * abl.abl_points] = 1;
  532. incremental_LSF(&lsf_results, abl.probePos, abl.measured_z);
  533. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  534. const float z = abl.measured_z + abl.Z_offset;
  535. z_values[abl.meshCount.x][abl.meshCount.y] = z;
  536. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(abl.meshCount, z));
  537. #endif
  538. abl.reenable = false;
  539. idle_no_sleep();
  540. } // inner
  541. } // outer
  542. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  543. // Probe at 3 arbitrary points
  544. LOOP_L_N(i, 3) {
  545. if (abl.verbose_level) SERIAL_ECHOLNPAIR("Probing point ", i + 1, "/3.");
  546. TERN_(HAS_STATUS_MESSAGE, ui.status_printf_P(0, PSTR(S_FMT " %i/3"), GET_TEXT(MSG_PROBING_MESH), int(i + 1)));
  547. // Retain the last probe position
  548. abl.probePos = points[i];
  549. abl.measured_z = faux ? 0.001 * random(-100, 101) : probe.probe_at_point(abl.probePos, raise_after, abl.verbose_level);
  550. if (isnan(abl.measured_z)) {
  551. set_bed_leveling_enabled(abl.reenable);
  552. break;
  553. }
  554. points[i].z = abl.measured_z;
  555. }
  556. if (!abl.dryrun && !isnan(abl.measured_z)) {
  557. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  558. if (planeNormal.z < 0) planeNormal *= -1;
  559. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  560. // Can't re-enable (on error) until the new grid is written
  561. abl.reenable = false;
  562. }
  563. #endif // AUTO_BED_LEVELING_3POINT
  564. TERN_(HAS_STATUS_MESSAGE, ui.reset_status());
  565. // Stow the probe. No raise for FIX_MOUNTED_PROBE.
  566. if (probe.stow()) {
  567. set_bed_leveling_enabled(abl.reenable);
  568. abl.measured_z = NAN;
  569. }
  570. }
  571. #endif // !PROBE_MANUALLY
  572. //
  573. // G29 Finishing Code
  574. //
  575. // Unless this is a dry run, auto bed leveling will
  576. // definitely be enabled after this point.
  577. //
  578. // If code above wants to continue leveling, it should
  579. // return or loop before this point.
  580. //
  581. if (DEBUGGING(LEVELING)) DEBUG_POS("> probing complete", current_position);
  582. #if ENABLED(PROBE_MANUALLY)
  583. g29_in_progress = false;
  584. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  585. #endif
  586. // Calculate leveling, print reports, correct the position
  587. if (!isnan(abl.measured_z)) {
  588. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  589. if (!abl.dryrun) extrapolate_unprobed_bed_level();
  590. print_bilinear_leveling_grid();
  591. refresh_bed_level();
  592. TERN_(ABL_BILINEAR_SUBDIVISION, print_bilinear_leveling_grid_virt());
  593. #elif ENABLED(AUTO_BED_LEVELING_LINEAR)
  594. // For LINEAR leveling calculate matrix, print reports, correct the position
  595. /**
  596. * solve the plane equation ax + by + d = z
  597. * A is the matrix with rows [x y 1] for all the probed points
  598. * B is the vector of the Z positions
  599. * the normal vector to the plane is formed by the coefficients of the
  600. * plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  601. * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  602. */
  603. struct { float a, b, d; } plane_equation_coefficients;
  604. finish_incremental_LSF(&lsf_results);
  605. plane_equation_coefficients.a = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below
  606. plane_equation_coefficients.b = -lsf_results.B; // but that is not yet tested.
  607. plane_equation_coefficients.d = -lsf_results.D;
  608. abl.mean /= abl.abl_points;
  609. if (abl.verbose_level) {
  610. SERIAL_ECHOPAIR_F("Eqn coefficients: a: ", plane_equation_coefficients.a, 8);
  611. SERIAL_ECHOPAIR_F(" b: ", plane_equation_coefficients.b, 8);
  612. SERIAL_ECHOPAIR_F(" d: ", plane_equation_coefficients.d, 8);
  613. if (abl.verbose_level > 2)
  614. SERIAL_ECHOPAIR_F("\nMean of sampled points: ", abl.mean, 8);
  615. SERIAL_EOL();
  616. }
  617. // Create the matrix but don't correct the position yet
  618. if (!abl.dryrun)
  619. planner.bed_level_matrix = matrix_3x3::create_look_at(
  620. vector_3(-plane_equation_coefficients.a, -plane_equation_coefficients.b, 1) // We can eliminate the '-' here and up above
  621. );
  622. // Show the Topography map if enabled
  623. if (abl.topography_map) {
  624. float min_diff = 999;
  625. auto print_topo_map = [&](PGM_P const title, const bool get_min) {
  626. SERIAL_ECHOPGM_P(title);
  627. for (int8_t yy = abl.grid_points.y - 1; yy >= 0; yy--) {
  628. LOOP_L_N(xx, abl.grid_points.x) {
  629. const int ind = abl.indexIntoAB[xx][yy];
  630. xyz_float_t tmp = { abl.eqnAMatrix[ind + 0 * abl.abl_points],
  631. abl.eqnAMatrix[ind + 1 * abl.abl_points], 0 };
  632. planner.bed_level_matrix.apply_rotation_xyz(tmp);
  633. if (get_min) NOMORE(min_diff, abl.eqnBVector[ind] - tmp.z);
  634. const float subval = get_min ? abl.mean : tmp.z + min_diff,
  635. diff = abl.eqnBVector[ind] - subval;
  636. SERIAL_CHAR(' '); if (diff >= 0.0) SERIAL_CHAR('+'); // Include + for column alignment
  637. SERIAL_ECHO_F(diff, 5);
  638. } // xx
  639. SERIAL_EOL();
  640. } // yy
  641. SERIAL_EOL();
  642. };
  643. print_topo_map(PSTR("\nBed Height Topography:\n"
  644. " +--- BACK --+\n"
  645. " | |\n"
  646. " L | (+) | R\n"
  647. " E | | I\n"
  648. " F | (-) N (+) | G\n"
  649. " T | | H\n"
  650. " | (-) | T\n"
  651. " | |\n"
  652. " O-- FRONT --+\n"
  653. " (0,0)\n"), true);
  654. if (abl.verbose_level > 3)
  655. print_topo_map(PSTR("\nCorrected Bed Height vs. Bed Topology:\n"), false);
  656. } // abl.topography_map
  657. #endif // AUTO_BED_LEVELING_LINEAR
  658. #if ABL_PLANAR
  659. // For LINEAR and 3POINT leveling correct the current position
  660. if (abl.verbose_level > 0)
  661. planner.bed_level_matrix.debug(PSTR("\n\nBed Level Correction Matrix:"));
  662. if (!abl.dryrun) {
  663. //
  664. // Correct the current XYZ position based on the tilted plane.
  665. //
  666. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position);
  667. xyze_pos_t converted = current_position;
  668. planner.force_unapply_leveling(converted); // use conversion machinery
  669. // Use the last measured distance to the bed, if possible
  670. if ( NEAR(current_position.x, abl.probePos.x - probe.offset_xy.x)
  671. && NEAR(current_position.y, abl.probePos.y - probe.offset_xy.y)
  672. ) {
  673. const float simple_z = current_position.z - abl.measured_z;
  674. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probed Z", simple_z, " Matrix Z", converted.z, " Discrepancy ", simple_z - converted.z);
  675. converted.z = simple_z;
  676. }
  677. // The rotated XY and corrected Z are now current_position
  678. current_position = converted;
  679. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
  680. }
  681. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  682. if (!abl.dryrun) {
  683. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("G29 uncorrected Z:", current_position.z);
  684. // Unapply the offset because it is going to be immediately applied
  685. // and cause compensation movement in Z
  686. const float fade_scaling_factor = TERN(ENABLE_LEVELING_FADE_HEIGHT, planner.fade_scaling_factor_for_z(current_position.z), 1);
  687. current_position.z -= fade_scaling_factor * bilinear_z_offset(current_position);
  688. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(" corrected Z:", current_position.z);
  689. }
  690. #endif // ABL_PLANAR
  691. // Auto Bed Leveling is complete! Enable if possible.
  692. planner.leveling_active = !abl.dryrun || abl.reenable;
  693. } // !isnan(abl.measured_z)
  694. // Restore state after probing
  695. if (!faux) restore_feedrate_and_scaling();
  696. // Sync the planner from the current_position
  697. if (planner.leveling_active) sync_plan_position();
  698. #if HAS_BED_PROBE
  699. probe.move_z_after_probing();
  700. #endif
  701. #ifdef Z_PROBE_END_SCRIPT
  702. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
  703. planner.synchronize();
  704. process_subcommands_now_P(PSTR(Z_PROBE_END_SCRIPT));
  705. #endif
  706. #if ENABLED(DWIN_CREALITY_LCD)
  707. DWIN_CompletedLeveling();
  708. #endif
  709. report_current_position();
  710. G29_RETURN(isnan(abl.measured_z));
  711. }
  712. #endif // HAS_ABL_NOT_UBL