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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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_EDITING)
  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. * Q # Multiplier Retraction Multiplier. Normally not needed. Retraction defaults to 1.0mm and
  84. * un-retraction is at 1.2mm These numbers will be scaled by the specified amount
  85. *
  86. * M # Random Randomize the order that the circles are drawn on the bed. The search for the closest
  87. * undrawn cicle is still done. But the distance to the location for each circle has a
  88. * random number of the size specified added to it. Specifying R50 will give an interesting
  89. * deviation from the normal behaviour on a 10 x 10 Mesh.
  90. * N # Nozzle Used to control the size of nozzle diameter. If not specified, a .4mm nozzle is assumed.
  91. * 'n' can be used instead if your host program does not appreciate you using 'N'.
  92. *
  93. * O # Ooooze How much your nozzle will Ooooze filament while getting in position to print. This
  94. * is over kill, but using this parameter will let you get the very first 'cicle' perfect
  95. * so you have a trophy to peel off of the bed and hang up to show how perfectly you have your
  96. * Mesh calibrated. If not specified, a filament length of .3mm is assumed.
  97. *
  98. * P # Prime Prime the nozzle with specified length of filament. If this parameter is not
  99. * given, no prime action will take place. If the parameter specifies an amount, that much
  100. * will be purged before continuing. If no amount is specified the command will start
  101. * purging filament until the user provides an LCD Click and then it will continue with
  102. * printing the Mesh. You can carefully remove the spent filament with a needle nose
  103. * pliers while holding the LCD Click wheel in a depressed state.
  104. *
  105. * R # Repeat Prints the number of patterns given as a parameter, starting at the current location.
  106. * If a parameter isn't given, every point will be printed unless G26 is interrupted.
  107. * This works the same way that the UBL G29 P4 R parameter works.
  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;
  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. float code_value_float();
  123. float code_value_linear_units();
  124. float code_value_axis_units(const AxisEnum axis);
  125. bool code_value_bool();
  126. bool code_has_value();
  127. void lcd_init();
  128. void lcd_setstatuspgm(const char* const message, const uint8_t level);
  129. bool prepare_move_to_destination_cartesian();
  130. void line_to_destination();
  131. void line_to_destination(float);
  132. void sync_plan_position_e();
  133. void chirp_at_user();
  134. // Private functions
  135. void un_retract_filament(float where[XYZE]);
  136. void retract_filament(float where[XYZE]);
  137. void look_for_lines_to_connect();
  138. bool parse_G26_parameters();
  139. void move_to(const float&, const float&, const float&, const float&) ;
  140. void print_line_from_here_to_there(const float&, const float&, const float&, const float&, const float&, const float&);
  141. bool turn_on_heaters();
  142. bool prime_nozzle();
  143. static uint16_t circle_flags[16], horizontal_mesh_line_flags[16], vertical_mesh_line_flags[16], continue_with_closest = 0;
  144. float g26_e_axis_feedrate = 0.020,
  145. random_deviation = 0.0,
  146. layer_height = LAYER_HEIGHT;
  147. static bool g26_retracted = false; // Track the retracted state of the nozzle so mismatched
  148. // retracts/recovers won't result in a bad state.
  149. float valid_trig_angle(float);
  150. mesh_index_pair find_closest_circle_to_print(const float&, const float&);
  151. static float extrusion_multiplier = EXTRUSION_MULTIPLIER,
  152. retraction_multiplier = RETRACTION_MULTIPLIER,
  153. nozzle = NOZZLE,
  154. filament_diameter = FILAMENT,
  155. prime_length = PRIME_LENGTH,
  156. x_pos, y_pos,
  157. ooze_amount = OOZE_AMOUNT;
  158. static int16_t bed_temp = BED_TEMP,
  159. hotend_temp = HOTEND_TEMP;
  160. static int8_t prime_flag = 0;
  161. static bool keep_heaters_on = false;
  162. static int16_t g26_repeats;
  163. /**
  164. * G26: Mesh Validation Pattern generation.
  165. *
  166. * Used to interactively edit UBL's Mesh by placing the
  167. * nozzle in a problem area and doing a G29 P4 R command.
  168. */
  169. void gcode_G26() {
  170. SERIAL_ECHOLNPGM("G26 command started. Waiting for heater(s).");
  171. float tmp, start_angle, end_angle;
  172. int i, xi, yi;
  173. mesh_index_pair location;
  174. // Don't allow Mesh Validation without homing first,
  175. // or if the parameter parsing did not go OK, abort
  176. if (axis_unhomed_error(true, true, true) || parse_G26_parameters()) return;
  177. if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
  178. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  179. stepper.synchronize();
  180. set_current_to_destination();
  181. }
  182. if (turn_on_heaters()) goto LEAVE;
  183. current_position[E_AXIS] = 0.0;
  184. sync_plan_position_e();
  185. if (prime_flag && prime_nozzle()) goto LEAVE;
  186. /**
  187. * Bed is preheated
  188. *
  189. * Nozzle is at temperature
  190. *
  191. * Filament is primed!
  192. *
  193. * It's "Show Time" !!!
  194. */
  195. ZERO(circle_flags);
  196. ZERO(horizontal_mesh_line_flags);
  197. ZERO(vertical_mesh_line_flags);
  198. // Move nozzle to the specified height for the first layer
  199. set_destination_to_current();
  200. destination[Z_AXIS] = layer_height;
  201. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0.0);
  202. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], ooze_amount);
  203. ubl.has_control_of_lcd_panel = true;
  204. //debug_current_and_destination(PSTR("Starting G26 Mesh Validation Pattern."));
  205. /**
  206. * Declare and generate a sin() & cos() table to be used during the circle drawing. This will lighten
  207. * the CPU load and make the arc drawing faster and more smooth
  208. */
  209. float sin_table[360 / 30 + 1], cos_table[360 / 30 + 1];
  210. for (i = 0; i <= 360 / 30; i++) {
  211. cos_table[i] = SIZE_OF_INTERSECTION_CIRCLES * cos(RADIANS(valid_trig_angle(i * 30.0)));
  212. sin_table[i] = SIZE_OF_INTERSECTION_CIRCLES * sin(RADIANS(valid_trig_angle(i * 30.0)));
  213. }
  214. do {
  215. if (ubl_lcd_clicked()) { // Check if the user wants to stop the Mesh Validation
  216. #if ENABLED(ULTRA_LCD)
  217. lcd_setstatuspgm(PSTR("Mesh Validation Stopped."), 99);
  218. lcd_quick_feedback();
  219. #endif
  220. while (!ubl_lcd_clicked()) { // Wait until the user is done pressing the
  221. idle(); // Encoder Wheel if that is why we are leaving
  222. lcd_reset_alert_level();
  223. lcd_setstatuspgm(PSTR(""));
  224. }
  225. while (ubl_lcd_clicked()) { // Wait until the user is done pressing the
  226. idle(); // Encoder Wheel if that is why we are leaving
  227. lcd_setstatuspgm(PSTR("Unpress Wheel"), 99);
  228. }
  229. goto LEAVE;
  230. }
  231. location = continue_with_closest
  232. ? find_closest_circle_to_print(current_position[X_AXIS], current_position[Y_AXIS])
  233. : find_closest_circle_to_print(x_pos, y_pos); // Find the closest Mesh Intersection to where we are now.
  234. if (location.x_index >= 0 && location.y_index >= 0) {
  235. const float circle_x = pgm_read_float(&ubl.mesh_index_to_xpos[location.x_index]),
  236. circle_y = pgm_read_float(&ubl.mesh_index_to_ypos[location.y_index]);
  237. // Let's do a couple of quick sanity checks. We can pull this code out later if we never see it catch a problem
  238. #ifdef DELTA
  239. if (HYPOT2(circle_x, circle_y) > sq(DELTA_PRINTABLE_RADIUS)) {
  240. SERIAL_ERROR_START;
  241. SERIAL_ERRORLNPGM("Attempt to print outside of DELTA_PRINTABLE_RADIUS.");
  242. goto LEAVE;
  243. }
  244. #endif
  245. // TODO: Change this to use `position_is_reachable`
  246. if (!WITHIN(circle_x, X_MIN_POS, X_MAX_POS) || !WITHIN(circle_y, Y_MIN_POS, Y_MAX_POS)) {
  247. SERIAL_ERROR_START;
  248. SERIAL_ERRORLNPGM("Attempt to print off the bed.");
  249. goto LEAVE;
  250. }
  251. xi = location.x_index; // Just to shrink the next few lines and make them easier to understand
  252. yi = location.y_index;
  253. if (ubl.g26_debug_flag) {
  254. SERIAL_ECHOPAIR(" Doing circle at: (xi=", xi);
  255. SERIAL_ECHOPAIR(", yi=", yi);
  256. SERIAL_CHAR(')');
  257. SERIAL_EOL;
  258. }
  259. start_angle = 0.0; // assume it is going to be a full circle
  260. end_angle = 360.0;
  261. if (xi == 0) { // Check for bottom edge
  262. start_angle = -90.0;
  263. end_angle = 90.0;
  264. if (yi == 0) // it is an edge, check for the two left corners
  265. start_angle = 0.0;
  266. else if (yi == GRID_MAX_POINTS_Y - 1)
  267. end_angle = 0.0;
  268. }
  269. else if (xi == GRID_MAX_POINTS_X - 1) { // Check for top edge
  270. start_angle = 90.0;
  271. end_angle = 270.0;
  272. if (yi == 0) // it is an edge, check for the two right corners
  273. end_angle = 180.0;
  274. else if (yi == GRID_MAX_POINTS_Y - 1)
  275. start_angle = 180.0;
  276. }
  277. else if (yi == 0) {
  278. start_angle = 0.0; // only do the top side of the cirlce
  279. end_angle = 180.0;
  280. }
  281. else if (yi == GRID_MAX_POINTS_Y - 1) {
  282. start_angle = 180.0; // only do the bottom side of the cirlce
  283. end_angle = 360.0;
  284. }
  285. for (tmp = start_angle; tmp < end_angle - 0.1; tmp += 30.0) {
  286. int tmp_div_30 = tmp / 30.0;
  287. if (tmp_div_30 < 0) tmp_div_30 += 360 / 30;
  288. if (tmp_div_30 > 11) tmp_div_30 -= 360 / 30;
  289. float x = circle_x + cos_table[tmp_div_30], // for speed, these are now a lookup table entry
  290. y = circle_y + sin_table[tmp_div_30],
  291. xe = circle_x + cos_table[tmp_div_30 + 1],
  292. ye = circle_y + sin_table[tmp_div_30 + 1];
  293. #ifdef DELTA
  294. if (HYPOT2(x, y) > sq(DELTA_PRINTABLE_RADIUS)) // Check to make sure this part of
  295. continue; // the 'circle' is on the bed. If
  296. #else // not, we need to skip
  297. x = constrain(x, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  298. y = constrain(y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  299. xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1);
  300. ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
  301. #endif
  302. //if (ubl.g26_debug_flag) {
  303. // char ccc, *cptr, seg_msg[50], seg_num[10];
  304. // strcpy(seg_msg, " segment: ");
  305. // strcpy(seg_num, " \n");
  306. // cptr = (char*) "01234567890ABCDEF????????";
  307. // ccc = cptr[tmp_div_30];
  308. // seg_num[1] = ccc;
  309. // strcat(seg_msg, seg_num);
  310. // debug_current_and_destination(seg_msg);
  311. //}
  312. print_line_from_here_to_there(LOGICAL_X_POSITION(x), LOGICAL_Y_POSITION(y), layer_height, LOGICAL_X_POSITION(xe), LOGICAL_Y_POSITION(ye), layer_height);
  313. }
  314. //debug_current_and_destination(PSTR("Looking for lines to connect."));
  315. look_for_lines_to_connect();
  316. //debug_current_and_destination(PSTR("Done with line connect."));
  317. }
  318. //debug_current_and_destination(PSTR("Done with current circle."));
  319. } while (location.x_index >= 0 && location.y_index >= 0 && g26_repeats--);
  320. LEAVE:
  321. lcd_reset_alert_level();
  322. lcd_setstatuspgm(PSTR("Leaving G26"));
  323. retract_filament(destination);
  324. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;
  325. //debug_current_and_destination(PSTR("ready to do Z-Raise."));
  326. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Raise the nozzle
  327. //debug_current_and_destination(PSTR("done doing Z-Raise."));
  328. destination[X_AXIS] = x_pos; // Move back to the starting position
  329. destination[Y_AXIS] = y_pos;
  330. //destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Keep the nozzle where it is
  331. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Move back to the starting position
  332. //debug_current_and_destination(PSTR("done doing X/Y move."));
  333. ubl.has_control_of_lcd_panel = false; // Give back control of the LCD Panel!
  334. if (!keep_heaters_on) {
  335. #if HAS_TEMP_BED
  336. thermalManager.setTargetBed(0);
  337. #endif
  338. thermalManager.setTargetHotend(0, 0);
  339. }
  340. }
  341. float valid_trig_angle(float d) {
  342. while (d > 360.0) d -= 360.0;
  343. while (d < 0.0) d += 360.0;
  344. return d;
  345. }
  346. mesh_index_pair find_closest_circle_to_print(const float &X, const float &Y) {
  347. float closest = 99999.99;
  348. mesh_index_pair return_val;
  349. return_val.x_index = return_val.y_index = -1;
  350. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  351. for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) {
  352. if (!is_bit_set(circle_flags, i, j)) {
  353. const float mx = pgm_read_float(&ubl.mesh_index_to_xpos[i]), // We found a circle that needs to be printed
  354. my = pgm_read_float(&ubl.mesh_index_to_ypos[j]);
  355. // Get the distance to this intersection
  356. float f = HYPOT(X - mx, Y - my);
  357. // It is possible that we are being called with the values
  358. // to let us find the closest circle to the start position.
  359. // But if this is not the case, add a small weighting to the
  360. // distance calculation to help it choose a better place to continue.
  361. f += HYPOT(x_pos - mx, y_pos - my) / 15.0;
  362. // Add in the specified amount of Random Noise to our search
  363. if (random_deviation > 1.0)
  364. f += random(0.0, random_deviation);
  365. if (f < closest) {
  366. closest = f; // We found a closer location that is still
  367. return_val.x_index = i; // un-printed --- save the data for it
  368. return_val.y_index = j;
  369. return_val.distance = closest;
  370. }
  371. }
  372. }
  373. }
  374. bit_set(circle_flags, return_val.x_index, return_val.y_index); // Mark this location as done.
  375. return return_val;
  376. }
  377. void look_for_lines_to_connect() {
  378. float sx, sy, ex, ey;
  379. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  380. for (uint8_t j = 0; j < GRID_MAX_POINTS_Y; j++) {
  381. if (i < GRID_MAX_POINTS_X) { // We can't connect to anything to the right than GRID_MAX_POINTS_X.
  382. // This is already a half circle because we are at the edge of the bed.
  383. 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
  384. if (!is_bit_set(horizontal_mesh_line_flags, i, j)) {
  385. //
  386. // We found two circles that need a horizontal line to connect them
  387. // Print it!
  388. //
  389. sx = pgm_read_float(&ubl.mesh_index_to_xpos[ i ]) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // right edge
  390. ex = pgm_read_float(&ubl.mesh_index_to_xpos[i + 1]) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // left edge
  391. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1);
  392. sy = ey = constrain(pgm_read_float(&ubl.mesh_index_to_ypos[j]), Y_MIN_POS + 1, Y_MAX_POS - 1);
  393. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  394. if (ubl.g26_debug_flag) {
  395. SERIAL_ECHOPAIR(" Connecting with horizontal line (sx=", sx);
  396. SERIAL_ECHOPAIR(", sy=", sy);
  397. SERIAL_ECHOPAIR(") -> (ex=", ex);
  398. SERIAL_ECHOPAIR(", ey=", ey);
  399. SERIAL_CHAR(')');
  400. SERIAL_EOL;
  401. //debug_current_and_destination(PSTR("Connecting horizontal line."));
  402. }
  403. print_line_from_here_to_there(LOGICAL_X_POSITION(sx), LOGICAL_Y_POSITION(sy), layer_height, LOGICAL_X_POSITION(ex), LOGICAL_Y_POSITION(ey), layer_height);
  404. bit_set(horizontal_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  405. }
  406. }
  407. if (j < GRID_MAX_POINTS_Y) { // We can't connect to anything further back than GRID_MAX_POINTS_Y.
  408. // This is already a half circle because we are at the edge of the bed.
  409. 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
  410. if (!is_bit_set( vertical_mesh_line_flags, i, j)) {
  411. //
  412. // We found two circles that need a vertical line to connect them
  413. // Print it!
  414. //
  415. sy = pgm_read_float(&ubl.mesh_index_to_ypos[ j ]) + (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // top edge
  416. ey = pgm_read_float(&ubl.mesh_index_to_ypos[j + 1]) - (SIZE_OF_INTERSECTION_CIRCLES - (SIZE_OF_CROSSHAIRS)); // bottom edge
  417. sx = ex = constrain(pgm_read_float(&ubl.mesh_index_to_xpos[i]), X_MIN_POS + 1, X_MAX_POS - 1);
  418. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  419. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  420. if (ubl.g26_debug_flag) {
  421. SERIAL_ECHOPAIR(" Connecting with vertical line (sx=", sx);
  422. SERIAL_ECHOPAIR(", sy=", sy);
  423. SERIAL_ECHOPAIR(") -> (ex=", ex);
  424. SERIAL_ECHOPAIR(", ey=", ey);
  425. SERIAL_CHAR(')');
  426. SERIAL_EOL;
  427. debug_current_and_destination(PSTR("Connecting vertical line."));
  428. }
  429. print_line_from_here_to_there(LOGICAL_X_POSITION(sx), LOGICAL_Y_POSITION(sy), layer_height, LOGICAL_X_POSITION(ex), LOGICAL_Y_POSITION(ey), layer_height);
  430. bit_set(vertical_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. void move_to(const float &x, const float &y, const float &z, const float &e_delta) {
  439. float feed_value;
  440. static float last_z = -999.99;
  441. bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
  442. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() has_xy_component:", (int)has_xy_component);
  443. if (z != last_z) {
  444. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() changing Z to ", (int)z);
  445. last_z = z;
  446. feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0); // Base the feed rate off of the configured Z_AXIS feed rate
  447. destination[X_AXIS] = current_position[X_AXIS];
  448. destination[Y_AXIS] = current_position[Y_AXIS];
  449. destination[Z_AXIS] = z; // We know the last_z==z or we wouldn't be in this block of code.
  450. destination[E_AXIS] = current_position[E_AXIS];
  451. ubl_line_to_destination(feed_value, 0);
  452. stepper.synchronize();
  453. set_destination_to_current();
  454. //if (ubl.g26_debug_flag) debug_current_and_destination(PSTR(" in move_to() done with Z move"));
  455. }
  456. // Check if X or Y is involved in the movement.
  457. // Yes: a 'normal' movement. No: a retract() or un_retract()
  458. feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
  459. if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() feed_value for XY:", feed_value);
  460. destination[X_AXIS] = x;
  461. destination[Y_AXIS] = y;
  462. destination[E_AXIS] += e_delta;
  463. //if (ubl.g26_debug_flag) debug_current_and_destination(PSTR(" in move_to() doing last move"));
  464. ubl_line_to_destination(feed_value, 0);
  465. //if (ubl.g26_debug_flag) debug_current_and_destination(PSTR(" in move_to() after last move"));
  466. stepper.synchronize();
  467. set_destination_to_current();
  468. }
  469. void retract_filament(float where[XYZE]) {
  470. if (!g26_retracted) { // Only retract if we are not already retracted!
  471. g26_retracted = true;
  472. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Decided to do retract.");
  473. move_to(where[X_AXIS], where[Y_AXIS], where[Z_AXIS], -1.0 * retraction_multiplier);
  474. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Retraction done.");
  475. }
  476. }
  477. void un_retract_filament(float where[XYZE]) {
  478. if (g26_retracted) { // Only un-retract if we are retracted.
  479. move_to(where[X_AXIS], where[Y_AXIS], where[Z_AXIS], 1.2 * retraction_multiplier);
  480. g26_retracted = false;
  481. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" unretract done.");
  482. }
  483. }
  484. /**
  485. * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one
  486. * to the other. But there are really three sets of coordinates involved. The first coordinate
  487. * is the present location of the nozzle. We don't necessarily want to print from this location.
  488. * We first need to move the nozzle to the start of line segment where we want to print. Once
  489. * there, we can use the two coordinates supplied to draw the line.
  490. *
  491. * Note: Although we assume the first set of coordinates is the start of the line and the second
  492. * set of coordinates is the end of the line, it does not always work out that way. This function
  493. * optimizes the movement to minimize the travel distance before it can start printing. This saves
  494. * a lot of time and eleminates a lot of non-sensical movement of the nozzle. However, it does
  495. * cause a lot of very little short retracement of th nozzle when it draws the very first line
  496. * segment of a 'circle'. The time this requires is very short and is easily saved by the other
  497. * cases where the optimization comes into play.
  498. */
  499. void print_line_from_here_to_there(const float &sx, const float &sy, const float &sz, const float &ex, const float &ey, const float &ez) {
  500. const float dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual line segment
  501. dy_s = current_position[Y_AXIS] - sy,
  502. dist_start = HYPOT2(dx_s, dy_s), // We don't need to do a sqrt(), we can compare the distance^2
  503. // to save computation time
  504. dx_e = current_position[X_AXIS] - ex, // find our distance from the end of the actual line segment
  505. dy_e = current_position[Y_AXIS] - ey,
  506. dist_end = HYPOT2(dx_e, dy_e),
  507. line_length = HYPOT(ex - sx, ey - sy);
  508. // If the end point of the line is closer to the nozzle, flip the direction,
  509. // moving from the end to the start. On very small lines the optimization isn't worth it.
  510. if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < abs(line_length)) {
  511. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Reversing start and end of print_line_from_here_to_there()");
  512. return print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
  513. }
  514. // Decide whether to retract & bump
  515. if (dist_start > 2.0) {
  516. retract_filament(destination);
  517. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" filament retracted.");
  518. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Z bumping by 0.500 to minimize scraping.");
  519. //todo: parameterize the bump height with a define
  520. move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]+0.500, 0.0); // Z bump to minimize scraping
  521. move_to(sx, sy, sz+0.500, 0.0); // Get to the starting point with no extrusion while bumped
  522. }
  523. move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion / un-Z bump
  524. const float e_pos_delta = line_length * g26_e_axis_feedrate * extrusion_multiplier;
  525. un_retract_filament(destination);
  526. //if (ubl.g26_debug_flag) {
  527. // SERIAL_ECHOLNPGM(" doing printing move.");
  528. // debug_current_and_destination(PSTR("doing final move_to() inside print_line_from_here_to_there()"));
  529. //}
  530. move_to(ex, ey, ez, e_pos_delta); // Get to the ending point with an appropriate amount of extrusion
  531. }
  532. /**
  533. * This function used to be inline code in G26. But there are so many
  534. * parameters it made sense to turn them into static globals and get
  535. * this code out of sight of the main routine.
  536. */
  537. bool parse_G26_parameters() {
  538. extrusion_multiplier = EXTRUSION_MULTIPLIER;
  539. retraction_multiplier = RETRACTION_MULTIPLIER;
  540. nozzle = NOZZLE;
  541. filament_diameter = FILAMENT;
  542. layer_height = LAYER_HEIGHT;
  543. prime_length = PRIME_LENGTH;
  544. bed_temp = BED_TEMP;
  545. hotend_temp = HOTEND_TEMP;
  546. ooze_amount = OOZE_AMOUNT;
  547. prime_flag = 0;
  548. keep_heaters_on = false;
  549. if (code_seen('B')) {
  550. bed_temp = code_value_temp_abs();
  551. if (!WITHIN(bed_temp, 15, 140)) {
  552. SERIAL_PROTOCOLLNPGM("?Specified bed temperature not plausible.");
  553. return UBL_ERR;
  554. }
  555. }
  556. if (code_seen('C')) continue_with_closest++;
  557. if (code_seen('L')) {
  558. layer_height = code_value_linear_units();
  559. if (!WITHIN(layer_height, 0.0, 2.0)) {
  560. SERIAL_PROTOCOLLNPGM("?Specified layer height not plausible.");
  561. return UBL_ERR;
  562. }
  563. }
  564. if (code_seen('Q')) {
  565. if (code_has_value()) {
  566. retraction_multiplier = code_value_float();
  567. if (!WITHIN(retraction_multiplier, 0.05, 15.0)) {
  568. SERIAL_PROTOCOLLNPGM("?Specified Retraction Multiplier not plausible.");
  569. return UBL_ERR;
  570. }
  571. }
  572. else {
  573. SERIAL_PROTOCOLLNPGM("?Retraction Multiplier must be specified.");
  574. return UBL_ERR;
  575. }
  576. }
  577. if (code_seen('N') || code_seen('n')) {
  578. nozzle = code_value_float();
  579. if (!WITHIN(nozzle, 0.1, 1.0)) {
  580. SERIAL_PROTOCOLLNPGM("?Specified nozzle size not plausible.");
  581. return UBL_ERR;
  582. }
  583. }
  584. if (code_seen('K')) keep_heaters_on++;
  585. if (code_seen('O') && code_has_value())
  586. ooze_amount = code_value_linear_units();
  587. if (code_seen('P')) {
  588. if (!code_has_value())
  589. prime_flag = -1;
  590. else {
  591. prime_flag++;
  592. prime_length = code_value_linear_units();
  593. if (!WITHIN(prime_length, 0.0, 25.0)) {
  594. SERIAL_PROTOCOLLNPGM("?Specified prime length not plausible.");
  595. return UBL_ERR;
  596. }
  597. }
  598. }
  599. if (code_seen('F')) {
  600. filament_diameter = code_value_linear_units();
  601. if (!WITHIN(filament_diameter, 1.0, 4.0)) {
  602. SERIAL_PROTOCOLLNPGM("?Specified filament size not plausible.");
  603. return UBL_ERR;
  604. }
  605. }
  606. extrusion_multiplier *= sq(1.75) / sq(filament_diameter); // If we aren't using 1.75mm filament, we need to
  607. // scale up or down the length needed to get the
  608. // same volume of filament
  609. extrusion_multiplier *= filament_diameter * sq(nozzle) / sq(0.3); // Scale up by nozzle size
  610. if (code_seen('H')) {
  611. hotend_temp = code_value_temp_abs();
  612. if (!WITHIN(hotend_temp, 165, 280)) {
  613. SERIAL_PROTOCOLLNPGM("?Specified nozzle temperature not plausible.");
  614. return UBL_ERR;
  615. }
  616. }
  617. if (code_seen('M')) {
  618. randomSeed(millis());
  619. random_deviation = code_has_value() ? code_value_float() : 50.0;
  620. }
  621. if (code_seen('R')) {
  622. g26_repeats = code_has_value() ? code_value_int() : 999;
  623. if (g26_repeats <= 0) {
  624. SERIAL_PROTOCOLLNPGM("?(R)epeat value not plausible; must be greater than 0.");
  625. return UBL_ERR;
  626. }
  627. g26_repeats--;
  628. }
  629. x_pos = current_position[X_AXIS];
  630. y_pos = current_position[Y_AXIS];
  631. if (code_seen('X')) {
  632. x_pos = code_value_axis_units(X_AXIS);
  633. if (!WITHIN(x_pos, X_MIN_POS, X_MAX_POS)) {
  634. SERIAL_PROTOCOLLNPGM("?Specified X coordinate not plausible.");
  635. return UBL_ERR;
  636. }
  637. }
  638. else
  639. if (code_seen('Y')) {
  640. y_pos = code_value_axis_units(Y_AXIS);
  641. if (!WITHIN(y_pos, Y_MIN_POS, Y_MAX_POS)) {
  642. SERIAL_PROTOCOLLNPGM("?Specified Y coordinate not plausible.");
  643. return UBL_ERR;
  644. }
  645. }
  646. /**
  647. * We save the question of what to do with the Unified Bed Leveling System's Activation until the very
  648. * end. The reason is, if one of the parameters specified up above is incorrect, we don't want to
  649. * alter the system's status. We wait until we know everything is correct before altering the state
  650. * of the system.
  651. */
  652. ubl.state.active = !code_seen('D');
  653. return UBL_OK;
  654. }
  655. bool exit_from_g26() {
  656. //strcpy(lcd_status_message, "Leaving G26"); // We can't do lcd_setstatus() without having it continue;
  657. lcd_reset_alert_level();
  658. lcd_setstatuspgm(PSTR("Leaving G26"));
  659. while (ubl_lcd_clicked()) idle();
  660. return UBL_ERR;
  661. }
  662. /**
  663. * Turn on the bed and nozzle heat and
  664. * wait for them to get up to temperature.
  665. */
  666. bool turn_on_heaters() {
  667. millis_t next;
  668. #if HAS_TEMP_BED
  669. #if ENABLED(ULTRA_LCD)
  670. if (bed_temp > 25) {
  671. lcd_setstatuspgm(PSTR("G26 Heating Bed."), 99);
  672. lcd_quick_feedback();
  673. #endif
  674. ubl.has_control_of_lcd_panel = true;
  675. thermalManager.setTargetBed(bed_temp);
  676. next = millis() + 5000UL;
  677. while (abs(thermalManager.degBed() - bed_temp) > 3) {
  678. if (ubl_lcd_clicked()) return exit_from_g26();
  679. if (PENDING(millis(), next)) {
  680. next = millis() + 5000UL;
  681. print_heaterstates();
  682. }
  683. idle();
  684. }
  685. #if ENABLED(ULTRA_LCD)
  686. }
  687. lcd_setstatuspgm(PSTR("G26 Heating Nozzle."), 99);
  688. lcd_quick_feedback();
  689. #endif
  690. #endif
  691. // Start heating the nozzle and wait for it to reach temperature.
  692. thermalManager.setTargetHotend(hotend_temp, 0);
  693. while (abs(thermalManager.degHotend(0) - hotend_temp) > 3) {
  694. if (ubl_lcd_clicked()) return exit_from_g26();
  695. if (PENDING(millis(), next)) {
  696. next = millis() + 5000UL;
  697. print_heaterstates();
  698. }
  699. idle();
  700. }
  701. #if ENABLED(ULTRA_LCD)
  702. lcd_reset_alert_level();
  703. lcd_setstatuspgm(PSTR(""));
  704. lcd_quick_feedback();
  705. #endif
  706. return UBL_OK;
  707. }
  708. /**
  709. * Prime the nozzle if needed. Return true on error.
  710. */
  711. bool prime_nozzle() {
  712. float Total_Prime = 0.0;
  713. if (prime_flag == -1) { // The user wants to control how much filament gets purged
  714. ubl.has_control_of_lcd_panel = true;
  715. lcd_setstatuspgm(PSTR("User-Controlled Prime"), 99);
  716. chirp_at_user();
  717. set_destination_to_current();
  718. un_retract_filament(destination); // Make sure G26 doesn't think the filament is retracted().
  719. while (!ubl_lcd_clicked()) {
  720. chirp_at_user();
  721. destination[E_AXIS] += 0.25;
  722. #ifdef PREVENT_LENGTHY_EXTRUDE
  723. Total_Prime += 0.25;
  724. if (Total_Prime >= EXTRUDE_MAXLENGTH) return UBL_ERR;
  725. #endif
  726. ubl_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0);
  727. stepper.synchronize(); // Without this synchronize, the purge is more consistent,
  728. // but because the planner has a buffer, we won't be able
  729. // to stop as quickly. So we put up with the less smooth
  730. // action to give the user a more responsive 'Stop'.
  731. set_destination_to_current();
  732. idle();
  733. }
  734. while (ubl_lcd_clicked()) idle(); // Debounce Encoder Wheel
  735. #if ENABLED(ULTRA_LCD)
  736. strcpy_P(lcd_status_message, PSTR("Done Priming")); // We can't do lcd_setstatuspgm() without having it continue;
  737. // So... We cheat to get a message up.
  738. lcd_setstatuspgm(PSTR("Done Priming"), 99);
  739. lcd_quick_feedback();
  740. #endif
  741. ubl.has_control_of_lcd_panel = false;
  742. }
  743. else {
  744. #if ENABLED(ULTRA_LCD)
  745. lcd_setstatuspgm(PSTR("Fixed Length Prime."), 99);
  746. lcd_quick_feedback();
  747. #endif
  748. set_destination_to_current();
  749. destination[E_AXIS] += prime_length;
  750. ubl_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0);
  751. stepper.synchronize();
  752. set_destination_to_current();
  753. retract_filament(destination);
  754. }
  755. return UBL_OK;
  756. }
  757. #endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_EDITING