G26_Mesh_Validation_Tool.cpp 37 KB

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