My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

G26.cpp 32KB

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