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 64KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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) && ENABLED(UBL_G26_MESH_EDITING)
  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. void lcd_setstatus(const char* message, bool persist);
  301. #endif
  302. void gcode_G29() {
  303. SERIAL_PROTOCOLLNPAIR("ubl.eeprom_start=", ubl.eeprom_start);
  304. if (ubl.eeprom_start < 0) {
  305. SERIAL_PROTOCOLLNPGM("?You need to enable your EEPROM and initialize it");
  306. SERIAL_PROTOCOLLNPGM("with M502, M500, M501 in that order.\n");
  307. return;
  308. }
  309. if (!code_seen('N') && axis_unhomed_error(true, true, true)) // Don't allow auto-leveling without homing first
  310. gcode_G28();
  311. if (g29_parameter_parsing()) return; // abort if parsing the simple parameters causes a problem,
  312. // Invalidate Mesh Points. This command is a little bit asymetrical because
  313. // it directly specifies the repetition count and does not use the 'R' parameter.
  314. if (code_seen('I')) {
  315. repetition_cnt = code_has_value() ? code_value_int() : 1;
  316. while (repetition_cnt--) {
  317. 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
  318. if (location.x_index < 0) {
  319. SERIAL_PROTOCOLLNPGM("Entire Mesh invalidated.\n");
  320. break; // No more invalid Mesh Points to populate
  321. }
  322. ubl.z_values[location.x_index][location.y_index] = NAN;
  323. }
  324. SERIAL_PROTOCOLLNPGM("Locations invalidated.\n");
  325. }
  326. if (code_seen('Q')) {
  327. const int test_pattern = code_has_value() ? code_value_int() : -1;
  328. if (!WITHIN(test_pattern, 0, 2)) {
  329. SERIAL_PROTOCOLLNPGM("Invalid test_pattern value. (0-2)\n");
  330. return;
  331. }
  332. SERIAL_PROTOCOLLNPGM("Loading test_pattern values.\n");
  333. switch (test_pattern) {
  334. case 0:
  335. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) { // Create a bowl shape - similar to
  336. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++) { // a poorly calibrated Delta.
  337. const float p1 = 0.5 * (UBL_MESH_NUM_X_POINTS) - x,
  338. p2 = 0.5 * (UBL_MESH_NUM_Y_POINTS) - y;
  339. ubl.z_values[x][y] += 2.0 * HYPOT(p1, p2);
  340. }
  341. }
  342. break;
  343. case 1:
  344. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++) { // Create a diagonal line several Mesh cells thick that is raised
  345. ubl.z_values[x][x] += 9.999;
  346. 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
  347. }
  348. break;
  349. case 2:
  350. // Allow the user to specify the height because 10mm is a little extreme in some cases.
  351. 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
  352. for (uint8_t y = (UBL_MESH_NUM_Y_POINTS) / 3; y < 2 * (UBL_MESH_NUM_Y_POINTS) / 3; y++) // the center of the bed
  353. ubl.z_values[x][y] += code_seen('C') ? ubl_constant : 9.99;
  354. break;
  355. }
  356. }
  357. /*
  358. if (code_seen('U')) {
  359. unlevel_value = code_value_int();
  360. //if (!WITHIN(unlevel_value, 0, 7)) {
  361. // SERIAL_PROTOCOLLNPGM("Invalid Unlevel value. (0-4)\n");
  362. // return;
  363. //}
  364. }
  365. //*/
  366. if (code_seen('P')) {
  367. phase_value = code_value_int();
  368. if (!WITHIN(phase_value, 0, 7)) {
  369. SERIAL_PROTOCOLLNPGM("Invalid Phase value. (0-4)\n");
  370. return;
  371. }
  372. switch (phase_value) {
  373. case 0:
  374. //
  375. // Zero Mesh Data
  376. //
  377. ubl.reset();
  378. SERIAL_PROTOCOLLNPGM("Mesh zeroed.\n");
  379. break;
  380. case 1:
  381. //
  382. // Invalidate Entire Mesh and Automatically Probe Mesh in areas that can be reached by the probe
  383. //
  384. if (!code_seen('C') ) {
  385. ubl.invalidate();
  386. SERIAL_PROTOCOLLNPGM("Mesh invalidated. Probing mesh.\n");
  387. }
  388. if (g29_verbose_level > 1) {
  389. SERIAL_ECHOPGM("Probing Mesh Points Closest to (");
  390. SERIAL_ECHO(x_pos);
  391. SERIAL_ECHOPAIR(",", y_pos);
  392. SERIAL_PROTOCOLLNPGM(")\n");
  393. }
  394. probe_entire_mesh(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER,
  395. code_seen('O') || code_seen('M'), code_seen('E'), code_seen('U'));
  396. break;
  397. case 2: {
  398. //
  399. // Manually Probe Mesh in areas that can't be reached by the probe
  400. //
  401. SERIAL_PROTOCOLLNPGM("Manually probing unreachable mesh locations.\n");
  402. do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
  403. if (!x_flag && !y_flag) { // use a good default location for the path
  404. x_pos = X_MIN_POS;
  405. y_pos = Y_MIN_POS;
  406. if (X_PROBE_OFFSET_FROM_EXTRUDER > 0) // The flipped > and < operators on these two comparisons is
  407. x_pos = X_MAX_POS; // intentional. It should cause the probed points to follow a
  408. if (Y_PROBE_OFFSET_FROM_EXTRUDER < 0) // nice path on Cartesian printers. It may make sense to
  409. y_pos = Y_MAX_POS; // have Delta printers default to the center of the bed.
  410. } // For now, until that is decided, it can be forced with the X
  411. // and Y parameters.
  412. if (code_seen('C')) {
  413. x_pos = current_position[X_AXIS];
  414. y_pos = current_position[Y_AXIS];
  415. }
  416. const float height = code_seen('H') && code_has_value() ? code_value_float() : Z_CLEARANCE_BETWEEN_PROBES;
  417. if (code_seen('B')) {
  418. card_thickness = code_has_value() ? code_value_float() : measure_business_card_thickness(height);
  419. if (fabs(card_thickness) > 1.5) {
  420. SERIAL_PROTOCOLLNPGM("?Error in Business Card measurement.\n");
  421. return;
  422. }
  423. }
  424. manually_probe_remaining_mesh(x_pos, y_pos, height, card_thickness, code_seen('O') || code_seen('M'));
  425. } break;
  426. case 3: {
  427. //
  428. // Populate invalid Mesh areas with a constant
  429. //
  430. const float height = code_seen('C') ? ubl_constant : 0.0;
  431. // If no repetition is specified, do the whole Mesh
  432. if (!repeat_flag) repetition_cnt = 9999;
  433. while (repetition_cnt--) {
  434. 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
  435. if (location.x_index < 0) break; // No more invalid Mesh Points to populate
  436. ubl.z_values[location.x_index][location.y_index] = height;
  437. }
  438. } break;
  439. case 4:
  440. //
  441. // Fine Tune (i.e., Edit) the Mesh
  442. //
  443. fine_tune_mesh(x_pos, y_pos, code_seen('O') || code_seen('M'));
  444. break;
  445. case 5:
  446. find_mean_mesh_height();
  447. break;
  448. case 6:
  449. shift_mesh_height();
  450. break;
  451. case 10:
  452. // [DEBUG] Pay no attention to this stuff. It can be removed soon.
  453. SERIAL_ECHO_START;
  454. SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
  455. KEEPALIVE_STATE(PAUSED_FOR_USER);
  456. ubl.has_control_of_lcd_panel++;
  457. while (!ubl_lcd_clicked()) {
  458. safe_delay(250);
  459. if (ubl.encoder_diff) {
  460. SERIAL_ECHOLN((int)ubl.encoder_diff);
  461. ubl.encoder_diff = 0;
  462. }
  463. }
  464. SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
  465. ubl.has_control_of_lcd_panel = false;
  466. KEEPALIVE_STATE(IN_HANDLER);
  467. break;
  468. case 11:
  469. // [DEBUG] wait_for_user code. Pay no attention to this stuff. It can be removed soon.
  470. SERIAL_ECHO_START;
  471. SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
  472. KEEPALIVE_STATE(PAUSED_FOR_USER);
  473. wait_for_user = true;
  474. while (wait_for_user) {
  475. safe_delay(250);
  476. if (ubl.encoder_diff) {
  477. SERIAL_ECHOLN((int)ubl.encoder_diff);
  478. ubl.encoder_diff = 0;
  479. }
  480. }
  481. SERIAL_ECHOLNPGM("G29 giving back control of LCD Panel.");
  482. KEEPALIVE_STATE(IN_HANDLER);
  483. break;
  484. }
  485. }
  486. if (code_seen('T')) {
  487. const float lx1 = LOGICAL_X_POSITION(ubl_3_point_1_X),
  488. lx2 = LOGICAL_X_POSITION(ubl_3_point_2_X),
  489. lx3 = LOGICAL_X_POSITION(ubl_3_point_3_X),
  490. ly1 = LOGICAL_Y_POSITION(ubl_3_point_1_Y),
  491. ly2 = LOGICAL_Y_POSITION(ubl_3_point_2_Y),
  492. ly3 = LOGICAL_Y_POSITION(ubl_3_point_3_Y);
  493. float z1 = probe_pt(lx1, ly1, false /*Stow Flag*/, g29_verbose_level),
  494. z2 = probe_pt(lx2, ly2, false /*Stow Flag*/, g29_verbose_level),
  495. z3 = probe_pt(lx3, ly3, true /*Stow Flag*/, g29_verbose_level);
  496. // We need to adjust z1, z2, z3 by the Mesh Height at these points. Just because they are non-zero doesn't mean
  497. // the Mesh is tilted! (We need to compensate each probe point by what the Mesh says that location's height is)
  498. z1 -= ubl.get_z_correction(lx1, ly1);
  499. z2 -= ubl.get_z_correction(lx2, ly2);
  500. z3 -= ubl.get_z_correction(lx3, ly3);
  501. do_blocking_move_to_xy((X_MAX_POS - (X_MIN_POS)) / 2.0, (Y_MAX_POS - (Y_MIN_POS)) / 2.0);
  502. tilt_mesh_based_on_3pts(z1, z2, z3);
  503. }
  504. //
  505. // Much of the 'What?' command can be eliminated. But until we are fully debugged, it is
  506. // good to have the extra information. Soon... we prune this to just a few items
  507. //
  508. if (code_seen('W')) g29_what_command();
  509. //
  510. // When we are fully debugged, the EEPROM dump command will get deleted also. But
  511. // right now, it is good to have the extra information. Soon... we prune this.
  512. //
  513. if (code_seen('J')) g29_eeprom_dump(); // EEPROM Dump
  514. //
  515. // When we are fully debugged, this may go away. But there are some valid
  516. // use cases for the users. So we can wait and see what to do with it.
  517. //
  518. if (code_seen('K')) // Kompare Current Mesh Data to Specified Stored Mesh
  519. g29_compare_current_mesh_to_stored_mesh();
  520. //
  521. // Load a Mesh from the EEPROM
  522. //
  523. if (code_seen('L')) { // Load Current Mesh Data
  524. storage_slot = code_has_value() ? code_value_int() : ubl.state.eeprom_storage_slot;
  525. const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(ubl.z_values);
  526. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  527. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  528. return;
  529. }
  530. ubl.load_mesh(storage_slot);
  531. ubl.state.eeprom_storage_slot = storage_slot;
  532. if (storage_slot != ubl.state.eeprom_storage_slot)
  533. ubl.store_state();
  534. SERIAL_PROTOCOLLNPGM("Done.\n");
  535. }
  536. //
  537. // Store a Mesh in the EEPROM
  538. //
  539. if (code_seen('S')) { // Store (or Save) Current Mesh Data
  540. storage_slot = code_has_value() ? code_value_int() : ubl.state.eeprom_storage_slot;
  541. if (storage_slot == -1) { // Special case, we are going to 'Export' the mesh to the
  542. SERIAL_ECHOLNPGM("G29 I 999"); // host in a form it can be reconstructed on a different machine
  543. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  544. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  545. if (!isnan(ubl.z_values[x][y])) {
  546. SERIAL_ECHOPAIR("M421 I ", x);
  547. SERIAL_ECHOPAIR(" J ", y);
  548. SERIAL_ECHOPGM(" Z ");
  549. SERIAL_ECHO_F(ubl.z_values[x][y], 6);
  550. SERIAL_EOL;
  551. }
  552. return;
  553. }
  554. const int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(ubl.z_values);
  555. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  556. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  557. SERIAL_PROTOCOLLNPAIR("?Use 0 to ", j - 1);
  558. goto LEAVE;
  559. }
  560. ubl.store_mesh(storage_slot);
  561. ubl.state.eeprom_storage_slot = storage_slot;
  562. //
  563. // if (storage_slot != ubl.state.eeprom_storage_slot)
  564. ubl.store_state(); // Always save an updated copy of the UBL State info
  565. SERIAL_PROTOCOLLNPGM("Done.\n");
  566. }
  567. if (code_seen('O') || code_seen('M'))
  568. ubl.display_map(code_has_value() ? code_value_int() : 0);
  569. if (code_seen('Z')) {
  570. if (code_has_value())
  571. ubl.state.z_offset = code_value_float(); // do the simple case. Just lock in the specified value
  572. else {
  573. save_ubl_active_state_and_disable();
  574. //measured_z = probe_pt(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER, ProbeDeployAndStow, g29_verbose_level);
  575. ubl.has_control_of_lcd_panel++; // Grab the LCD Hardware
  576. measured_z = 1.5;
  577. do_blocking_move_to_z(measured_z); // Get close to the bed, but leave some space so we don't damage anything
  578. // The user is not going to be locking in a new Z-Offset very often so
  579. // it won't be that painful to spin the Encoder Wheel for 1.5mm
  580. lcd_implementation_clear();
  581. lcd_z_offset_edit_setup(measured_z);
  582. KEEPALIVE_STATE(PAUSED_FOR_USER);
  583. do {
  584. measured_z = lcd_z_offset_edit();
  585. idle();
  586. do_blocking_move_to_z(measured_z);
  587. } while (!ubl_lcd_clicked());
  588. ubl.has_control_of_lcd_panel++; // There is a race condition for the Encoder Wheel getting clicked.
  589. // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
  590. // or here. So, until we are done looking for a long Encoder Wheel Press,
  591. // we need to take control of the panel
  592. KEEPALIVE_STATE(IN_HANDLER);
  593. lcd_return_to_status();
  594. const millis_t nxt = millis() + 1500UL;
  595. while (ubl_lcd_clicked()) { // debounce and watch for abort
  596. idle();
  597. if (ELAPSED(millis(), nxt)) {
  598. SERIAL_PROTOCOLLNPGM("\nZ-Offset Adjustment Stopped.");
  599. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  600. lcd_setstatus("Z-Offset Stopped", true);
  601. restore_ubl_active_state_and_leave();
  602. goto LEAVE;
  603. }
  604. }
  605. ubl.has_control_of_lcd_panel = false;
  606. safe_delay(20); // We don't want any switch noise.
  607. ubl.state.z_offset = measured_z;
  608. lcd_implementation_clear();
  609. restore_ubl_active_state_and_leave();
  610. }
  611. }
  612. LEAVE:
  613. #if ENABLED(ULTRA_LCD)
  614. lcd_setstatus(" ", true);
  615. lcd_quick_feedback();
  616. #endif
  617. ubl.has_control_of_lcd_panel = false;
  618. }
  619. void find_mean_mesh_height() {
  620. uint8_t x, y;
  621. int n;
  622. float sum, sum_of_diff_squared, sigma, difference, mean;
  623. sum = sum_of_diff_squared = 0.0;
  624. n = 0;
  625. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  626. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  627. if (!isnan(ubl.z_values[x][y])) {
  628. sum += ubl.z_values[x][y];
  629. n++;
  630. }
  631. mean = sum / n;
  632. //
  633. // Now do the sumation of the squares of difference from mean
  634. //
  635. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  636. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  637. if (!isnan(ubl.z_values[x][y])) {
  638. difference = (ubl.z_values[x][y] - mean);
  639. sum_of_diff_squared += difference * difference;
  640. }
  641. SERIAL_ECHOLNPAIR("# of samples: ", n);
  642. SERIAL_ECHOPGM("Mean Mesh Height: ");
  643. SERIAL_ECHO_F(mean, 6);
  644. SERIAL_EOL;
  645. sigma = sqrt(sum_of_diff_squared / (n + 1));
  646. SERIAL_ECHOPGM("Standard Deviation: ");
  647. SERIAL_ECHO_F(sigma, 6);
  648. SERIAL_EOL;
  649. if (c_flag)
  650. for (x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  651. for (y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  652. if (!isnan(ubl.z_values[x][y]))
  653. ubl.z_values[x][y] -= mean + ubl_constant;
  654. }
  655. void shift_mesh_height() {
  656. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  657. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  658. if (!isnan(ubl.z_values[x][y]))
  659. ubl.z_values[x][y] += ubl_constant;
  660. }
  661. /**
  662. * Probe all invalidated locations of the mesh that can be reached by the probe.
  663. * This attempts to fill in locations closest to the nozzle's start location first.
  664. */
  665. void probe_entire_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map, const bool stow_probe, bool do_furthest) {
  666. mesh_index_pair location;
  667. ubl.has_control_of_lcd_panel++;
  668. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  669. DEPLOY_PROBE();
  670. do {
  671. if (ubl_lcd_clicked()) {
  672. SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.\n");
  673. lcd_quick_feedback();
  674. STOW_PROBE();
  675. while (ubl_lcd_clicked()) idle();
  676. ubl.has_control_of_lcd_panel = false;
  677. restore_ubl_active_state_and_leave();
  678. safe_delay(50); // Debounce the Encoder wheel
  679. return;
  680. }
  681. 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
  682. if (location.x_index >= 0 && location.y_index >= 0) {
  683. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  684. rawy = ubl.mesh_index_to_ypos[location.y_index];
  685. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  686. if (!WITHIN(rawx, MIN_PROBE_X, MAX_PROBE_X) || !WITHIN(rawy, MIN_PROBE_Y, MAX_PROBE_Y)) {
  687. SERIAL_ERROR_START;
  688. SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
  689. ubl.has_control_of_lcd_panel = false;
  690. goto LEAVE;
  691. }
  692. const float measured_z = probe_pt(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy), stow_probe, g29_verbose_level);
  693. ubl.z_values[location.x_index][location.y_index] = measured_z;
  694. }
  695. if (do_ubl_mesh_map) ubl.display_map(map_type);
  696. } while (location.x_index >= 0 && location.y_index >= 0);
  697. LEAVE:
  698. STOW_PROBE();
  699. restore_ubl_active_state_and_leave();
  700. do_blocking_move_to_xy(
  701. constrain(lx - (X_PROBE_OFFSET_FROM_EXTRUDER), X_MIN_POS, X_MAX_POS),
  702. constrain(ly - (Y_PROBE_OFFSET_FROM_EXTRUDER), Y_MIN_POS, Y_MAX_POS)
  703. );
  704. }
  705. vector_3 tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3) {
  706. float c, d, t;
  707. int i, j;
  708. vector_3 v1 = vector_3( (ubl_3_point_1_X - ubl_3_point_2_X),
  709. (ubl_3_point_1_Y - ubl_3_point_2_Y),
  710. (z1 - z2) ),
  711. v2 = vector_3( (ubl_3_point_3_X - ubl_3_point_2_X),
  712. (ubl_3_point_3_Y - ubl_3_point_2_Y),
  713. (z3 - z2) ),
  714. normal = vector_3::cross(v1, v2);
  715. // printf("[%f,%f,%f] ", normal.x, normal.y, normal.z);
  716. /**
  717. * This code does two things. This vector is normal to the tilted plane.
  718. * However, we don't know its direction. We need it to point up. So if
  719. * Z is negative, we need to invert the sign of all components of the vector
  720. * We also need Z to be unity because we are going to be treating this triangle
  721. * as the sin() and cos() of the bed's tilt
  722. */
  723. const float inv_z = 1.0 / normal.z;
  724. normal.x *= inv_z;
  725. normal.y *= inv_z;
  726. normal.z = 1.0;
  727. //
  728. // All of 3 of these points should give us the same d constant
  729. //
  730. t = normal.x * ubl_3_point_1_X + normal.y * ubl_3_point_1_Y;
  731. d = t + normal.z * z1;
  732. c = d - t;
  733. SERIAL_ECHOPGM("d from 1st point: ");
  734. SERIAL_ECHO_F(d, 6);
  735. SERIAL_ECHOPGM(" c: ");
  736. SERIAL_ECHO_F(c, 6);
  737. SERIAL_EOL;
  738. t = normal.x * ubl_3_point_2_X + normal.y * ubl_3_point_2_Y;
  739. d = t + normal.z * z2;
  740. c = d - t;
  741. SERIAL_ECHOPGM("d from 2nd point: ");
  742. SERIAL_ECHO_F(d, 6);
  743. SERIAL_ECHOPGM(" c: ");
  744. SERIAL_ECHO_F(c, 6);
  745. SERIAL_EOL;
  746. t = normal.x * ubl_3_point_3_X + normal.y * ubl_3_point_3_Y;
  747. d = t + normal.z * z3;
  748. c = d - t;
  749. SERIAL_ECHOPGM("d from 3rd point: ");
  750. SERIAL_ECHO_F(d, 6);
  751. SERIAL_ECHOPGM(" c: ");
  752. SERIAL_ECHO_F(c, 6);
  753. SERIAL_EOL;
  754. for (i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  755. for (j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  756. c = -((normal.x * (UBL_MESH_MIN_X + i * (MESH_X_DIST)) + normal.y * (UBL_MESH_MIN_Y + j * (MESH_Y_DIST))) - d);
  757. ubl.z_values[i][j] += c;
  758. }
  759. }
  760. return normal;
  761. }
  762. float use_encoder_wheel_to_measure_point() {
  763. KEEPALIVE_STATE(PAUSED_FOR_USER);
  764. while (!ubl_lcd_clicked()) { // we need the loop to move the nozzle based on the encoder wheel here!
  765. idle();
  766. if (ubl.encoder_diff) {
  767. do_blocking_move_to_z(current_position[Z_AXIS] + 0.01 * float(ubl.encoder_diff));
  768. ubl.encoder_diff = 0;
  769. }
  770. }
  771. KEEPALIVE_STATE(IN_HANDLER);
  772. return current_position[Z_AXIS];
  773. }
  774. float measure_business_card_thickness(const float &in_height) {
  775. ubl.has_control_of_lcd_panel++;
  776. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  777. SERIAL_PROTOCOLLNPGM("Place Shim Under Nozzle and Perform Measurement.");
  778. do_blocking_move_to_z(in_height);
  779. 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);
  780. //, min( planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])/2.0);
  781. const float z1 = use_encoder_wheel_to_measure_point();
  782. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  783. ubl.has_control_of_lcd_panel = false;
  784. SERIAL_PROTOCOLLNPGM("Remove Shim and Measure Bed Height.");
  785. const float z2 = use_encoder_wheel_to_measure_point();
  786. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  787. if (g29_verbose_level > 1) {
  788. SERIAL_PROTOCOLPGM("Business Card is: ");
  789. SERIAL_PROTOCOL_F(abs(z1 - z2), 6);
  790. SERIAL_PROTOCOLLNPGM("mm thick.");
  791. }
  792. restore_ubl_active_state_and_leave();
  793. return abs(z1 - z2);
  794. }
  795. 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) {
  796. ubl.has_control_of_lcd_panel++;
  797. save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
  798. do_blocking_move_to_z(z_clearance);
  799. do_blocking_move_to_xy(lx, ly);
  800. float last_x = -9999.99, last_y = -9999.99;
  801. mesh_index_pair location;
  802. do {
  803. if (do_ubl_mesh_map) ubl.display_map(map_type);
  804. location = find_closest_mesh_point_of_type(INVALID, lx, ly, 0, NULL, false); // The '0' says we want to use the nozzle's position
  805. // It doesn't matter if the probe can't reach the NAN location. This is a manual probe.
  806. if (location.x_index < 0 && location.y_index < 0) continue;
  807. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  808. rawy = ubl.mesh_index_to_ypos[location.y_index];
  809. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  810. if (!WITHIN(rawx, X_MIN_POS, X_MAX_POS) || !WITHIN(rawy, Y_MIN_POS, Y_MAX_POS)) {
  811. SERIAL_ERROR_START;
  812. SERIAL_ERRORLNPGM("Attempt to probe off the bed.");
  813. ubl.has_control_of_lcd_panel = false;
  814. goto LEAVE;
  815. }
  816. const float xProbe = LOGICAL_X_POSITION(rawx),
  817. yProbe = LOGICAL_Y_POSITION(rawy),
  818. dx = xProbe - last_x,
  819. dy = yProbe - last_y;
  820. if (HYPOT(dx, dy) < BIG_RAISE_NOT_NEEDED)
  821. do_blocking_move_to_z(current_position[Z_AXIS] + SIZE_OF_LITTLE_RAISE);
  822. else
  823. do_blocking_move_to_z(z_clearance);
  824. do_blocking_move_to_xy(xProbe, yProbe);
  825. last_x = xProbe;
  826. last_y = yProbe;
  827. KEEPALIVE_STATE(PAUSED_FOR_USER);
  828. ubl.has_control_of_lcd_panel = true;
  829. while (!ubl_lcd_clicked()) { // we need the loop to move the nozzle based on the encoder wheel here!
  830. idle();
  831. if (ubl.encoder_diff) {
  832. do_blocking_move_to_z(current_position[Z_AXIS] + float(ubl.encoder_diff) / 100.0);
  833. ubl.encoder_diff = 0;
  834. }
  835. }
  836. const millis_t nxt = millis() + 1500L;
  837. while (ubl_lcd_clicked()) { // debounce and watch for abort
  838. idle();
  839. if (ELAPSED(millis(), nxt)) {
  840. SERIAL_PROTOCOLLNPGM("\nMesh only partially populated.");
  841. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  842. lcd_quick_feedback();
  843. while (ubl_lcd_clicked()) idle();
  844. ubl.has_control_of_lcd_panel = false;
  845. KEEPALIVE_STATE(IN_HANDLER);
  846. restore_ubl_active_state_and_leave();
  847. return;
  848. }
  849. }
  850. ubl.z_values[location.x_index][location.y_index] = current_position[Z_AXIS] - card_thickness;
  851. if (g29_verbose_level > 2) {
  852. SERIAL_PROTOCOLPGM("Mesh Point Measured at: ");
  853. SERIAL_PROTOCOL_F(ubl.z_values[location.x_index][location.y_index], 6);
  854. SERIAL_EOL;
  855. }
  856. } while (location.x_index >= 0 && location.y_index >= 0);
  857. if (do_ubl_mesh_map) ubl.display_map(map_type);
  858. LEAVE:
  859. restore_ubl_active_state_and_leave();
  860. KEEPALIVE_STATE(IN_HANDLER);
  861. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  862. do_blocking_move_to_xy(lx, ly);
  863. }
  864. bool g29_parameter_parsing() {
  865. #if ENABLED(ULTRA_LCD)
  866. lcd_setstatus("Doing G29 UBL !", true);
  867. lcd_quick_feedback();
  868. #endif
  869. g29_verbose_level = code_seen('V') ? code_value_int() : 0;
  870. if (!WITHIN(g29_verbose_level, 0, 4)) {
  871. SERIAL_PROTOCOLLNPGM("Invalid Verbose Level specified. (0-4)\n");
  872. return UBL_ERR;
  873. }
  874. x_flag = code_seen('X') && code_has_value();
  875. x_pos = x_flag ? code_value_float() : current_position[X_AXIS];
  876. if (!WITHIN(RAW_X_POSITION(x_pos), X_MIN_POS, X_MAX_POS)) {
  877. SERIAL_PROTOCOLLNPGM("Invalid X location specified.\n");
  878. return UBL_ERR;
  879. }
  880. y_flag = code_seen('Y') && code_has_value();
  881. y_pos = y_flag ? code_value_float() : current_position[Y_AXIS];
  882. if (!WITHIN(RAW_Y_POSITION(y_pos), Y_MIN_POS, Y_MAX_POS)) {
  883. SERIAL_PROTOCOLLNPGM("Invalid Y location specified.\n");
  884. return UBL_ERR;
  885. }
  886. if (x_flag != y_flag) {
  887. SERIAL_PROTOCOLLNPGM("Both X & Y locations must be specified.\n");
  888. return UBL_ERR;
  889. }
  890. if (code_seen('A')) { // Activate the Unified Bed Leveling System
  891. ubl.state.active = 1;
  892. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling System activated.\n");
  893. ubl.store_state();
  894. }
  895. c_flag = code_seen('C') && code_has_value();
  896. ubl_constant = c_flag ? code_value_float() : 0.0;
  897. if (code_seen('D')) { // Disable the Unified Bed Leveling System
  898. ubl.state.active = 0;
  899. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling System de-activated.\n");
  900. ubl.store_state();
  901. }
  902. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  903. if (code_seen('F') && code_has_value()) {
  904. const float fh = code_value_float();
  905. if (!WITHIN(fh, 0.0, 100.0)) {
  906. SERIAL_PROTOCOLLNPGM("?Bed Level Correction Fade Height Not Plausible.\n");
  907. return UBL_ERR;
  908. }
  909. ubl.state.g29_correction_fade_height = fh;
  910. ubl.state.g29_fade_height_multiplier = 1.0 / fh;
  911. }
  912. #endif
  913. repeat_flag = code_seen('R');
  914. repetition_cnt = repeat_flag ? (code_has_value() ? code_value_int() : 9999) : 1;
  915. if (repetition_cnt < 1) {
  916. SERIAL_PROTOCOLLNPGM("Invalid Repetition count.\n");
  917. return UBL_ERR;
  918. }
  919. map_type = code_seen('O') && code_has_value() ? code_value_int() : 0;
  920. if (!WITHIN(map_type, 0, 1)) {
  921. SERIAL_PROTOCOLLNPGM("Invalid map type.\n");
  922. return UBL_ERR;
  923. }
  924. /*
  925. if (code_seen('M')) { // Check if a map type was specified
  926. map_type = 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. //*/
  933. return UBL_OK;
  934. }
  935. /**
  936. * This function goes away after G29 debug is complete. But for right now, it is a handy
  937. * routine to dump binary data structures.
  938. */
  939. void dump(char * const str, const float &f) {
  940. char *ptr;
  941. SERIAL_PROTOCOL(str);
  942. SERIAL_PROTOCOL_F(f, 8);
  943. SERIAL_PROTOCOLPGM(" ");
  944. ptr = (char*)&f;
  945. for (uint8_t i = 0; i < 4; i++)
  946. SERIAL_PROTOCOLPAIR(" ", hex_byte(*ptr++));
  947. SERIAL_PROTOCOLPAIR(" isnan()=", isnan(f));
  948. SERIAL_PROTOCOLPAIR(" isinf()=", isinf(f));
  949. if (f == -INFINITY)
  950. SERIAL_PROTOCOLPGM(" Minus Infinity detected.");
  951. SERIAL_EOL;
  952. }
  953. static int ubl_state_at_invocation = 0,
  954. ubl_state_recursion_chk = 0;
  955. void save_ubl_active_state_and_disable() {
  956. ubl_state_recursion_chk++;
  957. if (ubl_state_recursion_chk != 1) {
  958. SERIAL_ECHOLNPGM("save_ubl_active_state_and_disabled() called multiple times in a row.");
  959. lcd_setstatus("save_UBL_active() error", true);
  960. lcd_quick_feedback();
  961. return;
  962. }
  963. ubl_state_at_invocation = ubl.state.active;
  964. ubl.state.active = 0;
  965. }
  966. void restore_ubl_active_state_and_leave() {
  967. if (--ubl_state_recursion_chk) {
  968. SERIAL_ECHOLNPGM("restore_ubl_active_state_and_leave() called too many times.");
  969. lcd_setstatus("restore_UBL_active() error", true);
  970. lcd_quick_feedback();
  971. return;
  972. }
  973. ubl.state.active = ubl_state_at_invocation;
  974. }
  975. /**
  976. * Much of the 'What?' command can be eliminated. But until we are fully debugged, it is
  977. * good to have the extra information. Soon... we prune this to just a few items
  978. */
  979. void g29_what_command() {
  980. const uint16_t k = E2END - ubl.eeprom_start;
  981. SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version 1.00 ");
  982. if (ubl.state.active)
  983. SERIAL_PROTOCOLCHAR('A');
  984. else
  985. SERIAL_PROTOCOLPGM("In");
  986. SERIAL_PROTOCOLLNPGM("ctive.\n");
  987. safe_delay(50);
  988. if (ubl.state.eeprom_storage_slot == -1)
  989. SERIAL_PROTOCOLPGM("No Mesh Loaded.");
  990. else {
  991. SERIAL_PROTOCOLPAIR("Mesh ", ubl.state.eeprom_storage_slot);
  992. SERIAL_PROTOCOLPGM(" Loaded.");
  993. }
  994. SERIAL_EOL;
  995. safe_delay(50);
  996. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  997. SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
  998. SERIAL_EOL;
  999. #endif
  1000. SERIAL_PROTOCOLPGM("z_offset: ");
  1001. SERIAL_PROTOCOL_F(ubl.state.z_offset, 6);
  1002. SERIAL_EOL;
  1003. safe_delay(50);
  1004. SERIAL_PROTOCOLPGM("X-Axis Mesh Points at: ");
  1005. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  1006. SERIAL_PROTOCOL_F(LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[i]), 1);
  1007. SERIAL_PROTOCOLPGM(" ");
  1008. safe_delay(50);
  1009. }
  1010. SERIAL_EOL;
  1011. SERIAL_PROTOCOLPGM("Y-Axis Mesh Points at: ");
  1012. for (uint8_t i = 0; i < UBL_MESH_NUM_Y_POINTS; i++) {
  1013. SERIAL_PROTOCOL_F(LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[i]), 1);
  1014. SERIAL_PROTOCOLPGM(" ");
  1015. safe_delay(50);
  1016. }
  1017. SERIAL_EOL;
  1018. #if HAS_KILL
  1019. SERIAL_PROTOCOLPAIR("Kill pin on :", KILL_PIN);
  1020. SERIAL_PROTOCOLLNPAIR(" state:", READ(KILL_PIN));
  1021. #endif
  1022. SERIAL_EOL;
  1023. safe_delay(50);
  1024. SERIAL_PROTOCOLLNPAIR("ubl_state_at_invocation :", ubl_state_at_invocation);
  1025. SERIAL_EOL;
  1026. SERIAL_PROTOCOLLNPAIR("ubl_state_recursion_chk :", ubl_state_recursion_chk);
  1027. SERIAL_EOL;
  1028. safe_delay(50);
  1029. SERIAL_PROTOCOLLNPAIR("Free EEPROM space starts at: 0x", hex_word(ubl.eeprom_start));
  1030. SERIAL_PROTOCOLLNPAIR("end of EEPROM : 0x", hex_word(E2END));
  1031. safe_delay(50);
  1032. SERIAL_PROTOCOLLNPAIR("sizeof(ubl) : ", (int)sizeof(ubl));
  1033. SERIAL_EOL;
  1034. SERIAL_PROTOCOLLNPAIR("z_value[][] size: ", (int)sizeof(ubl.z_values));
  1035. SERIAL_EOL;
  1036. safe_delay(50);
  1037. SERIAL_PROTOCOLLNPAIR("EEPROM free for UBL: 0x", hex_word(k));
  1038. safe_delay(50);
  1039. SERIAL_PROTOCOLPAIR("EEPROM can hold ", k / sizeof(ubl.z_values));
  1040. SERIAL_PROTOCOLLNPGM(" meshes.\n");
  1041. safe_delay(50);
  1042. SERIAL_PROTOCOLPAIR("sizeof(ubl.state) : ", (int)sizeof(ubl.state));
  1043. SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_X_POINTS ", UBL_MESH_NUM_X_POINTS);
  1044. SERIAL_PROTOCOLPAIR("\nUBL_MESH_NUM_Y_POINTS ", UBL_MESH_NUM_Y_POINTS);
  1045. safe_delay(50);
  1046. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_X ", UBL_MESH_MIN_X);
  1047. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MIN_Y ", UBL_MESH_MIN_Y);
  1048. safe_delay(50);
  1049. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_X ", UBL_MESH_MAX_X);
  1050. SERIAL_PROTOCOLPAIR("\nUBL_MESH_MAX_Y ", UBL_MESH_MAX_Y);
  1051. safe_delay(50);
  1052. SERIAL_PROTOCOLPGM("\nMESH_X_DIST ");
  1053. SERIAL_PROTOCOL_F(MESH_X_DIST, 6);
  1054. SERIAL_PROTOCOLPGM("\nMESH_Y_DIST ");
  1055. SERIAL_PROTOCOL_F(MESH_Y_DIST, 6);
  1056. SERIAL_EOL;
  1057. safe_delay(50);
  1058. if (!ubl.sanity_check())
  1059. SERIAL_PROTOCOLLNPGM("Unified Bed Leveling sanity checks passed.");
  1060. }
  1061. /**
  1062. * When we are fully debugged, the EEPROM dump command will get deleted also. But
  1063. * right now, it is good to have the extra information. Soon... we prune this.
  1064. */
  1065. void g29_eeprom_dump() {
  1066. unsigned char cccc;
  1067. uint16_t kkkk;
  1068. SERIAL_ECHO_START;
  1069. SERIAL_ECHOLNPGM("EEPROM Dump:");
  1070. for (uint16_t i = 0; i < E2END + 1; i += 16) {
  1071. if (!(i & 0x3)) idle();
  1072. print_hex_word(i);
  1073. SERIAL_ECHOPGM(": ");
  1074. for (uint16_t j = 0; j < 16; j++) {
  1075. kkkk = i + j;
  1076. eeprom_read_block(&cccc, (void *)kkkk, 1);
  1077. print_hex_byte(cccc);
  1078. SERIAL_ECHO(' ');
  1079. }
  1080. SERIAL_EOL;
  1081. }
  1082. SERIAL_EOL;
  1083. }
  1084. /**
  1085. * When we are fully debugged, this may go away. But there are some valid
  1086. * use cases for the users. So we can wait and see what to do with it.
  1087. */
  1088. void g29_compare_current_mesh_to_stored_mesh() {
  1089. float tmp_z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS];
  1090. if (!code_has_value()) {
  1091. SERIAL_PROTOCOLLNPGM("?Mesh # required.\n");
  1092. return;
  1093. }
  1094. storage_slot = code_value_int();
  1095. int16_t j = (UBL_LAST_EEPROM_INDEX - ubl.eeprom_start) / sizeof(tmp_z_values);
  1096. if (!WITHIN(storage_slot, 0, j - 1) || ubl.eeprom_start <= 0) {
  1097. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available for use.\n");
  1098. return;
  1099. }
  1100. j = UBL_LAST_EEPROM_INDEX - (storage_slot + 1) * sizeof(tmp_z_values);
  1101. eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
  1102. SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
  1103. SERIAL_PROTOCOLLNPAIR(" loaded from EEPROM address 0x", hex_word(j)); // Soon, we can remove the extra clutter of printing
  1104. // the address in the EEPROM where the Mesh is stored.
  1105. for (uint8_t x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  1106. for (uint8_t y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  1107. ubl.z_values[x][y] -= tmp_z_values[x][y];
  1108. }
  1109. 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) {
  1110. float distance, closest = far_flag ? -99999.99 : 99999.99;
  1111. mesh_index_pair return_val;
  1112. return_val.x_index = return_val.y_index = -1;
  1113. const float current_x = current_position[X_AXIS],
  1114. current_y = current_position[Y_AXIS];
  1115. // Get our reference position. Either the nozzle or probe location.
  1116. const float px = lx - (probe_as_reference ? X_PROBE_OFFSET_FROM_EXTRUDER : 0),
  1117. py = ly - (probe_as_reference ? Y_PROBE_OFFSET_FROM_EXTRUDER : 0);
  1118. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  1119. for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
  1120. if ( (type == INVALID && isnan(ubl.z_values[i][j])) // Check to see if this location holds the right thing
  1121. || (type == REAL && !isnan(ubl.z_values[i][j]))
  1122. || (type == SET_IN_BITMAP && is_bit_set(bits, i, j))
  1123. ) {
  1124. // We only get here if we found a Mesh Point of the specified type
  1125. const float rawx = ubl.mesh_index_to_xpos[i], // Check if we can probe this mesh location
  1126. rawy = ubl.mesh_index_to_ypos[j];
  1127. // If using the probe as the reference there are some unreachable locations.
  1128. // Prune them from the list and ignore them till the next Phase (manual nozzle probing).
  1129. if (probe_as_reference &&
  1130. (!WITHIN(rawx, MIN_PROBE_X, MAX_PROBE_X) || !WITHIN(rawy, MIN_PROBE_Y, MAX_PROBE_Y))
  1131. ) continue;
  1132. // Unreachable. Check if it's the closest location to the nozzle.
  1133. // Add in a weighting factor that considers the current location of the nozzle.
  1134. const float mx = LOGICAL_X_POSITION(rawx), // Check if we can probe this mesh location
  1135. my = LOGICAL_Y_POSITION(rawy);
  1136. distance = HYPOT(px - mx, py - my) + HYPOT(current_x - mx, current_y - my) * 0.1;
  1137. if (far_flag) { // If doing the far_flag action, we want to be as far as possible
  1138. for (uint8_t k = 0; k < UBL_MESH_NUM_X_POINTS; k++) { // from the starting point and from any other probed points. We
  1139. for (uint8_t l = 0; l < UBL_MESH_NUM_Y_POINTS; l++) { // want the next point spread out and filling in any blank spaces
  1140. if (!isnan(ubl.z_values[k][l])) { // in the mesh. So we add in some of the distance to every probed
  1141. distance += sq(i - k) * (MESH_X_DIST) * .05 // point we can find.
  1142. + sq(j - l) * (MESH_Y_DIST) * .05;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. if (far_flag == (distance > closest) && distance != closest) { // if far_flag, look for farthest point
  1148. closest = distance; // We found a closer/farther location with
  1149. return_val.x_index = i; // the specified type of mesh value.
  1150. return_val.y_index = j;
  1151. return_val.distance = closest;
  1152. }
  1153. }
  1154. } // for j
  1155. } // for i
  1156. return return_val;
  1157. }
  1158. void fine_tune_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map) {
  1159. mesh_index_pair location;
  1160. uint16_t not_done[16];
  1161. int32_t round_off;
  1162. save_ubl_active_state_and_disable();
  1163. memset(not_done, 0xFF, sizeof(not_done));
  1164. #if ENABLED(ULTRA_LCD)
  1165. lcd_setstatus("Fine Tuning Mesh.", true);
  1166. #endif
  1167. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1168. do_blocking_move_to_xy(lx, ly);
  1169. do {
  1170. if (do_ubl_mesh_map) ubl.display_map(map_type);
  1171. 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
  1172. // It doesn't matter if the probe can not reach this
  1173. // location. This is a manual edit of the Mesh Point.
  1174. if (location.x_index < 0 && location.y_index < 0) continue; // abort if we can't find any more points.
  1175. bit_clear(not_done, location.x_index, location.y_index); // Mark this location as 'adjusted' so we will find a
  1176. // different location the next time through the loop
  1177. const float rawx = ubl.mesh_index_to_xpos[location.x_index],
  1178. rawy = ubl.mesh_index_to_ypos[location.y_index];
  1179. // TODO: Change to use `position_is_reachable` (for SCARA-compatibility)
  1180. 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.
  1181. SERIAL_ERROR_START;
  1182. SERIAL_ERRORLNPGM("Attempt to edit off the bed."); // This really can't happen, but do the check for now
  1183. ubl.has_control_of_lcd_panel = false;
  1184. goto FINE_TUNE_EXIT;
  1185. }
  1186. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE); // Move the nozzle to where we are going to edit
  1187. do_blocking_move_to_xy(LOGICAL_X_POSITION(rawx), LOGICAL_Y_POSITION(rawy));
  1188. float new_z = ubl.z_values[location.x_index][location.y_index];
  1189. round_off = (int32_t)(new_z * 1000.0); // we chop off the last digits just to be clean. We are rounding to the
  1190. new_z = float(round_off) / 1000.0;
  1191. KEEPALIVE_STATE(PAUSED_FOR_USER);
  1192. ubl.has_control_of_lcd_panel = true;
  1193. lcd_implementation_clear();
  1194. lcd_mesh_edit_setup(new_z);
  1195. do {
  1196. new_z = lcd_mesh_edit();
  1197. idle();
  1198. } while (!ubl_lcd_clicked());
  1199. lcd_return_to_status();
  1200. ubl.has_control_of_lcd_panel = true; // There is a race condition for the Encoder Wheel getting clicked.
  1201. // It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
  1202. // or here.
  1203. const millis_t nxt = millis() + 1500UL;
  1204. while (ubl_lcd_clicked()) { // debounce and watch for abort
  1205. idle();
  1206. if (ELAPSED(millis(), nxt)) {
  1207. lcd_return_to_status();
  1208. //SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
  1209. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1210. lcd_setstatus("Mesh Editing Stopped", true);
  1211. while (ubl_lcd_clicked()) idle();
  1212. goto FINE_TUNE_EXIT;
  1213. }
  1214. }
  1215. safe_delay(20); // We don't want any switch noise.
  1216. ubl.z_values[location.x_index][location.y_index] = new_z;
  1217. lcd_implementation_clear();
  1218. } while (location.x_index >= 0 && location.y_index >= 0 && --repetition_cnt);
  1219. FINE_TUNE_EXIT:
  1220. ubl.has_control_of_lcd_panel = false;
  1221. KEEPALIVE_STATE(IN_HANDLER);
  1222. if (do_ubl_mesh_map) ubl.display_map(map_type);
  1223. restore_ubl_active_state_and_leave();
  1224. do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
  1225. do_blocking_move_to_xy(lx, ly);
  1226. #if ENABLED(ULTRA_LCD)
  1227. lcd_setstatus("Done Editing Mesh", true);
  1228. #endif
  1229. SERIAL_ECHOLNPGM("Done Editing Mesh.");
  1230. }
  1231. #endif // AUTO_BED_LEVELING_UBL