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

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