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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 OOOOZE_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 int 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 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 E_Pos_Delta,
  153. Extrusion_Multiplier = EXTRUSION_MULTIPLIER,
  154. Retraction_Multiplier = RETRACTION_MULTIPLIER,
  155. Nozzle = NOZZLE,
  156. Filament = FILAMENT,
  157. Prime_Length = PRIME_LENGTH,
  158. X_Pos, Y_Pos,
  159. bed_temp = BED_TEMP,
  160. hotend_temp = HOTEND_TEMP,
  161. Ooooze_Amount = OOOOZE_AMOUNT;
  162. int8_t Prime_Flag = 0;
  163. bool Keep_Heaters_On = false,
  164. G26_Debug_flag = false;
  165. /**
  166. * These support functions allow the use of large bit arrays of flags that take very
  167. * little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
  168. * to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
  169. * in the future.
  170. */
  171. void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
  172. void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
  173. bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
  174. /**
  175. * G26: Mesh Validation Pattern generation.
  176. *
  177. * Used to interactively edit UBL's Mesh by placing the
  178. * nozzle in a problem area and doing a G29 P4 R command.
  179. */
  180. void gcode_G26() {
  181. float circle_x, circle_y, x, y, xe, ye, tmp,
  182. start_angle, end_angle;
  183. int i, xi, yi, lcd_init_counter = 0;
  184. mesh_index_pair location;
  185. if (axis_unhomed_error(true, true, true)) // Don't allow Mesh Validation without homing first
  186. gcode_G28();
  187. if (parse_G26_parameters()) return; // If the paramter parsing did not go OK, we abort the command
  188. if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
  189. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  190. stepper.synchronize();
  191. set_current_to_destination();
  192. }
  193. if (turn_on_heaters()) // Turn on the heaters, leave the command if anything
  194. goto LEAVE; // has gone wrong.
  195. axis_relative_modes[E_AXIS] = false; // Get things setup so we can take control of the
  196. //relative_mode = false; // planner and stepper motors!
  197. current_position[E_AXIS] = 0.0;
  198. sync_plan_position_e();
  199. if (Prime_Flag && prime_nozzle()) // if prime_nozzle() returns an error, we just bail out.
  200. goto LEAVE;
  201. /**
  202. * Bed is preheated
  203. *
  204. * Nozzle is at temperature
  205. *
  206. * Filament is primed!
  207. *
  208. * It's "Show Time" !!!
  209. */
  210. // Clear all of the flags we need
  211. ZERO(circle_flags);
  212. ZERO(horizontal_mesh_line_flags);
  213. ZERO(vertical_mesh_line_flags);
  214. //
  215. // Move nozzle to the specified height for the first layer
  216. //
  217. set_destination_to_current();
  218. destination[Z_AXIS] = Layer_Height;
  219. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0.0);
  220. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], Ooooze_Amount);
  221. UBL_has_control_of_LCD_Panel = 1; // Take control of the LCD Panel!
  222. debug_current_and_destination((char *)"Starting G26 Mesh Validation Pattern.");
  223. do {
  224. if (G29_lcd_clicked()) { // 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. while (G29_lcd_clicked()) idle(); // Debounce the switch click
  227. #if ENABLED(ULTRA_LCD)
  228. lcd_setstatus("Mesh Validation Stopped.", true);
  229. lcd_quick_feedback();
  230. #endif
  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 = blm.map_x_index_to_bed_location(location.x_index);
  239. circle_y = blm.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_PROTOCOLLNPGM("?Error: Attempt to print outside of DELTA_PRINTABLE_RADIUS.");
  244. goto LEAVE;
  245. }
  246. #endif
  247. if (circle_x < X_MIN_POS || circle_x > X_MAX_POS || circle_y < Y_MIN_POS || circle_y > Y_MAX_POS) {
  248. SERIAL_PROTOCOLLNPGM("?Error: Attempt to print off the bed.");
  249. goto LEAVE;
  250. }
  251. xi = location.x_index; // Just to shrink the next few lines and make them easier to understand
  252. yi = location.y_index;
  253. if (G26_Debug_flag) {
  254. SERIAL_ECHOPGM(" Doing circle at: (xi=");
  255. SERIAL_ECHO(xi);
  256. SERIAL_ECHOPGM(", yi=");
  257. SERIAL_ECHO(yi);
  258. SERIAL_ECHOLNPGM(")");
  259. }
  260. start_angle = 0.0; // assume it is going to be a full circle
  261. end_angle = 360.0;
  262. if (xi == 0) { // Check for bottom edge
  263. start_angle = -90.0;
  264. end_angle = 90.0;
  265. if (yi == 0) // it is an edge, check for the two left corners
  266. start_angle = 0.0;
  267. else if (yi == UBL_MESH_NUM_Y_POINTS - 1)
  268. end_angle = 0.0;
  269. }
  270. else if (xi == UBL_MESH_NUM_X_POINTS - 1) { // Check for top edge
  271. start_angle = 90.0;
  272. end_angle = 270.0;
  273. if (yi == 0) // it is an edge, check for the two right corners
  274. end_angle = 180.0;
  275. else if (yi == UBL_MESH_NUM_Y_POINTS - 1)
  276. start_angle = 180.0;
  277. }
  278. else if (yi == 0) {
  279. start_angle = 0.0; // only do the top side of the cirlce
  280. end_angle = 180.0;
  281. }
  282. else if (yi == UBL_MESH_NUM_Y_POINTS - 1) {
  283. start_angle = 180.0; // only do the bottom side of the cirlce
  284. end_angle = 360.0;
  285. }
  286. /**
  287. * Declare and generate a sin() & cos() table to be used during the circle drawing. This will lighten
  288. * the CPU load and make the arc drawing faster and more smooth
  289. */
  290. float sin_table[360 / 30 + 1], cos_table[360 / 30 + 1];
  291. int tmp_div_30;
  292. for (i = 0; i <= 360 / 30; i++) {
  293. cos_table[i] = SIZE_OF_INTERSECTION_CIRCLES * cos(RADIANS(valid_trig_angle(i * 30.0)));
  294. sin_table[i] = SIZE_OF_INTERSECTION_CIRCLES * sin(RADIANS(valid_trig_angle(i * 30.0)));
  295. }
  296. for (tmp = start_angle; tmp < end_angle - 0.1; tmp += 30.0) {
  297. tmp_div_30 = tmp / 30.0;
  298. if (tmp_div_30 < 0) tmp_div_30 += 360 / 30;
  299. x = circle_x + cos_table[tmp_div_30]; // for speed, these are now a lookup table entry
  300. y = circle_y + sin_table[tmp_div_30];
  301. if (tmp_div_30 > 11) tmp_div_30 -= 360 / 30;
  302. xe = circle_x + cos_table[tmp_div_30 + 1]; // for speed, these are now a lookup table entry
  303. ye = circle_y + sin_table[tmp_div_30 + 1];
  304. #ifdef DELTA
  305. if (HYPOT2(x, y) > sq(DELTA_PRINTABLE_RADIUS)) // Check to make sure this part of
  306. continue; // the 'circle' is on the bed. If
  307. #else // not, we need to skip
  308. x = constrain(x, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  309. y = constrain(y, Y_MIN_POS + 1, Y_MAX_POS - 1);
  310. xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1);
  311. ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1);
  312. #endif
  313. if (G26_Debug_flag) {
  314. char ccc, *cptr, seg_msg[50], seg_num[10];
  315. strcpy(seg_msg, " segment: ");
  316. strcpy(seg_num, " \n");
  317. cptr = (char *) "01234567890ABCDEF????????";
  318. ccc = cptr[tmp_div_30];
  319. seg_num[1] = ccc;
  320. strcat(seg_msg, seg_num);
  321. debug_current_and_destination(seg_msg);
  322. }
  323. print_line_from_here_to_there(x, y, Layer_Height, xe, ye, Layer_Height);
  324. }
  325. lcd_init_counter++;
  326. if (lcd_init_counter > 10) {
  327. lcd_init_counter = 0;
  328. lcd_init(); // Some people's LCD Displays are locking up. This might help them
  329. }
  330. debug_current_and_destination((char *)"Looking for lines to connect.");
  331. look_for_lines_to_connect();
  332. debug_current_and_destination((char *)"Done with line connect.");
  333. }
  334. debug_current_and_destination((char *)"Done with current circle.");
  335. }
  336. while (location.x_index >= 0 && location.y_index >= 0) ;
  337. LEAVE:
  338. retract_filament();
  339. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Raise the nozzle
  340. debug_current_and_destination((char *)"ready to do Z-Raise.");
  341. move_to( destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Raise the nozzle
  342. debug_current_and_destination((char *)"done doing Z-Raise.");
  343. destination[X_AXIS] = X_Pos; // Move back to the starting position
  344. destination[Y_AXIS] = Y_Pos;
  345. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES; // Keep the nozzle where it is
  346. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 0); // Move back to the starting position
  347. debug_current_and_destination((char *)"done doing X/Y move.");
  348. UBL_has_control_of_LCD_Panel = 0; // Give back control of the LCD Panel!
  349. if (!Keep_Heaters_On) {
  350. #if HAS_TEMP_BED
  351. thermalManager.setTargetBed(0.0);
  352. #endif
  353. thermalManager.setTargetHotend(0.0, 0);
  354. }
  355. lcd_init(); // Some people's LCD Displays are locking up. This might help them
  356. }
  357. float valid_trig_angle(float d) {
  358. while (d > 360.0) d -= 360.0;
  359. while (d < 0.0) d += 360.0;
  360. return d;
  361. }
  362. mesh_index_pair find_closest_circle_to_print( float X, float Y) {
  363. float f, mx, my, dx, dy, closest = 99999.99;
  364. mesh_index_pair return_val;
  365. return_val.x_index = return_val.y_index = -1;
  366. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  367. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  368. if (!is_bit_set(circle_flags, i, j)) {
  369. mx = blm.map_x_index_to_bed_location(i); // We found a circle that needs to be printed
  370. my = blm.map_y_index_to_bed_location(j);
  371. dx = X - mx; // Get the distance to this intersection
  372. dy = Y - my;
  373. f = HYPOT(dx, dy);
  374. dx = X_Pos - mx; // It is possible that we are being called with the values
  375. dy = Y_Pos - my; // to let us find the closest circle to the start position.
  376. f += HYPOT(dx, dy) / 15.0; // But if this is not the case,
  377. // we are going to add in a small
  378. // weighting to the distance calculation to help it choose
  379. // a better place to continue.
  380. if (Random_Deviation > 1.0)
  381. f += random(0.0, Random_Deviation); // Add in the specified amount of Random Noise to our search
  382. if (f < closest) {
  383. closest = f; // We found a closer location that is still
  384. return_val.x_index = i; // un-printed --- save the data for it
  385. return_val.y_index = j;
  386. return_val.distance= closest;
  387. }
  388. }
  389. }
  390. }
  391. bit_set(circle_flags, return_val.x_index, return_val.y_index); // Mark this location as done.
  392. return return_val;
  393. }
  394. void look_for_lines_to_connect() {
  395. float sx, sy, ex, ey;
  396. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  397. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  398. if (i < UBL_MESH_NUM_X_POINTS) { // We can't connect to anything to the right than UBL_MESH_NUM_X_POINTS.
  399. // This is already a half circle because we are at the edge of the bed.
  400. 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
  401. if (!is_bit_set(horizontal_mesh_line_flags, i, j)) {
  402. //
  403. // We found two circles that need a horizontal line to connect them
  404. // Print it!
  405. //
  406. sx = blm.map_x_index_to_bed_location(i);
  407. sx = sx + SIZE_OF_INTERSECTION_CIRCLES - SIZE_OF_CROSS_HAIRS; // get the right edge of the circle
  408. sy = blm.map_y_index_to_bed_location(j);
  409. ex = blm.map_x_index_to_bed_location(i + 1);
  410. ex = ex - SIZE_OF_INTERSECTION_CIRCLES + SIZE_OF_CROSS_HAIRS; // get the left edge of the circle
  411. ey = sy;
  412. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  413. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  414. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  415. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  416. if (G26_Debug_flag) {
  417. SERIAL_ECHOPGM(" Connecting with horizontal line (sx=");
  418. SERIAL_ECHO(sx);
  419. SERIAL_ECHOPGM(", sy=");
  420. SERIAL_ECHO(sy);
  421. SERIAL_ECHOPGM(") -> (ex=");
  422. SERIAL_ECHO(ex);
  423. SERIAL_ECHOPGM(", ey=");
  424. SERIAL_ECHO(ey);
  425. SERIAL_ECHOLNPGM(")");
  426. debug_current_and_destination((char *)"Connecting horizontal line.");
  427. }
  428. print_line_from_here_to_there(sx, sy, Layer_Height, ex, ey, Layer_Height);
  429. bit_set(horizontal_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  430. }
  431. }
  432. if (j < UBL_MESH_NUM_Y_POINTS) { // We can't connect to anything further back than UBL_MESH_NUM_Y_POINTS.
  433. // This is already a half circle because we are at the edge of the bed.
  434. 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
  435. if (!is_bit_set( vertical_mesh_line_flags, i, j)) {
  436. //
  437. // We found two circles that need a vertical line to connect them
  438. // Print it!
  439. //
  440. sx = blm.map_x_index_to_bed_location(i);
  441. sy = blm.map_y_index_to_bed_location(j);
  442. sy = sy + SIZE_OF_INTERSECTION_CIRCLES - SIZE_OF_CROSS_HAIRS; // get the top edge of the circle
  443. ex = sx;
  444. ey = blm.map_y_index_to_bed_location(j + 1);
  445. ey = ey - SIZE_OF_INTERSECTION_CIRCLES + SIZE_OF_CROSS_HAIRS; // get the bottom edge of the circle
  446. sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops
  447. sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1);
  448. ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1);
  449. ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1);
  450. if (G26_Debug_flag) {
  451. SERIAL_ECHOPGM(" Connecting with vertical line (sx=");
  452. SERIAL_ECHO(sx);
  453. SERIAL_ECHOPGM(", sy=");
  454. SERIAL_ECHO(sy);
  455. SERIAL_ECHOPGM(") -> (ex=");
  456. SERIAL_ECHO(ex);
  457. SERIAL_ECHOPGM(", ey=");
  458. SERIAL_ECHO(ey);
  459. SERIAL_ECHOLNPGM(")");
  460. debug_current_and_destination((char *)"Connecting vertical line.");
  461. }
  462. print_line_from_here_to_there(sx, sy, Layer_Height, ex, ey, Layer_Height);
  463. bit_set( vertical_mesh_line_flags, i, j); // Mark it as done so we don't do it again
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. void debug_current_and_destination(char *title) {
  472. float dx, dy, de, xy_dist, fpmm;
  473. // if the title message starts with a '!' it is so important, we are going to
  474. // ignore the status of the G26_Debug_Flag
  475. if (*title != '!' && !G26_Debug_flag) return;
  476. dx = current_position[X_AXIS] - destination[X_AXIS];
  477. dy = current_position[Y_AXIS] - destination[Y_AXIS];
  478. de = destination[E_AXIS] - current_position[E_AXIS];
  479. if (de == 0.0) return;
  480. xy_dist = HYPOT(dx, dy);
  481. if (xy_dist == 0.0) {
  482. return;
  483. //SERIAL_ECHOPGM(" FPMM=");
  484. //fpmm = de;
  485. //SERIAL_PROTOCOL_F(fpmm, 6);
  486. }
  487. else {
  488. SERIAL_ECHOPGM(" fpmm=");
  489. fpmm = de / xy_dist;
  490. SERIAL_PROTOCOL_F(fpmm, 6);
  491. }
  492. SERIAL_ECHOPGM(" current=( ");
  493. SERIAL_PROTOCOL_F(current_position[X_AXIS], 6);
  494. SERIAL_ECHOPGM(", ");
  495. SERIAL_PROTOCOL_F(current_position[Y_AXIS], 6);
  496. SERIAL_ECHOPGM(", ");
  497. SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
  498. SERIAL_ECHOPGM(", ");
  499. SERIAL_PROTOCOL_F(current_position[E_AXIS], 6);
  500. SERIAL_ECHOPGM(" ) destination=( ");
  501. if (current_position[X_AXIS] == destination[X_AXIS])
  502. SERIAL_ECHOPGM("-------------");
  503. else
  504. SERIAL_PROTOCOL_F(destination[X_AXIS], 6);
  505. SERIAL_ECHOPGM(", ");
  506. if (current_position[Y_AXIS] == destination[Y_AXIS])
  507. SERIAL_ECHOPGM("-------------");
  508. else
  509. SERIAL_PROTOCOL_F(destination[Y_AXIS], 6);
  510. SERIAL_ECHOPGM(", ");
  511. if (current_position[Z_AXIS] == destination[Z_AXIS])
  512. SERIAL_ECHOPGM("-------------");
  513. else
  514. SERIAL_PROTOCOL_F(destination[Z_AXIS], 6);
  515. SERIAL_ECHOPGM(", ");
  516. if (current_position[E_AXIS] == destination[E_AXIS])
  517. SERIAL_ECHOPGM("-------------");
  518. else
  519. SERIAL_PROTOCOL_F(destination[E_AXIS], 6);
  520. SERIAL_ECHOPGM(" ) ");
  521. SERIAL_ECHO(title);
  522. SERIAL_EOL;
  523. SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66. Right Switch is on pin 65
  524. //if (been_to_2_6) {
  525. //while ((digitalRead(66) & 0x01) != 0)
  526. // idle();
  527. //}
  528. }
  529. void move_to(const float &x, const float &y, const float &z, const float &e_delta) {
  530. float feed_value;
  531. static float last_z = -999.99;
  532. bool has_XY_component = (x != current_position[X_AXIS] || y != current_position[Y_AXIS]); // Check if X or Y is involved in the movement.
  533. if (G26_Debug_flag) {
  534. SERIAL_ECHOPAIR("in move_to() has_XY_component:", (int)has_XY_component);
  535. SERIAL_EOL;
  536. }
  537. if (z != last_z) {
  538. if (G26_Debug_flag) {
  539. SERIAL_ECHOPAIR("in move_to() changing Z to ", (int)z);
  540. SERIAL_EOL;
  541. }
  542. last_z = z;
  543. feed_value = planner.max_feedrate_mm_s[Z_AXIS]/(3.0); // Base the feed rate off of the configured Z_AXIS feed rate
  544. destination[X_AXIS] = current_position[X_AXIS];
  545. destination[Y_AXIS] = current_position[Y_AXIS];
  546. destination[Z_AXIS] = z; // We know the last_z==z or we wouldn't be in this block of code.
  547. destination[E_AXIS] = current_position[E_AXIS];
  548. UBL_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
  549. stepper.synchronize();
  550. set_destination_to_current();
  551. if (G26_Debug_flag)
  552. debug_current_and_destination((char *)" in move_to() done with Z move");
  553. }
  554. // Check if X or Y is involved in the movement.
  555. // Yes: a 'normal' movement. No: a retract() or un_retract()
  556. feed_value = has_XY_component ? PLANNER_XY_FEEDRATE() / 10.0 : planner.max_feedrate_mm_s[E_AXIS] / 1.5;
  557. if (G26_Debug_flag) {
  558. SERIAL_ECHOPAIR("in move_to() feed_value for XY:", feed_value);
  559. SERIAL_EOL;
  560. }
  561. destination[X_AXIS] = x;
  562. destination[Y_AXIS] = y;
  563. destination[E_AXIS] += e_delta;
  564. if (G26_Debug_flag)
  565. debug_current_and_destination((char *)" in move_to() doing last move");
  566. UBL_line_to_destination(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feed_value, 0);
  567. if (G26_Debug_flag)
  568. debug_current_and_destination((char *)" in move_to() after last move");
  569. stepper.synchronize();
  570. set_destination_to_current();
  571. }
  572. void retract_filament() {
  573. if (!retracted) { // Only retract if we are not already retracted!
  574. retracted = true;
  575. if (G26_Debug_flag) SERIAL_ECHOLNPGM(" Decided to do retract.");
  576. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], -1.0 * Retraction_Multiplier);
  577. if (G26_Debug_flag) SERIAL_ECHOLNPGM(" Retraction done.");
  578. }
  579. }
  580. void un_retract_filament() {
  581. if (retracted) { // Only un-retract if we are retracted.
  582. move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], 1.2 * Retraction_Multiplier);
  583. retracted = false;
  584. if (G26_Debug_flag) SERIAL_ECHOLNPGM(" unretract done.");
  585. }
  586. }
  587. /**
  588. * print_line_from_here_to_there() takes two cartesian coordinates and draws a line from one
  589. * to the other. But there are really three sets of coordinates involved. The first coordinate
  590. * is the present location of the nozzle. We don't necessarily want to print from this location.
  591. * We first need to move the nozzle to the start of line segment where we want to print. Once
  592. * there, we can use the two coordinates supplied to draw the line.
  593. *
  594. * Note: Although we assume the first set of coordinates is the start of the line and the second
  595. * set of coordinates is the end of the line, it does not always work out that way. This function
  596. * optimizes the movement to minimize the travel distance before it can start printing. This saves
  597. * a lot of time and eleminates a lot of non-sensical movement of the nozzle. However, it does
  598. * cause a lot of very little short retracement of th nozzle when it draws the very first line
  599. * segment of a 'circle'. The time this requires is very short and is easily saved by the other
  600. * cases where the optimization comes into play.
  601. */
  602. void print_line_from_here_to_there( float sx, float sy, float sz, float ex, float ey, float ez) {
  603. float dx, dy, dx_s, dy_s, dx_e, dy_e, dist_start, dist_end, Line_Length;
  604. dx_s = current_position[X_AXIS] - sx; // find our distance from the start of the actual line segment
  605. dy_s = current_position[Y_AXIS] - sy;
  606. dist_start = HYPOT2(dx_s, dy_s); // We don't need to do a sqrt(), we can compare the distance^2
  607. // to save computation time
  608. dx_e = current_position[X_AXIS] - ex; // find our distance from the end of the actual line segment
  609. dy_e = current_position[Y_AXIS] - ey;
  610. dist_end = HYPOT2(dx_e, dy_e);
  611. dx = ex - sx;
  612. dy = ey - sy;
  613. Line_Length = HYPOT(dx, dy);
  614. // If the end point of the line is closer to the nozzle, we are going to
  615. // flip the direction of this line. We will print it from the end to the start.
  616. // On very small lines we don't do the optimization because it just isn't worth it.
  617. //
  618. if (dist_end < dist_start && (SIZE_OF_INTERSECTION_CIRCLES) < abs(Line_Length)) {
  619. if (G26_Debug_flag)
  620. SERIAL_ECHOLNPGM(" Reversing start and end of print_line_from_here_to_there()");
  621. print_line_from_here_to_there(ex, ey, ez, sx, sy, sz);
  622. return;
  623. }
  624. // Now decide if we should retract.
  625. if (dist_start > 2.0) {
  626. retract_filament();
  627. if (G26_Debug_flag)
  628. SERIAL_ECHOLNPGM(" filament retracted.");
  629. }
  630. move_to(sx, sy, sz, 0.0); // Get to the starting point with no extrusion
  631. E_Pos_Delta = Line_Length * G26_E_AXIS_feedrate * Extrusion_Multiplier;
  632. un_retract_filament();
  633. if (G26_Debug_flag) {
  634. SERIAL_ECHOLNPGM(" doing printing move.");
  635. debug_current_and_destination((char *)"doing final move_to() inside print_line_from_here_to_there()");
  636. }
  637. move_to(ex, ey, ez, E_Pos_Delta); // Get to the ending point with an appropriate amount of extrusion
  638. }
  639. /**
  640. * This function used to be inline code in G26. But there are so many
  641. * parameters it made sense to turn them into static globals and get
  642. * this code out of sight of the main routine.
  643. */
  644. bool parse_G26_parameters() {
  645. Extrusion_Multiplier = EXTRUSION_MULTIPLIER;
  646. Retraction_Multiplier = RETRACTION_MULTIPLIER;
  647. Nozzle = NOZZLE;
  648. Filament = FILAMENT;
  649. Layer_Height = LAYER_HEIGHT;
  650. Prime_Length = PRIME_LENGTH;
  651. bed_temp = BED_TEMP;
  652. hotend_temp = HOTEND_TEMP;
  653. Ooooze_Amount = OOOOZE_AMOUNT;
  654. Prime_Flag = 0;
  655. Keep_Heaters_On = false;
  656. if (code_seen('B')) {
  657. bed_temp = code_value_float();
  658. if (bed_temp < 15.0 || bed_temp > 140.0) {
  659. SERIAL_PROTOCOLLNPGM("?Specified bed temperature not plausible.");
  660. return UBL_ERR;
  661. }
  662. }
  663. if (code_seen('C')) Continue_with_closest++;
  664. if (code_seen('L')) {
  665. Layer_Height = code_value_float();
  666. if (Layer_Height<0.0 || Layer_Height>2.0) {
  667. SERIAL_PROTOCOLLNPGM("?Specified layer height not plausible.");
  668. return UBL_ERR;
  669. }
  670. }
  671. if (code_seen('Q')) {
  672. if (code_has_value()) {
  673. Retraction_Multiplier = code_value_float();
  674. if (Retraction_Multiplier<.05 || Retraction_Multiplier>15.0) {
  675. SERIAL_PROTOCOLLNPGM("?Specified Retraction Multiplier not plausible.");
  676. return UBL_ERR;
  677. }
  678. }
  679. else {
  680. SERIAL_PROTOCOLLNPGM("?Retraction Multiplier must be specified.");
  681. return UBL_ERR;
  682. }
  683. }
  684. if (code_seen('N')) {
  685. Nozzle = code_value_float();
  686. if (Nozzle < 0.1 || Nozzle > 1.0) {
  687. SERIAL_PROTOCOLLNPGM("?Specified nozzle size not plausible.");
  688. return UBL_ERR;
  689. }
  690. }
  691. if (code_seen('K')) Keep_Heaters_On++;
  692. if (code_seen('O') && code_has_value())
  693. Ooooze_Amount = code_value_float();
  694. if (code_seen('P')) {
  695. if (!code_has_value())
  696. Prime_Flag = -1;
  697. else {
  698. Prime_Flag++;
  699. Prime_Length = code_value_float();
  700. if (Prime_Length < 0.0 || Prime_Length > 25.0) {
  701. SERIAL_PROTOCOLLNPGM("?Specified prime length not plausible.");
  702. return UBL_ERR;
  703. }
  704. }
  705. }
  706. if (code_seen('F')) {
  707. Filament = code_value_float();
  708. if (Filament < 1.0 || Filament > 4.0) {
  709. SERIAL_PROTOCOLLNPGM("?Specified filament size not plausible.");
  710. return UBL_ERR;
  711. }
  712. }
  713. Extrusion_Multiplier *= sq(1.75) / sq(Filament); // If we aren't using 1.75mm filament, we need to
  714. // scale up or down the length needed to get the
  715. // same volume of filament
  716. Extrusion_Multiplier *= Filament * sq(Nozzle) / sq(0.3); // Scale up by nozzle size
  717. if (code_seen('H')) {
  718. hotend_temp = code_value_float();
  719. if (hotend_temp < 165.0 || hotend_temp > 280.0) {
  720. SERIAL_PROTOCOLLNPGM("?Specified nozzle temperature not plausible.");
  721. return UBL_ERR;
  722. }
  723. }
  724. if (code_seen('R')) {
  725. randomSeed(millis());
  726. Random_Deviation = code_has_value() ? code_value_float() : 50.0;
  727. }
  728. X_Pos = current_position[X_AXIS];
  729. Y_Pos = current_position[Y_AXIS];
  730. if (code_seen('X')) {
  731. X_Pos = code_value_float();
  732. if (X_Pos < X_MIN_POS || X_Pos > X_MAX_POS) {
  733. SERIAL_PROTOCOLLNPGM("?Specified X coordinate not plausible.");
  734. return UBL_ERR;
  735. }
  736. }
  737. else
  738. if (code_seen('Y')) {
  739. Y_Pos = code_value_float();
  740. if (Y_Pos < Y_MIN_POS || Y_Pos > Y_MAX_POS) {
  741. SERIAL_PROTOCOLLNPGM("?Specified Y coordinate not plausible.");
  742. return UBL_ERR;
  743. }
  744. }
  745. /**
  746. * We save the question of what to do with the Unified Bed Leveling System's Activation until the very
  747. * end. The reason is, if one of the parameters specified up above is incorrect, we don't want to
  748. * alter the system's status. We wait until we know everything is correct before altering the state
  749. * of the system.
  750. */
  751. blm.state.active = !code_seen('D');
  752. return UBL_OK;
  753. }
  754. /**
  755. * Turn on the bed and nozzle heat and
  756. * wait for them to get up to temperature.
  757. */
  758. bool turn_on_heaters() {
  759. #if HAS_TEMP_BED
  760. #if ENABLED(ULTRA_LCD)
  761. if (bed_temp > 25) {
  762. lcd_setstatus("G26 Heating Bed.", true);
  763. lcd_quick_feedback();
  764. #endif
  765. UBL_has_control_of_LCD_Panel++;
  766. thermalManager.setTargetBed(bed_temp);
  767. while (abs(thermalManager.degBed() - bed_temp) > 3) {
  768. if (G29_lcd_clicked()) {
  769. strcpy(lcd_status_message, "Leaving G26"); // We can't do lcd_setstatus() without having it continue;
  770. while (G29_lcd_clicked()) idle(); // Debounce the switch
  771. lcd_setstatus("Leaving G26", true); // Now we do it right.
  772. return UBL_ERR;
  773. }
  774. idle();
  775. }
  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. while (abs(thermalManager.degHotend(0) - hotend_temp) > 3) {
  785. if (G29_lcd_clicked()) {
  786. strcpy(lcd_status_message, "Leaving G26"); // We can't do lcd_setstatus() without having it continue;
  787. while (G29_lcd_clicked()) idle(); // Debounce the switch
  788. lcd_setstatus("Leaving G26", true); // Now we do it right.
  789. return UBL_ERR;
  790. }
  791. idle();
  792. }
  793. #if ENABLED(ULTRA_LCD)
  794. lcd_setstatus("", true);
  795. lcd_quick_feedback();
  796. #endif
  797. return UBL_OK;
  798. }
  799. /**
  800. * Prime the nozzle if needed. Return true on error.
  801. */
  802. bool prime_nozzle() {
  803. float Total_Prime = 0.0;
  804. if (Prime_Flag == -1) { // The user wants to control how much filament gets purged
  805. lcd_setstatus("User Controled Prime", true);
  806. chirp_at_user();
  807. set_destination_to_current();
  808. un_retract_filament(); // Lets make sure the G26 command doesn't think the filament is
  809. // retracted(). We are here because we want to prime the nozzle.
  810. // So let's just unretract just to be sure.
  811. UBL_has_control_of_LCD_Panel++;
  812. while (!G29_lcd_clicked()) {
  813. chirp_at_user();
  814. destination[E_AXIS] += 0.25;
  815. #ifdef PREVENT_LENGTHY_EXTRUDE
  816. Total_Prime += 0.25;
  817. if (Total_Prime >= EXTRUDE_MAXLENGTH) return UBL_ERR;
  818. #endif
  819. UBL_line_to_destination(
  820. destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS],
  821. //planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0, 0xFFFF, 0xFFFF);
  822. planner.max_feedrate_mm_s[E_AXIS] / 15.0, 0
  823. );
  824. stepper.synchronize(); // Without this synchronize, the purge is more consistent,
  825. // but because the planner has a buffer, we won't be able
  826. // to stop as quickly. So we put up with the less smooth
  827. // action to give the user a more responsive 'Stop'.
  828. set_destination_to_current();
  829. idle();
  830. }
  831. strcpy(lcd_status_message, "Done Priming"); // We can't do lcd_setstatus() without having it continue;
  832. // So... We cheat to get a message up.
  833. while (G29_lcd_clicked()) idle(); // Debounce the switch
  834. #if ENABLED(ULTRA_LCD)
  835. UBL_has_control_of_LCD_Panel = 0;
  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