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.

ubl_motion.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include "../../../inc/MarlinConfig.h"
  23. #if ENABLED(AUTO_BED_LEVELING_UBL)
  24. #include "../bedlevel.h"
  25. #include "../../../module/planner.h"
  26. #include "../../../module/stepper.h"
  27. #include "../../../module/motion.h"
  28. #if ENABLED(DELTA)
  29. #include "../../../module/delta.h"
  30. #endif
  31. #include "../../../MarlinCore.h"
  32. #include <math.h>
  33. #if !UBL_SEGMENTED
  34. void unified_bed_leveling::line_to_destination_cartesian(const_feedRate_t scaled_fr_mm_s, const uint8_t extruder) {
  35. /**
  36. * Much of the nozzle movement will be within the same cell. So we will do as little computation
  37. * as possible to determine if this is the case. If this move is within the same cell, we will
  38. * just do the required Z-Height correction, call the Planner's buffer_line() routine, and leave
  39. */
  40. #if HAS_POSITION_MODIFIERS
  41. xyze_pos_t start = current_position, end = destination;
  42. planner.apply_modifiers(start);
  43. planner.apply_modifiers(end);
  44. #else
  45. const xyze_pos_t &start = current_position, &end = destination;
  46. #endif
  47. const xy_int8_t istart = cell_indexes(start), iend = cell_indexes(end);
  48. // A move within the same cell needs no splitting
  49. if (istart == iend) {
  50. FINAL_MOVE:
  51. // When UBL_Z_RAISE_WHEN_OFF_MESH is disabled Z correction is extrapolated from the edge of the mesh
  52. #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
  53. // For a move off the UBL mesh, use a constant Z raise
  54. if (!cell_index_x_valid(end.x) || !cell_index_y_valid(end.y)) {
  55. // Note: There is no Z Correction in this case. We are off the mesh and don't know what
  56. // a reasonable correction would be, UBL_Z_RAISE_WHEN_OFF_MESH will be used instead of
  57. // a calculated (Bi-Linear interpolation) correction.
  58. end.z += UBL_Z_RAISE_WHEN_OFF_MESH;
  59. planner.buffer_segment(end, scaled_fr_mm_s, extruder);
  60. current_position = destination;
  61. return;
  62. }
  63. #endif
  64. // The distance is always MESH_X_DIST so multiply by the constant reciprocal.
  65. const float xratio = (end.x - mesh_index_to_xpos(iend.x)) * RECIPROCAL(MESH_X_DIST),
  66. yratio = (end.y - mesh_index_to_ypos(iend.y)) * RECIPROCAL(MESH_Y_DIST),
  67. z1 = z_values[iend.x][iend.y ] + xratio * (z_values[iend.x + 1][iend.y ] - z_values[iend.x][iend.y ]),
  68. z2 = z_values[iend.x][iend.y + 1] + xratio * (z_values[iend.x + 1][iend.y + 1] - z_values[iend.x][iend.y + 1]);
  69. // X cell-fraction done. Interpolate the two Z offsets with the Y fraction for the final Z offset.
  70. const float z0 = (z1 + (z2 - z1) * yratio) * planner.fade_scaling_factor_for_z(end.z);
  71. // Undefined parts of the Mesh in z_values[][] are NAN.
  72. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  73. if (!ISNAN(z0)) end.z += z0;
  74. planner.buffer_segment(end, scaled_fr_mm_s, extruder);
  75. current_position = destination;
  76. return;
  77. }
  78. /**
  79. * Past this point the move is known to cross one or more mesh lines. Check for the most common
  80. * case - crossing only one X or Y line - after details are worked out to reduce computation.
  81. */
  82. const xy_float_t dist = end - start;
  83. const xy_bool_t neg { dist.x < 0, dist.y < 0 };
  84. const xy_int8_t ineg { int8_t(neg.x), int8_t(neg.y) };
  85. const xy_float_t sign { neg.x ? -1.0f : 1.0f, neg.y ? -1.0f : 1.0f };
  86. const xy_int8_t iadd { int8_t(iend.x == istart.x ? 0 : sign.x), int8_t(iend.y == istart.y ? 0 : sign.y) };
  87. /**
  88. * Compute the extruder scaling factor for each partial move, checking for
  89. * zero-length moves that would result in an infinite scaling factor.
  90. * A float divide is required for this, but then it just multiplies.
  91. * Also select a scaling factor based on the larger of the X and Y
  92. * components. The larger of the two is used to preserve precision.
  93. */
  94. const xy_float_t ad = sign * dist;
  95. const bool use_x_dist = ad.x > ad.y;
  96. float on_axis_distance = use_x_dist ? dist.x : dist.y,
  97. e_position = end.e - start.e,
  98. z_position = end.z - start.z;
  99. const float e_normalized_dist = e_position / on_axis_distance, // Allow divide by zero
  100. z_normalized_dist = z_position / on_axis_distance;
  101. xy_int8_t icell = istart;
  102. const float ratio = dist.y / dist.x, // Allow divide by zero
  103. c = start.y - ratio * start.x;
  104. const bool inf_normalized_flag = isinf(e_normalized_dist),
  105. inf_ratio_flag = isinf(ratio);
  106. /**
  107. * Handle vertical lines that stay within one column.
  108. * These need not be perfectly vertical.
  109. */
  110. if (iadd.x == 0) { // Vertical line?
  111. icell.y += ineg.y; // Line going down? Just go to the bottom.
  112. while (icell.y != iend.y + ineg.y) {
  113. icell.y += iadd.y;
  114. const float next_mesh_line_y = mesh_index_to_ypos(icell.y);
  115. /**
  116. * Skip the calculations for an infinite slope.
  117. * For others the next X is the same so this can continue.
  118. * Calculate X at the next Y mesh line.
  119. */
  120. const float rx = inf_ratio_flag ? start.x : (next_mesh_line_y - c) / ratio;
  121. float z0 = z_correction_for_x_on_horizontal_mesh_line(rx, icell.x, icell.y)
  122. * planner.fade_scaling_factor_for_z(end.z);
  123. // Undefined parts of the Mesh in z_values[][] are NAN.
  124. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  125. if (ISNAN(z0)) z0 = 0.0;
  126. const float ry = mesh_index_to_ypos(icell.y);
  127. /**
  128. * Without this check, it's possible to generate a zero length move, as in the case where
  129. * the line is heading down, starting exactly on a mesh line boundary. Since this is rare
  130. * it might be fine to remove this check and let planner.buffer_segment() filter it out.
  131. */
  132. if (ry != start.y) {
  133. if (!inf_normalized_flag) { // fall-through faster than branch
  134. on_axis_distance = use_x_dist ? rx - start.x : ry - start.y;
  135. e_position = start.e + on_axis_distance * e_normalized_dist;
  136. z_position = start.z + on_axis_distance * z_normalized_dist;
  137. }
  138. else {
  139. e_position = end.e;
  140. z_position = end.z;
  141. }
  142. planner.buffer_segment(rx, ry, z_position + z0, e_position, scaled_fr_mm_s, extruder);
  143. } //else printf("FIRST MOVE PRUNED ");
  144. }
  145. // At the final destination? Usually not, but when on a Y Mesh Line it's completed.
  146. if (xy_pos_t(current_position) != xy_pos_t(end))
  147. goto FINAL_MOVE;
  148. current_position = destination;
  149. return;
  150. }
  151. /**
  152. * Handle horizontal lines that stay within one row.
  153. * These need not be perfectly horizontal.
  154. */
  155. if (iadd.y == 0) { // Horizontal line?
  156. icell.x += ineg.x; // Heading left? Just go to the left edge of the cell for the first move.
  157. while (icell.x != iend.x + ineg.x) {
  158. icell.x += iadd.x;
  159. const float rx = mesh_index_to_xpos(icell.x);
  160. const float ry = ratio * rx + c; // Calculate Y at the next X mesh line
  161. float z0 = z_correction_for_y_on_vertical_mesh_line(ry, icell.x, icell.y)
  162. * planner.fade_scaling_factor_for_z(end.z);
  163. // Undefined parts of the Mesh in z_values[][] are NAN.
  164. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  165. if (ISNAN(z0)) z0 = 0.0;
  166. /**
  167. * Without this check, it's possible to generate a zero length move, as in the case where
  168. * the line is heading left, starting exactly on a mesh line boundary. Since this is rare
  169. * it might be fine to remove this check and let planner.buffer_segment() filter it out.
  170. */
  171. if (rx != start.x) {
  172. if (!inf_normalized_flag) {
  173. on_axis_distance = use_x_dist ? rx - start.x : ry - start.y;
  174. e_position = start.e + on_axis_distance * e_normalized_dist; // is based on X or Y because this is a horizontal move
  175. z_position = start.z + on_axis_distance * z_normalized_dist;
  176. }
  177. else {
  178. e_position = end.e;
  179. z_position = end.z;
  180. }
  181. if (!planner.buffer_segment(rx, ry, z_position + z0, e_position, scaled_fr_mm_s, extruder))
  182. break;
  183. } //else printf("FIRST MOVE PRUNED ");
  184. }
  185. if (xy_pos_t(current_position) != xy_pos_t(end))
  186. goto FINAL_MOVE;
  187. current_position = destination;
  188. return;
  189. }
  190. /**
  191. * Generic case of a line crossing both X and Y Mesh lines.
  192. */
  193. xy_int8_t cnt = (istart - iend).ABS();
  194. icell += ineg;
  195. while (cnt) {
  196. const float next_mesh_line_x = mesh_index_to_xpos(icell.x + iadd.x),
  197. next_mesh_line_y = mesh_index_to_ypos(icell.y + iadd.y),
  198. ry = ratio * next_mesh_line_x + c, // Calculate Y at the next X mesh line
  199. rx = (next_mesh_line_y - c) / ratio; // Calculate X at the next Y mesh line
  200. // (No need to worry about ratio == 0.
  201. // In that case, it was already detected
  202. // as a vertical line move above.)
  203. if (neg.x == (rx > next_mesh_line_x)) { // Check if we hit the Y line first
  204. // Yes! Crossing a Y Mesh Line next
  205. float z0 = z_correction_for_x_on_horizontal_mesh_line(rx, icell.x - ineg.x, icell.y + iadd.y)
  206. * planner.fade_scaling_factor_for_z(end.z);
  207. // Undefined parts of the Mesh in z_values[][] are NAN.
  208. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  209. if (ISNAN(z0)) z0 = 0.0;
  210. if (!inf_normalized_flag) {
  211. on_axis_distance = use_x_dist ? rx - start.x : next_mesh_line_y - start.y;
  212. e_position = start.e + on_axis_distance * e_normalized_dist;
  213. z_position = start.z + on_axis_distance * z_normalized_dist;
  214. }
  215. else {
  216. e_position = end.e;
  217. z_position = end.z;
  218. }
  219. if (!planner.buffer_segment(rx, next_mesh_line_y, z_position + z0, e_position, scaled_fr_mm_s, extruder))
  220. break;
  221. icell.y += iadd.y;
  222. cnt.y--;
  223. }
  224. else {
  225. // Yes! Crossing a X Mesh Line next
  226. float z0 = z_correction_for_y_on_vertical_mesh_line(ry, icell.x + iadd.x, icell.y - ineg.y)
  227. * planner.fade_scaling_factor_for_z(end.z);
  228. // Undefined parts of the Mesh in z_values[][] are NAN.
  229. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  230. if (ISNAN(z0)) z0 = 0.0;
  231. if (!inf_normalized_flag) {
  232. on_axis_distance = use_x_dist ? next_mesh_line_x - start.x : ry - start.y;
  233. e_position = start.e + on_axis_distance * e_normalized_dist;
  234. z_position = start.z + on_axis_distance * z_normalized_dist;
  235. }
  236. else {
  237. e_position = end.e;
  238. z_position = end.z;
  239. }
  240. if (!planner.buffer_segment(next_mesh_line_x, ry, z_position + z0, e_position, scaled_fr_mm_s, extruder))
  241. break;
  242. icell.x += iadd.x;
  243. cnt.x--;
  244. }
  245. if (cnt.x < 0 || cnt.y < 0) break; // Too far! Exit the loop and go to FINAL_MOVE
  246. }
  247. if (xy_pos_t(current_position) != xy_pos_t(end))
  248. goto FINAL_MOVE;
  249. current_position = destination;
  250. }
  251. #else // UBL_SEGMENTED
  252. #if IS_SCARA
  253. #define DELTA_SEGMENT_MIN_LENGTH 0.25 // SCARA minimum segment size is 0.25mm
  254. #elif ENABLED(DELTA)
  255. #define DELTA_SEGMENT_MIN_LENGTH 0.10 // mm (still subject to DELTA_SEGMENTS_PER_SECOND)
  256. #else // CARTESIAN
  257. #ifdef LEVELED_SEGMENT_LENGTH
  258. #define DELTA_SEGMENT_MIN_LENGTH LEVELED_SEGMENT_LENGTH
  259. #else
  260. #define DELTA_SEGMENT_MIN_LENGTH 1.00 // mm (similar to G2/G3 arc segmentation)
  261. #endif
  262. #endif
  263. /**
  264. * Prepare a segmented linear move for DELTA/SCARA/CARTESIAN with UBL and FADE semantics.
  265. * This calls planner.buffer_segment multiple times for small incremental moves.
  266. * Returns true if did NOT move, false if moved (requires current_position update).
  267. */
  268. bool _O2 unified_bed_leveling::line_to_destination_segmented(const_feedRate_t scaled_fr_mm_s) {
  269. if (!position_is_reachable(destination)) // fail if moving outside reachable boundary
  270. return true; // did not move, so current_position still accurate
  271. const xyze_pos_t total = destination - current_position;
  272. const float cart_xy_mm_2 = HYPOT2(total.x, total.y),
  273. cart_xy_mm = SQRT(cart_xy_mm_2); // Total XY distance
  274. #if IS_KINEMATIC
  275. const float seconds = cart_xy_mm / scaled_fr_mm_s; // Duration of XY move at requested rate
  276. uint16_t segments = LROUND(segments_per_second * seconds), // Preferred number of segments for distance @ feedrate
  277. seglimit = LROUND(cart_xy_mm * RECIPROCAL(DELTA_SEGMENT_MIN_LENGTH)); // Number of segments at minimum segment length
  278. NOMORE(segments, seglimit); // Limit to minimum segment length (fewer segments)
  279. #else
  280. uint16_t segments = LROUND(cart_xy_mm * RECIPROCAL(DELTA_SEGMENT_MIN_LENGTH)); // Cartesian fixed segment length
  281. #endif
  282. NOLESS(segments, 1U); // Must have at least one segment
  283. const float inv_segments = 1.0f / segments, // Reciprocal to save calculation
  284. segment_xyz_mm = SQRT(cart_xy_mm_2 + sq(total.z)) * inv_segments; // Length of each segment
  285. #if ENABLED(SCARA_FEEDRATE_SCALING)
  286. const float inv_duration = scaled_fr_mm_s / segment_xyz_mm;
  287. #endif
  288. xyze_float_t diff = total * inv_segments;
  289. // Note that E segment distance could vary slightly as z mesh height
  290. // changes for each segment, but small enough to ignore.
  291. xyze_pos_t raw = current_position;
  292. // Just do plain segmentation if UBL is inactive or the target is above the fade height
  293. if (!planner.leveling_active || !planner.leveling_active_at_z(destination.z)) {
  294. while (--segments) {
  295. raw += diff;
  296. planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, segment_xyz_mm
  297. #if ENABLED(SCARA_FEEDRATE_SCALING)
  298. , inv_duration
  299. #endif
  300. );
  301. }
  302. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, segment_xyz_mm
  303. #if ENABLED(SCARA_FEEDRATE_SCALING)
  304. , inv_duration
  305. #endif
  306. );
  307. return false; // Did not set current from destination
  308. }
  309. // Otherwise perform per-segment leveling
  310. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  311. const float fade_scaling_factor = planner.fade_scaling_factor_for_z(destination.z);
  312. #endif
  313. // Move to first segment destination
  314. raw += diff;
  315. for (;;) { // for each mesh cell encountered during the move
  316. // Compute mesh cell invariants that remain constant for all segments within cell.
  317. // Note for cell index, if point is outside the mesh grid (in MESH_INSET perimeter)
  318. // the bilinear interpolation from the adjacent cell within the mesh will still work.
  319. // Inner loop will exit each time (because out of cell bounds) but will come back
  320. // in top of loop and again re-find same adjacent cell and use it, just less efficient
  321. // for mesh inset area.
  322. xy_int8_t icell = {
  323. int8_t((raw.x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST)),
  324. int8_t((raw.y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST))
  325. };
  326. LIMIT(icell.x, 0, GRID_MAX_CELLS_X);
  327. LIMIT(icell.y, 0, GRID_MAX_CELLS_Y);
  328. float z_x0y0 = z_values[icell.x ][icell.y ], // z at lower left corner
  329. z_x1y0 = z_values[icell.x+1][icell.y ], // z at upper left corner
  330. z_x0y1 = z_values[icell.x ][icell.y+1], // z at lower right corner
  331. z_x1y1 = z_values[icell.x+1][icell.y+1]; // z at upper right corner
  332. if (ISNAN(z_x0y0)) z_x0y0 = 0; // ideally activating planner.leveling_active (G29 A)
  333. if (ISNAN(z_x1y0)) z_x1y0 = 0; // should refuse if any invalid mesh points
  334. if (ISNAN(z_x0y1)) z_x0y1 = 0; // in order to avoid ISNAN tests per cell,
  335. if (ISNAN(z_x1y1)) z_x1y1 = 0; // thus guessing zero for undefined points
  336. const xy_pos_t pos = { mesh_index_to_xpos(icell.x), mesh_index_to_ypos(icell.y) };
  337. xy_pos_t cell = raw - pos;
  338. const float z_xmy0 = (z_x1y0 - z_x0y0) * RECIPROCAL(MESH_X_DIST), // z slope per x along y0 (lower left to lower right)
  339. z_xmy1 = (z_x1y1 - z_x0y1) * RECIPROCAL(MESH_X_DIST); // z slope per x along y1 (upper left to upper right)
  340. float z_cxy0 = z_x0y0 + z_xmy0 * cell.x; // z height along y0 at cell.x (changes for each cell.x in cell)
  341. const float z_cxy1 = z_x0y1 + z_xmy1 * cell.x, // z height along y1 at cell.x
  342. z_cxyd = z_cxy1 - z_cxy0; // z height difference along cell.x from y0 to y1
  343. float z_cxym = z_cxyd * RECIPROCAL(MESH_Y_DIST); // z slope per y along cell.x from pos.y to y1 (changes for each cell.x in cell)
  344. // float z_cxcy = z_cxy0 + z_cxym * cell.y; // interpolated mesh z height along cell.x at cell.y (do inside the segment loop)
  345. // As subsequent segments step through this cell, the z_cxy0 intercept will change
  346. // and the z_cxym slope will change, both as a function of cell.x within the cell, and
  347. // each change by a constant for fixed segment lengths.
  348. const float z_sxy0 = z_xmy0 * diff.x, // per-segment adjustment to z_cxy0
  349. z_sxym = (z_xmy1 - z_xmy0) * RECIPROCAL(MESH_Y_DIST) * diff.x; // per-segment adjustment to z_cxym
  350. for (;;) { // for all segments within this mesh cell
  351. if (--segments == 0) raw = destination; // if this is last segment, use destination for exact
  352. const float z_cxcy = (z_cxy0 + z_cxym * cell.y) // interpolated mesh z height along cell.x at cell.y
  353. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  354. * fade_scaling_factor // apply fade factor to interpolated mesh height
  355. #endif
  356. ;
  357. planner.buffer_line(raw.x, raw.y, raw.z + z_cxcy, raw.e, scaled_fr_mm_s, active_extruder, segment_xyz_mm
  358. #if ENABLED(SCARA_FEEDRATE_SCALING)
  359. , inv_duration
  360. #endif
  361. );
  362. if (segments == 0) // done with last segment
  363. return false; // didn't set current from destination
  364. raw += diff;
  365. cell += diff;
  366. if (!WITHIN(cell.x, 0, MESH_X_DIST) || !WITHIN(cell.y, 0, MESH_Y_DIST)) // done within this cell, break to next
  367. break;
  368. // Next segment still within same mesh cell, adjust the per-segment
  369. // slope and intercept to compute next z height.
  370. z_cxy0 += z_sxy0; // adjust z_cxy0 by per-segment z_sxy0
  371. z_cxym += z_sxym; // adjust z_cxym by per-segment z_sxym
  372. } // segment loop
  373. } // cell loop
  374. return false; // caller will update current_position
  375. }
  376. #endif // UBL_SEGMENTED
  377. #endif // AUTO_BED_LEVELING_UBL