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.

UBL_G29.cpp 62KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. #include "MarlinConfig.h"
  23. #if ENABLED(AUTO_BED_LEVELING_UBL)
  24. //#include "vector_3.h"
  25. //#include "qr_solve.h"
  26. #include "UBL.h"
  27. #include "Marlin.h"
  28. #include "hex_print_routines.h"
  29. #include "configuration_store.h"
  30. #include "planner.h"
  31. #include "ultralcd.h"
  32. #include <math.h>
  33. void lcd_babystep_z();
  34. void lcd_return_to_status();
  35. bool lcd_clicked();
  36. void lcd_implementation_clear();
  37. void lcd_mesh_edit_setup(float initial);
  38. float lcd_mesh_edit();
  39. void lcd_z_offset_edit_setup(float);
  40. float lcd_z_offset_edit();
  41. extern float meshedit_done;
  42. extern long babysteps_done;
  43. extern float code_value_float();
  44. extern bool code_value_bool();
  45. extern bool code_has_value();
  46. extern float probe_pt(float x, float y, bool, int);
  47. extern bool set_probe_deployed(bool);
  48. #define DEPLOY_PROBE() set_probe_deployed(true)
  49. #define STOW_PROBE() set_probe_deployed(false)
  50. bool ProbeStay = true;
  51. constexpr float ubl_3_point_1_X = UBL_PROBE_PT_1_X,
  52. ubl_3_point_1_Y = UBL_PROBE_PT_1_Y,
  53. ubl_3_point_2_X = UBL_PROBE_PT_2_X,
  54. ubl_3_point_2_Y = UBL_PROBE_PT_2_Y,
  55. ubl_3_point_3_X = UBL_PROBE_PT_3_X,
  56. ubl_3_point_3_Y = UBL_PROBE_PT_3_Y;
  57. #define SIZE_OF_LITTLE_RAISE 0
  58. #define BIG_RAISE_NOT_NEEDED 0
  59. extern void lcd_quick_feedback();
  60. /**
  61. * G29: Unified Bed Leveling by Roxy
  62. *
  63. * Parameters understood by this leveling system:
  64. *
  65. * A Activate Activate the Unified Bed Leveling system.
  66. *
  67. * B # Business Use the 'Business Card' mode of the Manual Probe subsystem. This is invoked as
  68. * G29 P2 B The mode of G29 P2 allows you to use a bussiness card or recipe card
  69. * as a shim that the nozzle will pinch as it is lowered. The idea is that you
  70. * can easily feel the nozzle getting to the same height by the amount of resistance
  71. * the business card exhibits to movement. You should try to achieve the same amount
  72. * of resistance on each probed point to facilitate accurate and repeatable measurements.
  73. * You should be very careful not to drive the nozzle into the bussiness card with a
  74. * lot of force as it is very possible to cause damage to your printer if your are
  75. * careless. If you use the B option with G29 P2 B you can leave the number parameter off
  76. * on its first use to enable measurement of the business card thickness. Subsequent usage
  77. * of the B parameter can have the number previously measured supplied to the command.
  78. * Incidently, you are much better off using something like a Spark Gap feeler gauge than
  79. * something that compresses like a Business Card.
  80. *
  81. * C Continue Continue, Constant, Current Location. This is not a primary command. C is used to
  82. * further refine the behaviour of several other commands. Issuing a G29 P1 C will
  83. * continue the generation of a partially constructed Mesh without invalidating what has
  84. * been done. Issuing a G29 P2 C will tell the Manual Probe subsystem to use the current
  85. * location in its search for the closest unmeasured Mesh Point. When used with a G29 Z C
  86. * it indicates to use the current location instead of defaulting to the center of the print bed.
  87. *
  88. * D Disable Disable the Unified Bed Leveling system.
  89. *
  90. * E Stow_probe Stow the probe after each sampled point.
  91. *
  92. * F # Fade * Fade the amount of Mesh Based Compensation over a specified height. At the
  93. * specified height, no correction is applied and natural printer kenimatics take over. If no
  94. * number is specified for the command, 10mm is assumed to be reasonable.
  95. *
  96. * G # Grid * Perform a Grid Based Leveling of the current Mesh using a grid with n points on a side.
  97. *
  98. * H # Height Specify the Height to raise the nozzle after each manual probe of the bed. The
  99. * default is 5mm.
  100. *
  101. * I # Invalidate Invalidate specified number of Mesh Points. The nozzle location is used unless
  102. * the X and Y parameter are used. If no number is specified, only the closest Mesh
  103. * point to the location is invalidated. The M parameter is available as well to produce
  104. * a map after the operation. This command is useful to invalidate a portion of the
  105. * Mesh so it can be adjusted using other tools in the Unified Bed Leveling System. When
  106. * attempting to invalidate an isolated bad point in the mesh, the M option will indicate
  107. * where the nozzle is positioned in the Mesh with (#). You can move the nozzle around on
  108. * the bed and use this feature to select the center of the area (or cell) you want to
  109. * invalidate.
  110. *
  111. * K # Kompare Kompare current Mesh with stored Mesh # replacing current Mesh with the result. This
  112. * command literally performs a diff between two Meshes.
  113. *
  114. * L Load * Load Mesh from the previously activated location in the EEPROM.
  115. *
  116. * L # Load * Load Mesh from the specified location in the EEPROM. Set this location as activated
  117. * for subsequent Load and Store operations.
  118. *
  119. * O Map * Display the Mesh Map Topology.
  120. * The parameter can be specified alone (ie. G29 O) or in combination with many of the
  121. * other commands. The Mesh Map option works with all of the Phase
  122. * commands (ie. G29 P4 R 5 X 50 Y100 C -.1 O) The Map parameter can also of a Map Type
  123. * specified. A map type of 0 is the default is user readable. A map type of 1 can
  124. * be specified and is suitable to Cut & Paste into Excel to allow graphing of the user's
  125. * mesh.
  126. *
  127. * N No Home G29 normally insists that a G28 has been performed. You can over rule this with an
  128. * N option. In general, you should not do this. This can only be done safely with
  129. * commands that do not move the nozzle.
  130. *
  131. * The P or Phase commands are used for the bulk of the work to setup a Mesh. In general, your Mesh will
  132. * start off being initialized with a G29 P0 or a G29 P1. Further refinement of the Mesh happens with
  133. * each additional Phase that processes it.
  134. *
  135. * P0 Phase 0 Zero Mesh Data and turn off the Mesh Compensation System. This reverts the
  136. * 3D Printer to the same state it was in before the Unified Bed Leveling Compensation
  137. * was turned on. Setting the entire Mesh to Zero is a special case that allows
  138. * a subsequent G or T leveling operation for backward compatibility.
  139. *
  140. * P1 Phase 1 Invalidate entire Mesh and continue with automatic generation of the Mesh data using
  141. * the Z-Probe. Depending upon the values of DELTA_PROBEABLE_RADIUS and
  142. * DELTA_PRINTABLE_RADIUS some area of the bed will not have Mesh Data automatically
  143. * generated. This will be handled in Phase 2. If the Phase 1 command is given the
  144. * C (Continue) parameter it does not invalidate the Mesh prior to automatically
  145. * probing needed locations. This allows you to invalidate portions of the Mesh but still
  146. * use the automatic probing capabilities of the Unified Bed Leveling System. An X and Y
  147. * parameter can be given to prioritize where the command should be trying to measure points.
  148. * If the X and Y parameters are not specified the current probe position is used. Phase 1
  149. * allows you to specify the M (Map) parameter so you can watch the generation of the Mesh.
  150. * Phase 1 also watches for the LCD Panel's Encoder Switch being held in a depressed state.
  151. * It will suspend generation of the Mesh if it sees the user request that. (This check is
  152. * only done between probe points. You will need to press and hold the switch until the
  153. * Phase 1 command can detect it.)
  154. *
  155. * P2 Phase 2 Probe areas of the Mesh that can't be automatically handled. Phase 2 respects an H
  156. * parameter to control the height between Mesh points. The default height for movement
  157. * between Mesh points is 5mm. A smaller number can be used to make this part of the
  158. * calibration less time consuming. You will be running the nozzle down until it just barely
  159. * touches the glass. You should have the nozzle clean with no plastic obstructing your view.
  160. * Use caution and move slowly. It is possible to damage your printer if you are careless.
  161. * Note that this command will use the configuration #define SIZE_OF_LITTLE_RAISE if the
  162. * nozzle is moving a distance of less than BIG_RAISE_NOT_NEEDED.
  163. *
  164. * The H parameter can be set negative if your Mesh dips in a large area. You can press
  165. * and hold the LCD Panel's encoder wheel to terminate the current Phase 2 command. You
  166. * can then re-issue the G29 P 2 command with an H parameter that is more suitable for the
  167. * area you are manually probing. Note that the command tries to start you in a corner
  168. * of the bed where movement will be predictable. You can force the location to be used in
  169. * the distance calculations by using the X and Y parameters. You may find it is helpful to
  170. * print out a Mesh Map (G29 O ) to understand where the mesh is invalidated and where
  171. * the nozzle will need to move in order to complete the command. The C parameter is
  172. * available on the Phase 2 command also and indicates the search for points to measure should
  173. * be done based on the current location of the nozzle.
  174. *
  175. * A B parameter is also available for this command and described up above. It places the
  176. * manual probe subsystem into Business Card mode where the thickness of a business care is
  177. * measured and then used to accurately set the nozzle height in all manual probing for the
  178. * duration of the command. (S for Shim mode would be a better parameter name, but S is needed
  179. * for Save or Store of the Mesh to EEPROM) A Business card can be used, but you will have
  180. * better results if you use a flexible Shim that does not compress very much. That makes it
  181. * easier for you to get the nozzle to press with similar amounts of force against the shim so you
  182. * can get accurate measurements. As you are starting to touch the nozzle against the shim try
  183. * to get it to grasp the shim with the same force as when you measured the thickness of the
  184. * shim at the start of the command.
  185. *
  186. * Phase 2 allows the O (Map) parameter to be specified. This helps the user see the progression
  187. * of the Mesh being built.
  188. *
  189. * P3 Phase 3 Fill the unpopulated regions of the Mesh with a fixed value. The C parameter is
  190. * used to specify the 'constant' value to fill all invalid areas of the Mesh. If no C parameter
  191. * is specified, a value of 0.0 is assumed. The R parameter can be given to specify the number
  192. * of points to set. If the R parameter is specified the current nozzle position is used to
  193. * find the closest points to alter unless the X and Y parameter are used to specify the fill
  194. * location.
  195. *
  196. * P4 Phase 4 Fine tune the Mesh. The Delta Mesh Compensation System assume the existance of
  197. * an LCD Panel. It is possible to fine tune the mesh without the use of an LCD Panel.
  198. * (More work and details on doing this later!)
  199. * The System will search for the closest Mesh Point to the nozzle. It will move the
  200. * nozzle to this location. The user can use the LCD Panel to carefully adjust the nozzle
  201. * so it is just barely touching the bed. When the user clicks the control, the System
  202. * will lock in that height for that point in the Mesh Compensation System.
  203. *
  204. * Phase 4 has several additional parameters that the user may find helpful. Phase 4
  205. * can be started at a specific location by specifying an X and Y parameter. Phase 4
  206. * can be requested to continue the adjustment of Mesh Points by using the R(epeat)
  207. * parameter. If the Repetition count is not specified, it is assumed the user wishes
  208. * to adjust the entire matrix. The nozzle is moved to the Mesh Point being edited.
  209. * The command can be terminated early (or after the area of interest has been edited) by
  210. * pressing and holding the encoder wheel until the system recognizes the exit request.
  211. * Phase 4's general form is G29 P4 [R # of points] [X position] [Y position]
  212. *
  213. * Phase 4 is intended to be used with the G26 Mesh Validation Command. Using the
  214. * information left on the printer's bed from the G26 command it is very straight forward
  215. * and easy to fine tune the Mesh. One concept that is important to remember and that
  216. * will make using the Phase 4 command easy to use is this: You are editing the Mesh Points.
  217. * If you have too little clearance and not much plastic was extruded in an area, you want to
  218. * LOWER the Mesh Point at the location. If you did not get good adheasion, you want to
  219. * RAISE the Mesh Point at that location.
  220. *
  221. *
  222. * P5 Phase 5 Find Mean Mesh Height and Standard Deviation. Typically, it is easier to use and
  223. * work with the Mesh if it is Mean Adjusted. You can specify a C parameter to
  224. * Correct the Mesh to a 0.00 Mean Height. Adding a C parameter will automatically
  225. * execute a G29 P6 C <mean height>.
  226. *
  227. * P6 Phase 6 Shift Mesh height. The entire Mesh's height is adjusted by the height specified
  228. * with the C parameter. Being able to adjust the height of a Mesh is useful tool. It
  229. * can be used to compensate for poorly calibrated Z-Probes and other errors. Ideally,
  230. * you should have the Mesh adjusted for a Mean Height of 0.00 and the Z-Probe measuring
  231. * 0.000 at the Z Home location.
  232. *
  233. * Q Test * Load specified Test Pattern to assist in checking correct operation of system. This
  234. * command is not anticipated to be of much value to the typical user. It is intended
  235. * for developers to help them verify correct operation of the Unified Bed Leveling System.
  236. *
  237. * S Store Store the current Mesh in the Activated area of the EEPROM. It will also store the
  238. * current state of the Unified Bed Leveling system in the EEPROM.
  239. *
  240. * S # Store Store the current Mesh at the specified location in EEPROM. Activate this location
  241. * for subsequent Load and Store operations. It will also store the current state of
  242. * the Unified Bed Leveling system in the EEPROM.
  243. *
  244. * S -1 Store Store the current Mesh as a print out that is suitable to be feed back into
  245. * the system at a later date. The text generated can be saved and later sent by PronterFace or
  246. * Repetier Host to reconstruct the current mesh on another machine.
  247. *
  248. * T 3-Point Perform a 3 Point Bed Leveling on the current Mesh
  249. *
  250. * U Unlevel Perform a probe of the outer perimeter to assist in physically leveling unlevel beds.
  251. * Only used for G29 P1 O U It will speed up the probing of the edge of the bed. This
  252. * is useful when the entire bed does not need to be probed because it will be adjusted.
  253. *
  254. * W What? Display valuable data the Unified Bed Leveling System knows.
  255. *
  256. * X # * * X Location for this line of commands
  257. *
  258. * Y # * * Y Location for this line of commands
  259. *
  260. * Z Zero * Probes to set the Z Height of the nozzle. The entire Mesh can be raised or lowered
  261. * by just doing a G29 Z
  262. *
  263. * Z # Zero * The entire Mesh can be raised or lowered to conform with the specified difference.
  264. * zprobe_zoffset is added to the calculation.
  265. *
  266. *
  267. * Release Notes:
  268. * You MUST do M502, M500 to initialize the storage. Failure to do this will cause all
  269. * kinds of problems. Enabling EEPROM Storage is highly recommended. With EEPROM Storage
  270. * of the mesh, you are limited to 3-Point and Grid Leveling. (G29 P0 T and G29 P0 G
  271. * respectively.)
  272. *
  273. * When you do a G28 and then a G29 P1 to automatically build your first mesh, you are going to notice
  274. * the Unified Bed Leveling probes points further and further away from the starting location. (The
  275. * starting location defaults to the center of the bed.) The original Grid and Mesh leveling used
  276. * a Zig Zag pattern. The new pattern is better, especially for people with Delta printers. This
  277. * allows you to get the center area of the Mesh populated (and edited) quicker. This allows you to
  278. * perform a small print and check out your settings quicker. You do not need to populate the
  279. * entire mesh to use it. (You don't want to spend a lot of time generating a mesh only to realize
  280. * you don't have the resolution or zprobe_zoffset set correctly. The Mesh generation
  281. * gathers points closest to where the nozzle is located unless you specify an (X,Y) coordinate pair.
  282. *
  283. * The Unified Bed Leveling uses a lot of EEPROM storage to hold its data. And it takes some effort
  284. * to get this Mesh data correct for a user's printer. We do not want this data destroyed as
  285. * new versions of Marlin add or subtract to the items stored in EEPROM. So, for the benefit of
  286. * the users, we store the Mesh data at the end of the EEPROM and do not keep it contiguous with the
  287. * other data stored in the EEPROM. (For sure the developers are going to complain about this, but
  288. * this is going to be helpful to the users!)
  289. *
  290. * The foundation of this Bed Leveling System is built on Epatel's Mesh Bed Leveling code. A big
  291. * 'Thanks!' to him and the creators of 3-Point and Grid Based leveling. Combining their contributions
  292. * we now have the functionality and features of all three systems combined.
  293. */
  294. // The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
  295. static int g29_verbose_level, phase_value = -1, repetition_cnt,
  296. storage_slot = 0, map_type; //unlevel_value = -1;
  297. static bool repeat_flag, c_flag, x_flag, y_flag;
  298. static float x_pos, y_pos, measured_z, card_thickness = 0.0, ubl_constant = 0.0;
  299. #if ENABLED(ULTRA_LCD)
  300. extern void lcd_setstatus(const char* message, const bool persist);
  301. extern void lcd_setstatuspgm(const char* message, const uint8_t level);
  302. #endif
  303. void gcode_G29() {
  304. SERIAL_PROTOCOLLNPAIR("ubl.eeprom_start=", ubl.eeprom_start);
  305. if (ubl.eeprom_start < 0) {
  306. SERIAL_PROTOCOLLNPGM("?You need to enable your EEPROM and initialize it");
  307. SERIAL_PROTOCOLLNPGM("with M502, M500, M501 in that order.\n");
  308. return;
  309. }
  310. if (!code_seen('N') && axis_unhomed_error(true, true, true)) // Don't allow auto-leveling without homing first
  311. gcode_G28();
  312. if (g29_parameter_parsing()) return; // abort if parsing the simple parameters causes a problem,
  313. // Invalidate Mesh Points. This command is a little bit asymetrical because
  314. // it directly specifies the repetition count and does not use the 'R' parameter.
  315. if (code_seen('I')) {
  316. int cnt = 0;
  317. repetition_cnt = code_has_value() ? code_value_int() : 1;
  318. while (repetition_cnt--) {
  319. if (cnt>20) {
  320. cnt = 0;
  321. idle();
  322. }
  323. const mesh_index_pair location = find_closest_mesh_point_of_type(REAL, x_pos, y_pos, 0, NULL, false); // The '0' says we want to use the nozzle's position
  324. if (location.x_index < 0) {
  325. SERIAL_PROTOCOLLNPGM("Entire Mesh invalidated.\n");
  326. break; // No more invalid Mesh Points to populate
  327. }
  328. ubl.z_values[location.x_index][location.y_index] = NAN;
  329. }
  330. SERIAL_PROTOCOLLNPGM("Locations invalidated.\n");
  331. }
  332. if (code_seen('Q')) {
  333. const int test_pattern = code_has_value() ? code_value_int() : -1;
  334. if (!WITHIN(test_pattern, 0, 2)) {
  335. SERIAL_PROTOCOLLNPGM("Invalid test_pattern value. (0-2)\n");
  336. return;
  337. }
  338. SERIAL_PROTOCOLLNPGM("Loading test_pattern values.\n");
  339. switch (test_pattern) {
  340. case 0:
  341. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) { // Create a bowl shape - similar to
  342. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++) { // a poorly calibrated Delta.
  343. const float p1 = 0.5 * (UBL_MESH_NUM_X_POINTS) - x,
  344. p2 = 0.5 * (UBL_MESH_NUM_Y_POINTS) - y;
  345. ubl.z_values[x][y] += 2.0 * HYPOT(p1, p2);
  346. }
  347. }
  348. break;
  349. case 1:
  350. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) { // Create a diagonal line several Mesh cells thick that is raised
  351. ubl.z_values[x][x] += 9.999;
  352. ubl.z_values[x][x + (x < UBL_MESH_NUM_Y_POINTS - 1) ? 1 : -1] += 9.999; // We want the altered line several mesh points thick
  353. }
  354. break;
  355. case 2:
  356. // Allow the user to specify the height because 10mm is a little extreme in some cases.
  357. for (uint8_t x = (UBL_MESH_NUM_X_POINTS) / 3; x < 2 * (UBL_MESH_NUM_X_POINTS) / 3; x++) // Create a rectangular raised area in
  358. for (uint8_t y = (UBL_MESH_NUM_Y_POINTS) / 3; y < 2 * (UBL_MESH_NUM_Y_POINTS) / 3; y++) // the center of the bed
  359. ubl.z_values[x][y] += code_seen('C') ? ubl_constant : 9.99;
  360. break;
  361. }
  362. }
  363. /*
  364. if (code_seen('U')) {
  365. unlevel_value = code_value_int();
  366. //if (!WITHIN(unlevel_value, 0, 7)) {
  367. // SERIAL_PROTOCOLLNPGM("Invalid Unlevel value. (0-4)\n");
  368. // return;
  369. //}
  370. }
  371. //*/
  372. if (code_seen('P')) {
  373. phase_value = code_value_int();
  374. if (!WITHIN(phase_value, 0, 7)) {
  375. SERIAL_PROTOCOLLNPGM("Invalid Phase value. (0-4)\n");
  376. return;
  377. }
  378. switch (phase_value) {
  379. case 0:
  380. //
  381. // Zero Mesh Data
  382. //
  383. ubl.reset();
  384. SERIAL_PROTOCOLLNPGM("Mesh zeroed.\n");
  385. break;
  386. case 1:
  387. //
  388. // Invalidate Entire Mesh and Automatically Probe Mesh in areas that can be reached by the probe
  389. //
  390. if (!code_seen('C') ) {
  391. ubl.invalidate();
  392. SERIAL_PROTOCOLLNPGM("Mesh invalidated. Probing mesh.\n");
  393. }
  394. if (g29_verbose_level > 1) {
  395. SERIAL_ECHOPGM("Probing Mesh Points Closest to (");
  396. SERIAL_ECHO(x_pos);
  397. SERIAL_ECHOPAIR(",", y_pos);
  398. SERIAL_PROTOCOLLNPGM(")\n");
  399. }
  400. probe_entire_mesh(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER,
  401. code_seen('O') || code_seen('M'), code_seen('E'), code_seen('U'));
  402. break;
  403. case 2: {
  404. //
  405. // Manually Probe Mesh in areas that can't be reached by the probe
  406. //
  407. SERIAL_PROTOCOLLNPGM("Manually probing unreachable mesh locations.\n");
  408. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  409. if (!x_flag && !y_flag) { // use a good default location for the path
  410. x_pos = X_MIN_POS;
  411. y_pos = Y_MIN_POS;
  412. if (X_PROBE_OFFSET_FROM_EXTRUDER > 0) // The flipped > and < operators on these two comparisons is
  413. x_pos = X_MAX_POS; // intentional. It should cause the probed points to follow a
  414. if (Y_PROBE_OFFSET_FROM_EXTRUDER < 0) // nice path on Cartesian printers. It may make sense to
  415. y_pos = Y_MAX_POS; // have Delta printers default to the center of the bed.
  416. } // For now, until that is decided, it can be forced with the X
  417. // and Y parameters.
  418. if (code_seen('C')) {
  419. x_pos = current_position[X_AXIS];
  420. y_pos = current_position[Y_AXIS];
  421. }
  422. const float height = code_seen('H') && code_has_value() ? code_value_float() : Z_CLEARANCE_BETWEEN_PROBES;
  423. if (code_seen('B')) {
  424. card_thickness = code_has_value() ? code_value_float() : measure_business_card_thickness(height);
  425. if (fabs(card_thickness) > 1.5) {
  426. SERIAL_PROTOCOLLNPGM("?Error in Business Card measurement.\n");
  427. return;
  428. }
  429. }
  430. manually_probe_remaining_mesh(x_pos, y_pos, height, card_thickness, code_seen('O') || code_seen('M'));
  431. } break;
  432. case 3: {
  433. //
  434. // Populate invalid Mesh areas with a constant
  435. //
  436. const float height = code_seen('C') ? ubl_constant : 0.0;
  437. // If no repetition is specified, do the whole Mesh
  438. if (!repeat_flag) repetition_cnt = 9999;
  439. while (repetition_cnt--) {
  440. const mesh_index_pair location = find_closest_mesh_point_of_type(INVALID, x_pos, y_pos, 0, NULL, false); // The '0' says we want to use the nozzle's position
  441. if (location.x_index < 0) break; // No more invalid Mesh Points to populate
  442. ubl.z_values[location.x_index][location.y_index] = height;
  443. }
  444. } break;
  445. case 4:
  446. //
  447. // Fine Tune (i.e., Edit) the Mesh
  448. //
  449. fine_tune_mesh(x_pos, y_pos, code_seen('O') || code_seen('M'));
  450. break;
  451. case 5:
  452. find_mean_mesh_height();
  453. break;
  454. case 6:
  455. shift_mesh_height();
  456. break;
  457. case 10:
  458. // [DEBUG] Pay no attention to this stuff. It can be removed soon.
  459. SERIAL_ECHO_START;
  460. SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
  461. KEEPALIVE_STATE(PAUSED_FOR_USER);
  462. ubl.has_control_of_lcd_panel++;
  463. while (!ubl_lcd_clicked()) {
  464. safe_delay(250);
  465. if (ubl.encoder_diff) {
  466. SERIAL_ECHOLN((int)ubl.encoder_diff);
  467. ubl.encoder_diff = 0;
  468. }
  469. }
  470. SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
  471. ubl.has_control_of_lcd_panel = false;
  472. KEEPALIVE_STATE(IN_HANDLER);
  473. break;
  474. case 11:
  475. // [DEBUG] wait_for_user code. Pay no attention to this stuff. It can be removed soon.
  476. SERIAL_ECHO_START;
  477. SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
  478. KEEPALIVE_STATE(PAUSED_FOR_USER);
  479. wait_for_user = true;
  480. while (wait_for_user) {
  481. safe_delay(250);
  482. if (ubl.encoder_diff) {
  483. SERIAL_ECHOLN((int)ubl.encoder_diff);
  484. ubl.encoder_diff = 0;
  485. }
  486. }
  487. SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
  488. KEEPALIVE_STATE(IN_HANDLER);
  489. break;
  490. }
  491. }
  492. if (code_seen('T')) {
  493. const float lx1 = LOGICAL_X_POSITION(ubl_3_point_1_X),
  494. lx2 = LOGICAL_X_POSITION(ubl_3_point_2_X),
  495. lx3 = LOGICAL_X_POSITION(ubl_3_point_3_X),
  496. ly1 = LOGICAL_Y_POSITION(ubl_3_point_1_Y),
  497. ly2 = LOGICAL_Y_POSITION(ubl_3_point_2_Y),
  498. ly3 = LOGICAL_Y_POSITION(ubl_3_point_3_Y);
  499. float z1 = probe_pt(lx1, ly1, false /*Stow Flag*/, g29_verbose_level),
  500. z2 = probe_pt(lx2, ly2, false /*Stow Flag*/, g29_verbose_level),
  501. z3 = probe_pt(lx3, ly3, true /*Stow Flag*/, g29_verbose_level);
  502. // We need to adjust z1, z2, z3 by the Mesh Height at these points. Just because they are non-zero doesn't mean
  503. // the Mesh is tilted! (We need to compensate each probe point by what the Mesh says that location's height is)
  504. z1 -= ubl.get_z_correction(lx1, ly1);
  505. z2 -= ubl.get_z_correction(lx2, ly2);
  506. z3 -= ubl.get_z_correction(lx3, ly3);
  507. do_blocking_move_to_xy((X_MAX_POS - (X_MIN_POS)) / 2.0, (Y_MAX_POS - (Y_MIN_POS)) / 2.0);
  508. tilt_mesh_based_on_3pts(z1, z2, z3);
  509. }
  510. //
  511. // Much of the 'What?' command can be eliminated. But until we are fully debugged, it is
  512. // good to have the extra information. Soon... we prune this to just a few items
  513. //
  514. if (code_seen('W')) g29_what_command();
  515. //
  516. // When we are fully debugged, the EEPROM dump command will get deleted also. But
  517. // right now, it is good to have the extra information. Soon... we prune this.
  518. //
  519. if (code_seen('J')) g29_eeprom_dump(); // EEPROM Dump
  520. //
  521. // When we are fully debugged, this may go away. But there are some valid
  522. // use cases for the users. So we can wait and see what to do with it.
  523. //
  524. if (code_seen('K')) // Kompare Current Mesh Data to Specified Stored Mesh
  525. g29_compare_current_mesh_to_stored_mesh();
  526. //
  527. // Load a Mesh from the EEPROM
  528. //
  529. if (code_seen('L')) { // Load Current Mesh Data
  530. storage_slot = code_has_value() ? code_value_int() : ubl.state.eeprom_storage_slot;
  531. const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(ubl.z_values);
  532. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  533. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  534. return;
  535. }
  536. ubl.load_mesh(storage_slot);
  537. ubl.state.eeprom_storage_slot = storage_slot;
  538. if (storage_slot != ubl.state.eeprom_storage_slot)
  539. ubl.store_state();
  540. SERIAL_PROTOCOLLNPGM("Done.\n");
  541. }
  542. //
  543. // Store a Mesh in the EEPROM
  544. //
  545. if (code_seen('S')) { // Store (or Save) Current Mesh Data
  546. storage_slot = code_has_value() ? code_value_int() : ubl.state.eeprom_storage_slot;
  547. if (storage_slot == -1) { // Special case, we are going to 'Export' the mesh to the
  548. SERIAL_ECHOLNPGM("G29 I 999"); // host in a form it can be reconstructed on a different machine
  549. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  550. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  551. if (!isnan(ubl.z_values[x][y])) {
  552. SERIAL_ECHOPAIR("M421 I ", x);
  553. SERIAL_ECHOPAIR(" J ", y);
  554. SERIAL_ECHOPGM(" Z ");
  555. SERIAL_ECHO_F(ubl.z_values[x][y], 6);
  556. SERIAL_EOL;
  557. }
  558. return;
  559. }
  560. const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(ubl.z_values);
  561. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  562. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  563. SERIAL_PROTOCOLLNPAIR("?Use 0 to ", j - 1);
  564. goto LEAVE;
  565. }
  566. ubl.store_mesh(storage_slot);
  567. ubl.state.eeprom_storage_slot = storage_slot;
  568. //
  569. // if (storage_slot != ubl.state.eeprom_storage_slot)
  570. ubl.store_state(); // Always save an updated copy of the UBL State info
  571. SERIAL_PROTOCOLLNPGM("Done.\n");
  572. }
  573. if (code_seen('O') || code_seen('M'))
  574. ubl.display_map(code_has_value() ? code_value_int() : 0);
  575. if (code_seen('Z')) {
  576. if (code_has_value())
  577. ubl.state.z_offset = code_value_float(); // do the simple case. Just lock in the specified value
  578. else {
  579. save_ubl_active_state_and_disable();
  580. //measured_z = probe_pt(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER, ProbeDeployAndStow, g29_verbose_level);
  581. ubl.has_control_of_lcd_panel++; // Grab the LCD Hardware
  582. measured_z = 1.5;
  583. do_blocking_move_to_z(measured_z); // Get close to the bed, but leave some space so we don't damage anything
  584. // The user is not going to be locking in a new Z-Offset very often so
  585. // it won't be that painful to spin the Encoder Wheel for 1.5mm
  586. lcd_implementation_clear();
  587. lcd_z_offset_edit_setup(measured_z);
  588. KEEPALIVE_STATE(PAUSED_FOR_USER);
  589. do {
  590. measured_z = lcd_z_offset_edit();
  591. idle();
  592. do_blocking_move_to_z(measured_z);
  593. } while (!ubl_lcd_clicked());
  594. ubl.has_control_of_lcd_panel++; // There is a race condition for the Encoder Wheel getting clicked.
  595. // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
  596. // or here. So, until we are done looking for a long Encoder Wheel Press,
  597. // we need to take control of the panel
  598. KEEPALIVE_STATE(IN_HANDLER);
  599. lcd_return_to_status();
  600. const millis_t nxt = millis() + 1500UL;
  601. while (ubl_lcd_clicked()) { // debounce and watch for abort
  602. idle();
  603. if (ELAPSED(millis(), nxt)) {
  604. SERIAL_PROTOCOLLNPGM("\nZ-Offset Adjustment Stopped.");
  605. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  606. lcd_setstatuspgm("Z-Offset Stopped");
  607. restore_ubl_active_state_and_leave();
  608. goto LEAVE;
  609. }
  610. }
  611. ubl.has_control_of_lcd_panel = false;
  612. safe_delay(20); // We don't want any switch noise.
  613. ubl.state.z_offset = measured_z;
  614. lcd_implementation_clear();
  615. restore_ubl_active_state_and_leave();
  616. }
  617. }
  618. LEAVE:
  619. #if ENABLED(ULTRA_LCD)
  620. lcd_reset_alert_level();
  621. lcd_setstatuspgm("");
  622. lcd_quick_feedback();
  623. #endif
  624. ubl.has_control_of_lcd_panel = false;
  625. }
  626. void find_mean_mesh_height() {
  627. uint8_t x, y;
  628. int n;
  629. float sum, sum_of_diff_squared, sigma, difference, mean;
  630. sum = sum_of_diff_squared = 0.0;
  631. n = 0;
  632. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  633. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  634. if (!isnan(ubl.z_values[x][y])) {
  635. sum += ubl.z_values[x][y];
  636. n++;
  637. }
  638. mean = sum / n;
  639. //
  640. // Now do the sumation of the squares of difference from mean
  641. //
  642. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  643. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  644. if (!isnan(ubl.z_values[x][y])) {
  645. difference = (ubl.z_values[x][y] - mean);
  646. sum_of_diff_squared += difference * difference;
  647. }
  648. SERIAL_ECHOLNPAIR("# of samples: ", n);
  649. SERIAL_ECHOPGM("Mean Mesh Height: ");
  650. SERIAL_ECHO_F(mean, 6);
  651. SERIAL_EOL;
  652. sigma = sqrt(sum_of_diff_squared / (n + 1));
  653. SERIAL_ECHOPGM("Standard Deviation: ");
  654. SERIAL_ECHO_F(sigma, 6);
  655. SERIAL_EOL;
  656. if (c_flag)
  657. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  658. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  659. if (!isnan(ubl.z_values[x][y]))
  660. ubl.z_values[x][y] -= mean + ubl_constant;
  661. }
  662. void shift_mesh_height() {
  663. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  664. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  665. if (!isnan(ubl.z_values[x][y]))
  666. ubl.z_values[x][y] += ubl_constant;
  667. }
  668. /**
  669. * Probe all invalidated locations of the mesh that can be reached by the probe.
  670. * This attempts to fill in locations closest to the nozzle's start location first.
  671. */
  672. void probe_entire_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map, const bool stow_probe, bool do_furthest) {
  673. mesh_index_pair location;
  674. ubl.has_control_of_lcd_panel++;
  675. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  676. DEPLOY_PROBE();
  677. do {
  678. if (ubl_lcd_clicked()) {
  679. SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.\n");
  680. lcd_quick_feedback();
  681. STOW_PROBE();
  682. while (ubl_lcd_clicked()) idle();
  683. ubl.has_control_of_lcd_panel = false;
  684. restore_ubl_active_state_and_leave();
  685. safe_delay(50); // Debounce the Encoder wheel
  686. return;
  687. }
  688. location = find_closest_mesh_point_of_type(INVALID, lx, ly, 1, NULL, do_furthest ); // the '1' says we want the location to be relative to the probe
  689. if (location.x_index >= 0 && location.y_index >= 0) {
  690. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  691. rawy = ubl.mesh_index_to_ypos[location.y_index];
  692. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  693. if (!WITHIN(rawx, MIN_PROBE_X, MAX_PROBE_X) || !WITHIN(rawy, MIN_PROBE_Y, MAX_PROBE_Y)) {
  694. SERIAL_ERROR_START;
  695. SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
  696. ubl.has_control_of_lcd_panel = false;
  697. goto LEAVE;
  698. }
  699. const float measured_z = probe_pt(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy), stow_probe, g29_verbose_level);
  700. ubl.z_values[location.x_index][location.y_index] = measured_z;
  701. }
  702. if (do_ubl_mesh_map) ubl.display_map(map_type);
  703. } while (location.x_index >= 0 && location.y_index >= 0);
  704. LEAVE:
  705. STOW_PROBE();
  706. restore_ubl_active_state_and_leave();
  707. do_blocking_move_to_xy(
  708. constrain(lx - (X_PROBE_OFFSET_FROM_EXTRUDER), X_MIN_POS, X_MAX_POS),
  709. constrain(ly - (Y_PROBE_OFFSET_FROM_EXTRUDER), Y_MIN_POS, Y_MAX_POS)
  710. );
  711. }
  712. vector_3 tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3) {
  713. float c, d, t;
  714. int i, j;
  715. vector_3 v1 = vector_3( (ubl_3_point_1_X - ubl_3_point_2_X),
  716. (ubl_3_point_1_Y - ubl_3_point_2_Y),
  717. (z1 - z2) ),
  718. v2 = vector_3( (ubl_3_point_3_X - ubl_3_point_2_X),
  719. (ubl_3_point_3_Y - ubl_3_point_2_Y),
  720. (z3 - z2) ),
  721. normal = vector_3::cross(v1, v2);
  722. // printf("[%f,%f,%f] ", normal.x, normal.y, normal.z);
  723. /**
  724. * This code does two things. This vector is normal to the tilted plane.
  725. * However, we don't know its direction. We need it to point up. So if
  726. * Z is negative, we need to invert the sign of all components of the vector
  727. * We also need Z to be unity because we are going to be treating this triangle
  728. * as the sin() and cos() of the bed's tilt
  729. */
  730. const float inv_z = 1.0 / normal.z;
  731. normal.x *= inv_z;
  732. normal.y *= inv_z;
  733. normal.z = 1.0;
  734. //
  735. // All of 3 of these points should give us the same d constant
  736. //
  737. t = normal.x * ubl_3_point_1_X + normal.y * ubl_3_point_1_Y;
  738. d = t + normal.z * z1;
  739. c = d - t;
  740. SERIAL_ECHOPGM("d from 1st point: ");
  741. SERIAL_ECHO_F(d, 6);
  742. SERIAL_ECHOPGM(" c: ");
  743. SERIAL_ECHO_F(c, 6);
  744. SERIAL_EOL;
  745. t = normal.x * ubl_3_point_2_X + normal.y * ubl_3_point_2_Y;
  746. d = t + normal.z * z2;
  747. c = d - t;
  748. SERIAL_ECHOPGM("d from 2nd point: ");
  749. SERIAL_ECHO_F(d, 6);
  750. SERIAL_ECHOPGM(" c: ");
  751. SERIAL_ECHO_F(c, 6);
  752. SERIAL_EOL;
  753. t = normal.x * ubl_3_point_3_X + normal.y * ubl_3_point_3_Y;
  754. d = t + normal.z * z3;
  755. c = d - t;
  756. SERIAL_ECHOPGM("d from 3rd point: ");
  757. SERIAL_ECHO_F(d, 6);
  758. SERIAL_ECHOPGM(" c: ");
  759. SERIAL_ECHO_F(c, 6);
  760. SERIAL_EOL;
  761. for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  762. for (j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  763. c = -((normal.x * (UBL_MESH_MIN_X + i * (MESH_X_DIST)) + normal.y * (UBL_MESH_MIN_Y + j * (MESH_Y_DIST))) - d);
  764. ubl.z_values[i][j] += c;
  765. }
  766. }
  767. return normal;
  768. }
  769. float use_encoder_wheel_to_measure_point() {
  770. KEEPALIVE_STATE(PAUSED_FOR_USER);
  771. while (!ubl_lcd_clicked()) { // we need the loop to move the nozzle based on the encoder wheel here!
  772. idle();
  773. if (ubl.encoder_diff) {
  774. do_blocking_move_to_z(current_position[Z_AXIS] + 0.01 * float(ubl.encoder_diff));
  775. ubl.encoder_diff = 0;
  776. }
  777. }
  778. KEEPALIVE_STATE(IN_HANDLER);
  779. return current_position[Z_AXIS];
  780. }
  781. float measure_business_card_thickness(const float &in_height) {
  782. ubl.has_control_of_lcd_panel++;
  783. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  784. SERIAL_PROTOCOLLNPGM("Place Shim Under Nozzle and Perform Measurement.");
  785. do_blocking_move_to_z(in_height);
  786. do_blocking_move_to_xy((float(X_MAX_POS) - float(X_MIN_POS)) / 2.0, (float(Y_MAX_POS) - float(Y_MIN_POS)) / 2.0);
  787. //, min( planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])/2.0);
  788. const float z1 = use_encoder_wheel_to_measure_point();
  789. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  790. ubl.has_control_of_lcd_panel = false;
  791. SERIAL_PROTOCOLLNPGM("Remove Shim and Measure Bed Height.");
  792. const float z2 = use_encoder_wheel_to_measure_point();
  793. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  794. if (g29_verbose_level > 1) {
  795. SERIAL_PROTOCOLPGM("Business Card is: ");
  796. SERIAL_PROTOCOL_F(abs(z1 - z2), 6);
  797. SERIAL_PROTOCOLLNPGM("mm thick.");
  798. }
  799. restore_ubl_active_state_and_leave();
  800. return abs(z1 - z2);
  801. }
  802. void manually_probe_remaining_mesh(const float &lx, const float &ly, const float &z_clearance, const float &card_thickness, const bool do_ubl_mesh_map) {
  803. ubl.has_control_of_lcd_panel++;
  804. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  805. do_blocking_move_to_z(z_clearance);
  806. do_blocking_move_to_xy(lx, ly);
  807. float last_x = -9999.99, last_y = -9999.99;
  808. mesh_index_pair location;
  809. do {
  810. if (do_ubl_mesh_map) ubl.display_map(map_type);
  811. location = find_closest_mesh_point_of_type(INVALID, lx, ly, 0, NULL, false); // The '0' says we want to use the nozzle's position
  812. // It doesn't matter if the probe can't reach the NAN location. This is a manual probe.
  813. if (location.x_index < 0 && location.y_index < 0) continue;
  814. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  815. rawy = ubl.mesh_index_to_ypos[location.y_index];
  816. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  817. if (!WITHIN(rawx, X_MIN_POS, X_MAX_POS) || !WITHIN(rawy, Y_MIN_POS, Y_MAX_POS)) {
  818. SERIAL_ERROR_START;
  819. SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
  820. ubl.has_control_of_lcd_panel = false;
  821. goto LEAVE;
  822. }
  823. const float xProbe = LOGICAL_X_POSITION(rawx),
  824. yProbe = LOGICAL_Y_POSITION(rawy),
  825. dx = xProbe - last_x,
  826. dy = yProbe - last_y;
  827. if (HYPOT(dx, dy) < BIG_RAISE_NOT_NEEDED)
  828. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  829. else
  830. do_blocking_move_to_z(z_clearance);
  831. do_blocking_move_to_xy(xProbe, yProbe);
  832. last_x = xProbe;
  833. last_y = yProbe;
  834. KEEPALIVE_STATE(PAUSED_FOR_USER);
  835. ubl.has_control_of_lcd_panel = true;
  836. while (!ubl_lcd_clicked()) { // we need the loop to move the nozzle based on the encoder wheel here!
  837. idle();
  838. if (ubl.encoder_diff) {
  839. do_blocking_move_to_z(current_position[Z_AXIS] + float(ubl.encoder_diff) / 100.0);
  840. ubl.encoder_diff = 0;
  841. }
  842. }
  843. const millis_t nxt = millis() + 1500L;
  844. while (ubl_lcd_clicked()) { // debounce and watch for abort
  845. idle();
  846. if (ELAPSED(millis(), nxt)) {
  847. SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.");
  848. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  849. lcd_quick_feedback();
  850. while (ubl_lcd_clicked()) idle();
  851. ubl.has_control_of_lcd_panel = false;
  852. KEEPALIVE_STATE(IN_HANDLER);
  853. restore_ubl_active_state_and_leave();
  854. return;
  855. }
  856. }
  857. ubl.z_values[location.x_index][location.y_index] = current_position[Z_AXIS] - card_thickness;
  858. if (g29_verbose_level > 2) {
  859. SERIAL_PROTOCOLPGM("Mesh Point Measured at: ");
  860. SERIAL_PROTOCOL_F(ubl.z_values[location.x_index][location.y_index], 6);
  861. SERIAL_EOL;
  862. }
  863. } while (location.x_index >= 0 && location.y_index >= 0);
  864. if (do_ubl_mesh_map) ubl.display_map(map_type);
  865. LEAVE:
  866. restore_ubl_active_state_and_leave();
  867. KEEPALIVE_STATE(IN_HANDLER);
  868. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  869. do_blocking_move_to_xy(lx, ly);
  870. }
  871. bool g29_parameter_parsing() {
  872. #if ENABLED(ULTRA_LCD)
  873. lcd_setstatuspgm("Doing G29 UBL!");
  874. lcd_quick_feedback();
  875. #endif
  876. g29_verbose_level = code_seen('V') ? code_value_int() : 0;
  877. if (!WITHIN(g29_verbose_level, 0, 4)) {
  878. SERIAL_PROTOCOLLNPGM("Invalid Verbose Level specified. (0-4)\n");
  879. return UBL_ERR;
  880. }
  881. x_flag = code_seen('X') && code_has_value();
  882. x_pos = x_flag ? code_value_float() : current_position[X_AXIS];
  883. if (!WITHIN(RAW_X_POSITION(x_pos), X_MIN_POS, X_MAX_POS)) {
  884. SERIAL_PROTOCOLLNPGM("Invalid X location specified.\n");
  885. return UBL_ERR;
  886. }
  887. y_flag = code_seen('Y') && code_has_value();
  888. y_pos = y_flag ? code_value_float() : current_position[Y_AXIS];
  889. if (!WITHIN(RAW_Y_POSITION(y_pos), Y_MIN_POS, Y_MAX_POS)) {
  890. SERIAL_PROTOCOLLNPGM("Invalid Y location specified.\n");
  891. return UBL_ERR;
  892. }
  893. if (x_flag != y_flag) {
  894. SERIAL_PROTOCOLLNPGM("Both X & Y locations must be specified.\n");
  895. return UBL_ERR;
  896. }
  897. if (code_seen('A')) { // Activate the Unified Bed Leveling System
  898. ubl.state.active = 1;
  899. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling System activated.\n");
  900. ubl.store_state();
  901. }
  902. c_flag = code_seen('C') && code_has_value();
  903. ubl_constant = c_flag ? code_value_float() : 0.0;
  904. if (code_seen('D')) { // Disable the Unified Bed Leveling System
  905. ubl.state.active = 0;
  906. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling System de-activated.\n");
  907. ubl.store_state();
  908. }
  909. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  910. if (code_seen('F') && code_has_value()) {
  911. const float fh = code_value_float();
  912. if (!WITHIN(fh, 0.0, 100.0)) {
  913. SERIAL_PROTOCOLLNPGM("?Bed Level Correction Fade Height Not Plausible.\n");
  914. return UBL_ERR;
  915. }
  916. ubl.state.g29_correction_fade_height = fh;
  917. ubl.state.g29_fade_height_multiplier = 1.0 / fh;
  918. }
  919. #endif
  920. repeat_flag = code_seen('R');
  921. repetition_cnt = repeat_flag ? (code_has_value() ? code_value_int() : 9999) : 1;
  922. if (repetition_cnt < 1) {
  923. SERIAL_PROTOCOLLNPGM("Invalid Repetition count.\n");
  924. return UBL_ERR;
  925. }
  926. map_type = code_seen('O') && code_has_value() ? code_value_int() : 0;
  927. if (!WITHIN(map_type, 0, 1)) {
  928. SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
  929. return UBL_ERR;
  930. }
  931. /*
  932. if (code_seen('M')) { // Check if a map type was specified
  933. map_type = code_has_value() ? code_value_int() : 0;
  934. if (!WITHIN(map_type, 0, 1)) {
  935. SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
  936. return UBL_ERR;
  937. }
  938. }
  939. //*/
  940. return UBL_OK;
  941. }
  942. /**
  943. * This function goes away after G29 debug is complete. But for right now, it is a handy
  944. * routine to dump binary data structures.
  945. */
  946. void dump(char * const str, const float &f) {
  947. char *ptr;
  948. SERIAL_PROTOCOL(str);
  949. SERIAL_PROTOCOL_F(f, 8);
  950. SERIAL_PROTOCOLPGM(" ");
  951. ptr = (char*)&f;
  952. for (uint8_t i = 0; i < 4; i++)
  953. SERIAL_PROTOCOLPAIR(" ", hex_byte(*ptr++));
  954. SERIAL_PROTOCOLPAIR(" isnan()=", isnan(f));
  955. SERIAL_PROTOCOLPAIR(" isinf()=", isinf(f));
  956. if (f == -INFINITY)
  957. SERIAL_PROTOCOLPGM(" Minus Infinity detected.");
  958. SERIAL_EOL;
  959. }
  960. static int ubl_state_at_invocation = 0,
  961. ubl_state_recursion_chk = 0;
  962. void save_ubl_active_state_and_disable() {
  963. ubl_state_recursion_chk++;
  964. if (ubl_state_recursion_chk != 1) {
  965. SERIAL_ECHOLNPGM("save_ubl_active_state_and_disabled() called multiple times in a row.");
  966. lcd_setstatuspgm("save_UBL_active() error");
  967. lcd_quick_feedback();
  968. return;
  969. }
  970. ubl_state_at_invocation = ubl.state.active;
  971. ubl.state.active = 0;
  972. }
  973. void restore_ubl_active_state_and_leave() {
  974. if (--ubl_state_recursion_chk) {
  975. SERIAL_ECHOLNPGM("restore_ubl_active_state_and_leave() called too many times.");
  976. lcd_setstatuspgm("restore_UBL_active() error");
  977. lcd_quick_feedback();
  978. return;
  979. }
  980. ubl.state.active = ubl_state_at_invocation;
  981. }
  982. /**
  983. * Much of the 'What?' command can be eliminated. But until we are fully debugged, it is
  984. * good to have the extra information. Soon... we prune this to just a few items
  985. */
  986. void g29_what_command() {
  987. const uint16_t k = E2END - ubl.eeprom_start;
  988. SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version " UBL_VERSION " ");
  989. if (ubl.state.active)
  990. SERIAL_PROTOCOLCHAR('A');
  991. else
  992. SERIAL_PROTOCOLPGM("Ina");
  993. SERIAL_PROTOCOLLNPGM("ctive.\n");
  994. safe_delay(50);
  995. if (ubl.state.eeprom_storage_slot == -1)
  996. SERIAL_PROTOCOLPGM("No Mesh Loaded.");
  997. else {
  998. SERIAL_PROTOCOLPAIR("Mesh ", ubl.state.eeprom_storage_slot);
  999. SERIAL_PROTOCOLPGM(" Loaded.");
  1000. }
  1001. SERIAL_EOL;
  1002. safe_delay(50);
  1003. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  1004. SERIAL_PROTOCOLLNPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
  1005. #endif
  1006. SERIAL_PROTOCOLPGM("z_offset: ");
  1007. SERIAL_PROTOCOL_F(ubl.state.z_offset, 6);
  1008. SERIAL_EOL;
  1009. safe_delay(50);
  1010. SERIAL_PROTOCOLPGM("X-Axis Mesh Points at: ");
  1011. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  1012. SERIAL_PROTOCOL_F(LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[i]), 1);
  1013. SERIAL_PROTOCOLPGM(" ");
  1014. safe_delay(50);
  1015. }
  1016. SERIAL_EOL;
  1017. SERIAL_PROTOCOLPGM("Y-Axis Mesh Points at: ");
  1018. for (uint8_t i = 0; i < UBL_MESH_NUM_Y_POINTS; i++) {
  1019. SERIAL_PROTOCOL_F(LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[i]), 1);
  1020. SERIAL_PROTOCOLPGM(" ");
  1021. safe_delay(50);
  1022. }
  1023. SERIAL_EOL;
  1024. #if HAS_KILL
  1025. SERIAL_PROTOCOLPAIR("Kill pin on :", KILL_PIN);
  1026. SERIAL_PROTOCOLLNPAIR(" state:", READ(KILL_PIN));
  1027. #endif
  1028. SERIAL_EOL;
  1029. safe_delay(50);
  1030. SERIAL_PROTOCOLLNPAIR("ubl_state_at_invocation :", ubl_state_at_invocation);
  1031. SERIAL_EOL;
  1032. SERIAL_PROTOCOLLNPAIR("ubl_state_recursion_chk :", ubl_state_recursion_chk);
  1033. SERIAL_EOL;
  1034. safe_delay(50);
  1035. SERIAL_PROTOCOLLNPAIR("Free EEPROM space starts at: 0x", hex_word(ubl.eeprom_start));
  1036. SERIAL_PROTOCOLLNPAIR("end of EEPROM : 0x", hex_word(E2END));
  1037. safe_delay(50);
  1038. SERIAL_PROTOCOLLNPAIR("sizeof(ubl) : ", (int)sizeof(ubl));
  1039. SERIAL_EOL;
  1040. SERIAL_PROTOCOLLNPAIR("z_value[][] size: ", (int)sizeof(ubl.z_values));
  1041. SERIAL_EOL;
  1042. safe_delay(50);
  1043. SERIAL_PROTOCOLLNPAIR("EEPROM free for UBL: 0x", hex_word(k));
  1044. safe_delay(50);
  1045. SERIAL_PROTOCOLPAIR("EEPROM can hold ", k / sizeof(ubl.z_values));
  1046. SERIAL_PROTOCOLLNPGM(" meshes.\n");
  1047. safe_delay(50);
  1048. SERIAL_PROTOCOLPAIR("sizeof(ubl.state) : ", (int)sizeof(ubl.state));
  1049. SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_X_POINTS ", UBL_MESH_NUM_X_POINTS);
  1050. SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_Y_POINTS ", UBL_MESH_NUM_Y_POINTS);
  1051. safe_delay(50);
  1052. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_X ", UBL_MESH_MIN_X);
  1053. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_Y ", UBL_MESH_MIN_Y);
  1054. safe_delay(50);
  1055. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_X ", UBL_MESH_MAX_X);
  1056. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_Y ", UBL_MESH_MAX_Y);
  1057. safe_delay(50);
  1058. SERIAL_PROTOCOLPGM("\nMESH_X_DIST ");
  1059. SERIAL_PROTOCOL_F(MESH_X_DIST, 6);
  1060. SERIAL_PROTOCOLPGM("\nMESH_Y_DIST ");
  1061. SERIAL_PROTOCOL_F(MESH_Y_DIST, 6);
  1062. SERIAL_EOL;
  1063. safe_delay(50);
  1064. if (!ubl.sanity_check())
  1065. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling sanity checks passed.");
  1066. }
  1067. /**
  1068. * When we are fully debugged, the EEPROM dump command will get deleted also. But
  1069. * right now, it is good to have the extra information. Soon... we prune this.
  1070. */
  1071. void g29_eeprom_dump() {
  1072. unsigned char cccc;
  1073. uint16_t kkkk;
  1074. SERIAL_ECHO_START;
  1075. SERIAL_ECHOLNPGM("EEPROM Dump:");
  1076. for (uint16_t i = 0; i < E2END + 1; i += 16) {
  1077. if (!(i & 0x3)) idle();
  1078. print_hex_word(i);
  1079. SERIAL_ECHOPGM(": ");
  1080. for (uint16_t j = 0; j < 16; j++) {
  1081. kkkk = i + j;
  1082. eeprom_read_block(&cccc, (void *)kkkk, 1);
  1083. print_hex_byte(cccc);
  1084. SERIAL_ECHO(' ');
  1085. }
  1086. SERIAL_EOL;
  1087. }
  1088. SERIAL_EOL;
  1089. }
  1090. /**
  1091. * When we are fully debugged, this may go away. But there are some valid
  1092. * use cases for the users. So we can wait and see what to do with it.
  1093. */
  1094. void g29_compare_current_mesh_to_stored_mesh() {
  1095. float tmp_z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS];
  1096. if (!code_has_value()) {
  1097. SERIAL_PROTOCOLLNPGM("?Mesh # required.\n");
  1098. return;
  1099. }
  1100. storage_slot = code_value_int();
  1101. int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(tmp_z_values);
  1102. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  1103. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  1104. return;
  1105. }
  1106. j = UBL_LAST_EEPROM_INDEX - (storage_slot + 1) * sizeof(tmp_z_values);
  1107. eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
  1108. SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
  1109. SERIAL_PROTOCOLLNPAIR(" loaded from EEPROM address 0x", hex_word(j)); // Soon, we can remove the extra clutter of printing
  1110. // the address in the EEPROM where the Mesh is stored.
  1111. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  1112. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  1113. ubl.z_values[x][y] -= tmp_z_values[x][y];
  1114. }
  1115. mesh_index_pair find_closest_mesh_point_of_type(const MeshPointType type, const float &lx, const float &ly, const bool probe_as_reference, unsigned int bits[16], bool far_flag) {
  1116. float distance, closest = far_flag ? -99999.99 : 99999.99;
  1117. mesh_index_pair return_val;
  1118. return_val.x_index = return_val.y_index = -1;
  1119. const float current_x = current_position[X_AXIS],
  1120. current_y = current_position[Y_AXIS];
  1121. // Get our reference position. Either the nozzle or probe location.
  1122. const float px = lx - (probe_as_reference ? X_PROBE_OFFSET_FROM_EXTRUDER : 0),
  1123. py = ly - (probe_as_reference ? Y_PROBE_OFFSET_FROM_EXTRUDER : 0);
  1124. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  1125. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  1126. if ( (type == INVALID && isnan(ubl.z_values[i][j])) // Check to see if this location holds the right thing
  1127. || (type == REAL && !isnan(ubl.z_values[i][j]))
  1128. || (type == SET_IN_BITMAP && is_bit_set(bits, i, j))
  1129. ) {
  1130. // We only get here if we found a Mesh Point of the specified type
  1131. const float rawx = ubl.mesh_index_to_xpos[i], // Check if we can probe this mesh location
  1132. rawy = ubl.mesh_index_to_ypos[j];
  1133. // If using the probe as the reference there are some unreachable locations.
  1134. // Prune them from the list and ignore them till the next Phase (manual nozzle probing).
  1135. if (probe_as_reference &&
  1136. (!WITHIN(rawx, MIN_PROBE_X, MAX_PROBE_X) || !WITHIN(rawy, MIN_PROBE_Y, MAX_PROBE_Y))
  1137. ) continue;
  1138. // Unreachable. Check if it's the closest location to the nozzle.
  1139. // Add in a weighting factor that considers the current location of the nozzle.
  1140. const float mx = LOGICAL_X_POSITION(rawx), // Check if we can probe this mesh location
  1141. my = LOGICAL_Y_POSITION(rawy);
  1142. distance = HYPOT(px - mx, py - my) + HYPOT(current_x - mx, current_y - my) * 0.1;
  1143. if (far_flag) { // If doing the far_flag action, we want to be as far as possible
  1144. for (uint8_t k = 0; k < UBL_MESH_NUM_X_POINTS; k++) { // from the starting point and from any other probed points. We
  1145. for (uint8_t l = 0; l < UBL_MESH_NUM_Y_POINTS; l++) { // want the next point spread out and filling in any blank spaces
  1146. if (!isnan(ubl.z_values[k][l])) { // in the mesh. So we add in some of the distance to every probed
  1147. distance += sq(i - k) * (MESH_X_DIST) * .05 // point we can find.
  1148. + sq(j - l) * (MESH_Y_DIST) * .05;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. if (far_flag == (distance > closest) && distance != closest) { // if far_flag, look for farthest point
  1154. closest = distance; // We found a closer/farther location with
  1155. return_val.x_index = i; // the specified type of mesh value.
  1156. return_val.y_index = j;
  1157. return_val.distance = closest;
  1158. }
  1159. }
  1160. } // for j
  1161. } // for i
  1162. return return_val;
  1163. }
  1164. void fine_tune_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map) {
  1165. mesh_index_pair location;
  1166. uint16_t not_done[16];
  1167. int32_t round_off;
  1168. save_ubl_active_state_and_disable();
  1169. memset(not_done, 0xFF, sizeof(not_done));
  1170. #if ENABLED(ULTRA_LCD)
  1171. lcd_setstatuspgm("Fine Tuning Mesh");
  1172. #endif
  1173. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1174. do_blocking_move_to_xy(lx, ly);
  1175. do {
  1176. if (do_ubl_mesh_map) ubl.display_map(map_type);
  1177. location = find_closest_mesh_point_of_type( SET_IN_BITMAP, lx, ly, 0, not_done, false); // The '0' says we want to use the nozzle's position
  1178. // It doesn't matter if the probe can not reach this
  1179. // location. This is a manual edit of the Mesh Point.
  1180. if (location.x_index < 0 && location.y_index < 0) continue; // abort if we can't find any more points.
  1181. bit_clear(not_done, location.x_index, location.y_index); // Mark this location as 'adjusted' so we will find a
  1182. // different location the next time through the loop
  1183. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  1184. rawy = ubl.mesh_index_to_ypos[location.y_index];
  1185. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  1186. if (!WITHIN(rawx, X_MIN_POS, X_MAX_POS) || !WITHIN(rawy, Y_MIN_POS, Y_MAX_POS)) { // In theory, we don't need this check.
  1187. SERIAL_ERROR_START;
  1188. SERIAL_ERRORLNPGM("Attempt to edit off the bed."); // This really can't happen, but do the check for now
  1189. ubl.has_control_of_lcd_panel = false;
  1190. goto FINE_TUNE_EXIT;
  1191. }
  1192. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE); // Move the nozzle to where we are going to edit
  1193. do_blocking_move_to_xy(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy));
  1194. float new_z = ubl.z_values[location.x_index][location.y_index];
  1195. round_off = (int32_t)(new_z * 1000.0); // we chop off the last digits just to be clean. We are rounding to the
  1196. new_z = float(round_off) / 1000.0;
  1197. KEEPALIVE_STATE(PAUSED_FOR_USER);
  1198. ubl.has_control_of_lcd_panel = true;
  1199. lcd_implementation_clear();
  1200. lcd_mesh_edit_setup(new_z);
  1201. do {
  1202. new_z = lcd_mesh_edit();
  1203. idle();
  1204. } while (!ubl_lcd_clicked());
  1205. lcd_return_to_status();
  1206. ubl.has_control_of_lcd_panel = true; // There is a race condition for the Encoder Wheel getting clicked.
  1207. // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
  1208. // or here.
  1209. const millis_t nxt = millis() + 1500UL;
  1210. while (ubl_lcd_clicked()) { // debounce and watch for abort
  1211. idle();
  1212. if (ELAPSED(millis(), nxt)) {
  1213. lcd_return_to_status();
  1214. //SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
  1215. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1216. lcd_setstatuspgm("Mesh Editing Stopped");
  1217. while (ubl_lcd_clicked()) idle();
  1218. goto FINE_TUNE_EXIT;
  1219. }
  1220. }
  1221. safe_delay(20); // We don't want any switch noise.
  1222. ubl.z_values[location.x_index][location.y_index] = new_z;
  1223. lcd_implementation_clear();
  1224. } while (location.x_index >= 0 && location.y_index >= 0 && --repetition_cnt);
  1225. FINE_TUNE_EXIT:
  1226. ubl.has_control_of_lcd_panel = false;
  1227. KEEPALIVE_STATE(IN_HANDLER);
  1228. if (do_ubl_mesh_map) ubl.display_map(map_type);
  1229. restore_ubl_active_state_and_leave();
  1230. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1231. do_blocking_move_to_xy(lx, ly);
  1232. #if ENABLED(ULTRA_LCD)
  1233. lcd_setstatuspgm("Done Editing Mesh");
  1234. #endif
  1235. SERIAL_ECHOLNPGM("Done Editing Mesh");
  1236. }
  1237. #endif // AUTO_BED_LEVELING_UBL