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_line_to_destination.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "MarlinConfig.h"
  23. #if ENABLED(AUTO_BED_LEVELING_UBL)
  24. #include "Marlin.h"
  25. #include "UBL.h"
  26. #include "planner.h"
  27. #include <avr/io.h>
  28. #include <math.h>
  29. extern float destination[XYZE];
  30. extern void set_current_to_destination();
  31. extern float destination[];
  32. void debug_current_and_destination(char *title) {
  33. // if the title message starts with a '!' it is so important, we are going to
  34. // ignore the status of the g26_debug_flag
  35. if (*title != '!' && !ubl.g26_debug_flag) return;
  36. const float de = destination[E_AXIS] - current_position[E_AXIS];
  37. if (de == 0.0) return;
  38. const float dx = current_position[X_AXIS] - destination[X_AXIS],
  39. dy = current_position[Y_AXIS] - destination[Y_AXIS],
  40. xy_dist = HYPOT(dx, dy);
  41. if (xy_dist == 0.0) {
  42. return;
  43. //SERIAL_ECHOPGM(" FPMM=");
  44. //const float fpmm = de / xy_dist;
  45. //SERIAL_PROTOCOL_F(fpmm, 6);
  46. }
  47. else {
  48. SERIAL_ECHOPGM(" fpmm=");
  49. const float fpmm = de / xy_dist;
  50. SERIAL_ECHO_F(fpmm, 6);
  51. }
  52. SERIAL_ECHOPGM(" current=( ");
  53. SERIAL_ECHO_F(current_position[X_AXIS], 6);
  54. SERIAL_ECHOPGM(", ");
  55. SERIAL_ECHO_F(current_position[Y_AXIS], 6);
  56. SERIAL_ECHOPGM(", ");
  57. SERIAL_ECHO_F(current_position[Z_AXIS], 6);
  58. SERIAL_ECHOPGM(", ");
  59. SERIAL_ECHO_F(current_position[E_AXIS], 6);
  60. SERIAL_ECHOPGM(" ) destination=( ");
  61. if (current_position[X_AXIS] == destination[X_AXIS])
  62. SERIAL_ECHOPGM("-------------");
  63. else
  64. SERIAL_ECHO_F(destination[X_AXIS], 6);
  65. SERIAL_ECHOPGM(", ");
  66. if (current_position[Y_AXIS] == destination[Y_AXIS])
  67. SERIAL_ECHOPGM("-------------");
  68. else
  69. SERIAL_ECHO_F(destination[Y_AXIS], 6);
  70. SERIAL_ECHOPGM(", ");
  71. if (current_position[Z_AXIS] == destination[Z_AXIS])
  72. SERIAL_ECHOPGM("-------------");
  73. else
  74. SERIAL_ECHO_F(destination[Z_AXIS], 6);
  75. SERIAL_ECHOPGM(", ");
  76. if (current_position[E_AXIS] == destination[E_AXIS])
  77. SERIAL_ECHOPGM("-------------");
  78. else
  79. SERIAL_ECHO_F(destination[E_AXIS], 6);
  80. SERIAL_ECHOPGM(" ) ");
  81. SERIAL_ECHO(title);
  82. SERIAL_EOL;
  83. SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66. Right Switch is on pin 65
  84. //if (been_to_2_6) {
  85. //while ((digitalRead(66) & 0x01) != 0)
  86. // idle();
  87. //}
  88. }
  89. void ubl_line_to_destination(const float &x_end, const float &y_end, const float &z_end, const float &e_end, const float &feed_rate, uint8_t extruder) {
  90. /**
  91. * Much of the nozzle movement will be within the same cell. So we will do as little computation
  92. * as possible to determine if this is the case. If this move is within the same cell, we will
  93. * just do the required Z-Height correction, call the Planner's buffer_line() routine, and leave
  94. */
  95. const float x_start = current_position[X_AXIS],
  96. y_start = current_position[Y_AXIS],
  97. z_start = current_position[Z_AXIS],
  98. e_start = current_position[E_AXIS];
  99. const int cell_start_xi = ubl.get_cell_index_x(RAW_X_POSITION(x_start)),
  100. cell_start_yi = ubl.get_cell_index_y(RAW_Y_POSITION(y_start)),
  101. cell_dest_xi = ubl.get_cell_index_x(RAW_X_POSITION(x_end)),
  102. cell_dest_yi = ubl.get_cell_index_y(RAW_Y_POSITION(y_end));
  103. if (ubl.g26_debug_flag) {
  104. SERIAL_ECHOPGM(" ubl_line_to_destination(xe=");
  105. SERIAL_ECHO(x_end);
  106. SERIAL_ECHOPGM(", ye=");
  107. SERIAL_ECHO(y_end);
  108. SERIAL_ECHOPGM(", ze=");
  109. SERIAL_ECHO(z_end);
  110. SERIAL_ECHOPGM(", ee=");
  111. SERIAL_ECHO(e_end);
  112. SERIAL_ECHOLNPGM(")");
  113. debug_current_and_destination((char*)"Start of ubl_line_to_destination()");
  114. }
  115. if (cell_start_xi == cell_dest_xi && cell_start_yi == cell_dest_yi) { // if the whole move is within the same cell,
  116. /**
  117. * we don't need to break up the move
  118. *
  119. * If we are moving off the print bed, we are going to allow the move at this level.
  120. * But we detect it and isolate it. For now, we just pass along the request.
  121. */
  122. if (cell_dest_xi < 0 || cell_dest_yi < 0 || cell_dest_xi >= UBL_MESH_NUM_X_POINTS || cell_dest_yi >= UBL_MESH_NUM_Y_POINTS) {
  123. // Note: There is no Z Correction in this case. We are off the grid and don't know what
  124. // a reasonable correction would be.
  125. planner.buffer_line(x_end, y_end, z_end + ubl.state.z_offset, e_end, feed_rate, extruder);
  126. set_current_to_destination();
  127. if (ubl.g26_debug_flag)
  128. debug_current_and_destination((char*)"out of bounds in ubl_line_to_destination()");
  129. return;
  130. }
  131. FINAL_MOVE:
  132. /**
  133. * Optimize some floating point operations here. We could call float get_z_correction(float x0, float y0) to
  134. * generate the correction for us. But we can lighten the load on the CPU by doing a modified version of the function.
  135. * We are going to only calculate the amount we are from the first mesh line towards the second mesh line once.
  136. * We will use this fraction in both of the original two Z Height calculations for the bi-linear interpolation. And,
  137. * instead of doing a generic divide of the distance, we know the distance is MESH_X_DIST so we can use the preprocessor
  138. * to create a 1-over number for us. That will allow us to do a floating point multiply instead of a floating point divide.
  139. */
  140. const float xratio = (RAW_X_POSITION(x_end) - ubl.mesh_index_to_xpos[cell_dest_xi]) * (1.0 / (MESH_X_DIST)),
  141. z1 = ubl.z_values[cell_dest_xi ][cell_dest_yi ] + xratio *
  142. (ubl.z_values[cell_dest_xi + 1][cell_dest_yi ] - ubl.z_values[cell_dest_xi][cell_dest_yi ]),
  143. z2 = ubl.z_values[cell_dest_xi ][cell_dest_yi + 1] + xratio *
  144. (ubl.z_values[cell_dest_xi + 1][cell_dest_yi + 1] - ubl.z_values[cell_dest_xi][cell_dest_yi + 1]);
  145. // we are done with the fractional X distance into the cell. Now with the two Z-Heights we have calculated, we
  146. // are going to apply the Y-Distance into the cell to interpolate the final Z correction.
  147. const float yratio = (RAW_Y_POSITION(y_end) - ubl.mesh_index_to_ypos[cell_dest_yi]) * (1.0 / (MESH_Y_DIST));
  148. float z0 = z1 + (z2 - z1) * yratio;
  149. /**
  150. * Debug code to use non-optimized get_z_correction() and to do a sanity check
  151. * that the correct value is being passed to planner.buffer_line()
  152. */
  153. /*
  154. z_optimized = z0;
  155. z0 = ubl.get_z_correction(x_end, y_end);
  156. if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
  157. debug_current_and_destination((char*)"FINAL_MOVE: z_correction()");
  158. if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
  159. if (isnan(z_optimized)) SERIAL_ECHO(" z_optimized==NAN ");
  160. SERIAL_ECHOPAIR(" x_end=", x_end);
  161. SERIAL_ECHOPAIR(" y_end=", y_end);
  162. SERIAL_ECHOPAIR(" z0=", z0);
  163. SERIAL_ECHOPAIR(" z_optimized=", z_optimized);
  164. SERIAL_ECHOPAIR(" err=",fabs(z_optimized - z0));
  165. SERIAL_EOL;
  166. }
  167. //*/
  168. z0 *= ubl.fade_scaling_factor_for_z(z_end);
  169. /**
  170. * If part of the Mesh is undefined, it will show up as NAN
  171. * in z_values[][] and propagate through the
  172. * calculations. If our correction is NAN, we throw it out
  173. * because part of the Mesh is undefined and we don't have the
  174. * information we need to complete the height correction.
  175. */
  176. if (isnan(z0)) z0 = 0.0;
  177. planner.buffer_line(x_end, y_end, z_end + z0 + ubl.state.z_offset, e_end, feed_rate, extruder);
  178. if (ubl.g26_debug_flag)
  179. debug_current_and_destination((char*)"FINAL_MOVE in ubl_line_to_destination()");
  180. set_current_to_destination();
  181. return;
  182. }
  183. /**
  184. * If we get here, we are processing a move that crosses at least one Mesh Line. We will check
  185. * for the simple case of just crossing X or just crossing Y Mesh Lines after we get all the details
  186. * of the move figured out. We can process the easy case of just crossing an X or Y Mesh Line with less
  187. * computation and in fact most lines are of this nature. We will check for that in the following
  188. * blocks of code:
  189. */
  190. const float dx = x_end - x_start,
  191. dy = y_end - y_start;
  192. const int left_flag = dx < 0.0 ? 1 : 0,
  193. down_flag = dy < 0.0 ? 1 : 0;
  194. const float adx = left_flag ? -dx : dx,
  195. ady = down_flag ? -dy : dy;
  196. const int dxi = cell_start_xi == cell_dest_xi ? 0 : left_flag ? -1 : 1,
  197. dyi = cell_start_yi == cell_dest_yi ? 0 : down_flag ? -1 : 1;
  198. /**
  199. * Compute the scaling factor for the extruder for each partial move.
  200. * We need to watch out for zero length moves because it will cause us to
  201. * have an infinate scaling factor. We are stuck doing a floating point
  202. * divide to get our scaling factor, but after that, we just multiply by this
  203. * number. We also pick our scaling factor based on whether the X or Y
  204. * component is larger. We use the biggest of the two to preserve precision.
  205. */
  206. const bool use_x_dist = adx > ady;
  207. float on_axis_distance = use_x_dist ? dx : dy,
  208. e_position = e_end - e_start,
  209. z_position = z_end - z_start;
  210. const float e_normalized_dist = e_position / on_axis_distance,
  211. z_normalized_dist = z_position / on_axis_distance;
  212. int current_xi = cell_start_xi, current_yi = cell_start_yi;
  213. const float m = dy / dx,
  214. c = y_start - m * x_start;
  215. const bool inf_normalized_flag = NEAR_ZERO(on_axis_distance),
  216. inf_m_flag = NEAR_ZERO(dx);
  217. /**
  218. * This block handles vertical lines. These are lines that stay within the same
  219. * X Cell column. They do not need to be perfectly vertical. They just can
  220. * not cross into another X Cell column.
  221. */
  222. if (dxi == 0) { // Check for a vertical line
  223. current_yi += down_flag; // Line is heading down, we just want to go to the bottom
  224. while (current_yi != cell_dest_yi + down_flag) {
  225. current_yi += dyi;
  226. const float next_mesh_line_y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi]);
  227. /**
  228. * inf_m_flag? the slope of the line is infinite, we won't do the calculations
  229. * else, we know the next X is the same so we can recover and continue!
  230. * Calculate X at the next Y mesh line
  231. */
  232. const float x = inf_m_flag ? x_start : (next_mesh_line_y - c) / m;
  233. float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
  234. /**
  235. * Debug code to use non-optimized get_z_correction() and to do a sanity check
  236. * that the correct value is being passed to planner.buffer_line()
  237. */
  238. /*
  239. z_optimized = z0;
  240. z0 = ubl.get_z_correction(x, next_mesh_line_y);
  241. if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
  242. debug_current_and_destination((char*)"VERTICAL z_correction()");
  243. if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
  244. if (isnan(z_optimized)) SERIAL_ECHO(" z_optimized==NAN ");
  245. SERIAL_ECHOPAIR(" x=", x);
  246. SERIAL_ECHOPAIR(" next_mesh_line_y=", next_mesh_line_y);
  247. SERIAL_ECHOPAIR(" z0=", z0);
  248. SERIAL_ECHOPAIR(" z_optimized=", z_optimized);
  249. SERIAL_ECHOPAIR(" err=",fabs(z_optimized-z0));
  250. SERIAL_ECHO("\n");
  251. }
  252. //*/
  253. z0 *= ubl.fade_scaling_factor_for_z(z_end);
  254. /**
  255. * If part of the Mesh is undefined, it will show up as NAN
  256. * in z_values[][] and propagate through the
  257. * calculations. If our correction is NAN, we throw it out
  258. * because part of the Mesh is undefined and we don't have the
  259. * information we need to complete the height correction.
  260. */
  261. if (isnan(z0)) z0 = 0.0;
  262. const float y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi]);
  263. /**
  264. * Without this check, it is possible for the algorithm to generate a zero length move in the case
  265. * where the line is heading down and it is starting right on a Mesh Line boundary. For how often that
  266. * happens, it might be best to remove the check and always 'schedule' the move because
  267. * the planner.buffer_line() routine will filter it if that happens.
  268. */
  269. if (y != y_start) {
  270. if (!inf_normalized_flag) {
  271. on_axis_distance = y - y_start; // we don't need to check if the extruder position
  272. e_position = e_start + on_axis_distance * e_normalized_dist; // is based on X or Y because this is a vertical move
  273. z_position = z_start + on_axis_distance * z_normalized_dist;
  274. }
  275. else {
  276. e_position = e_start;
  277. z_position = z_start;
  278. }
  279. planner.buffer_line(x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
  280. } //else printf("FIRST MOVE PRUNED ");
  281. }
  282. if (ubl.g26_debug_flag)
  283. debug_current_and_destination((char*)"vertical move done in ubl_line_to_destination()");
  284. //
  285. // Check if we are at the final destination. Usually, we won't be, but if it is on a Y Mesh Line, we are done.
  286. //
  287. if (current_position[X_AXIS] != x_end || current_position[Y_AXIS] != y_end)
  288. goto FINAL_MOVE;
  289. set_current_to_destination();
  290. return;
  291. }
  292. /**
  293. *
  294. * This block handles horizontal lines. These are lines that stay within the same
  295. * Y Cell row. They do not need to be perfectly horizontal. They just can
  296. * not cross into another Y Cell row.
  297. *
  298. */
  299. if (dyi == 0) { // Check for a horizontal line
  300. current_xi += left_flag; // Line is heading left, we just want to go to the left
  301. // edge of this cell for the first move.
  302. while (current_xi != cell_dest_xi + left_flag) {
  303. current_xi += dxi;
  304. const float next_mesh_line_x = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi]),
  305. y = m * next_mesh_line_x + c; // Calculate X at the next Y mesh line
  306. float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
  307. /**
  308. * Debug code to use non-optimized get_z_correction() and to do a sanity check
  309. * that the correct value is being passed to planner.buffer_line()
  310. */
  311. /*
  312. z_optimized = z0;
  313. z0 = ubl.get_z_correction(next_mesh_line_x, y);
  314. if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
  315. debug_current_and_destination((char*)"HORIZONTAL z_correction()");
  316. if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
  317. if (isnan(z_optimized)) SERIAL_ECHO(" z_optimized==NAN ");
  318. SERIAL_ECHOPAIR(" next_mesh_line_x=", next_mesh_line_x);
  319. SERIAL_ECHOPAIR(" y=", y);
  320. SERIAL_ECHOPAIR(" z0=", z0);
  321. SERIAL_ECHOPAIR(" z_optimized=", z_optimized);
  322. SERIAL_ECHOPAIR(" err=",fabs(z_optimized-z0));
  323. SERIAL_ECHO("\n");
  324. }
  325. //*/
  326. z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
  327. /**
  328. * If part of the Mesh is undefined, it will show up as NAN
  329. * in z_values[][] and propagate through the
  330. * calculations. If our correction is NAN, we throw it out
  331. * because part of the Mesh is undefined and we don't have the
  332. * information we need to complete the height correction.
  333. */
  334. if (isnan(z0)) z0 = 0.0;
  335. const float x = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi]);
  336. /**
  337. * Without this check, it is possible for the algorithm to generate a zero length move in the case
  338. * where the line is heading left and it is starting right on a Mesh Line boundary. For how often
  339. * that happens, it might be best to remove the check and always 'schedule' the move because
  340. * the planner.buffer_line() routine will filter it if that happens.
  341. */
  342. if (x != x_start) {
  343. if (!inf_normalized_flag) {
  344. on_axis_distance = x - x_start; // we don't need to check if the extruder position
  345. e_position = e_start + on_axis_distance * e_normalized_dist; // is based on X or Y because this is a horizontal move
  346. z_position = z_start + on_axis_distance * z_normalized_dist;
  347. }
  348. else {
  349. e_position = e_start;
  350. z_position = z_start;
  351. }
  352. planner.buffer_line(x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
  353. } //else printf("FIRST MOVE PRUNED ");
  354. }
  355. if (ubl.g26_debug_flag)
  356. debug_current_and_destination((char*)"horizontal move done in ubl_line_to_destination()");
  357. if (current_position[X_AXIS] != x_end || current_position[Y_AXIS] != y_end)
  358. goto FINAL_MOVE;
  359. set_current_to_destination();
  360. return;
  361. }
  362. /**
  363. *
  364. * This block handles the generic case of a line crossing both X and Y Mesh lines.
  365. *
  366. */
  367. int xi_cnt = cell_start_xi - cell_dest_xi,
  368. yi_cnt = cell_start_yi - cell_dest_yi;
  369. if (xi_cnt < 0) xi_cnt = -xi_cnt;
  370. if (yi_cnt < 0) yi_cnt = -yi_cnt;
  371. current_xi += left_flag;
  372. current_yi += down_flag;
  373. while (xi_cnt > 0 || yi_cnt > 0) {
  374. const float next_mesh_line_x = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi + dxi]),
  375. next_mesh_line_y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi + dyi]),
  376. y = m * next_mesh_line_x + c, // Calculate Y at the next X mesh line
  377. x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line (we don't have to worry
  378. // about m being equal to 0.0 If this was the case, we would have
  379. // detected this as a vertical line move up above and we wouldn't
  380. // be down here doing a generic type of move.
  381. if (left_flag == (x > next_mesh_line_x)) { // Check if we hit the Y line first
  382. //
  383. // Yes! Crossing a Y Mesh Line next
  384. //
  385. float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi - left_flag, current_yi + dyi);
  386. /**
  387. * Debug code to use non-optimized get_z_correction() and to do a sanity check
  388. * that the correct value is being passed to planner.buffer_line()
  389. */
  390. /*
  391. z_optimized = z0;
  392. z0 = ubl.get_z_correction(x, next_mesh_line_y);
  393. if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
  394. debug_current_and_destination((char*)"General_1: z_correction()");
  395. if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
  396. if (isnan(z_optimized)) SERIAL_ECHO(" z_optimized==NAN "); {
  397. SERIAL_ECHOPAIR(" x=", x);
  398. }
  399. SERIAL_ECHOPAIR(" next_mesh_line_y=", next_mesh_line_y);
  400. SERIAL_ECHOPAIR(" z0=", z0);
  401. SERIAL_ECHOPAIR(" z_optimized=", z_optimized);
  402. SERIAL_ECHOPAIR(" err=",fabs(z_optimized-z0));
  403. SERIAL_ECHO("\n");
  404. }
  405. //*/
  406. z0 *= ubl.fade_scaling_factor_for_z(z_end);
  407. /**
  408. * If part of the Mesh is undefined, it will show up as NAN
  409. * in z_values[][] and propagate through the
  410. * calculations. If our correction is NAN, we throw it out
  411. * because part of the Mesh is undefined and we don't have the
  412. * information we need to complete the height correction.
  413. */
  414. if (isnan(z0)) z0 = 0.0;
  415. if (!inf_normalized_flag) {
  416. on_axis_distance = use_x_dist ? x - x_start : next_mesh_line_y - y_start;
  417. e_position = e_start + on_axis_distance * e_normalized_dist;
  418. z_position = z_start + on_axis_distance * z_normalized_dist;
  419. }
  420. else {
  421. e_position = e_start;
  422. z_position = z_start;
  423. }
  424. planner.buffer_line(x, next_mesh_line_y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
  425. current_yi += dyi;
  426. yi_cnt--;
  427. }
  428. else {
  429. //
  430. // Yes! Crossing a X Mesh Line next
  431. //
  432. float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi + dxi, current_yi - down_flag);
  433. /**
  434. * Debug code to use non-optimized get_z_correction() and to do a sanity check
  435. * that the correct value is being passed to planner.buffer_line()
  436. */
  437. /*
  438. z_optimized = z0;
  439. z0 = ubl.get_z_correction(next_mesh_line_x, y);
  440. if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
  441. debug_current_and_destination((char*)"General_2: z_correction()");
  442. if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
  443. if (isnan(z_optimized)) SERIAL_ECHO(" z_optimized==NAN ");
  444. SERIAL_ECHOPAIR(" next_mesh_line_x=", next_mesh_line_x);
  445. SERIAL_ECHOPAIR(" y=", y);
  446. SERIAL_ECHOPAIR(" z0=", z0);
  447. SERIAL_ECHOPAIR(" z_optimized=", z_optimized);
  448. SERIAL_ECHOPAIR(" err=",fabs(z_optimized-z0));
  449. SERIAL_ECHO("\n");
  450. }
  451. //*/
  452. z0 *= ubl.fade_scaling_factor_for_z(z_end);
  453. /**
  454. * If part of the Mesh is undefined, it will show up as NAN
  455. * in z_values[][] and propagate through the
  456. * calculations. If our correction is NAN, we throw it out
  457. * because part of the Mesh is undefined and we don't have the
  458. * information we need to complete the height correction.
  459. */
  460. if (isnan(z0)) z0 = 0.0;
  461. if (!inf_normalized_flag) {
  462. on_axis_distance = use_x_dist ? next_mesh_line_x - x_start : y - y_start;
  463. e_position = e_start + on_axis_distance * e_normalized_dist;
  464. z_position = z_start + on_axis_distance * z_normalized_dist;
  465. }
  466. else {
  467. e_position = e_start;
  468. z_position = z_start;
  469. }
  470. planner.buffer_line(next_mesh_line_x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
  471. current_xi += dxi;
  472. xi_cnt--;
  473. }
  474. }
  475. if (ubl.g26_debug_flag)
  476. debug_current_and_destination((char*)"generic move done in ubl_line_to_destination()");
  477. if (current_position[0] != x_end || current_position[1] != y_end)
  478. goto FINAL_MOVE;
  479. set_current_to_destination();
  480. }
  481. #endif