My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UBL_G29.cpp 63KB

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