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

UBL_G29.cpp 62KB

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