My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

G29.cpp 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE));
  196. reset_stepper_timeout();
  197. const bool seenQ = EITHER(DEBUG_LEVELING_FEATURE, PROBE_MANUALLY) && parser.seen('Q');
  198. // G29 Q is also available if debugging
  199. #if ENABLED(DEBUG_LEVELING_FEATURE)
  200. const uint8_t old_debug_flags = marlin_debug_flags;
  201. if (seenQ) marlin_debug_flags |= MARLIN_DEBUG_LEVELING;
  202. DEBUG_SECTION(log_G29, "G29", DEBUGGING(LEVELING));
  203. if (DEBUGGING(LEVELING)) log_machine_info();
  204. marlin_debug_flags = old_debug_flags;
  205. if (DISABLED(PROBE_MANUALLY) && seenQ) G29_RETURN(false);
  206. #endif
  207. const bool seenA = TERN0(PROBE_MANUALLY, parser.seen('A')),
  208. no_action = seenA || seenQ,
  209. faux = ENABLED(DEBUG_LEVELING_FEATURE) && DISABLED(PROBE_MANUALLY) ? parser.boolval('C') : no_action;
  210. if (!no_action && planner.leveling_active && parser.boolval('O')) { // Auto-level only if needed
  211. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> Auto-level not needed, skip");
  212. G29_RETURN(false);
  213. }
  214. // Send 'N' to force homing before G29 (internal only)
  215. if (parser.seen('N'))
  216. process_subcommands_now_P(TERN(G28_L0_ENSURES_LEVELING_OFF, PSTR("G28L0"), G28_STR));
  217. // Don't allow auto-leveling without homing first
  218. if (homing_needed_error()) G29_RETURN(false);
  219. #if ENABLED(AUTO_BED_LEVELING_3POINT)
  220. vector_3 points[3];
  221. probe.get_three_points(points);
  222. #endif
  223. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  224. struct linear_fit_data lsf_results;
  225. #endif
  226. /**
  227. * On the initial G29 fetch command parameters.
  228. */
  229. if (!g29_in_progress) {
  230. TERN_(HAS_MULTI_HOTEND, if (active_extruder) tool_change(0));
  231. #if EITHER(PROBE_MANUALLY, AUTO_BED_LEVELING_LINEAR)
  232. abl.abl_probe_index = -1;
  233. #endif
  234. abl.reenable = planner.leveling_active;
  235. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  236. const bool seen_w = parser.seen('W');
  237. if (seen_w) {
  238. if (!leveling_is_valid()) {
  239. SERIAL_ERROR_MSG("No bilinear grid");
  240. G29_RETURN(false);
  241. }
  242. const float rz = parser.seenval('Z') ? RAW_Z_POSITION(parser.value_linear_units()) : current_position.z;
  243. if (!WITHIN(rz, -10, 10)) {
  244. SERIAL_ERROR_MSG("Bad Z value");
  245. G29_RETURN(false);
  246. }
  247. const float rx = RAW_X_POSITION(parser.linearval('X', MFNAN)),
  248. ry = RAW_Y_POSITION(parser.linearval('Y', MFNAN));
  249. int8_t i = parser.byteval('I', -1), j = parser.byteval('J', -1);
  250. if (!ISNAN(rx) && !ISNAN(ry)) {
  251. // Get nearest i / j from rx / ry
  252. i = (rx - bilinear_start.x + 0.5 * abl.gridSpacing.x) / abl.gridSpacing.x;
  253. j = (ry - bilinear_start.y + 0.5 * abl.gridSpacing.y) / abl.gridSpacing.y;
  254. LIMIT(i, 0, (GRID_MAX_POINTS_X) - 1);
  255. LIMIT(j, 0, (GRID_MAX_POINTS_Y) - 1);
  256. }
  257. if (WITHIN(i, 0, (GRID_MAX_POINTS_X) - 1) && WITHIN(j, 0, (GRID_MAX_POINTS_Y) - 1)) {
  258. set_bed_leveling_enabled(false);
  259. z_values[i][j] = rz;
  260. TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
  261. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(i, j, rz));
  262. set_bed_leveling_enabled(abl.reenable);
  263. if (abl.reenable) report_current_position();
  264. }
  265. G29_RETURN(false);
  266. } // parser.seen('W')
  267. #else
  268. constexpr bool seen_w = false;
  269. #endif
  270. // Jettison bed leveling data
  271. if (!seen_w && parser.seen('J')) {
  272. reset_bed_level();
  273. G29_RETURN(false);
  274. }
  275. abl.verbose_level = parser.intval('V');
  276. if (!WITHIN(abl.verbose_level, 0, 4)) {
  277. SERIAL_ECHOLNPGM("?(V)erbose level implausible (0-4).");
  278. G29_RETURN(false);
  279. }
  280. abl.dryrun = parser.boolval('D') || TERN0(PROBE_MANUALLY, no_action);
  281. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  282. incremental_LSF_reset(&lsf_results);
  283. abl.topography_map = abl.verbose_level > 2 || parser.boolval('T');
  284. // X and Y specify points in each direction, overriding the default
  285. // These values may be saved with the completed mesh
  286. abl.grid_points.set(
  287. parser.byteval('X', GRID_MAX_POINTS_X),
  288. parser.byteval('Y', GRID_MAX_POINTS_Y)
  289. );
  290. if (parser.seenval('P')) abl.grid_points.x = abl.grid_points.y = parser.value_int();
  291. if (!WITHIN(abl.grid_points.x, 2, GRID_MAX_POINTS_X)) {
  292. SERIAL_ECHOLNPGM("?Probe points (X) implausible (2-" STRINGIFY(GRID_MAX_POINTS_X) ").");
  293. G29_RETURN(false);
  294. }
  295. if (!WITHIN(abl.grid_points.y, 2, GRID_MAX_POINTS_Y)) {
  296. SERIAL_ECHOLNPGM("?Probe points (Y) implausible (2-" STRINGIFY(GRID_MAX_POINTS_Y) ").");
  297. G29_RETURN(false);
  298. }
  299. abl.abl_points = abl.grid_points.x * abl.grid_points.y;
  300. abl.mean = 0;
  301. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  302. abl.Z_offset = parser.linearval('Z');
  303. #endif
  304. #if ABL_USES_GRID
  305. xy_probe_feedrate_mm_s = MMM_TO_MMS(parser.linearval('S', XY_PROBE_FEEDRATE));
  306. const float x_min = probe.min_x(), x_max = probe.max_x(),
  307. y_min = probe.min_y(), y_max = probe.max_y();
  308. if (parser.seen('H')) {
  309. const int16_t size = (int16_t)parser.value_linear_units();
  310. abl.probe_position_lf.set(_MAX((X_CENTER) - size / 2, x_min), _MAX((Y_CENTER) - size / 2, y_min));
  311. abl.probe_position_rb.set(_MIN(abl.probe_position_lf.x + size, x_max), _MIN(abl.probe_position_lf.y + size, y_max));
  312. }
  313. else {
  314. abl.probe_position_lf.set(parser.linearval('L', x_min), parser.linearval('F', y_min));
  315. abl.probe_position_rb.set(parser.linearval('R', x_max), parser.linearval('B', y_max));
  316. }
  317. if (!probe.good_bounds(abl.probe_position_lf, abl.probe_position_rb)) {
  318. if (DEBUGGING(LEVELING)) {
  319. DEBUG_ECHOLNPAIR("G29 L", abl.probe_position_lf.x, " R", abl.probe_position_rb.x,
  320. " F", abl.probe_position_lf.y, " B", abl.probe_position_rb.y);
  321. }
  322. SERIAL_ECHOLNPGM("? (L,R,F,B) out of bounds.");
  323. G29_RETURN(false);
  324. }
  325. // Probe at the points of a lattice grid
  326. abl.gridSpacing.set((abl.probe_position_rb.x - abl.probe_position_lf.x) / (abl.grid_points.x - 1),
  327. (abl.probe_position_rb.y - abl.probe_position_lf.y) / (abl.grid_points.y - 1));
  328. #endif // ABL_USES_GRID
  329. if (abl.verbose_level > 0) {
  330. SERIAL_ECHOPGM("G29 Auto Bed Leveling");
  331. if (abl.dryrun) SERIAL_ECHOPGM(" (DRYRUN)");
  332. SERIAL_EOL();
  333. }
  334. planner.synchronize();
  335. #if ENABLED(AUTO_BED_LEVELING_3POINT)
  336. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> 3-point Leveling");
  337. points[0].z = points[1].z = points[2].z = 0; // Probe at 3 arbitrary points
  338. #endif
  339. #if BOTH(AUTO_BED_LEVELING_BILINEAR, EXTENSIBLE_UI)
  340. ExtUI::onMeshLevelingStart();
  341. #endif
  342. if (!faux) {
  343. remember_feedrate_scaling_off();
  344. #if ENABLED(PREHEAT_BEFORE_LEVELING)
  345. if (!abl.dryrun) probe.preheat_for_probing(LEVELING_NOZZLE_TEMP, LEVELING_BED_TEMP);
  346. #endif
  347. }
  348. // Disable auto bed leveling during G29.
  349. // Be formal so G29 can be done successively without G28.
  350. if (!no_action) set_bed_leveling_enabled(false);
  351. // Deploy certain probes before starting probing
  352. #if HAS_BED_PROBE
  353. if (ENABLED(BLTOUCH))
  354. do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE);
  355. else if (probe.deploy()) {
  356. set_bed_leveling_enabled(abl.reenable);
  357. G29_RETURN(false);
  358. }
  359. #endif
  360. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  361. if (TERN1(PROBE_MANUALLY, !no_action)
  362. && (abl.gridSpacing != bilinear_grid_spacing || abl.probe_position_lf != bilinear_start)
  363. ) {
  364. // Reset grid to 0.0 or "not probed". (Also disables ABL)
  365. reset_bed_level();
  366. // Initialize a grid with the given dimensions
  367. bilinear_grid_spacing = abl.gridSpacing;
  368. bilinear_start = abl.probe_position_lf;
  369. // Can't re-enable (on error) until the new grid is written
  370. abl.reenable = false;
  371. }
  372. #endif // AUTO_BED_LEVELING_BILINEAR
  373. } // !g29_in_progress
  374. #if ENABLED(PROBE_MANUALLY)
  375. // For manual probing, get the next index to probe now.
  376. // On the first probe this will be incremented to 0.
  377. if (!no_action) {
  378. ++abl.abl_probe_index;
  379. g29_in_progress = true;
  380. }
  381. // Abort current G29 procedure, go back to idle state
  382. if (seenA && g29_in_progress) {
  383. SERIAL_ECHOLNPGM("Manual G29 aborted");
  384. SET_SOFT_ENDSTOP_LOOSE(false);
  385. set_bed_leveling_enabled(abl.reenable);
  386. g29_in_progress = false;
  387. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  388. }
  389. // Query G29 status
  390. if (abl.verbose_level || seenQ) {
  391. SERIAL_ECHOPGM("Manual G29 ");
  392. if (g29_in_progress) {
  393. SERIAL_ECHOPAIR("point ", _MIN(abl.abl_probe_index + 1, abl.abl_points));
  394. SERIAL_ECHOLNPAIR(" of ", abl.abl_points);
  395. }
  396. else
  397. SERIAL_ECHOLNPGM("idle");
  398. }
  399. if (no_action) G29_RETURN(false);
  400. if (abl.abl_probe_index == 0) {
  401. // For the initial G29 S2 save software endstop state
  402. SET_SOFT_ENDSTOP_LOOSE(true);
  403. // Move close to the bed before the first point
  404. do_blocking_move_to_z(0);
  405. }
  406. else {
  407. #if EITHER(AUTO_BED_LEVELING_LINEAR, AUTO_BED_LEVELING_3POINT)
  408. const uint16_t index = abl.abl_probe_index - 1;
  409. #endif
  410. // For G29 after adjusting Z.
  411. // Save the previous Z before going to the next point
  412. abl.measured_z = current_position.z;
  413. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  414. abl.mean += abl.measured_z;
  415. abl.eqnBVector[index] = abl.measured_z;
  416. abl.eqnAMatrix[index + 0 * abl.abl_points] = abl.probePos.x;
  417. abl.eqnAMatrix[index + 1 * abl.abl_points] = abl.probePos.y;
  418. abl.eqnAMatrix[index + 2 * abl.abl_points] = 1;
  419. incremental_LSF(&lsf_results, abl.probePos, abl.measured_z);
  420. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  421. points[index].z = abl.measured_z;
  422. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  423. const float newz = abl.measured_z + abl.Z_offset;
  424. z_values[abl.meshCount.x][abl.meshCount.y] = newz;
  425. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(abl.meshCount, newz));
  426. 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);
  427. #endif
  428. }
  429. //
  430. // If there's another point to sample, move there with optional lift.
  431. //
  432. #if ABL_USES_GRID
  433. // Skip any unreachable points
  434. while (abl.abl_probe_index < abl.abl_points) {
  435. // Set abl.meshCount.x, abl.meshCount.y based on abl.abl_probe_index, with zig-zag
  436. PR_OUTER_VAR = abl.abl_probe_index / PR_INNER_SIZE;
  437. PR_INNER_VAR = abl.abl_probe_index - (PR_OUTER_VAR * PR_INNER_SIZE);
  438. // Probe in reverse order for every other row/column
  439. const bool zig = (PR_OUTER_VAR & 1); // != ((PR_OUTER_SIZE) & 1);
  440. if (zig) PR_INNER_VAR = (PR_INNER_SIZE - 1) - PR_INNER_VAR;
  441. abl.probePos = abl.probe_position_lf + abl.gridSpacing * abl.meshCount.asFloat();
  442. TERN_(AUTO_BED_LEVELING_LINEAR, abl.indexIntoAB[abl.meshCount.x][abl.meshCount.y] = abl.abl_probe_index);
  443. // Keep looping till a reachable point is found
  444. if (position_is_reachable(abl.probePos)) break;
  445. ++abl.abl_probe_index;
  446. }
  447. // Is there a next point to move to?
  448. if (abl.abl_probe_index < abl.abl_points) {
  449. _manual_goto_xy(abl.probePos); // Can be used here too!
  450. // Disable software endstops to allow manual adjustment
  451. // If G29 is not completed, they will not be re-enabled
  452. SET_SOFT_ENDSTOP_LOOSE(true);
  453. G29_RETURN(false);
  454. }
  455. else {
  456. // Leveling done! Fall through to G29 finishing code below
  457. SERIAL_ECHOLNPGM("Grid probing done.");
  458. // Re-enable software endstops, if needed
  459. SET_SOFT_ENDSTOP_LOOSE(false);
  460. }
  461. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  462. // Probe at 3 arbitrary points
  463. if (abl.abl_probe_index < abl.abl_points) {
  464. abl.probePos = points[abl.abl_probe_index];
  465. _manual_goto_xy(abl.probePos);
  466. // Disable software endstops to allow manual adjustment
  467. // If G29 is not completed, they will not be re-enabled
  468. SET_SOFT_ENDSTOP_LOOSE(true);
  469. G29_RETURN(false);
  470. }
  471. else {
  472. SERIAL_ECHOLNPGM("3-point probing done.");
  473. // Re-enable software endstops, if needed
  474. SET_SOFT_ENDSTOP_LOOSE(false);
  475. if (!abl.dryrun) {
  476. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  477. if (planeNormal.z < 0) planeNormal *= -1;
  478. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  479. // Can't re-enable (on error) until the new grid is written
  480. abl.reenable = false;
  481. }
  482. }
  483. #endif // AUTO_BED_LEVELING_3POINT
  484. #else // !PROBE_MANUALLY
  485. {
  486. const ProbePtRaise raise_after = parser.boolval('E') ? PROBE_PT_STOW : PROBE_PT_RAISE;
  487. abl.measured_z = 0;
  488. #if ABL_USES_GRID
  489. bool zig = PR_OUTER_SIZE & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION
  490. abl.measured_z = 0;
  491. // Outer loop is X with PROBE_Y_FIRST enabled
  492. // Outer loop is Y with PROBE_Y_FIRST disabled
  493. for (PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_SIZE && !ISNAN(abl.measured_z); PR_OUTER_VAR++) {
  494. int8_t inStart, inStop, inInc;
  495. if (zig) { // Zig away from origin
  496. inStart = 0; // Left or front
  497. inStop = PR_INNER_SIZE; // Right or back
  498. inInc = 1; // Zig right
  499. }
  500. else { // Zag towards origin
  501. inStart = PR_INNER_SIZE - 1; // Right or back
  502. inStop = -1; // Left or front
  503. inInc = -1; // Zag left
  504. }
  505. zig ^= true; // zag
  506. // An index to print current state
  507. uint8_t pt_index = (PR_OUTER_VAR) * (PR_INNER_SIZE) + 1;
  508. // Inner loop is Y with PROBE_Y_FIRST enabled
  509. // Inner loop is X with PROBE_Y_FIRST disabled
  510. for (PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; pt_index++, PR_INNER_VAR += inInc) {
  511. abl.probePos = abl.probe_position_lf + abl.gridSpacing * abl.meshCount.asFloat();
  512. TERN_(AUTO_BED_LEVELING_LINEAR, abl.indexIntoAB[abl.meshCount.x][abl.meshCount.y] = ++abl.abl_probe_index); // 0...
  513. // Avoid probing outside the round or hexagonal area
  514. if (TERN0(IS_KINEMATIC, !probe.can_reach(abl.probePos))) continue;
  515. if (abl.verbose_level) SERIAL_ECHOLNPAIR("Probing mesh point ", pt_index, "/", abl.abl_points, ".");
  516. 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)));
  517. abl.measured_z = faux ? 0.001f * random(-100, 101) : probe.probe_at_point(abl.probePos, raise_after, abl.verbose_level);
  518. if (ISNAN(abl.measured_z)) {
  519. set_bed_leveling_enabled(abl.reenable);
  520. break; // Breaks out of both loops
  521. }
  522. #if ENABLED(PROBE_TEMP_COMPENSATION)
  523. temp_comp.compensate_measurement(TSI_BED, thermalManager.degBed(), abl.measured_z);
  524. temp_comp.compensate_measurement(TSI_PROBE, thermalManager.degProbe(), abl.measured_z);
  525. TERN_(USE_TEMP_EXT_COMPENSATION, temp_comp.compensate_measurement(TSI_EXT, thermalManager.degHotend(), abl.measured_z));
  526. #endif
  527. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  528. abl.mean += abl.measured_z;
  529. abl.eqnBVector[abl.abl_probe_index] = abl.measured_z;
  530. abl.eqnAMatrix[abl.abl_probe_index + 0 * abl.abl_points] = abl.probePos.x;
  531. abl.eqnAMatrix[abl.abl_probe_index + 1 * abl.abl_points] = abl.probePos.y;
  532. abl.eqnAMatrix[abl.abl_probe_index + 2 * abl.abl_points] = 1;
  533. incremental_LSF(&lsf_results, abl.probePos, abl.measured_z);
  534. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  535. const float z = abl.measured_z + abl.Z_offset;
  536. z_values[abl.meshCount.x][abl.meshCount.y] = z;
  537. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(abl.meshCount, z));
  538. #endif
  539. abl.reenable = false;
  540. idle_no_sleep();
  541. } // inner
  542. } // outer
  543. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  544. // Probe at 3 arbitrary points
  545. LOOP_L_N(i, 3) {
  546. if (abl.verbose_level) SERIAL_ECHOLNPAIR("Probing point ", i + 1, "/3.");
  547. TERN_(HAS_STATUS_MESSAGE, ui.status_printf_P(0, PSTR(S_FMT " %i/3"), GET_TEXT(MSG_PROBING_MESH), int(i + 1)));
  548. // Retain the last probe position
  549. abl.probePos = points[i];
  550. abl.measured_z = faux ? 0.001 * random(-100, 101) : probe.probe_at_point(abl.probePos, raise_after, abl.verbose_level);
  551. if (ISNAN(abl.measured_z)) {
  552. set_bed_leveling_enabled(abl.reenable);
  553. break;
  554. }
  555. points[i].z = abl.measured_z;
  556. }
  557. if (!abl.dryrun && !ISNAN(abl.measured_z)) {
  558. vector_3 planeNormal = vector_3::cross(points[0] - points[1], points[2] - points[1]).get_normal();
  559. if (planeNormal.z < 0) planeNormal *= -1;
  560. planner.bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  561. // Can't re-enable (on error) until the new grid is written
  562. abl.reenable = false;
  563. }
  564. #endif // AUTO_BED_LEVELING_3POINT
  565. TERN_(HAS_STATUS_MESSAGE, ui.reset_status());
  566. // Stow the probe. No raise for FIX_MOUNTED_PROBE.
  567. if (probe.stow()) {
  568. set_bed_leveling_enabled(abl.reenable);
  569. abl.measured_z = MFNAN;
  570. }
  571. }
  572. #endif // !PROBE_MANUALLY
  573. //
  574. // G29 Finishing Code
  575. //
  576. // Unless this is a dry run, auto bed leveling will
  577. // definitely be enabled after this point.
  578. //
  579. // If code above wants to continue leveling, it should
  580. // return or loop before this point.
  581. //
  582. if (DEBUGGING(LEVELING)) DEBUG_POS("> probing complete", current_position);
  583. #if ENABLED(PROBE_MANUALLY)
  584. g29_in_progress = false;
  585. TERN_(LCD_BED_LEVELING, ui.wait_for_move = false);
  586. #endif
  587. // Calculate leveling, print reports, correct the position
  588. if (!ISNAN(abl.measured_z)) {
  589. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  590. if (!abl.dryrun) extrapolate_unprobed_bed_level();
  591. print_bilinear_leveling_grid();
  592. refresh_bed_level();
  593. TERN_(ABL_BILINEAR_SUBDIVISION, print_bilinear_leveling_grid_virt());
  594. #elif ENABLED(AUTO_BED_LEVELING_LINEAR)
  595. // For LINEAR leveling calculate matrix, print reports, correct the position
  596. /**
  597. * solve the plane equation ax + by + d = z
  598. * A is the matrix with rows [x y 1] for all the probed points
  599. * B is the vector of the Z positions
  600. * the normal vector to the plane is formed by the coefficients of the
  601. * plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  602. * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  603. */
  604. struct { float a, b, d; } plane_equation_coefficients;
  605. finish_incremental_LSF(&lsf_results);
  606. plane_equation_coefficients.a = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below
  607. plane_equation_coefficients.b = -lsf_results.B; // but that is not yet tested.
  608. plane_equation_coefficients.d = -lsf_results.D;
  609. abl.mean /= abl.abl_points;
  610. if (abl.verbose_level) {
  611. SERIAL_ECHOPAIR_F("Eqn coefficients: a: ", plane_equation_coefficients.a, 8);
  612. SERIAL_ECHOPAIR_F(" b: ", plane_equation_coefficients.b, 8);
  613. SERIAL_ECHOPAIR_F(" d: ", plane_equation_coefficients.d, 8);
  614. if (abl.verbose_level > 2)
  615. SERIAL_ECHOPAIR_F("\nMean of sampled points: ", abl.mean, 8);
  616. SERIAL_EOL();
  617. }
  618. // Create the matrix but don't correct the position yet
  619. if (!abl.dryrun)
  620. planner.bed_level_matrix = matrix_3x3::create_look_at(
  621. vector_3(-plane_equation_coefficients.a, -plane_equation_coefficients.b, 1) // We can eliminate the '-' here and up above
  622. );
  623. // Show the Topography map if enabled
  624. if (abl.topography_map) {
  625. float min_diff = 999;
  626. auto print_topo_map = [&](PGM_P const title, const bool get_min) {
  627. SERIAL_ECHOPGM_P(title);
  628. for (int8_t yy = abl.grid_points.y - 1; yy >= 0; yy--) {
  629. LOOP_L_N(xx, abl.grid_points.x) {
  630. const int ind = abl.indexIntoAB[xx][yy];
  631. xyz_float_t tmp = { abl.eqnAMatrix[ind + 0 * abl.abl_points],
  632. abl.eqnAMatrix[ind + 1 * abl.abl_points], 0 };
  633. planner.bed_level_matrix.apply_rotation_xyz(tmp);
  634. if (get_min) NOMORE(min_diff, abl.eqnBVector[ind] - tmp.z);
  635. const float subval = get_min ? abl.mean : tmp.z + min_diff,
  636. diff = abl.eqnBVector[ind] - subval;
  637. SERIAL_CHAR(' '); if (diff >= 0.0) SERIAL_CHAR('+'); // Include + for column alignment
  638. SERIAL_ECHO_F(diff, 5);
  639. } // xx
  640. SERIAL_EOL();
  641. } // yy
  642. SERIAL_EOL();
  643. };
  644. print_topo_map(PSTR("\nBed Height Topography:\n"
  645. " +--- BACK --+\n"
  646. " | |\n"
  647. " L | (+) | R\n"
  648. " E | | I\n"
  649. " F | (-) N (+) | G\n"
  650. " T | | H\n"
  651. " | (-) | T\n"
  652. " | |\n"
  653. " O-- FRONT --+\n"
  654. " (0,0)\n"), true);
  655. if (abl.verbose_level > 3)
  656. print_topo_map(PSTR("\nCorrected Bed Height vs. Bed Topology:\n"), false);
  657. } // abl.topography_map
  658. #endif // AUTO_BED_LEVELING_LINEAR
  659. #if ABL_PLANAR
  660. // For LINEAR and 3POINT leveling correct the current position
  661. if (abl.verbose_level > 0)
  662. planner.bed_level_matrix.debug(PSTR("\n\nBed Level Correction Matrix:"));
  663. if (!abl.dryrun) {
  664. //
  665. // Correct the current XYZ position based on the tilted plane.
  666. //
  667. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 uncorrected XYZ", current_position);
  668. xyze_pos_t converted = current_position;
  669. planner.force_unapply_leveling(converted); // use conversion machinery
  670. // Use the last measured distance to the bed, if possible
  671. if ( NEAR(current_position.x, abl.probePos.x - probe.offset_xy.x)
  672. && NEAR(current_position.y, abl.probePos.y - probe.offset_xy.y)
  673. ) {
  674. const float simple_z = current_position.z - abl.measured_z;
  675. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probed Z", simple_z, " Matrix Z", converted.z, " Discrepancy ", simple_z - converted.z);
  676. converted.z = simple_z;
  677. }
  678. // The rotated XY and corrected Z are now current_position
  679. current_position = converted;
  680. if (DEBUGGING(LEVELING)) DEBUG_POS("G29 corrected XYZ", current_position);
  681. }
  682. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  683. if (!abl.dryrun) {
  684. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("G29 uncorrected Z:", current_position.z);
  685. // Unapply the offset because it is going to be immediately applied
  686. // and cause compensation movement in Z
  687. const float fade_scaling_factor = TERN(ENABLE_LEVELING_FADE_HEIGHT, planner.fade_scaling_factor_for_z(current_position.z), 1);
  688. current_position.z -= fade_scaling_factor * bilinear_z_offset(current_position);
  689. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(" corrected Z:", current_position.z);
  690. }
  691. #endif // ABL_PLANAR
  692. // Auto Bed Leveling is complete! Enable if possible.
  693. planner.leveling_active = !abl.dryrun || abl.reenable;
  694. } // !ISNAN(abl.measured_z)
  695. // Restore state after probing
  696. if (!faux) restore_feedrate_and_scaling();
  697. // Sync the planner from the current_position
  698. if (planner.leveling_active) sync_plan_position();
  699. #if HAS_BED_PROBE
  700. probe.move_z_after_probing();
  701. #endif
  702. #ifdef Z_PROBE_END_SCRIPT
  703. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
  704. planner.synchronize();
  705. process_subcommands_now_P(PSTR(Z_PROBE_END_SCRIPT));
  706. #endif
  707. #if ENABLED(DWIN_CREALITY_LCD)
  708. DWIN_CompletedLeveling();
  709. #endif
  710. report_current_position();
  711. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE));
  712. G29_RETURN(ISNAN(abl.measured_z));
  713. }
  714. #endif // HAS_ABL_NOT_UBL