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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. const float z_normalized_dist = (end.z - start.z) / on_axis_distance; // Allow divide by zero
  98. #if HAS_EXTRUDERS
  99. const float e_normalized_dist = (end.e - start.e) / on_axis_distance;
  100. const bool inf_normalized_flag = isinf(e_normalized_dist);
  101. #endif
  102. xy_int8_t icell = istart;
  103. const float ratio = dist.y / dist.x, // Allow divide by zero
  104. c = start.y - ratio * start.x;
  105. const bool inf_ratio_flag = isinf(ratio);
  106. xyze_pos_t dest; // Stores XYZE for segmented moves
  107. /**
  108. * Handle vertical lines that stay within one column.
  109. * These need not be perfectly vertical.
  110. */
  111. if (iadd.x == 0) { // Vertical line?
  112. icell.y += ineg.y; // Line going down? Just go to the bottom.
  113. while (icell.y != iend.y + ineg.y) {
  114. icell.y += iadd.y;
  115. const float next_mesh_line_y = mesh_index_to_ypos(icell.y);
  116. /**
  117. * Skip the calculations for an infinite slope.
  118. * For others the next X is the same so this can continue.
  119. * Calculate X at the next Y mesh line.
  120. */
  121. dest.x = inf_ratio_flag ? start.x : (next_mesh_line_y - c) / ratio;
  122. float z0 = z_correction_for_x_on_horizontal_mesh_line(dest.x, icell.x, icell.y)
  123. * planner.fade_scaling_factor_for_z(end.z);
  124. // Undefined parts of the Mesh in z_values[][] are NAN.
  125. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  126. if (isnan(z0)) z0 = 0.0;
  127. dest.y = mesh_index_to_ypos(icell.y);
  128. /**
  129. * Without this check, it's possible to generate a zero length move, as in the case where
  130. * the line is heading down, starting exactly on a mesh line boundary. Since this is rare
  131. * it might be fine to remove this check and let planner.buffer_segment() filter it out.
  132. */
  133. if (dest.y != start.y) {
  134. if (!inf_normalized_flag) { // fall-through faster than branch
  135. on_axis_distance = use_x_dist ? dest.x - start.x : dest.y - start.y;
  136. TERN_(HAS_EXTRUDERS, dest.e = start.e + on_axis_distance * e_normalized_dist);
  137. dest.z = start.z + on_axis_distance * z_normalized_dist;
  138. }
  139. else {
  140. TERN_(HAS_EXTRUDERS, dest.e = end.e);
  141. dest.z = end.z;
  142. }
  143. dest.z += z0;
  144. planner.buffer_segment(dest, scaled_fr_mm_s, extruder);
  145. } //else printf("FIRST MOVE PRUNED ");
  146. }
  147. // At the final destination? Usually not, but when on a Y Mesh Line it's completed.
  148. if (xy_pos_t(current_position) != xy_pos_t(end))
  149. goto FINAL_MOVE;
  150. current_position = destination;
  151. return;
  152. }
  153. /**
  154. * Handle horizontal lines that stay within one row.
  155. * These need not be perfectly horizontal.
  156. */
  157. if (iadd.y == 0) { // Horizontal line?
  158. icell.x += ineg.x; // Heading left? Just go to the left edge of the cell for the first move.
  159. while (icell.x != iend.x + ineg.x) {
  160. icell.x += iadd.x;
  161. dest.x = mesh_index_to_xpos(icell.x);
  162. dest.y = ratio * dest.x + c; // Calculate Y at the next X mesh line
  163. float z0 = z_correction_for_y_on_vertical_mesh_line(dest.y, icell.x, icell.y)
  164. * planner.fade_scaling_factor_for_z(end.z);
  165. // Undefined parts of the Mesh in z_values[][] are NAN.
  166. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  167. if (isnan(z0)) z0 = 0.0;
  168. /**
  169. * Without this check, it's possible to generate a zero length move, as in the case where
  170. * the line is heading left, starting exactly on a mesh line boundary. Since this is rare
  171. * it might be fine to remove this check and let planner.buffer_segment() filter it out.
  172. */
  173. if (dest.x != start.x) {
  174. if (!inf_normalized_flag) {
  175. on_axis_distance = use_x_dist ? dest.x - start.x : dest.y - start.y;
  176. TERN_(HAS_EXTRUDERS, dest.e = start.e + on_axis_distance * e_normalized_dist); // Based on X or Y because the move is horizontal
  177. dest.z = start.z + on_axis_distance * z_normalized_dist;
  178. }
  179. else {
  180. TERN_(HAS_EXTRUDERS, dest.e = end.e);
  181. dest.z = end.z;
  182. }
  183. dest.z += z0;
  184. if (!planner.buffer_segment(dest, scaled_fr_mm_s, extruder)) break;
  185. } //else printf("FIRST MOVE PRUNED ");
  186. }
  187. if (xy_pos_t(current_position) != xy_pos_t(end))
  188. goto FINAL_MOVE;
  189. current_position = destination;
  190. return;
  191. }
  192. /**
  193. * Generic case of a line crossing both X and Y Mesh lines.
  194. */
  195. xy_int8_t cnt = (istart - iend).ABS();
  196. icell += ineg;
  197. while (cnt) {
  198. const float next_mesh_line_x = mesh_index_to_xpos(icell.x + iadd.x),
  199. next_mesh_line_y = mesh_index_to_ypos(icell.y + iadd.y);
  200. dest.y = ratio * next_mesh_line_x + c; // Calculate Y at the next X mesh line
  201. dest.x = (next_mesh_line_y - c) / ratio; // Calculate X at the next Y mesh line
  202. // (No need to worry about ratio == 0.
  203. // In that case, it was already detected
  204. // as a vertical line move above.)
  205. if (neg.x == (dest.x > next_mesh_line_x)) { // Check if we hit the Y line first
  206. // Yes! Crossing a Y Mesh Line next
  207. float z0 = z_correction_for_x_on_horizontal_mesh_line(dest.x, icell.x - ineg.x, icell.y + iadd.y)
  208. * planner.fade_scaling_factor_for_z(end.z);
  209. // Undefined parts of the Mesh in z_values[][] are NAN.
  210. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  211. if (isnan(z0)) z0 = 0.0;
  212. dest.y = next_mesh_line_y;
  213. if (!inf_normalized_flag) {
  214. on_axis_distance = use_x_dist ? dest.x - start.x : dest.y - start.y;
  215. TERN_(HAS_EXTRUDERS, dest.e = start.e + on_axis_distance * e_normalized_dist);
  216. dest.z = start.z + on_axis_distance * z_normalized_dist;
  217. }
  218. else {
  219. TERN_(HAS_EXTRUDERS, dest.e = end.e);
  220. dest.z = end.z;
  221. }
  222. dest.z += z0;
  223. if (!planner.buffer_segment(dest, scaled_fr_mm_s, extruder)) break;
  224. icell.y += iadd.y;
  225. cnt.y--;
  226. }
  227. else {
  228. // Yes! Crossing a X Mesh Line next
  229. float z0 = z_correction_for_y_on_vertical_mesh_line(dest.y, icell.x + iadd.x, icell.y - ineg.y)
  230. * planner.fade_scaling_factor_for_z(end.z);
  231. // Undefined parts of the Mesh in z_values[][] are NAN.
  232. // Replace NAN corrections with 0.0 to prevent NAN propagation.
  233. if (isnan(z0)) z0 = 0.0;
  234. dest.x = next_mesh_line_x;
  235. if (!inf_normalized_flag) {
  236. on_axis_distance = use_x_dist ? dest.x - start.x : dest.y - start.y;
  237. TERN_(HAS_EXTRUDERS, dest.e = start.e + on_axis_distance * e_normalized_dist);
  238. dest.z = start.z + on_axis_distance * z_normalized_dist;
  239. }
  240. else {
  241. TERN_(HAS_EXTRUDERS, dest.e = end.e);
  242. dest.z = end.z;
  243. }
  244. dest.z += z0;
  245. if (!planner.buffer_segment(dest, scaled_fr_mm_s, extruder)) break;
  246. icell.x += iadd.x;
  247. cnt.x--;
  248. }
  249. if (cnt.x < 0 || cnt.y < 0) break; // Too far! Exit the loop and go to FINAL_MOVE
  250. }
  251. if (xy_pos_t(current_position) != xy_pos_t(end))
  252. goto FINAL_MOVE;
  253. current_position = destination;
  254. }
  255. #else // UBL_SEGMENTED
  256. #if IS_SCARA
  257. #define DELTA_SEGMENT_MIN_LENGTH 0.25 // SCARA minimum segment size is 0.25mm
  258. #elif ENABLED(DELTA)
  259. #define DELTA_SEGMENT_MIN_LENGTH 0.10 // mm (still subject to DELTA_SEGMENTS_PER_SECOND)
  260. #else // CARTESIAN
  261. #ifdef LEVELED_SEGMENT_LENGTH
  262. #define DELTA_SEGMENT_MIN_LENGTH LEVELED_SEGMENT_LENGTH
  263. #else
  264. #define DELTA_SEGMENT_MIN_LENGTH 1.00 // mm (similar to G2/G3 arc segmentation)
  265. #endif
  266. #endif
  267. /**
  268. * Prepare a segmented linear move for DELTA/SCARA/CARTESIAN with UBL and FADE semantics.
  269. * This calls planner.buffer_segment multiple times for small incremental moves.
  270. * Returns true if did NOT move, false if moved (requires current_position update).
  271. */
  272. bool _O2 unified_bed_leveling::line_to_destination_segmented(const_feedRate_t scaled_fr_mm_s) {
  273. if (!position_is_reachable(destination)) // fail if moving outside reachable boundary
  274. return true; // did not move, so current_position still accurate
  275. const xyze_pos_t total = destination - current_position;
  276. const float cart_xy_mm_2 = HYPOT2(total.x, total.y),
  277. cart_xy_mm = SQRT(cart_xy_mm_2); // Total XY distance
  278. #if IS_KINEMATIC
  279. const float seconds = cart_xy_mm / scaled_fr_mm_s; // Duration of XY move at requested rate
  280. uint16_t segments = LROUND(segments_per_second * seconds), // Preferred number of segments for distance @ feedrate
  281. seglimit = LROUND(cart_xy_mm * RECIPROCAL(DELTA_SEGMENT_MIN_LENGTH)); // Number of segments at minimum segment length
  282. NOMORE(segments, seglimit); // Limit to minimum segment length (fewer segments)
  283. #else
  284. uint16_t segments = LROUND(cart_xy_mm * RECIPROCAL(DELTA_SEGMENT_MIN_LENGTH)); // Cartesian fixed segment length
  285. #endif
  286. NOLESS(segments, 1U); // Must have at least one segment
  287. const float inv_segments = 1.0f / segments, // Reciprocal to save calculation
  288. segment_xyz_mm = SQRT(cart_xy_mm_2 + sq(total.z)) * inv_segments; // Length of each segment
  289. #if ENABLED(SCARA_FEEDRATE_SCALING)
  290. const float inv_duration = scaled_fr_mm_s / segment_xyz_mm;
  291. #endif
  292. xyze_float_t diff = total * inv_segments;
  293. // Note that E segment distance could vary slightly as z mesh height
  294. // changes for each segment, but small enough to ignore.
  295. xyze_pos_t raw = current_position;
  296. // Just do plain segmentation if UBL is inactive or the target is above the fade height
  297. if (!planner.leveling_active || !planner.leveling_active_at_z(destination.z)) {
  298. while (--segments) {
  299. raw += diff;
  300. planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, segment_xyz_mm
  301. OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
  302. );
  303. }
  304. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, segment_xyz_mm
  305. OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
  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. const float oldz = raw.z; raw.z += z_cxcy;
  358. planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, segment_xyz_mm OPTARG(SCARA_FEEDRATE_SCALING, inv_duration) );
  359. raw.z = oldz;
  360. if (segments == 0) // done with last segment
  361. return false; // didn't set current from destination
  362. raw += diff;
  363. cell += diff;
  364. if (!WITHIN(cell.x, 0, MESH_X_DIST) || !WITHIN(cell.y, 0, MESH_Y_DIST)) // done within this cell, break to next
  365. break;
  366. // Next segment still within same mesh cell, adjust the per-segment
  367. // slope and intercept to compute next z height.
  368. z_cxy0 += z_sxy0; // adjust z_cxy0 by per-segment z_sxy0
  369. z_cxym += z_sxym; // adjust z_cxym by per-segment z_sxym
  370. } // segment loop
  371. } // cell loop
  372. return false; // caller will update current_position
  373. }
  374. #endif // UBL_SEGMENTED
  375. #endif // AUTO_BED_LEVELING_UBL