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

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