My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UBL_G29.cpp 61KB

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