My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

G26_Mesh_Validation_Tool.cpp 40KB

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