My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UBL_line_to_destination.cpp 23KB

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