My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

G26.cpp 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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.) During G26 retraction will use the length
  74. * specified by this parameter (1mm by default). Recover will be 1.2x the retract distance.
  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. #if ENABLED(EXTENSIBLE_UI)
  110. #include "../../lcd/extui/ui_api.h"
  111. #endif
  112. #if ENABLED(UBL_HILBERT_CURVE)
  113. #include "../../feature/bedlevel/hilbert_curve.h"
  114. #endif
  115. #define EXTRUSION_MULTIPLIER 1.0
  116. #define PRIME_LENGTH 10.0
  117. #define OOZE_AMOUNT 0.3
  118. #define INTERSECTION_CIRCLE_RADIUS 5
  119. #define CROSSHAIRS_SIZE 3
  120. #ifndef G26_RETRACT_MULTIPLIER
  121. #define G26_RETRACT_MULTIPLIER 1.0 // x 1mm
  122. #endif
  123. #ifndef G26_XY_FEEDRATE
  124. #define G26_XY_FEEDRATE (PLANNER_XY_FEEDRATE() / 3.0)
  125. #endif
  126. #ifndef G26_XY_FEEDRATE_TRAVEL
  127. #define G26_XY_FEEDRATE_TRAVEL (PLANNER_XY_FEEDRATE() / 1.5)
  128. #endif
  129. #if CROSSHAIRS_SIZE >= INTERSECTION_CIRCLE_RADIUS
  130. #error "CROSSHAIRS_SIZE must be less than INTERSECTION_CIRCLE_RADIUS."
  131. #endif
  132. #define G26_OK false
  133. #define G26_ERR true
  134. #if ENABLED(ARC_SUPPORT)
  135. void plan_arc(const xyze_pos_t&, const ab_float_t&, const bool, const uint8_t);
  136. #endif
  137. constexpr float g26_e_axis_feedrate = 0.025;
  138. static MeshFlags circle_flags;
  139. float g26_random_deviation = 0.0;
  140. #if HAS_MARLINUI_MENU
  141. /**
  142. * If the LCD is clicked, cancel, wait for release, return true
  143. */
  144. bool user_canceled() {
  145. if (!ui.button_pressed()) return false; // Return if the button isn't pressed
  146. ui.set_status(GET_TEXT_F(MSG_G26_CANCELED), 99);
  147. TERN_(HAS_MARLINUI_MENU, ui.quick_feedback());
  148. ui.wait_for_release();
  149. return true;
  150. }
  151. #endif
  152. void move_to(const_float_t rx, const_float_t ry, const_float_t z, const_float_t e_delta) {
  153. static float last_z = -999.99;
  154. const xy_pos_t dest = { rx, ry };
  155. const bool has_xy_component = dest != current_position, // Check if X or Y is involved in the movement.
  156. has_e_component = e_delta != 0.0;
  157. if (z != last_z) {
  158. last_z = z;
  159. destination.set(current_position.x, current_position.y, z, current_position.e);
  160. const feedRate_t fr_mm_s = planner.settings.max_feedrate_mm_s[Z_AXIS] * 0.5f; // Use half of the Z_AXIS max feed rate
  161. prepare_internal_move_to_destination(fr_mm_s);
  162. }
  163. // If X or Y in combination with E is involved do a 'normal' move.
  164. // If X or Y with no E is involved do a 'fast' move
  165. // Otherwise retract/recover/hop.
  166. destination = dest;
  167. destination.e += e_delta;
  168. const feedRate_t fr_mm_s = has_xy_component
  169. ? (has_e_component ? feedRate_t(G26_XY_FEEDRATE) : feedRate_t(G26_XY_FEEDRATE_TRAVEL))
  170. : planner.settings.max_feedrate_mm_s[E_AXIS] * 0.666f;
  171. prepare_internal_move_to_destination(fr_mm_s);
  172. }
  173. void move_to(const xyz_pos_t &where, const_float_t de) { move_to(where.x, where.y, where.z, de); }
  174. typedef struct {
  175. float extrusion_multiplier = EXTRUSION_MULTIPLIER,
  176. retraction_multiplier = G26_RETRACT_MULTIPLIER,
  177. layer_height = MESH_TEST_LAYER_HEIGHT,
  178. prime_length = PRIME_LENGTH;
  179. celsius_t bed_temp = MESH_TEST_BED_TEMP,
  180. hotend_temp = MESH_TEST_HOTEND_TEMP;
  181. float nozzle = MESH_TEST_NOZZLE_SIZE,
  182. filament_diameter = DEFAULT_NOMINAL_FILAMENT_DIA,
  183. ooze_amount; // 'O' ... OOZE_AMOUNT
  184. bool continue_with_closest, // 'C'
  185. keep_heaters_on; // 'K'
  186. xy_pos_t xy_pos; // = { 0, 0 }
  187. int8_t prime_flag = 0;
  188. bool g26_retracted = false; // Track the retracted state during G26 so mismatched
  189. // retracts/recovers don't result in a bad state.
  190. void retract_filament(const xyz_pos_t &where) {
  191. if (!g26_retracted) { // Only retract if we are not already retracted!
  192. g26_retracted = true;
  193. move_to(where, -1.0f * retraction_multiplier);
  194. }
  195. }
  196. // TODO: Parameterize the Z lift with a define
  197. void retract_lift_move(const xyz_pos_t &s) {
  198. retract_filament(destination);
  199. move_to(current_position.x, current_position.y, current_position.z + 0.5f, 0.0f); // Z lift to minimize scraping
  200. move_to(s.x, s.y, s.z + 0.5f, 0.0f); // Get to the starting point with no extrusion while lifted
  201. }
  202. void recover_filament(const xyz_pos_t &where) {
  203. if (g26_retracted) { // Only un-retract if we are retracted.
  204. move_to(where, 1.2f * retraction_multiplier);
  205. g26_retracted = false;
  206. }
  207. }
  208. /**
  209. * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one
  210. * to the other. But there are really three sets of coordinates involved. The first coordinate
  211. * is the present location of the nozzle. We don't necessarily want to print from this location.
  212. * We first need to move the nozzle to the start of line segment where we want to print. Once
  213. * there, we can use the two coordinates supplied to draw the line.
  214. *
  215. * Note: Although we assume the first set of coordinates is the start of the line and the second
  216. * set of coordinates is the end of the line, it does not always work out that way. This function
  217. * optimizes the movement to minimize the travel distance before it can start printing. This saves
  218. * a lot of time and eliminates a lot of nonsensical movement of the nozzle. However, it does
  219. * cause a lot of very little short retracement of th nozzle when it draws the very first line
  220. * segment of a 'circle'. The time this requires is very short and is easily saved by the other
  221. * cases where the optimization comes into play.
  222. */
  223. void print_line_from_here_to_there(const xyz_pos_t &s, const xyz_pos_t &e) {
  224. // Distances to the start / end of the line
  225. xy_float_t svec = current_position - s, evec = current_position - e;
  226. const float dist_start = HYPOT2(svec.x, svec.y),
  227. dist_end = HYPOT2(evec.x, evec.y),
  228. line_length = HYPOT(e.x - s.x, e.y - s.y);
  229. // If the end point of the line is closer to the nozzle, flip the direction,
  230. // moving from the end to the start. On very small lines the optimization isn't worth it.
  231. if (dist_end < dist_start && (INTERSECTION_CIRCLE_RADIUS) < ABS(line_length))
  232. return print_line_from_here_to_there(e, s);
  233. // Decide whether to retract & lift
  234. if (dist_start > 2.0) retract_lift_move(s);
  235. move_to(s, 0.0); // Get to the starting point with no extrusion / un-Z lift
  236. const float e_pos_delta = line_length * g26_e_axis_feedrate * extrusion_multiplier;
  237. recover_filament(destination);
  238. move_to(e, e_pos_delta); // Get to the ending point with an appropriate amount of extrusion
  239. }
  240. void connect_neighbor_with_line(const xy_int8_t &p1, int8_t dx, int8_t dy) {
  241. xy_int8_t p2;
  242. p2.x = p1.x + dx;
  243. p2.y = p1.y + dy;
  244. if (p2.x < 0 || p2.x >= (GRID_MAX_POINTS_X)) return;
  245. if (p2.y < 0 || p2.y >= (GRID_MAX_POINTS_Y)) return;
  246. if (circle_flags.marked(p1.x, p1.y) && circle_flags.marked(p2.x, p2.y)) {
  247. xyz_pos_t s, e;
  248. s.x = _GET_MESH_X(p1.x) + (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)) * dx;
  249. e.x = _GET_MESH_X(p2.x) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)) * dx;
  250. s.y = _GET_MESH_Y(p1.y) + (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)) * dy;
  251. e.y = _GET_MESH_Y(p2.y) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)) * dy;
  252. s.z = e.z = layer_height;
  253. #if HAS_ENDSTOPS
  254. LIMIT(s.y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  255. LIMIT(e.y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  256. LIMIT(s.x, X_MIN_POS + 1, X_MAX_POS - 1);
  257. LIMIT(e.x, X_MIN_POS + 1, X_MAX_POS - 1);
  258. #endif
  259. if (position_is_reachable(s.x, s.y) && position_is_reachable(e.x, e.y))
  260. print_line_from_here_to_there(s, e);
  261. }
  262. }
  263. /**
  264. * Turn on the bed and nozzle heat and
  265. * wait for them to get up to temperature.
  266. */
  267. bool turn_on_heaters() {
  268. SERIAL_ECHOLNPGM("Waiting for heatup.");
  269. #if HAS_HEATED_BED
  270. if (bed_temp > 25) {
  271. #if HAS_WIRED_LCD
  272. ui.set_status(GET_TEXT_F(MSG_G26_HEATING_BED), 99);
  273. ui.quick_feedback();
  274. TERN_(HAS_MARLINUI_MENU, ui.capture());
  275. #endif
  276. thermalManager.setTargetBed(bed_temp);
  277. // Wait for the temperature to stabilize
  278. if (!thermalManager.wait_for_bed(true OPTARG(G26_CLICK_CAN_CANCEL, true)))
  279. return G26_ERR;
  280. }
  281. #else
  282. UNUSED(bed_temp);
  283. #endif // HAS_HEATED_BED
  284. // Start heating the active nozzle
  285. #if HAS_WIRED_LCD
  286. ui.set_status(GET_TEXT_F(MSG_G26_HEATING_NOZZLE), 99);
  287. ui.quick_feedback();
  288. #endif
  289. thermalManager.setTargetHotend(hotend_temp, active_extruder);
  290. // Wait for the temperature to stabilize
  291. if (!thermalManager.wait_for_hotend(active_extruder, true OPTARG(G26_CLICK_CAN_CANCEL, true)))
  292. return G26_ERR;
  293. #if HAS_WIRED_LCD
  294. ui.reset_status();
  295. ui.quick_feedback();
  296. #endif
  297. return G26_OK;
  298. }
  299. /**
  300. * Prime the nozzle if needed. Return true on error.
  301. */
  302. bool prime_nozzle() {
  303. const feedRate_t fr_slow_e = planner.settings.max_feedrate_mm_s[E_AXIS] / 15.0f;
  304. #if HAS_MARLINUI_MENU && !HAS_TOUCH_BUTTONS // ui.button_pressed issue with touchscreen
  305. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  306. float Total_Prime = 0.0;
  307. #endif
  308. if (prime_flag == -1) { // The user wants to control how much filament gets purged
  309. ui.capture();
  310. ui.set_status(GET_TEXT_F(MSG_G26_MANUAL_PRIME), 99);
  311. ui.chirp();
  312. destination = current_position;
  313. recover_filament(destination); // Make sure G26 doesn't think the filament is retracted().
  314. while (!ui.button_pressed()) {
  315. ui.chirp();
  316. destination.e += 0.25;
  317. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  318. Total_Prime += 0.25;
  319. if (Total_Prime >= EXTRUDE_MAXLENGTH) {
  320. ui.release();
  321. return G26_ERR;
  322. }
  323. #endif
  324. prepare_internal_move_to_destination(fr_slow_e);
  325. destination = current_position;
  326. planner.synchronize(); // Without this synchronize, the purge is more consistent,
  327. // but because the planner has a buffer, we won't be able
  328. // to stop as quickly. So we put up with the less smooth
  329. // action to give the user a more responsive 'Stop'.
  330. }
  331. ui.wait_for_release();
  332. ui.set_status(GET_TEXT_F(MSG_G26_PRIME_DONE), 99);
  333. ui.quick_feedback();
  334. ui.release();
  335. }
  336. else
  337. #endif
  338. {
  339. #if HAS_WIRED_LCD
  340. ui.set_status(GET_TEXT_F(MSG_G26_FIXED_LENGTH), 99);
  341. ui.quick_feedback();
  342. #endif
  343. destination = current_position;
  344. destination.e += prime_length;
  345. prepare_internal_move_to_destination(fr_slow_e);
  346. destination.e -= prime_length;
  347. retract_filament(destination);
  348. }
  349. return G26_OK;
  350. }
  351. /**
  352. * Find the nearest point at which to print a circle
  353. */
  354. mesh_index_pair find_closest_circle_to_print(const xy_pos_t &pos) {
  355. mesh_index_pair out_point;
  356. out_point.pos = -1;
  357. #if ENABLED(UBL_HILBERT_CURVE)
  358. auto test_func = [](uint8_t i, uint8_t j, void *data) -> bool {
  359. if (!circle_flags.marked(i, j)) {
  360. mesh_index_pair *out_point = (mesh_index_pair*)data;
  361. out_point->pos.set(i, j); // Save its data
  362. return true;
  363. }
  364. return false;
  365. };
  366. hilbert_curve::search_from_closest(pos, test_func, &out_point);
  367. #else
  368. float closest = 99999.99;
  369. GRID_LOOP(i, j) {
  370. if (!circle_flags.marked(i, j)) {
  371. // We found a circle that needs to be printed
  372. const xy_pos_t m = { _GET_MESH_X(i), _GET_MESH_Y(j) };
  373. // Get the distance to this intersection
  374. float f = (pos - m).magnitude();
  375. // It is possible that we are being called with the values
  376. // to let us find the closest circle to the start position.
  377. // But if this is not the case, add a small weighting to the
  378. // distance calculation to help it choose a better place to continue.
  379. f += (xy_pos - m).magnitude() / 15.0f;
  380. // Add the specified amount of Random Noise to our search
  381. if (g26_random_deviation > 1.0) f += random(0.0, g26_random_deviation);
  382. if (f < closest) {
  383. closest = f; // Found a closer un-printed location
  384. out_point.pos.set(i, j); // Save its data
  385. out_point.distance = closest;
  386. }
  387. }
  388. }
  389. #endif
  390. circle_flags.mark(out_point); // Mark this location as done.
  391. return out_point;
  392. }
  393. } g26_helper_t;
  394. /**
  395. * G26: Mesh Validation Pattern generation.
  396. *
  397. * Used to interactively edit the mesh by placing the
  398. * nozzle in a problem area and doing a G29 P4 R command.
  399. *
  400. * Parameters:
  401. *
  402. * B Bed Temperature
  403. * C Continue from the Closest mesh point
  404. * D Disable leveling before starting
  405. * F Filament diameter
  406. * H Hotend Temperature
  407. * K Keep heaters on when completed
  408. * L Layer Height
  409. * O Ooze extrusion length
  410. * P Prime length
  411. * Q Retraction multiplier
  412. * R Repetitions (number of grid points)
  413. * S Nozzle Size (diameter) in mm
  414. * T Tool index to change to, if included
  415. * U Random deviation (50 if no value given)
  416. * X X position
  417. * Y Y position
  418. */
  419. void GcodeSuite::G26() {
  420. SERIAL_ECHOLNPGM("G26 starting...");
  421. // Don't allow Mesh Validation without homing first,
  422. // or if the parameter parsing did not go OK, abort
  423. if (homing_needed_error()) return;
  424. // Change the tool first, if specified
  425. if (parser.seenval('T')) tool_change(parser.value_int());
  426. g26_helper_t g26;
  427. g26.ooze_amount = parser.linearval('O', OOZE_AMOUNT);
  428. g26.continue_with_closest = parser.boolval('C');
  429. g26.keep_heaters_on = parser.boolval('K');
  430. // Accept 'I' if temperature presets are defined
  431. #if HAS_PREHEAT
  432. const uint8_t preset_index = parser.seenval('I') ? _MIN(parser.value_byte(), PREHEAT_COUNT - 1) + 1 : 0;
  433. #endif
  434. #if HAS_HEATED_BED
  435. // Get a temperature from 'I' or 'B'
  436. celsius_t bedtemp = 0;
  437. // Use the 'I' index if temperature presets are defined
  438. #if HAS_PREHEAT
  439. if (preset_index) bedtemp = ui.material_preset[preset_index - 1].bed_temp;
  440. #endif
  441. // Look for 'B' Bed Temperature
  442. if (parser.seenval('B')) bedtemp = parser.value_celsius();
  443. if (bedtemp) {
  444. if (!WITHIN(bedtemp, 40, BED_MAX_TARGET)) {
  445. SERIAL_ECHOLNPGM("?Specified bed temperature not plausible (40-", BED_MAX_TARGET, "C).");
  446. return;
  447. }
  448. g26.bed_temp = bedtemp;
  449. }
  450. #endif // HAS_HEATED_BED
  451. if (parser.seenval('L')) {
  452. g26.layer_height = parser.value_linear_units();
  453. if (!WITHIN(g26.layer_height, 0.0, 2.0)) {
  454. SERIAL_ECHOLNPGM("?Specified layer height not plausible.");
  455. return;
  456. }
  457. }
  458. if (parser.seen('Q')) {
  459. if (parser.has_value()) {
  460. g26.retraction_multiplier = parser.value_float();
  461. if (!WITHIN(g26.retraction_multiplier, 0.05, 15.0)) {
  462. SERIAL_ECHOLNPGM("?Specified Retraction Multiplier not plausible.");
  463. return;
  464. }
  465. }
  466. else {
  467. SERIAL_ECHOLNPGM("?Retraction Multiplier must be specified.");
  468. return;
  469. }
  470. }
  471. if (parser.seenval('S')) {
  472. g26.nozzle = parser.value_float();
  473. if (!WITHIN(g26.nozzle, 0.1, 2.0)) {
  474. SERIAL_ECHOLNPGM("?Specified nozzle size not plausible.");
  475. return;
  476. }
  477. }
  478. if (parser.seen('P')) {
  479. if (!parser.has_value()) {
  480. #if HAS_MARLINUI_MENU
  481. g26.prime_flag = -1;
  482. #else
  483. SERIAL_ECHOLNPGM("?Prime length must be specified when not using an LCD.");
  484. return;
  485. #endif
  486. }
  487. else {
  488. g26.prime_flag++;
  489. g26.prime_length = parser.value_linear_units();
  490. if (!WITHIN(g26.prime_length, 0.0, 25.0)) {
  491. SERIAL_ECHOLNPGM("?Specified prime length not plausible.");
  492. return;
  493. }
  494. }
  495. }
  496. if (parser.seenval('F')) {
  497. g26.filament_diameter = parser.value_linear_units();
  498. if (!WITHIN(g26.filament_diameter, 1.0, 4.0)) {
  499. SERIAL_ECHOLNPGM("?Specified filament size not plausible.");
  500. return;
  501. }
  502. }
  503. g26.extrusion_multiplier *= sq(1.75) / sq(g26.filament_diameter); // If we aren't using 1.75mm filament, we need to
  504. // scale up or down the length needed to get the
  505. // same volume of filament
  506. g26.extrusion_multiplier *= g26.filament_diameter * sq(g26.nozzle) / sq(0.3); // Scale up by nozzle size
  507. // Get a temperature from 'I' or 'H'
  508. celsius_t noztemp = 0;
  509. // Accept 'I' if temperature presets are defined
  510. #if HAS_PREHEAT
  511. if (preset_index) noztemp = ui.material_preset[preset_index - 1].hotend_temp;
  512. #endif
  513. // Look for 'H' Hotend Temperature
  514. if (parser.seenval('H')) noztemp = parser.value_celsius();
  515. // If any preset or temperature was specified
  516. if (noztemp) {
  517. if (!WITHIN(noztemp, 165, (HEATER_0_MAXTEMP) - (HOTEND_OVERSHOOT))) {
  518. SERIAL_ECHOLNPGM("?Specified nozzle temperature not plausible.");
  519. return;
  520. }
  521. g26.hotend_temp = noztemp;
  522. }
  523. // 'U' to Randomize and optionally set circle deviation
  524. if (parser.seen('U')) {
  525. randomSeed(millis());
  526. // This setting will persist for the next G26
  527. g26_random_deviation = parser.has_value() ? parser.value_float() : 50.0;
  528. }
  529. // Get repeat from 'R', otherwise do one full circuit
  530. int16_t g26_repeats;
  531. #if HAS_MARLINUI_MENU
  532. g26_repeats = parser.intval('R', GRID_MAX_POINTS + 1);
  533. #else
  534. if (parser.seen('R'))
  535. g26_repeats = parser.has_value() ? parser.value_int() : GRID_MAX_POINTS + 1;
  536. else {
  537. SERIAL_ECHOLNPGM("?(R)epeat must be specified when not using an LCD.");
  538. return;
  539. }
  540. #endif
  541. if (g26_repeats < 1) {
  542. SERIAL_ECHOLNPGM("?(R)epeat value not plausible; must be at least 1.");
  543. return;
  544. }
  545. // Set a position with 'X' and/or 'Y'. Default: current_position
  546. g26.xy_pos.set(parser.seenval('X') ? RAW_X_POSITION(parser.value_linear_units()) : current_position.x,
  547. parser.seenval('Y') ? RAW_Y_POSITION(parser.value_linear_units()) : current_position.y);
  548. if (!position_is_reachable(g26.xy_pos)) {
  549. SERIAL_ECHOLNPGM("?Specified X,Y coordinate out of bounds.");
  550. return;
  551. }
  552. /**
  553. * Wait until all parameters are verified before altering the state!
  554. */
  555. set_bed_leveling_enabled(!parser.seen_test('D'));
  556. do_z_clearance(Z_CLEARANCE_BETWEEN_PROBES);
  557. #if DISABLED(NO_VOLUMETRICS)
  558. bool volumetric_was_enabled = parser.volumetric_enabled;
  559. parser.volumetric_enabled = false;
  560. planner.calculate_volumetric_multipliers();
  561. #endif
  562. if (g26.turn_on_heaters() != G26_OK) goto LEAVE;
  563. current_position.e = 0.0;
  564. sync_plan_position_e();
  565. if (g26.prime_flag && g26.prime_nozzle() != G26_OK) goto LEAVE;
  566. /**
  567. * Bed is preheated
  568. *
  569. * Nozzle is at temperature
  570. *
  571. * Filament is primed!
  572. *
  573. * It's "Show Time" !!!
  574. */
  575. circle_flags.reset();
  576. // Move nozzle to the specified height for the first layer
  577. destination = current_position;
  578. destination.z = g26.layer_height;
  579. move_to(destination, 0.0);
  580. move_to(destination, g26.ooze_amount);
  581. TERN_(HAS_MARLINUI_MENU, ui.capture());
  582. #if DISABLED(ARC_SUPPORT)
  583. /**
  584. * Pre-generate radius offset values at 30 degree intervals to reduce CPU load.
  585. */
  586. #define A_INT 30
  587. #define _ANGS (360 / A_INT)
  588. #define A_CNT (_ANGS / 2)
  589. #define _IND(A) ((A + _ANGS * 8) % _ANGS)
  590. #define _COS(A) (trig_table[_IND(A) % A_CNT] * (_IND(A) >= A_CNT ? -1 : 1))
  591. #define _SIN(A) (-_COS((A + A_CNT / 2) % _ANGS))
  592. #if A_CNT & 1
  593. #error "A_CNT must be a positive value. Please change A_INT."
  594. #endif
  595. float trig_table[A_CNT];
  596. LOOP_L_N(i, A_CNT)
  597. trig_table[i] = INTERSECTION_CIRCLE_RADIUS * cos(RADIANS(i * A_INT));
  598. #endif // !ARC_SUPPORT
  599. mesh_index_pair location;
  600. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(location.pos, ExtUI::G26_START));
  601. do {
  602. // Find the nearest confluence
  603. location = g26.find_closest_circle_to_print(g26.continue_with_closest ? xy_pos_t(current_position) : g26.xy_pos);
  604. if (location.valid()) {
  605. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(location.pos, ExtUI::G26_POINT_START));
  606. const xy_pos_t circle = _GET_MESH_POS(location.pos);
  607. // If this mesh location is outside the printable radius, skip it.
  608. if (!position_is_reachable(circle)) continue;
  609. // Determine where to start and end the circle,
  610. // which is always drawn counter-clockwise.
  611. const xy_int8_t st = location;
  612. const bool f = st.y == 0,
  613. r = st.x >= GRID_MAX_POINTS_X - 1,
  614. b = st.y >= GRID_MAX_POINTS_Y - 1;
  615. #if ENABLED(ARC_SUPPORT)
  616. #define ARC_LENGTH(quarters) (INTERSECTION_CIRCLE_RADIUS * M_PI * (quarters) / 2)
  617. #define INTERSECTION_CIRCLE_DIAM ((INTERSECTION_CIRCLE_RADIUS) * 2)
  618. xy_float_t e = { circle.x + INTERSECTION_CIRCLE_RADIUS, circle.y };
  619. xyz_float_t s = e;
  620. // Figure out where to start and end the arc - we always print counterclockwise
  621. float arc_length = ARC_LENGTH(4);
  622. if (st.x == 0) { // left edge
  623. if (!f) { s.x = circle.x; s.y -= INTERSECTION_CIRCLE_RADIUS; }
  624. if (!b) { e.x = circle.x; e.y += INTERSECTION_CIRCLE_RADIUS; }
  625. arc_length = (f || b) ? ARC_LENGTH(1) : ARC_LENGTH(2);
  626. }
  627. else if (r) { // right edge
  628. if (b) s.set(circle.x - (INTERSECTION_CIRCLE_RADIUS), circle.y);
  629. else s.set(circle.x, circle.y + INTERSECTION_CIRCLE_RADIUS);
  630. if (f) e.set(circle.x - (INTERSECTION_CIRCLE_RADIUS), circle.y);
  631. else e.set(circle.x, circle.y - (INTERSECTION_CIRCLE_RADIUS));
  632. arc_length = (f || b) ? ARC_LENGTH(1) : ARC_LENGTH(2);
  633. }
  634. else if (f) {
  635. e.x -= INTERSECTION_CIRCLE_DIAM;
  636. arc_length = ARC_LENGTH(2);
  637. }
  638. else if (b) {
  639. s.x -= INTERSECTION_CIRCLE_DIAM;
  640. arc_length = ARC_LENGTH(2);
  641. }
  642. const ab_float_t arc_offset = circle - s;
  643. const xy_float_t dist = current_position - s; // Distance from the start of the actual circle
  644. const float dist_start = HYPOT2(dist.x, dist.y);
  645. const xyze_pos_t endpoint = {
  646. e.x, e.y, g26.layer_height,
  647. current_position.e + (arc_length * g26_e_axis_feedrate * g26.extrusion_multiplier)
  648. };
  649. if (dist_start > 2.0) {
  650. s.z = g26.layer_height + 0.5f;
  651. g26.retract_lift_move(s);
  652. }
  653. s.z = g26.layer_height;
  654. move_to(s, 0.0); // Get to the starting point with no extrusion / un-Z lift
  655. g26.recover_filament(destination);
  656. { REMEMBER(fr, feedrate_mm_s, PLANNER_XY_FEEDRATE() * 0.1f);
  657. plan_arc(endpoint, arc_offset, false, 0); // Draw a counter-clockwise arc
  658. destination = current_position;
  659. }
  660. if (TERN0(HAS_MARLINUI_MENU, user_canceled())) goto LEAVE; // Check if the user wants to stop the Mesh Validation
  661. #else // !ARC_SUPPORT
  662. int8_t start_ind = -2, end_ind = 9; // Assume a full circle (from 5:00 to 5:00)
  663. if (st.x == 0) { // Left edge? Just right half.
  664. start_ind = f ? 0 : -3; // 03:00 to 12:00 for front-left
  665. end_ind = b ? 0 : 2; // 06:00 to 03:00 for back-left
  666. }
  667. else if (r) { // Right edge? Just left half.
  668. start_ind = b ? 6 : 3; // 12:00 to 09:00 for front-right
  669. end_ind = f ? 5 : 8; // 09:00 to 06:00 for back-right
  670. }
  671. else if (f) { // Front edge? Just back half.
  672. start_ind = 0; // 03:00
  673. end_ind = 5; // 09:00
  674. }
  675. else if (b) { // Back edge? Just front half.
  676. start_ind = 6; // 09:00
  677. end_ind = 11; // 03:00
  678. }
  679. for (int8_t ind = start_ind; ind <= end_ind; ind++) {
  680. if (TERN0(HAS_MARLINUI_MENU, user_canceled())) goto LEAVE; // Check if the user wants to stop the Mesh Validation
  681. xyz_float_t p = { circle.x + _COS(ind ), circle.y + _SIN(ind ), g26.layer_height },
  682. q = { circle.x + _COS(ind + 1), circle.y + _SIN(ind + 1), g26.layer_height };
  683. #if IS_KINEMATIC
  684. // Check to make sure this segment is entirely on the bed, skip if not.
  685. if (!position_is_reachable(p) || !position_is_reachable(q)) continue;
  686. #elif HAS_ENDSTOPS
  687. LIMIT(p.x, X_MIN_POS + 1, X_MAX_POS - 1); // Prevent hitting the endstops
  688. LIMIT(p.y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  689. LIMIT(q.x, X_MIN_POS + 1, X_MAX_POS - 1);
  690. LIMIT(q.y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  691. #endif
  692. g26.print_line_from_here_to_there(p, q);
  693. SERIAL_FLUSH(); // Prevent host M105 buffer overrun.
  694. }
  695. #endif // !ARC_SUPPORT
  696. g26.connect_neighbor_with_line(location.pos, -1, 0);
  697. g26.connect_neighbor_with_line(location.pos, 1, 0);
  698. g26.connect_neighbor_with_line(location.pos, 0, -1);
  699. g26.connect_neighbor_with_line(location.pos, 0, 1);
  700. planner.synchronize();
  701. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(location.pos, ExtUI::G26_POINT_FINISH));
  702. if (TERN0(HAS_MARLINUI_MENU, user_canceled())) goto LEAVE;
  703. }
  704. SERIAL_FLUSH(); // Prevent host M105 buffer overrun.
  705. } while (--g26_repeats && location.valid());
  706. LEAVE:
  707. ui.set_status(GET_TEXT_F(MSG_G26_LEAVING), -1);
  708. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(location, ExtUI::G26_FINISH));
  709. g26.retract_filament(destination);
  710. destination.z = Z_CLEARANCE_BETWEEN_PROBES;
  711. move_to(destination, 0); // Raise the nozzle
  712. #if DISABLED(NO_VOLUMETRICS)
  713. parser.volumetric_enabled = volumetric_was_enabled;
  714. planner.calculate_volumetric_multipliers();
  715. #endif
  716. TERN_(HAS_MARLINUI_MENU, ui.release()); // Give back control of the LCD
  717. if (!g26.keep_heaters_on) {
  718. TERN_(HAS_HEATED_BED, thermalManager.setTargetBed(0));
  719. thermalManager.setTargetHotend(active_extruder, 0);
  720. }
  721. }
  722. #endif // G26_MESH_VALIDATION