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.

G26_Mesh_Validation_Tool.cpp 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. /**
  23. * Marlin Firmware -- G26 - Mesh Validation Tool
  24. */
  25. #include "MarlinConfig.h"
  26. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_VALIDATION)
  27. #include "ubl.h"
  28. #include "Marlin.h"
  29. #include "planner.h"
  30. #include "stepper.h"
  31. #include "temperature.h"
  32. #include "ultralcd.h"
  33. #include "gcode.h"
  34. #define EXTRUSION_MULTIPLIER 1.0
  35. #define RETRACTION_MULTIPLIER 1.0
  36. #define PRIME_LENGTH 10.0
  37. #define OOZE_AMOUNT 0.3
  38. #define SIZE_OF_INTERSECTION_CIRCLES 5
  39. #define SIZE_OF_CROSSHAIRS 3
  40. #if SIZE_OF_CROSSHAIRS >= SIZE_OF_INTERSECTION_CIRCLES
  41. #error "SIZE_OF_CROSSHAIRS must be less than SIZE_OF_INTERSECTION_CIRCLES."
  42. #endif
  43. /**
  44. * G26 Mesh Validation Tool
  45. *
  46. * G26 is a Mesh Validation Tool intended to provide support for the Marlin Unified Bed Leveling System.
  47. * In order to fully utilize and benefit from the Marlin Unified Bed Leveling System an accurate Mesh must
  48. * be defined. G29 is designed to allow the user to quickly validate the correctness of her Mesh. It will
  49. * first heat the bed and nozzle. It will then print lines and circles along the Mesh Cell boundaries and
  50. * the intersections of those lines (respectively).
  51. *
  52. * This action allows the user to immediately see where the Mesh is properly defined and where it needs to
  53. * be edited. The command will generate the Mesh lines closest to the nozzle's starting position. Alternatively
  54. * the user can specify the X and Y position of interest with command parameters. This allows the user to
  55. * focus on a particular area of the Mesh where attention is needed.
  56. *
  57. * B # Bed Set the Bed Temperature. If not specified, a default of 60 C. will be assumed.
  58. *
  59. * C Current When searching for Mesh Intersection points to draw, use the current nozzle location
  60. * as the base for any distance comparison.
  61. *
  62. * D Disable Disable the Unified Bed Leveling System. In the normal case the user is invoking this
  63. * command to see how well a Mesh as been adjusted to match a print surface. In order to do
  64. * this the Unified Bed Leveling System is turned on by the G26 command. The D parameter
  65. * alters the command's normal behaviour and disables the Unified Bed Leveling System even if
  66. * it is on.
  67. *
  68. * H # Hotend Set the Nozzle Temperature. If not specified, a default of 205 C. will be assumed.
  69. *
  70. * F # Filament Used to specify the diameter of the filament being used. If not specified
  71. * 1.75mm filament is assumed. If you are not getting acceptable results by using the
  72. * 'correct' numbers, you can scale this number up or down a little bit to change the amount
  73. * of filament that is being extruded during the printing of the various lines on the bed.
  74. *
  75. * K Keep-On Keep the heaters turned on at the end of the command.
  76. *
  77. * L # Layer Layer height. (Height of nozzle above bed) If not specified .20mm will be used.
  78. *
  79. * O # Ooooze How much your nozzle will Ooooze filament while getting in position to print. This
  80. * is over kill, but using this parameter will let you get the very first 'circle' perfect
  81. * so you have a trophy to peel off of the bed and hang up to show how perfectly you have your
  82. * Mesh calibrated. If not specified, a filament length of .3mm is assumed.
  83. *
  84. * P # Prime Prime the nozzle with specified length of filament. If this parameter is not
  85. * given, no prime action will take place. If the parameter specifies an amount, that much
  86. * will be purged before continuing. If no amount is specified the command will start
  87. * purging filament until the user provides an LCD Click and then it will continue with
  88. * printing the Mesh. You can carefully remove the spent filament with a needle nose
  89. * pliers while holding the LCD Click wheel in a depressed state. If you do not have
  90. * an LCD, you must specify a value if you use P.
  91. *
  92. * Q # Multiplier Retraction Multiplier. Normally not needed. Retraction defaults to 1.0mm and
  93. * un-retraction is at 1.2mm These numbers will be scaled by the specified amount
  94. *
  95. * R # Repeat Prints the number of patterns given as a parameter, starting at the current location.
  96. * If a parameter isn't given, every point will be printed unless G26 is interrupted.
  97. * This works the same way that the UBL G29 P4 R parameter works.
  98. *
  99. * NOTE: If you do not have an LCD, you -must- specify R. This is to ensure that you are
  100. * aware that there's some risk associated with printing without the ability to abort in
  101. * cases where mesh point Z value may be inaccurate. As above, if you do not include a
  102. * parameter, every point will be printed.
  103. *
  104. * S # Nozzle Used to control the size of nozzle diameter. If not specified, a .4mm nozzle is assumed.
  105. *
  106. * U # Random Randomize the order that the circles are drawn on the bed. The search for the closest
  107. * undrawn cicle is still done. But the distance to the location for each circle has a
  108. * random number of the size specified added to it. Specifying S50 will give an interesting
  109. * deviation from the normal behaviour on a 10 x 10 Mesh.
  110. *
  111. * X # X Coord. Specify the starting location of the drawing activity.
  112. *
  113. * Y # Y Coord. Specify the starting location of the drawing activity.
  114. */
  115. // External references
  116. extern float feedrate_mm_s; // must set before calling prepare_move_to_destination
  117. extern Planner planner;
  118. #if ENABLED(ULTRA_LCD)
  119. extern char lcd_status_message[];
  120. #endif
  121. extern float destination[XYZE];
  122. void set_destination_from_current();
  123. void prepare_move_to_destination();
  124. inline void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); }
  125. inline void set_current_from_destination() { COPY(current_position, destination); }
  126. #if ENABLED(NEWPANEL)
  127. void lcd_setstatusPGM(const char* const message, const int8_t level);
  128. void chirp_at_user();
  129. #endif
  130. // Private functions
  131. static uint16_t circle_flags[16], horizontal_mesh_line_flags[16], vertical_mesh_line_flags[16];
  132. float g26_e_axis_feedrate = 0.020,
  133. random_deviation = 0.0;
  134. static bool g26_retracted = false; // Track the retracted state of the nozzle so mismatched
  135. // retracts/recovers won't result in a bad state.
  136. float valid_trig_angle(float);
  137. float unified_bed_leveling::g26_extrusion_multiplier,
  138. unified_bed_leveling::g26_retraction_multiplier,
  139. unified_bed_leveling::g26_nozzle,
  140. unified_bed_leveling::g26_filament_diameter,
  141. unified_bed_leveling::g26_layer_height,
  142. unified_bed_leveling::g26_prime_length,
  143. unified_bed_leveling::g26_x_pos,
  144. unified_bed_leveling::g26_y_pos,
  145. unified_bed_leveling::g26_ooze_amount;
  146. int16_t unified_bed_leveling::g26_bed_temp,
  147. unified_bed_leveling::g26_hotend_temp;
  148. int8_t unified_bed_leveling::g26_prime_flag;
  149. bool unified_bed_leveling::g26_continue_with_closest,
  150. unified_bed_leveling::g26_keep_heaters_on;
  151. int16_t unified_bed_leveling::g26_repeats;
  152. void unified_bed_leveling::G26_line_to_destination(const float &feed_rate) {
  153. const float save_feedrate = feedrate_mm_s;
  154. feedrate_mm_s = feed_rate; // use specified feed rate
  155. prepare_move_to_destination(); // will ultimately call ubl.line_to_destination_cartesian or ubl.prepare_linear_move_to for UBL_DELTA
  156. feedrate_mm_s = save_feedrate; // restore global feed rate
  157. }
  158. #if ENABLED(NEWPANEL)
  159. /**
  160. * Detect ubl_lcd_clicked, debounce it, and return true for cancel
  161. */
  162. bool user_canceled() {
  163. if (!ubl_lcd_clicked()) return false;
  164. safe_delay(10); // Wait for click to settle
  165. #if ENABLED(ULTRA_LCD)
  166. lcd_setstatusPGM(PSTR("Mesh Validation Stopped."), 99);
  167. lcd_quick_feedback();
  168. #endif
  169. while (!ubl_lcd_clicked()) idle(); // Wait for button release
  170. // If the button is suddenly pressed again,
  171. // ask the user to resolve the issue
  172. lcd_setstatusPGM(PSTR("Release button"), 99); // will never appear...
  173. while (ubl_lcd_clicked()) idle(); // unless this loop happens
  174. lcd_reset_status();
  175. return true;
  176. }
  177. #endif
  178. /**
  179. * G26: Mesh Validation Pattern generation.
  180. *
  181. * Used to interactively edit UBL's Mesh by placing the
  182. * nozzle in a problem area and doing a G29 P4 R command.
  183. */
  184. void unified_bed_leveling::G26() {
  185. SERIAL_ECHOLNPGM("G26 command started. Waiting for heater(s).");
  186. float tmp, start_angle, end_angle;
  187. int i, xi, yi;
  188. mesh_index_pair location;
  189. // Don't allow Mesh Validation without homing first,
  190. // or if the parameter parsing did not go OK, abort
  191. if (axis_unhomed_error() || parse_G26_parameters()) return;
  192. if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
  193. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  194. stepper.synchronize();
  195. set_current_from_destination();
  196. }
  197. if (turn_on_heaters()) goto LEAVE;
  198. current_position[E_AXIS] = 0.0;
  199. sync_plan_position_e();
  200. if (g26_prime_flag && prime_nozzle()) goto LEAVE;
  201. /**
  202. * Bed is preheated
  203. *
  204. * Nozzle is at temperature
  205. *
  206. * Filament is primed!
  207. *
  208. * It's "Show Time" !!!
  209. */
  210. ZERO(circle_flags);
  211. ZERO(horizontal_mesh_line_flags);
  212. ZERO(vertical_mesh_line_flags);
  213. // Move nozzle to the specified height for the first layer
  214. set_destination_from_current();
  215. destination[Z_AXIS] = g26_layer_height;
  216. move_to(destination, 0.0);
  217. move_to(destination, g26_ooze_amount);
  218. has_control_of_lcd_panel = true;
  219. //debug_current_and_destination(PSTR("Starting G26 Mesh Validation Pattern."));
  220. /**
  221. * Declare and generate a sin() & cos() table to be used during the circle drawing. This will lighten
  222. * the CPU load and make the arc drawing faster and more smooth
  223. */
  224. float sin_table[360 / 30 + 1], cos_table[360 / 30 + 1];
  225. for (i = 0; i <= 360 / 30; i++) {
  226. cos_table[i] = SIZE_OF_INTERSECTION_CIRCLES * cos(RADIANS(valid_trig_angle(i * 30.0)));
  227. sin_table[i] = SIZE_OF_INTERSECTION_CIRCLES * sin(RADIANS(valid_trig_angle(i * 30.0)));
  228. }
  229. do {
  230. location = g26_continue_with_closest
  231. ? find_closest_circle_to_print(current_position[X_AXIS], current_position[Y_AXIS])
  232. : find_closest_circle_to_print(g26_x_pos, g26_y_pos); // Find the closest Mesh Intersection to where we are now.
  233. if (location.x_index >= 0 && location.y_index >= 0) {
  234. const float circle_x = mesh_index_to_xpos(location.x_index),
  235. circle_y = mesh_index_to_ypos(location.y_index);
  236. // If this mesh location is outside the printable_radius, skip it.
  237. if (!position_is_reachable(circle_x, circle_y)) continue;
  238. xi = location.x_index; // Just to shrink the next few lines and make them easier to understand
  239. yi = location.y_index;
  240. if (g26_debug_flag) {
  241. SERIAL_ECHOPAIR(" Doing circle at: (xi=", xi);
  242. SERIAL_ECHOPAIR(", yi=", yi);
  243. SERIAL_CHAR(')');
  244. SERIAL_EOL();
  245. }
  246. start_angle = 0.0; // assume it is going to be a full circle
  247. end_angle = 360.0;
  248. if (xi == 0) { // Check for bottom edge
  249. start_angle = -90.0;
  250. end_angle = 90.0;
  251. if (yi == 0) // it is an edge, check for the two left corners
  252. start_angle = 0.0;
  253. else if (yi == GRID_MAX_POINTS_Y - 1)
  254. end_angle = 0.0;
  255. }
  256. else if (xi == GRID_MAX_POINTS_X - 1) { // Check for top edge
  257. start_angle = 90.0;
  258. end_angle = 270.0;
  259. if (yi == 0) // it is an edge, check for the two right corners
  260. end_angle = 180.0;
  261. else if (yi == GRID_MAX_POINTS_Y - 1)
  262. start_angle = 180.0;
  263. }
  264. else if (yi == 0) {
  265. start_angle = 0.0; // only do the top side of the cirlce
  266. end_angle = 180.0;
  267. }
  268. else if (yi == GRID_MAX_POINTS_Y - 1) {
  269. start_angle = 180.0; // only do the bottom side of the cirlce
  270. end_angle = 360.0;
  271. }
  272. for (tmp = start_angle; tmp < end_angle - 0.1; tmp += 30.0) {
  273. #if ENABLED(NEWPANEL)
  274. if (user_canceled()) goto LEAVE; // Check if the user wants to stop the Mesh Validation
  275. #endif
  276. int tmp_div_30 = tmp / 30.0;
  277. if (tmp_div_30 < 0) tmp_div_30 += 360 / 30;
  278. if (tmp_div_30 > 11) tmp_div_30 -= 360 / 30;
  279. float rx = circle_x + cos_table[tmp_div_30], // for speed, these are now a lookup table entry
  280. ry = circle_y + sin_table[tmp_div_30],
  281. xe = circle_x + cos_table[tmp_div_30 + 1],
  282. ye = circle_y + sin_table[tmp_div_30 + 1];
  283. #if IS_KINEMATIC
  284. // Check to make sure this segment is entirely on the bed, skip if not.
  285. if (!position_is_reachable(rx, ry) || !position_is_reachable(xe, ye)) continue;
  286. #else // not, we need to skip
  287. rx = constrain(rx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  288. ry = constrain(ry, Y_MIN_POS + 1, Y_MAX_POS - 1);
  289. xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1);
  290. ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
  291. #endif
  292. //if (g26_debug_flag) {
  293. // char ccc, *cptr, seg_msg[50], seg_num[10];
  294. // strcpy(seg_msg, " segment: ");
  295. // strcpy(seg_num, " \n");
  296. // cptr = (char*) "01234567890ABCDEF????????";
  297. // ccc = cptr[tmp_div_30];
  298. // seg_num[1] = ccc;
  299. // strcat(seg_msg, seg_num);
  300. // debug_current_and_destination(seg_msg);
  301. //}
  302. print_line_from_here_to_there(rx, ry, g26_layer_height, xe, ye, g26_layer_height);
  303. }
  304. if (look_for_lines_to_connect())
  305. goto LEAVE;
  306. }
  307. } while (--g26_repeats && location.x_index >= 0 && location.y_index >= 0);
  308. LEAVE:
  309. lcd_setstatusPGM(PSTR("Leaving G26"), -1);
  310. retract_filament(destination);
  311. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;
  312. //debug_current_and_destination(PSTR("ready to do Z-Raise."));
  313. move_to(destination, 0); // Raise the nozzle
  314. //debug_current_and_destination(PSTR("done doing Z-Raise."));
  315. destination[X_AXIS] = g26_x_pos; // Move back to the starting position
  316. destination[Y_AXIS] = g26_y_pos;
  317. //destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Keep the nozzle where it is
  318. move_to(destination, 0); // Move back to the starting position
  319. //debug_current_and_destination(PSTR("done doing X/Y move."));
  320. has_control_of_lcd_panel = false; // Give back control of the LCD Panel!
  321. if (!g26_keep_heaters_on) {
  322. #if HAS_TEMP_BED
  323. thermalManager.setTargetBed(0);
  324. #endif
  325. thermalManager.setTargetHotend(0, 0);
  326. }
  327. }
  328. float valid_trig_angle(float d) {
  329. while (d > 360.0) d -= 360.0;
  330. while (d < 0.0) d += 360.0;
  331. return d;
  332. }
  333. mesh_index_pair unified_bed_leveling::find_closest_circle_to_print(const float &X, const float &Y) {
  334. float closest = 99999.99;
  335. mesh_index_pair return_val;
  336. return_val.x_index = return_val.y_index = -1;
  337. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  338. for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) {
  339. if (!is_bit_set(circle_flags, i, j)) {
  340. const float mx = mesh_index_to_xpos(i), // We found a circle that needs to be printed
  341. my = mesh_index_to_ypos(j);
  342. // Get the distance to this intersection
  343. float f = HYPOT(X - mx, Y - my);
  344. // It is possible that we are being called with the values
  345. // to let us find the closest circle to the start position.
  346. // But if this is not the case, add a small weighting to the
  347. // distance calculation to help it choose a better place to continue.
  348. f += HYPOT(g26_x_pos - mx, g26_y_pos - my) / 15.0;
  349. // Add in the specified amount of Random Noise to our search
  350. if (random_deviation > 1.0)
  351. f += random(0.0, random_deviation);
  352. if (f < closest) {
  353. closest = f; // We found a closer location that is still
  354. return_val.x_index = i; // un-printed --- save the data for it
  355. return_val.y_index = j;
  356. return_val.distance = closest;
  357. }
  358. }
  359. }
  360. }
  361. bit_set(circle_flags, return_val.x_index, return_val.y_index); // Mark this location as done.
  362. return return_val;
  363. }
  364. bool unified_bed_leveling::look_for_lines_to_connect() {
  365. float sx, sy, ex, ey;
  366. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  367. for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) {
  368. #if ENABLED(NEWPANEL)
  369. if (user_canceled()) return true; // Check if the user wants to stop the Mesh Validation
  370. #endif
  371. if (i < GRID_MAX_POINTS_X) { // We can't connect to anything to the right than GRID_MAX_POINTS_X.
  372. // This is already a half circle because we are at the edge of the bed.
  373. if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i + 1, j)) { // check if we can do a line to the left
  374. if (!is_bit_set(horizontal_mesh_line_flags, i, j)) {
  375. //
  376. // We found two circles that need a horizontal line to connect them
  377. // Print it!
  378. //
  379. sx = mesh_index_to_xpos( i ) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // right edge
  380. ex = mesh_index_to_xpos(i + 1) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // left edge
  381. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1);
  382. sy = ey = constrain(mesh_index_to_ypos(j), Y_MIN_POS + 1, Y_MAX_POS - 1);
  383. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  384. if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) {
  385. if (g26_debug_flag) {
  386. SERIAL_ECHOPAIR(" Connecting with horizontal line (sx=", sx);
  387. SERIAL_ECHOPAIR(", sy=", sy);
  388. SERIAL_ECHOPAIR(") -> (ex=", ex);
  389. SERIAL_ECHOPAIR(", ey=", ey);
  390. SERIAL_CHAR(')');
  391. SERIAL_EOL();
  392. //debug_current_and_destination(PSTR("Connecting horizontal line."));
  393. }
  394. print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height);
  395. }
  396. bit_set(horizontal_mesh_line_flags, i, j); // Mark it as done so we don't do it again, even if we skipped it
  397. }
  398. }
  399. if (j < GRID_MAX_POINTS_Y) { // We can't connect to anything further back than GRID_MAX_POINTS_Y.
  400. // This is already a half circle because we are at the edge of the bed.
  401. if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i, j + 1)) { // check if we can do a line straight down
  402. if (!is_bit_set( vertical_mesh_line_flags, i, j)) {
  403. //
  404. // We found two circles that need a vertical line to connect them
  405. // Print it!
  406. //
  407. sy = mesh_index_to_ypos( j ) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // top edge
  408. ey = mesh_index_to_ypos(j + 1) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // bottom edge
  409. sx = ex = constrain(mesh_index_to_xpos(i), X_MIN_POS + 1, X_MAX_POS - 1);
  410. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  411. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  412. if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) {
  413. if (g26_debug_flag) {
  414. SERIAL_ECHOPAIR(" Connecting with vertical line (sx=", sx);
  415. SERIAL_ECHOPAIR(", sy=", sy);
  416. SERIAL_ECHOPAIR(") -> (ex=", ex);
  417. SERIAL_ECHOPAIR(", ey=", ey);
  418. SERIAL_CHAR(')');
  419. SERIAL_EOL();
  420. debug_current_and_destination(PSTR("Connecting vertical line."));
  421. }
  422. print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height);
  423. }
  424. bit_set(vertical_mesh_line_flags, i, j); // Mark it as done so we don't do it again, even if skipped
  425. }
  426. }
  427. }
  428. }
  429. }
  430. }
  431. return false;
  432. }
  433. void unified_bed_leveling::move_to(const float &x, const float &y, const float &z, const float &e_delta) {
  434. float feed_value;
  435. static float last_z = -999.99;
  436. bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
  437. if (z != last_z) {
  438. last_z = z;
  439. feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0); // Base the feed rate off of the configured Z_AXIS feed rate
  440. destination[X_AXIS] = current_position[X_AXIS];
  441. destination[Y_AXIS] = current_position[Y_AXIS];
  442. destination[Z_AXIS] = z; // We know the last_z==z or we wouldn't be in this block of code.
  443. destination[E_AXIS] = current_position[E_AXIS];
  444. G26_line_to_destination(feed_value);
  445. stepper.synchronize();
  446. set_destination_from_current();
  447. }
  448. // Check if X or Y is involved in the movement.
  449. // Yes: a 'normal' movement. No: a retract() or recover()
  450. feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
  451. if (g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() feed_value for XY:", feed_value);
  452. destination[X_AXIS] = x;
  453. destination[Y_AXIS] = y;
  454. destination[E_AXIS] += e_delta;
  455. G26_line_to_destination(feed_value);
  456. stepper.synchronize();
  457. set_destination_from_current();
  458. }
  459. void unified_bed_leveling::retract_filament(const float where[XYZE]) {
  460. if (!g26_retracted) { // Only retract if we are not already retracted!
  461. g26_retracted = true;
  462. move_to(where, -1.0 * g26_retraction_multiplier);
  463. }
  464. }
  465. void unified_bed_leveling::recover_filament(const float where[XYZE]) {
  466. if (g26_retracted) { // Only un-retract if we are retracted.
  467. move_to(where, 1.2 * g26_retraction_multiplier);
  468. g26_retracted = false;
  469. }
  470. }
  471. /**
  472. * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one
  473. * to the other. But there are really three sets of coordinates involved. The first coordinate
  474. * is the present location of the nozzle. We don't necessarily want to print from this location.
  475. * We first need to move the nozzle to the start of line segment where we want to print. Once
  476. * there, we can use the two coordinates supplied to draw the line.
  477. *
  478. * Note: Although we assume the first set of coordinates is the start of the line and the second
  479. * set of coordinates is the end of the line, it does not always work out that way. This function
  480. * optimizes the movement to minimize the travel distance before it can start printing. This saves
  481. * a lot of time and eliminates a lot of nonsensical movement of the nozzle. However, it does
  482. * cause a lot of very little short retracement of th nozzle when it draws the very first line
  483. * segment of a 'circle'. The time this requires is very short and is easily saved by the other
  484. * cases where the optimization comes into play.
  485. */
  486. void unified_bed_leveling::print_line_from_here_to_there(const float &sx, const float &sy, const float &sz, const float &ex, const float &ey, const float &ez) {
  487. const float dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual line segment
  488. dy_s = current_position[Y_AXIS] - sy,
  489. dist_start = HYPOT2(dx_s, dy_s), // We don't need to do a sqrt(), we can compare the distance^2
  490. // to save computation time
  491. dx_e = current_position[X_AXIS] - ex, // find our distance from the end of the actual line segment
  492. dy_e = current_position[Y_AXIS] - ey,
  493. dist_end = HYPOT2(dx_e, dy_e),
  494. line_length = HYPOT(ex - sx, ey - sy);
  495. // If the end point of the line is closer to the nozzle, flip the direction,
  496. // moving from the end to the start. On very small lines the optimization isn't worth it.
  497. if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < FABS(line_length))
  498. return print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
  499. // Decide whether to retract & bump
  500. if (dist_start > 2.0) {
  501. retract_filament(destination);
  502. //todo: parameterize the bump height with a define
  503. move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + 0.500, 0.0); // Z bump to minimize scraping
  504. move_to(sx, sy, sz + 0.500, 0.0); // Get to the starting point with no extrusion while bumped
  505. }
  506. move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion / un-Z bump
  507. const float e_pos_delta = line_length * g26_e_axis_feedrate * g26_extrusion_multiplier;
  508. recover_filament(destination);
  509. move_to(ex, ey, ez, e_pos_delta); // Get to the ending point with an appropriate amount of extrusion
  510. }
  511. /**
  512. * This function used to be inline code in G26. But there are so many
  513. * parameters it made sense to turn them into static globals and get
  514. * this code out of sight of the main routine.
  515. */
  516. bool unified_bed_leveling::parse_G26_parameters() {
  517. g26_extrusion_multiplier = EXTRUSION_MULTIPLIER;
  518. g26_retraction_multiplier = RETRACTION_MULTIPLIER;
  519. g26_nozzle = MESH_TEST_NOZZLE_SIZE;
  520. g26_filament_diameter = DEFAULT_NOMINAL_FILAMENT_DIA;
  521. g26_layer_height = MESH_TEST_LAYER_HEIGHT;
  522. g26_prime_length = PRIME_LENGTH;
  523. g26_bed_temp = MESH_TEST_BED_TEMP;
  524. g26_hotend_temp = MESH_TEST_HOTEND_TEMP;
  525. g26_prime_flag = 0;
  526. g26_ooze_amount = parser.linearval('O', OOZE_AMOUNT);
  527. g26_keep_heaters_on = parser.boolval('K');
  528. g26_continue_with_closest = parser.boolval('C');
  529. if (parser.seenval('B')) {
  530. g26_bed_temp = parser.value_celsius();
  531. if (!WITHIN(g26_bed_temp, 15, 140)) {
  532. SERIAL_PROTOCOLLNPGM("?Specified bed temperature not plausible.");
  533. return UBL_ERR;
  534. }
  535. }
  536. if (parser.seenval('L')) {
  537. g26_layer_height = parser.value_linear_units();
  538. if (!WITHIN(g26_layer_height, 0.0, 2.0)) {
  539. SERIAL_PROTOCOLLNPGM("?Specified layer height not plausible.");
  540. return UBL_ERR;
  541. }
  542. }
  543. if (parser.seen('Q')) {
  544. if (parser.has_value()) {
  545. g26_retraction_multiplier = parser.value_float();
  546. if (!WITHIN(g26_retraction_multiplier, 0.05, 15.0)) {
  547. SERIAL_PROTOCOLLNPGM("?Specified Retraction Multiplier not plausible.");
  548. return UBL_ERR;
  549. }
  550. }
  551. else {
  552. SERIAL_PROTOCOLLNPGM("?Retraction Multiplier must be specified.");
  553. return UBL_ERR;
  554. }
  555. }
  556. if (parser.seenval('S')) {
  557. g26_nozzle = parser.value_float();
  558. if (!WITHIN(g26_nozzle, 0.1, 1.0)) {
  559. SERIAL_PROTOCOLLNPGM("?Specified nozzle size not plausible.");
  560. return UBL_ERR;
  561. }
  562. }
  563. if (parser.seen('P')) {
  564. if (!parser.has_value()) {
  565. #if ENABLED(NEWPANEL)
  566. g26_prime_flag = -1;
  567. #else
  568. SERIAL_PROTOCOLLNPGM("?Prime length must be specified when not using an LCD.");
  569. return UBL_ERR;
  570. #endif
  571. }
  572. else {
  573. g26_prime_flag++;
  574. g26_prime_length = parser.value_linear_units();
  575. if (!WITHIN(g26_prime_length, 0.0, 25.0)) {
  576. SERIAL_PROTOCOLLNPGM("?Specified prime length not plausible.");
  577. return UBL_ERR;
  578. }
  579. }
  580. }
  581. if (parser.seenval('F')) {
  582. g26_filament_diameter = parser.value_linear_units();
  583. if (!WITHIN(g26_filament_diameter, 1.0, 4.0)) {
  584. SERIAL_PROTOCOLLNPGM("?Specified filament size not plausible.");
  585. return UBL_ERR;
  586. }
  587. }
  588. g26_extrusion_multiplier *= sq(1.75) / sq(g26_filament_diameter); // If we aren't using 1.75mm filament, we need to
  589. // scale up or down the length needed to get the
  590. // same volume of filament
  591. g26_extrusion_multiplier *= g26_filament_diameter * sq(g26_nozzle) / sq(0.3); // Scale up by nozzle size
  592. if (parser.seenval('H')) {
  593. g26_hotend_temp = parser.value_celsius();
  594. if (!WITHIN(g26_hotend_temp, 165, 280)) {
  595. SERIAL_PROTOCOLLNPGM("?Specified nozzle temperature not plausible.");
  596. return UBL_ERR;
  597. }
  598. }
  599. if (parser.seen('U')) {
  600. randomSeed(millis());
  601. // This setting will persist for the next G26
  602. random_deviation = parser.has_value() ? parser.value_float() : 50.0;
  603. }
  604. #if ENABLED(NEWPANEL)
  605. g26_repeats = parser.intval('R', GRID_MAX_POINTS + 1);
  606. #else
  607. if (!parser.seen('R')) {
  608. SERIAL_PROTOCOLLNPGM("?(R)epeat must be specified when not using an LCD.");
  609. return UBL_ERR;
  610. }
  611. else
  612. g26_repeats = parser.has_value() ? parser.value_int() : GRID_MAX_POINTS + 1;
  613. #endif
  614. if (g26_repeats < 1) {
  615. SERIAL_PROTOCOLLNPGM("?(R)epeat value not plausible; must be at least 1.");
  616. return UBL_ERR;
  617. }
  618. g26_x_pos = parser.seenval('X') ? RAW_X_POSITION(parser.value_linear_units()) : current_position[X_AXIS];
  619. g26_y_pos = parser.seenval('Y') ? RAW_Y_POSITION(parser.value_linear_units()) : current_position[Y_AXIS];
  620. if (!position_is_reachable(g26_x_pos, g26_y_pos)) {
  621. SERIAL_PROTOCOLLNPGM("?Specified X,Y coordinate out of bounds.");
  622. return UBL_ERR;
  623. }
  624. /**
  625. * Wait until all parameters are verified before altering the state!
  626. */
  627. set_bed_leveling_enabled(!parser.seen('D'));
  628. return UBL_OK;
  629. }
  630. #if ENABLED(NEWPANEL)
  631. bool unified_bed_leveling::exit_from_g26() {
  632. lcd_setstatusPGM(PSTR("Leaving G26"), -1);
  633. while (ubl_lcd_clicked()) idle();
  634. return UBL_ERR;
  635. }
  636. #endif
  637. /**
  638. * Turn on the bed and nozzle heat and
  639. * wait for them to get up to temperature.
  640. */
  641. bool unified_bed_leveling::turn_on_heaters() {
  642. millis_t next = millis() + 5000UL;
  643. #if HAS_TEMP_BED
  644. #if ENABLED(ULTRA_LCD)
  645. if (g26_bed_temp > 25) {
  646. lcd_setstatusPGM(PSTR("G26 Heating Bed."), 99);
  647. lcd_quick_feedback();
  648. #endif
  649. has_control_of_lcd_panel = true;
  650. thermalManager.setTargetBed(g26_bed_temp);
  651. while (abs(thermalManager.degBed() - g26_bed_temp) > 3) {
  652. #if ENABLED(NEWPANEL)
  653. if (ubl_lcd_clicked()) return exit_from_g26();
  654. #endif
  655. if (ELAPSED(millis(), next)) {
  656. next = millis() + 5000UL;
  657. print_heaterstates();
  658. SERIAL_EOL();
  659. }
  660. idle();
  661. }
  662. #if ENABLED(ULTRA_LCD)
  663. }
  664. lcd_setstatusPGM(PSTR("G26 Heating Nozzle."), 99);
  665. lcd_quick_feedback();
  666. #endif
  667. #endif
  668. // Start heating the nozzle and wait for it to reach temperature.
  669. thermalManager.setTargetHotend(g26_hotend_temp, 0);
  670. while (abs(thermalManager.degHotend(0) - g26_hotend_temp) > 3) {
  671. #if ENABLED(NEWPANEL)
  672. if (ubl_lcd_clicked()) return exit_from_g26();
  673. #endif
  674. if (ELAPSED(millis(), next)) {
  675. next = millis() + 5000UL;
  676. print_heaterstates();
  677. SERIAL_EOL();
  678. }
  679. idle();
  680. }
  681. #if ENABLED(ULTRA_LCD)
  682. lcd_reset_status();
  683. lcd_quick_feedback();
  684. #endif
  685. return UBL_OK;
  686. }
  687. /**
  688. * Prime the nozzle if needed. Return true on error.
  689. */
  690. bool unified_bed_leveling::prime_nozzle() {
  691. #if ENABLED(NEWPANEL)
  692. float Total_Prime = 0.0;
  693. if (g26_prime_flag == -1) { // The user wants to control how much filament gets purged
  694. has_control_of_lcd_panel = true;
  695. lcd_setstatusPGM(PSTR("User-Controlled Prime"), 99);
  696. chirp_at_user();
  697. set_destination_from_current();
  698. recover_filament(destination); // Make sure G26 doesn't think the filament is retracted().
  699. while (!ubl_lcd_clicked()) {
  700. chirp_at_user();
  701. destination[E_AXIS] += 0.25;
  702. #ifdef PREVENT_LENGTHY_EXTRUDE
  703. Total_Prime += 0.25;
  704. if (Total_Prime >= EXTRUDE_MAXLENGTH) return UBL_ERR;
  705. #endif
  706. G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
  707. stepper.synchronize(); // Without this synchronize, the purge is more consistent,
  708. // but because the planner has a buffer, we won't be able
  709. // to stop as quickly. So we put up with the less smooth
  710. // action to give the user a more responsive 'Stop'.
  711. set_destination_from_current();
  712. idle();
  713. }
  714. while (ubl_lcd_clicked()) idle(); // Debounce Encoder Wheel
  715. #if ENABLED(ULTRA_LCD)
  716. strcpy_P(lcd_status_message, PSTR("Done Priming")); // We can't do lcd_setstatusPGM() without having it continue;
  717. // So... We cheat to get a message up.
  718. lcd_setstatusPGM(PSTR("Done Priming"), 99);
  719. lcd_quick_feedback();
  720. #endif
  721. has_control_of_lcd_panel = false;
  722. }
  723. else {
  724. #else
  725. {
  726. #endif
  727. #if ENABLED(ULTRA_LCD)
  728. lcd_setstatusPGM(PSTR("Fixed Length Prime."), 99);
  729. lcd_quick_feedback();
  730. #endif
  731. set_destination_from_current();
  732. destination[E_AXIS] += g26_prime_length;
  733. G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
  734. stepper.synchronize();
  735. set_destination_from_current();
  736. retract_filament(destination);
  737. }
  738. return UBL_OK;
  739. }
  740. #endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_VALIDATION