My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

G26_Mesh_Validation_Tool.cpp 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Marlin Firmware -- G26 - Mesh Validation Tool
  24. */
  25. #include "MarlinConfig.h"
  26. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_EDITING)
  27. #include "Marlin.h"
  28. #include "Configuration.h"
  29. #include "planner.h"
  30. #include "stepper.h"
  31. #include "temperature.h"
  32. #include "UBL.h"
  33. #include "ultralcd.h"
  34. #define EXTRUSION_MULTIPLIER 1.0 // This is too much clutter for the main Configuration.h file But
  35. #define RETRACTION_MULTIPLIER 1.0 // some user have expressed an interest in being able to customize
  36. #define NOZZLE 0.3 // these numbers for their printer so they don't need to type all
  37. #define FILAMENT 1.75 // the options every time they do a Mesh Validation Print.
  38. #define LAYER_HEIGHT 0.2
  39. #define PRIME_LENGTH 10.0 // So, we put these number in an easy to find and change place.
  40. #define BED_TEMP 60.0
  41. #define HOTEND_TEMP 205.0
  42. #define OOZE_AMOUNT 0.3
  43. #define SIZE_OF_INTERSECTION_CIRCLES 5
  44. #define SIZE_OF_CROSS_HAIRS 3 // cross hairs inside the circle. This number should be
  45. // less than SIZE_OR_INTERSECTION_CIRCLES
  46. /**
  47. * Roxy's G26 Mesh Validation Tool
  48. *
  49. * G26 Is a Mesh Validation Tool intended to provide support for the Marlin Unified Bed Leveling System.
  50. * In order to fully utilize and benefit from the Marlin Unified Bed Leveling System an accurate Mesh must
  51. * be defined. G29 is designed to allow the user to quickly validate the correctness of her Mesh. It will
  52. * first heat the bed and nozzle. It will then print lines and circles along the Mesh Cell boundaries and
  53. * the intersections of those lines (respectively).
  54. *
  55. * This action allows the user to immediately see where the Mesh is properly defined and where it needs to
  56. * be edited. The command will generate the Mesh lines closest to the nozzle's starting position. Alternatively
  57. * the user can specify the X and Y position of interest with command parameters. This allows the user to
  58. * focus on a particular area of the Mesh where attention is needed.
  59. *
  60. * B # Bed Set the Bed Temperature. If not specified, a default of 60 C. will be assumed.
  61. *
  62. * C Current When searching for Mesh Intersection points to draw, use the current nozzle location
  63. * as the base for any distance comparison.
  64. *
  65. * D Disable Disable the Unified Bed Leveling System. In the normal case the user is invoking this
  66. * command to see how well a Mesh as been adjusted to match a print surface. In order to do
  67. * this the Unified Bed Leveling System is turned on by the G26 command. The D parameter
  68. * alters the command's normal behaviour and disables the Unified Bed Leveling System even if
  69. * it is on.
  70. *
  71. * H # Hotend Set the Nozzle Temperature. If not specified, a default of 205 C. will be assumed.
  72. *
  73. * F # Filament Used to specify the diameter of the filament being used. If not specified
  74. * 1.75mm filament is assumed. If you are not getting acceptable results by using the
  75. * 'correct' numbers, you can scale this number up or down a little bit to change the amount
  76. * of filament that is being extruded during the printing of the various lines on the bed.
  77. *
  78. * K Keep-On Keep the heaters turned on at the end of the command.
  79. *
  80. * L # Layer Layer height. (Height of nozzle above bed) If not specified .20mm will be used.
  81. *
  82. * Q # Multiplier Retraction Multiplier. Normally not needed. Retraction defaults to 1.0mm and
  83. * un-retraction is at 1.2mm These numbers will be scaled by the specified amount
  84. *
  85. * N # Nozzle Used to control the size of nozzle diameter. If not specified, a .4mm nozzle is assumed.
  86. *
  87. * O # Ooooze How much your nozzle will Ooooze filament while getting in position to print. This
  88. * is over kill, but using this parameter will let you get the very first 'cicle' perfect
  89. * so you have a trophy to peel off of the bed and hang up to show how perfectly you have your
  90. * Mesh calibrated. If not specified, a filament length of .3mm is assumed.
  91. *
  92. * P # Prime Prime the nozzle with specified length of filament. If this parameter is not
  93. * given, no prime action will take place. If the parameter specifies an amount, that much
  94. * will be purged before continuing. If no amount is specified the command will start
  95. * purging filament until the user provides an LCD Click and then it will continue with
  96. * printing the Mesh. You can carefully remove the spent filament with a needle nose
  97. * pliers while holding the LCD Click wheel in a depressed state.
  98. *
  99. * R # Random Randomize the order that the circles are drawn on the bed. The search for the closest
  100. * undrawn cicle is still done. But the distance to the location for each circle has a
  101. * random number of the size specified added to it. Specifying R50 will give an interesting
  102. * deviation from the normal behaviour on a 10 x 10 Mesh.
  103. *
  104. * X # X coordinate Specify the starting location of the drawing activity.
  105. *
  106. * Y # Y coordinate Specify the starting location of the drawing activity.
  107. */
  108. extern float feedrate;
  109. extern Planner planner;
  110. //#if ENABLED(ULTRA_LCD)
  111. extern char lcd_status_message[];
  112. //#endif
  113. extern float destination[];
  114. extern void set_destination_to_current();
  115. extern void set_current_to_destination();
  116. extern float code_value_float();
  117. extern bool code_value_bool();
  118. extern bool code_has_value();
  119. extern void lcd_init();
  120. extern void lcd_setstatuspgm(const char* const message, uint8_t level);
  121. #define PLANNER_XY_FEEDRATE() (min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])) //bob
  122. bool prepare_move_to_destination_cartesian();
  123. void line_to_destination();
  124. void line_to_destination(float );
  125. void gcode_G28();
  126. void sync_plan_position_e();
  127. void un_retract_filament();
  128. void retract_filament();
  129. void look_for_lines_to_connect();
  130. bool parse_G26_parameters();
  131. void move_to(const float&, const float&, const float&, const float&) ;
  132. void print_line_from_here_to_there(float sx, float sy, float sz, float ex, float ey, float ez);
  133. bool turn_on_heaters();
  134. bool prime_nozzle();
  135. void chirp_at_user();
  136. static uint16_t circle_flags[16], horizontal_mesh_line_flags[16], vertical_mesh_line_flags[16], continue_with_closest = 0;
  137. float g26_e_axis_feedrate = 0.020,
  138. random_deviation = 0.0,
  139. layer_height = LAYER_HEIGHT;
  140. bool g26_retracted = false; // We keep track of the state of the nozzle to know if it
  141. // is currently retracted or not. This allows us to be
  142. // less careful because mis-matched retractions and un-retractions
  143. // won't leave us in a bad state.
  144. float valid_trig_angle(float);
  145. mesh_index_pair find_closest_circle_to_print(float, float);
  146. void ubl_line_to_destination(const float&, const float&, const float&, const float&, const float&, uint8_t);
  147. //uint16_t x_splits = 0xFFFF, uint16_t y_splits = 0xFFFF); /* needed for the old mesh_buffer_line() routine */
  148. static float extrusion_multiplier = EXTRUSION_MULTIPLIER,
  149. retraction_multiplier = RETRACTION_MULTIPLIER,
  150. nozzle = NOZZLE,
  151. filament_diameter = FILAMENT,
  152. prime_length = PRIME_LENGTH,
  153. x_pos, y_pos,
  154. bed_temp = BED_TEMP,
  155. hotend_temp = HOTEND_TEMP,
  156. ooze_amount = OOZE_AMOUNT;
  157. int8_t prime_flag = 0;
  158. bool keep_heaters_on = false;
  159. /**
  160. * G26: Mesh Validation Pattern generation.
  161. *
  162. * Used to interactively edit UBL's Mesh by placing the
  163. * nozzle in a problem area and doing a G29 P4 R command.
  164. */
  165. void gcode_G26() {
  166. float circle_x, circle_y, x, y, xe, ye, tmp,
  167. start_angle, end_angle;
  168. int i, xi, yi, lcd_init_counter = 0;
  169. mesh_index_pair location;
  170. if (axis_unhomed_error(true, true, true)) // Don't allow Mesh Validation without homing first
  171. gcode_G28();
  172. if (parse_G26_parameters()) return; // If the paramter parsing did not go OK, we abort the command
  173. if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
  174. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  175. stepper.synchronize();
  176. set_current_to_destination();
  177. }
  178. ubl.has_control_of_lcd_panel = true; // Take control of the LCD Panel!
  179. if (turn_on_heaters()) // Turn on the heaters, leave the command if anything
  180. goto LEAVE; // has gone wrong.
  181. current_position[E_AXIS] = 0.0;
  182. sync_plan_position_e();
  183. if (prime_flag && prime_nozzle()) // if prime_nozzle() returns an error, we just bail out.
  184. goto LEAVE;
  185. /**
  186. * Bed is preheated
  187. *
  188. * Nozzle is at temperature
  189. *
  190. * Filament is primed!
  191. *
  192. * It's "Show Time" !!!
  193. */
  194. // Clear all of the flags we need
  195. ZERO(circle_flags);
  196. ZERO(horizontal_mesh_line_flags);
  197. ZERO(vertical_mesh_line_flags);
  198. //
  199. // Move nozzle to the specified height for the first layer
  200. //
  201. set_destination_to_current();
  202. destination[Z_AXIS] = layer_height;
  203. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0.0);
  204. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], ooze_amount);
  205. ubl.has_control_of_lcd_panel = true; // Take control of the LCD Panel!
  206. //debug_current_and_destination((char*)"Starting G26 Mesh Validation Pattern.");
  207. /**
  208. * Declare and generate a sin() & cos() table to be used during the circle drawing. This will lighten
  209. * the CPU load and make the arc drawing faster and more smooth
  210. */
  211. float sin_table[360 / 30 + 1], cos_table[360 / 30 + 1];
  212. for (i = 0; i <= 360 / 30; i++) {
  213. cos_table[i] = SIZE_OF_INTERSECTION_CIRCLES * cos(RADIANS(valid_trig_angle(i * 30.0)));
  214. sin_table[i] = SIZE_OF_INTERSECTION_CIRCLES * sin(RADIANS(valid_trig_angle(i * 30.0)));
  215. }
  216. do {
  217. if (ubl_lcd_clicked()) { // Check if the user wants to stop the Mesh Validation
  218. #if ENABLED(ULTRA_LCD)
  219. lcd_setstatuspgm(PSTR("Mesh Validation Stopped."), 99);
  220. lcd_quick_feedback();
  221. #endif
  222. while (!ubl_lcd_clicked()) { // Wait until the user is done pressing the
  223. idle(); // Encoder Wheel if that is why we are leaving
  224. lcd_reset_alert_level();
  225. lcd_setstatuspgm(PSTR(""));
  226. }
  227. while (ubl_lcd_clicked()) { // Wait until the user is done pressing the
  228. idle(); // Encoder Wheel if that is why we are leaving
  229. lcd_setstatuspgm(PSTR("Unpress Wheel"), 99);
  230. }
  231. goto LEAVE;
  232. }
  233. if (continue_with_closest)
  234. location = find_closest_circle_to_print(current_position[X_AXIS], current_position[Y_AXIS]);
  235. else
  236. location = find_closest_circle_to_print(x_pos, y_pos); // Find the closest Mesh Intersection to where we are now.
  237. if (location.x_index >= 0 && location.y_index >= 0) {
  238. circle_x = ubl.map_x_index_to_bed_location(location.x_index);
  239. circle_y = ubl.map_y_index_to_bed_location(location.y_index);
  240. // Let's do a couple of quick sanity checks. We can pull this code out later if we never see it catch a problem
  241. #ifdef DELTA
  242. if (HYPOT2(circle_x, circle_y) > sq(DELTA_PRINTABLE_RADIUS)) {
  243. SERIAL_ERROR_START;
  244. SERIAL_ERRORLNPGM("Attempt to print outside of DELTA_PRINTABLE_RADIUS.");
  245. goto LEAVE;
  246. }
  247. #endif
  248. // TODO: Change this to use `position_is_reachable`
  249. if (circle_x < (X_MIN_POS) || circle_x > (X_MAX_POS) || circle_y < (Y_MIN_POS) || circle_y > (Y_MAX_POS)) {
  250. SERIAL_ERROR_START;
  251. SERIAL_ERRORLNPGM("Attempt to print off the bed.");
  252. goto LEAVE;
  253. }
  254. xi = location.x_index; // Just to shrink the next few lines and make them easier to understand
  255. yi = location.y_index;
  256. if (ubl.g26_debug_flag) {
  257. SERIAL_ECHOPAIR(" Doing circle at: (xi=", xi);
  258. SERIAL_ECHOPAIR(", yi=", yi);
  259. SERIAL_CHAR(')');
  260. SERIAL_EOL;
  261. }
  262. start_angle = 0.0; // assume it is going to be a full circle
  263. end_angle = 360.0;
  264. if (xi == 0) { // Check for bottom edge
  265. start_angle = -90.0;
  266. end_angle = 90.0;
  267. if (yi == 0) // it is an edge, check for the two left corners
  268. start_angle = 0.0;
  269. else if (yi == UBL_MESH_NUM_Y_POINTS - 1)
  270. end_angle = 0.0;
  271. }
  272. else if (xi == UBL_MESH_NUM_X_POINTS - 1) { // Check for top edge
  273. start_angle = 90.0;
  274. end_angle = 270.0;
  275. if (yi == 0) // it is an edge, check for the two right corners
  276. end_angle = 180.0;
  277. else if (yi == UBL_MESH_NUM_Y_POINTS - 1)
  278. start_angle = 180.0;
  279. }
  280. else if (yi == 0) {
  281. start_angle = 0.0; // only do the top side of the cirlce
  282. end_angle = 180.0;
  283. }
  284. else if (yi == UBL_MESH_NUM_Y_POINTS - 1) {
  285. start_angle = 180.0; // only do the bottom side of the cirlce
  286. end_angle = 360.0;
  287. }
  288. for (tmp = start_angle; tmp < end_angle - 0.1; tmp += 30.0) {
  289. int tmp_div_30 = tmp / 30.0;
  290. if (tmp_div_30 < 0) tmp_div_30 += 360 / 30;
  291. x = circle_x + cos_table[tmp_div_30]; // for speed, these are now a lookup table entry
  292. y = circle_y + sin_table[tmp_div_30];
  293. if (tmp_div_30 > 11) tmp_div_30 -= 360 / 30;
  294. xe = circle_x + cos_table[tmp_div_30 + 1]; // for speed, these are now a lookup table entry
  295. ye = circle_y + sin_table[tmp_div_30 + 1];
  296. #ifdef DELTA
  297. if (HYPOT2(x, y) > sq(DELTA_PRINTABLE_RADIUS)) // Check to make sure this part of
  298. continue; // the 'circle' is on the bed. If
  299. #else // not, we need to skip
  300. x = constrain(x, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  301. y = constrain(y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  302. xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1);
  303. ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
  304. #endif
  305. //if (ubl.g26_debug_flag) {
  306. // char ccc, *cptr, seg_msg[50], seg_num[10];
  307. // strcpy(seg_msg, " segment: ");
  308. // strcpy(seg_num, " \n");
  309. // cptr = (char*) "01234567890ABCDEF????????";
  310. // ccc = cptr[tmp_div_30];
  311. // seg_num[1] = ccc;
  312. // strcat(seg_msg, seg_num);
  313. // debug_current_and_destination(seg_msg);
  314. //}
  315. print_line_from_here_to_there(x, y, layer_height, xe, ye, layer_height);
  316. }
  317. //lcd_init_counter++;
  318. //if (lcd_init_counter > 10) {
  319. // lcd_init_counter = 0;
  320. // lcd_init(); // Some people's LCD Displays are locking up. This might help them
  321. // ubl.has_control_of_lcd_panel = true; // Make sure UBL still is controlling the LCD Panel
  322. //}
  323. //debug_current_and_destination((char*)"Looking for lines to connect.");
  324. look_for_lines_to_connect();
  325. //debug_current_and_destination((char*)"Done with line connect.");
  326. }
  327. //debug_current_and_destination((char*)"Done with current circle.");
  328. }
  329. while (location.x_index >= 0 && location.y_index >= 0);
  330. LEAVE:
  331. lcd_reset_alert_level();
  332. lcd_setstatuspgm(PSTR("Leaving G26"));
  333. retract_filament();
  334. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;
  335. //debug_current_and_destination((char*)"ready to do Z-Raise.");
  336. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Raise the nozzle
  337. //debug_current_and_destination((char*)"done doing Z-Raise.");
  338. destination[X_AXIS] = x_pos; // Move back to the starting position
  339. destination[Y_AXIS] = y_pos;
  340. //destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Keep the nozzle where it is
  341. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Move back to the starting position
  342. //debug_current_and_destination((char*)"done doing X/Y move.");
  343. ubl.has_control_of_lcd_panel = false; // Give back control of the LCD Panel!
  344. if (!keep_heaters_on) {
  345. #if HAS_TEMP_BED
  346. thermalManager.setTargetBed(0.0);
  347. #endif
  348. thermalManager.setTargetHotend(0.0, 0);
  349. }
  350. }
  351. float valid_trig_angle(float d) {
  352. while (d > 360.0) d -= 360.0;
  353. while (d < 0.0) d += 360.0;
  354. return d;
  355. }
  356. mesh_index_pair find_closest_circle_to_print( float X, float Y) {
  357. float f, mx, my, dx, dy, closest = 99999.99;
  358. mesh_index_pair return_val;
  359. return_val.x_index = return_val.y_index = -1;
  360. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  361. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  362. if (!is_bit_set(circle_flags, i, j)) {
  363. mx = ubl.map_x_index_to_bed_location(i); // We found a circle that needs to be printed
  364. my = ubl.map_y_index_to_bed_location(j);
  365. dx = X - mx; // Get the distance to this intersection
  366. dy = Y - my;
  367. f = HYPOT(dx, dy);
  368. dx = x_pos - mx; // It is possible that we are being called with the values
  369. dy = y_pos - my; // to let us find the closest circle to the start position.
  370. f += HYPOT(dx, dy) / 15.0; // But if this is not the case,
  371. // we are going to add in a small
  372. // weighting to the distance calculation to help it choose
  373. // a better place to continue.
  374. if (random_deviation > 1.0)
  375. f += random(0.0, random_deviation); // Add in the specified amount of Random Noise to our search
  376. if (f < closest) {
  377. closest = f; // We found a closer location that is still
  378. return_val.x_index = i; // un-printed --- save the data for it
  379. return_val.y_index = j;
  380. return_val.distance= closest;
  381. }
  382. }
  383. }
  384. }
  385. bit_set(circle_flags, return_val.x_index, return_val.y_index); // Mark this location as done.
  386. return return_val;
  387. }
  388. void look_for_lines_to_connect() {
  389. float sx, sy, ex, ey;
  390. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  391. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  392. if (i < UBL_MESH_NUM_X_POINTS) { // We can't connect to anything to the right than UBL_MESH_NUM_X_POINTS.
  393. // This is already a half circle because we are at the edge of the bed.
  394. if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i + 1, j)) { // check if we can do a line to the left
  395. if (!is_bit_set(horizontal_mesh_line_flags, i, j)) {
  396. //
  397. // We found two circles that need a horizontal line to connect them
  398. // Print it!
  399. //
  400. sx = ubl.map_x_index_to_bed_location(i);
  401. sx = sx + SIZE_OF_INTERSECTION_CIRCLES - SIZE_OF_CROSS_HAIRS; // get the right edge of the circle
  402. sy = ubl.map_y_index_to_bed_location(j);
  403. ex = ubl.map_x_index_to_bed_location(i + 1);
  404. ex = ex - SIZE_OF_INTERSECTION_CIRCLES + SIZE_OF_CROSS_HAIRS; // get the left edge of the circle
  405. ey = sy;
  406. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  407. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  408. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  409. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  410. if (ubl.g26_debug_flag) {
  411. SERIAL_ECHOPAIR(" Connecting with horizontal line (sx=", sx);
  412. SERIAL_ECHOPAIR(", sy=", sy);
  413. SERIAL_ECHOPAIR(") -> (ex=", ex);
  414. SERIAL_ECHOPAIR(", ey=", ey);
  415. SERIAL_CHAR(')');
  416. SERIAL_EOL;
  417. //debug_current_and_destination((char*)"Connecting horizontal line.");
  418. }
  419. print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
  420. bit_set(horizontal_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  421. }
  422. }
  423. if (j < UBL_MESH_NUM_Y_POINTS) { // We can't connect to anything further back than UBL_MESH_NUM_Y_POINTS.
  424. // This is already a half circle because we are at the edge of the bed.
  425. if (is_bit_set(circle_flags, i, j) && is_bit_set(circle_flags, i, j + 1)) { // check if we can do a line straight down
  426. if (!is_bit_set( vertical_mesh_line_flags, i, j)) {
  427. //
  428. // We found two circles that need a vertical line to connect them
  429. // Print it!
  430. //
  431. sx = ubl.map_x_index_to_bed_location(i);
  432. sy = ubl.map_y_index_to_bed_location(j);
  433. sy = sy + SIZE_OF_INTERSECTION_CIRCLES - SIZE_OF_CROSS_HAIRS; // get the top edge of the circle
  434. ex = sx;
  435. ey = ubl.map_y_index_to_bed_location(j + 1);
  436. ey = ey - SIZE_OF_INTERSECTION_CIRCLES + SIZE_OF_CROSS_HAIRS; // get the bottom edge of the circle
  437. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  438. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  439. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  440. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  441. if (ubl.g26_debug_flag) {
  442. SERIAL_ECHOPAIR(" Connecting with vertical line (sx=", sx);
  443. SERIAL_ECHOPAIR(", sy=", sy);
  444. SERIAL_ECHOPAIR(") -> (ex=", ex);
  445. SERIAL_ECHOPAIR(", ey=", ey);
  446. SERIAL_CHAR(')');
  447. SERIAL_EOL;
  448. debug_current_and_destination((char*)"Connecting vertical line.");
  449. }
  450. print_line_from_here_to_there(sx, sy, layer_height, ex, ey, layer_height);
  451. bit_set( vertical_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. void move_to(const float &x, const float &y, const float &z, const float &e_delta) {
  460. float feed_value;
  461. static float last_z = -999.99;
  462. bool has_xy_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
  463. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() has_xy_component:", (int)has_xy_component);
  464. if (z != last_z) {
  465. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() changing Z to ", (int)z);
  466. last_z = z;
  467. feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0); // Base the feed rate off of the configured Z_AXIS feed rate
  468. destination[X_AXIS] = current_position[X_AXIS];
  469. destination[Y_AXIS] = current_position[Y_AXIS];
  470. destination[Z_AXIS] = z; // We know the last_z==z or we wouldn't be in this block of code.
  471. destination[E_AXIS] = current_position[E_AXIS];
  472. ubl_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
  473. stepper.synchronize();
  474. set_destination_to_current();
  475. //if (ubl.g26_debug_flag) debug_current_and_destination((char*)" in move_to() done with Z move");
  476. }
  477. // Check if X or Y is involved in the movement.
  478. // Yes: a 'normal' movement. No: a retract() or un_retract()
  479. feed_value = has_xy_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
  480. if (ubl.g26_debug_flag) SERIAL_ECHOLNPAIR("in move_to() feed_value for XY:", feed_value);
  481. destination[X_AXIS] = x;
  482. destination[Y_AXIS] = y;
  483. destination[E_AXIS] += e_delta;
  484. //if (ubl.g26_debug_flag) debug_current_and_destination((char*)" in move_to() doing last move");
  485. ubl_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
  486. //if (ubl.g26_debug_flag) debug_current_and_destination((char*)" in move_to() after last move");
  487. stepper.synchronize();
  488. set_destination_to_current();
  489. }
  490. void retract_filament() {
  491. if (!g26_retracted) { // Only retract if we are not already retracted!
  492. g26_retracted = true;
  493. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Decided to do retract.");
  494. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], -1.0 * retraction_multiplier);
  495. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Retraction done.");
  496. }
  497. }
  498. void un_retract_filament() {
  499. if (g26_retracted) { // Only un-retract if we are retracted.
  500. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 1.2 * retraction_multiplier);
  501. g26_retracted = false;
  502. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" unretract done.");
  503. }
  504. }
  505. /**
  506. * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one
  507. * to the other. But there are really three sets of coordinates involved. The first coordinate
  508. * is the present location of the nozzle. We don't necessarily want to print from this location.
  509. * We first need to move the nozzle to the start of line segment where we want to print. Once
  510. * there, we can use the two coordinates supplied to draw the line.
  511. *
  512. * Note: Although we assume the first set of coordinates is the start of the line and the second
  513. * set of coordinates is the end of the line, it does not always work out that way. This function
  514. * optimizes the movement to minimize the travel distance before it can start printing. This saves
  515. * a lot of time and eleminates a lot of non-sensical movement of the nozzle. However, it does
  516. * cause a lot of very little short retracement of th nozzle when it draws the very first line
  517. * segment of a 'circle'. The time this requires is very short and is easily saved by the other
  518. * cases where the optimization comes into play.
  519. */
  520. void print_line_from_here_to_there( float sx, float sy, float sz, float ex, float ey, float ez) {
  521. const float dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual line segment
  522. dy_s = current_position[Y_AXIS] - sy,
  523. dist_start = HYPOT2(dx_s, dy_s), // We don't need to do a sqrt(), we can compare the distance^2
  524. // to save computation time
  525. dx_e = current_position[X_AXIS] - ex, // find our distance from the end of the actual line segment
  526. dy_e = current_position[Y_AXIS] - ey,
  527. dist_end = HYPOT2(dx_e, dy_e),
  528. dx = ex - sx,
  529. dy = ey - sy,
  530. line_length = HYPOT(dx, dy);
  531. // If the end point of the line is closer to the nozzle, we are going to
  532. // flip the direction of this line. We will print it from the end to the start.
  533. // On very small lines we don't do the optimization because it just isn't worth it.
  534. //
  535. if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < abs(line_length)) {
  536. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" Reversing start and end of print_line_from_here_to_there()");
  537. print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
  538. return;
  539. }
  540. // Now decide if we should retract.
  541. if (dist_start > 2.0) {
  542. retract_filament();
  543. //if (ubl.g26_debug_flag) SERIAL_ECHOLNPGM(" filament retracted.");
  544. }
  545. move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion
  546. const float e_pos_delta = line_length * g26_e_axis_feedrate * extrusion_multiplier;
  547. un_retract_filament();
  548. //if (ubl.g26_debug_flag) {
  549. // SERIAL_ECHOLNPGM(" doing printing move.");
  550. // debug_current_and_destination((char*)"doing final move_to() inside print_line_from_here_to_there()");
  551. //}
  552. move_to(ex, ey, ez, e_pos_delta); // Get to the ending point with an appropriate amount of extrusion
  553. }
  554. /**
  555. * This function used to be inline code in G26. But there are so many
  556. * parameters it made sense to turn them into static globals and get
  557. * this code out of sight of the main routine.
  558. */
  559. bool parse_G26_parameters() {
  560. extrusion_multiplier = EXTRUSION_MULTIPLIER;
  561. retraction_multiplier = RETRACTION_MULTIPLIER;
  562. nozzle = NOZZLE;
  563. filament_diameter = FILAMENT;
  564. layer_height = LAYER_HEIGHT;
  565. prime_length = PRIME_LENGTH;
  566. bed_temp = BED_TEMP;
  567. hotend_temp = HOTEND_TEMP;
  568. ooze_amount = OOZE_AMOUNT;
  569. prime_flag = 0;
  570. keep_heaters_on = false;
  571. if (code_seen('B')) {
  572. bed_temp = code_value_float();
  573. if (bed_temp < 15.0 || bed_temp > 140.0) {
  574. SERIAL_PROTOCOLLNPGM("?Specified bed temperature not plausible.");
  575. return UBL_ERR;
  576. }
  577. }
  578. if (code_seen('C')) continue_with_closest++;
  579. if (code_seen('L')) {
  580. layer_height = code_value_float();
  581. if (layer_height < 0.0 || layer_height > 2.0) {
  582. SERIAL_PROTOCOLLNPGM("?Specified layer height not plausible.");
  583. return UBL_ERR;
  584. }
  585. }
  586. if (code_seen('Q')) {
  587. if (code_has_value()) {
  588. retraction_multiplier = code_value_float();
  589. if (retraction_multiplier < 0.05 || retraction_multiplier > 15.0) {
  590. SERIAL_PROTOCOLLNPGM("?Specified Retraction Multiplier not plausible.");
  591. return UBL_ERR;
  592. }
  593. }
  594. else {
  595. SERIAL_PROTOCOLLNPGM("?Retraction Multiplier must be specified.");
  596. return UBL_ERR;
  597. }
  598. }
  599. if (code_seen('N')) {
  600. nozzle = code_value_float();
  601. if (nozzle < 0.1 || nozzle > 1.0) {
  602. SERIAL_PROTOCOLLNPGM("?Specified nozzle size not plausible.");
  603. return UBL_ERR;
  604. }
  605. }
  606. if (code_seen('K')) keep_heaters_on++;
  607. if (code_seen('O') && code_has_value())
  608. ooze_amount = code_value_float();
  609. if (code_seen('P')) {
  610. if (!code_has_value())
  611. prime_flag = -1;
  612. else {
  613. prime_flag++;
  614. prime_length = code_value_float();
  615. if (prime_length < 0.0 || prime_length > 25.0) {
  616. SERIAL_PROTOCOLLNPGM("?Specified prime length not plausible.");
  617. return UBL_ERR;
  618. }
  619. }
  620. }
  621. if (code_seen('F')) {
  622. filament_diameter = code_value_float();
  623. if (filament_diameter < 1.0 || filament_diameter > 4.0) {
  624. SERIAL_PROTOCOLLNPGM("?Specified filament size not plausible.");
  625. return UBL_ERR;
  626. }
  627. }
  628. extrusion_multiplier *= sq(1.75) / sq(filament_diameter); // If we aren't using 1.75mm filament, we need to
  629. // scale up or down the length needed to get the
  630. // same volume of filament
  631. extrusion_multiplier *= filament_diameter * sq(nozzle) / sq(0.3); // Scale up by nozzle size
  632. if (code_seen('H')) {
  633. hotend_temp = code_value_float();
  634. if (hotend_temp < 165.0 || hotend_temp > 280.0) {
  635. SERIAL_PROTOCOLLNPGM("?Specified nozzle temperature not plausible.");
  636. return UBL_ERR;
  637. }
  638. }
  639. if (code_seen('R')) {
  640. randomSeed(millis());
  641. random_deviation = code_has_value() ? code_value_float() : 50.0;
  642. }
  643. x_pos = current_position[X_AXIS];
  644. y_pos = current_position[Y_AXIS];
  645. if (code_seen('X')) {
  646. x_pos = code_value_float();
  647. if (x_pos < X_MIN_POS || x_pos > X_MAX_POS) {
  648. SERIAL_PROTOCOLLNPGM("?Specified X coordinate not plausible.");
  649. return UBL_ERR;
  650. }
  651. }
  652. else
  653. if (code_seen('Y')) {
  654. y_pos = code_value_float();
  655. if (y_pos < Y_MIN_POS || y_pos > Y_MAX_POS) {
  656. SERIAL_PROTOCOLLNPGM("?Specified Y coordinate not plausible.");
  657. return UBL_ERR;
  658. }
  659. }
  660. /**
  661. * We save the question of what to do with the Unified Bed Leveling System's Activation until the very
  662. * end. The reason is, if one of the parameters specified up above is incorrect, we don't want to
  663. * alter the system's status. We wait until we know everything is correct before altering the state
  664. * of the system.
  665. */
  666. ubl.state.active = !code_seen('D');
  667. return UBL_OK;
  668. }
  669. bool exit_from_g26() {
  670. //strcpy(lcd_status_message, "Leaving G26"); // We can't do lcd_setstatus() without having it continue;
  671. lcd_reset_alert_level();
  672. lcd_setstatuspgm(PSTR("Leaving G26"));
  673. while (ubl_lcd_clicked()) idle();
  674. return UBL_ERR;
  675. }
  676. /**
  677. * Turn on the bed and nozzle heat and
  678. * wait for them to get up to temperature.
  679. */
  680. bool turn_on_heaters() {
  681. #if HAS_TEMP_BED
  682. #if ENABLED(ULTRA_LCD)
  683. if (bed_temp > 25) {
  684. lcd_setstatuspgm(PSTR("G26 Heating Bed."), 99);
  685. lcd_quick_feedback();
  686. #endif
  687. ubl.has_control_of_lcd_panel = true;
  688. thermalManager.setTargetBed(bed_temp);
  689. while (abs(thermalManager.degBed() - bed_temp) > 3) {
  690. if (ubl_lcd_clicked()) return exit_from_g26();
  691. idle();
  692. }
  693. #if ENABLED(ULTRA_LCD)
  694. }
  695. lcd_setstatuspgm(PSTR("G26 Heating Nozzle."), 99);
  696. lcd_quick_feedback();
  697. #endif
  698. #endif
  699. // Start heating the nozzle and wait for it to reach temperature.
  700. thermalManager.setTargetHotend(hotend_temp, 0);
  701. while (abs(thermalManager.degHotend(0) - hotend_temp) > 3) {
  702. if (ubl_lcd_clicked()) return exit_from_g26();
  703. idle();
  704. }
  705. #if ENABLED(ULTRA_LCD)
  706. lcd_reset_alert_level();
  707. lcd_setstatuspgm(PSTR(""));
  708. lcd_quick_feedback();
  709. #endif
  710. return UBL_OK;
  711. }
  712. /**
  713. * Prime the nozzle if needed. Return true on error.
  714. */
  715. bool prime_nozzle() {
  716. float Total_Prime = 0.0;
  717. if (prime_flag == -1) { // The user wants to control how much filament gets purged
  718. lcd_setstatuspgm(PSTR("User-Controlled Prime"), 99);
  719. chirp_at_user();
  720. set_destination_to_current();
  721. un_retract_filament(); // Lets make sure the G26 command doesn't think the filament is
  722. // retracted(). We are here because we want to prime the nozzle.
  723. // So let's just unretract just to be sure.
  724. while (!ubl_lcd_clicked()) {
  725. chirp_at_user();
  726. destination[E_AXIS] += 0.25;
  727. #ifdef PREVENT_LENGTHY_EXTRUDE
  728. Total_Prime += 0.25;
  729. if (Total_Prime >= EXTRUDE_MAXLENGTH) return UBL_ERR;
  730. #endif
  731. ubl_line_to_destination(
  732. destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
  733. planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
  734. );
  735. stepper.synchronize(); // Without this synchronize, the purge is more consistent,
  736. // but because the planner has a buffer, we won't be able
  737. // to stop as quickly. So we put up with the less smooth
  738. // action to give the user a more responsive 'Stop'.
  739. set_destination_to_current();
  740. idle();
  741. }
  742. while (ubl_lcd_clicked()) idle(); // Debounce Encoder Wheel
  743. #if ENABLED(ULTRA_LCD)
  744. strcpy_P(lcd_status_message, PSTR("Done Priming")); // We can't do lcd_setstatuspgm() without having it continue;
  745. // So... We cheat to get a message up.
  746. lcd_setstatuspgm(PSTR("Done Priming"), 99);
  747. lcd_quick_feedback();
  748. #endif
  749. }
  750. else {
  751. #if ENABLED(ULTRA_LCD)
  752. lcd_setstatuspgm(PSTR("Fixed Length Prime."), 99);
  753. lcd_quick_feedback();
  754. #endif
  755. set_destination_to_current();
  756. destination[E_AXIS] += prime_length;
  757. ubl_line_to_destination(
  758. destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
  759. planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
  760. );
  761. stepper.synchronize();
  762. set_destination_to_current();
  763. retract_filament();
  764. }
  765. return UBL_OK;
  766. }
  767. #endif // AUTO_BED_LEVELING_UBL && UBL_G26_MESH_EDITING