My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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