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 34KB

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