My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UBL_G29.cpp 64KB

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