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.cpp 35KB

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