My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

G26_Mesh_Validation_Tool.cpp 36KB

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