mesh_bed_calibration.cpp 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172
  1. #include "Marlin.h"
  2. #include "Configuration.h"
  3. #include "ConfigurationStore.h"
  4. #include "language.h"
  5. #include "mesh_bed_calibration.h"
  6. #include "mesh_bed_leveling.h"
  7. #include "stepper.h"
  8. #include "ultralcd.h"
  9. #ifdef TMC2130
  10. #include "tmc2130.h"
  11. #endif //TMC2130
  12. uint8_t world2machine_correction_mode;
  13. float world2machine_rotation_and_skew[2][2];
  14. float world2machine_rotation_and_skew_inv[2][2];
  15. float world2machine_shift[2];
  16. // Weight of the Y coordinate for the least squares fitting of the bed induction sensor targets.
  17. // Only used for the first row of the points, which may not befully in reach of the sensor.
  18. #define WEIGHT_FIRST_ROW_X_HIGH (1.f)
  19. #define WEIGHT_FIRST_ROW_X_LOW (0.35f)
  20. #define WEIGHT_FIRST_ROW_Y_HIGH (0.3f)
  21. #define WEIGHT_FIRST_ROW_Y_LOW (0.0f)
  22. // Scaling of the real machine axes against the programmed dimensions in the firmware.
  23. // The correction is tiny, here around 0.5mm on 250mm length.
  24. //#define MACHINE_AXIS_SCALE_X ((250.f - 0.5f) / 250.f)
  25. //#define MACHINE_AXIS_SCALE_Y ((250.f - 0.5f) / 250.f)
  26. #define MACHINE_AXIS_SCALE_X 1.f
  27. #define MACHINE_AXIS_SCALE_Y 1.f
  28. #define BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN (0.8f)
  29. #define BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X (0.8f)
  30. #define BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y (1.5f)
  31. #define MIN_BED_SENSOR_POINT_RESPONSE_DMR (2.0f)
  32. //#define Y_MIN_POS_FOR_BED_CALIBRATION (MANUAL_Y_HOME_POS-0.2f)
  33. #define Y_MIN_POS_FOR_BED_CALIBRATION (Y_MIN_POS)
  34. // Distances toward the print bed edge may not be accurate.
  35. #define Y_MIN_POS_CALIBRATION_POINT_ACCURATE (Y_MIN_POS + 3.f)
  36. // When the measured point center is out of reach of the sensor, Y coordinate will be ignored
  37. // by the Least Squares fitting and the X coordinate will be weighted low.
  38. #define Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH (Y_MIN_POS - 0.5f)
  39. // 0.12 degrees equals to an offset of 0.5mm on 250mm length.
  40. const float bed_skew_angle_mild = (0.12f * M_PI / 180.f);
  41. // 0.25 degrees equals to an offset of 1.1mm on 250mm length.
  42. const float bed_skew_angle_extreme = (0.25f * M_PI / 180.f);
  43. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  44. // The points are ordered in a zig-zag fashion to speed up the calibration.
  45. #ifdef HEATBED_V2
  46. /**
  47. * [0,0] bed print area point X coordinate in bed coordinates ver. 05d/24V
  48. */
  49. #define BED_PRINT_ZERO_REF_X 2.f
  50. /**
  51. * [0,0] bed print area point Y coordinate in bed coordinates ver. 05d/24V
  52. */
  53. #define BED_PRINT_ZERO_REF_Y 9.4f
  54. /**
  55. * @brief Positions of the bed reference points in print area coordinates. ver. 05d/24V
  56. *
  57. * Numeral constants are in bed coordinates, subtracting macro defined values converts it to print area coordinates.
  58. *
  59. * The points are the following:
  60. * MK2: center front, center right, center rear, center left.
  61. * MK25 and MK3: front left, front right, rear right, rear left
  62. */
  63. const float bed_ref_points_4[] PROGMEM = {
  64. 37.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
  65. 18.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
  66. 245.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
  67. 18.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
  68. 245.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
  69. 210.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
  70. 37.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
  71. 210.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y
  72. };
  73. #else
  74. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  75. // The points are the following: center front, center right, center rear, center left.
  76. const float bed_ref_points_4[] PROGMEM = {
  77. 115.f - BED_ZERO_REF_X, 8.4f - BED_ZERO_REF_Y,
  78. 216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  79. 115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  80. 13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y
  81. };
  82. #endif //not HEATBED_V2
  83. static inline float sqr(float x) { return x * x; }
  84. #ifdef HEATBED_V2
  85. static inline bool point_on_1st_row(const uint8_t /*i*/)
  86. {
  87. return false;
  88. }
  89. #else //HEATBED_V2
  90. static inline bool point_on_1st_row(const uint8_t i)
  91. {
  92. return (i < 3);
  93. }
  94. #endif //HEATBED_V2
  95. // Weight of a point coordinate in a least squares optimization.
  96. // The first row of points may not be fully reachable
  97. // and the y values may be shortened a bit by the bed carriage
  98. // pulling the belt up.
  99. static inline float point_weight_x(const uint8_t i, const float &y)
  100. {
  101. float w = 1.f;
  102. if (point_on_1st_row(i)) {
  103. if (y >= Y_MIN_POS_CALIBRATION_POINT_ACCURATE) {
  104. w = WEIGHT_FIRST_ROW_X_HIGH;
  105. } else if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  106. // If the point is fully outside, give it some weight.
  107. w = WEIGHT_FIRST_ROW_X_LOW;
  108. } else {
  109. // Linearly interpolate the weight from 1 to WEIGHT_FIRST_ROW_X.
  110. float t = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) / (Y_MIN_POS_CALIBRATION_POINT_ACCURATE - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  111. w = (1.f - t) * WEIGHT_FIRST_ROW_X_LOW + t * WEIGHT_FIRST_ROW_X_HIGH;
  112. }
  113. }
  114. return w;
  115. }
  116. // Weight of a point coordinate in a least squares optimization.
  117. // The first row of points may not be fully reachable
  118. // and the y values may be shortened a bit by the bed carriage
  119. // pulling the belt up.
  120. static inline float point_weight_y(const uint8_t i, const float &y)
  121. {
  122. float w = 1.f;
  123. if (point_on_1st_row(i)) {
  124. if (y >= Y_MIN_POS_CALIBRATION_POINT_ACCURATE) {
  125. w = WEIGHT_FIRST_ROW_Y_HIGH;
  126. } else if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  127. // If the point is fully outside, give it some weight.
  128. w = WEIGHT_FIRST_ROW_Y_LOW;
  129. } else {
  130. // Linearly interpolate the weight from 1 to WEIGHT_FIRST_ROW_X.
  131. float t = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) / (Y_MIN_POS_CALIBRATION_POINT_ACCURATE - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  132. w = (1.f - t) * WEIGHT_FIRST_ROW_Y_LOW + t * WEIGHT_FIRST_ROW_Y_HIGH;
  133. }
  134. }
  135. return w;
  136. }
  137. /**
  138. * @brief Calculate machine skew and offset
  139. *
  140. * Non-Linear Least Squares fitting of the bed to the measured induction points
  141. * using the Gauss-Newton method.
  142. * This method will maintain a unity length of the machine axes,
  143. * which is the correct approach if the sensor points are not measured precisely.
  144. * @param measured_pts Matrix of 2D points (maximum 18 floats)
  145. * @param npts Number of points (maximum 9)
  146. * @param true_pts
  147. * @param [out] vec_x Resulting correction matrix. X axis vector
  148. * @param [out] vec_y Resulting correction matrix. Y axis vector
  149. * @param [out] cntr Resulting correction matrix. [0;0] pont offset
  150. * @param verbosity_level
  151. * @return BedSkewOffsetDetectionResultType
  152. */
  153. BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
  154. const float *measured_pts,
  155. uint8_t npts,
  156. const float *true_pts,
  157. float *vec_x,
  158. float *vec_y,
  159. float *cntr,
  160. int8_t
  161. #ifdef SUPPORT_VERBOSITY
  162. verbosity_level
  163. #endif //SUPPORT_VERBOSITY
  164. )
  165. {
  166. float angleDiff;
  167. #ifdef SUPPORT_VERBOSITY
  168. if (verbosity_level >= 10) {
  169. SERIAL_ECHOLNPGM("calculate machine skew and offset LS");
  170. // Show the initial state, before the fitting.
  171. SERIAL_ECHOPGM("X vector, initial: ");
  172. MYSERIAL.print(vec_x[0], 5);
  173. SERIAL_ECHOPGM(", ");
  174. MYSERIAL.print(vec_x[1], 5);
  175. SERIAL_ECHOLNPGM("");
  176. SERIAL_ECHOPGM("Y vector, initial: ");
  177. MYSERIAL.print(vec_y[0], 5);
  178. SERIAL_ECHOPGM(", ");
  179. MYSERIAL.print(vec_y[1], 5);
  180. SERIAL_ECHOLNPGM("");
  181. SERIAL_ECHOPGM("center, initial: ");
  182. MYSERIAL.print(cntr[0], 5);
  183. SERIAL_ECHOPGM(", ");
  184. MYSERIAL.print(cntr[1], 5);
  185. SERIAL_ECHOLNPGM("");
  186. for (uint8_t i = 0; i < npts; ++i) {
  187. SERIAL_ECHOPGM("point #");
  188. MYSERIAL.print(int(i));
  189. SERIAL_ECHOPGM(" measured: (");
  190. MYSERIAL.print(measured_pts[i * 2], 5);
  191. SERIAL_ECHOPGM(", ");
  192. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  193. SERIAL_ECHOPGM("); target: (");
  194. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  195. SERIAL_ECHOPGM(", ");
  196. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  197. SERIAL_ECHOPGM("), error: ");
  198. MYSERIAL.print(sqrt(
  199. sqr(pgm_read_float(true_pts + i * 2) - measured_pts[i * 2]) +
  200. sqr(pgm_read_float(true_pts + i * 2 + 1) - measured_pts[i * 2 + 1])), 5);
  201. SERIAL_ECHOLNPGM("");
  202. }
  203. delay_keep_alive(100);
  204. }
  205. #endif // SUPPORT_VERBOSITY
  206. // Run some iterations of the Gauss-Newton method of non-linear least squares.
  207. // Initial set of parameters:
  208. // X,Y offset
  209. cntr[0] = 0.f;
  210. cntr[1] = 0.f;
  211. // Rotation of the machine X axis from the bed X axis.
  212. float a1 = 0;
  213. // Rotation of the machine Y axis from the bed Y axis.
  214. float a2 = 0;
  215. for (int8_t iter = 0; iter < 100; ++iter) {
  216. float c1 = cos(a1) * MACHINE_AXIS_SCALE_X;
  217. float s1 = sin(a1) * MACHINE_AXIS_SCALE_X;
  218. float c2 = cos(a2) * MACHINE_AXIS_SCALE_Y;
  219. float s2 = sin(a2) * MACHINE_AXIS_SCALE_Y;
  220. // Prepare the Normal equation for the Gauss-Newton method.
  221. float A[4][4] = { 0.f };
  222. float b[4] = { 0.f };
  223. float acc;
  224. delay_keep_alive(0); //manage heater, reset watchdog, manage inactivity
  225. for (uint8_t r = 0; r < 4; ++r) {
  226. for (uint8_t c = 0; c < 4; ++c) {
  227. acc = 0;
  228. // J^T times J
  229. for (uint8_t i = 0; i < npts; ++i) {
  230. // First for the residuum in the x axis:
  231. if (r != 1 && c != 1) {
  232. float a =
  233. (r == 0) ? 1.f :
  234. ((r == 2) ? (-s1 * measured_pts[2 * i]) :
  235. (-c2 * measured_pts[2 * i + 1]));
  236. float b =
  237. (c == 0) ? 1.f :
  238. ((c == 2) ? (-s1 * measured_pts[2 * i]) :
  239. (-c2 * measured_pts[2 * i + 1]));
  240. float w = point_weight_x(i, measured_pts[2 * i + 1]);
  241. acc += a * b * w;
  242. }
  243. // Second for the residuum in the y axis.
  244. // The first row of the points have a low weight, because their position may not be known
  245. // with a sufficient accuracy.
  246. if (r != 0 && c != 0) {
  247. float a =
  248. (r == 1) ? 1.f :
  249. ((r == 2) ? ( c1 * measured_pts[2 * i]) :
  250. (-s2 * measured_pts[2 * i + 1]));
  251. float b =
  252. (c == 1) ? 1.f :
  253. ((c == 2) ? ( c1 * measured_pts[2 * i]) :
  254. (-s2 * measured_pts[2 * i + 1]));
  255. float w = point_weight_y(i, measured_pts[2 * i + 1]);
  256. acc += a * b * w;
  257. }
  258. }
  259. A[r][c] = acc;
  260. }
  261. // J^T times f(x)
  262. acc = 0.f;
  263. for (uint8_t i = 0; i < npts; ++i) {
  264. {
  265. float j =
  266. (r == 0) ? 1.f :
  267. ((r == 1) ? 0.f :
  268. ((r == 2) ? (-s1 * measured_pts[2 * i]) :
  269. (-c2 * measured_pts[2 * i + 1])));
  270. float fx = c1 * measured_pts[2 * i] - s2 * measured_pts[2 * i + 1] + cntr[0] - pgm_read_float(true_pts + i * 2);
  271. float w = point_weight_x(i, measured_pts[2 * i + 1]);
  272. acc += j * fx * w;
  273. }
  274. {
  275. float j =
  276. (r == 0) ? 0.f :
  277. ((r == 1) ? 1.f :
  278. ((r == 2) ? ( c1 * measured_pts[2 * i]) :
  279. (-s2 * measured_pts[2 * i + 1])));
  280. float fy = s1 * measured_pts[2 * i] + c2 * measured_pts[2 * i + 1] + cntr[1] - pgm_read_float(true_pts + i * 2 + 1);
  281. float w = point_weight_y(i, measured_pts[2 * i + 1]);
  282. acc += j * fy * w;
  283. }
  284. }
  285. b[r] = -acc;
  286. }
  287. // Solve for h by a Gauss iteration method.
  288. float h[4] = { 0.f };
  289. for (uint8_t gauss_iter = 0; gauss_iter < 100; ++gauss_iter) {
  290. h[0] = (b[0] - A[0][1] * h[1] - A[0][2] * h[2] - A[0][3] * h[3]) / A[0][0];
  291. h[1] = (b[1] - A[1][0] * h[0] - A[1][2] * h[2] - A[1][3] * h[3]) / A[1][1];
  292. h[2] = (b[2] - A[2][0] * h[0] - A[2][1] * h[1] - A[2][3] * h[3]) / A[2][2];
  293. h[3] = (b[3] - A[3][0] * h[0] - A[3][1] * h[1] - A[3][2] * h[2]) / A[3][3];
  294. }
  295. // and update the current position with h.
  296. // It may be better to use the Levenberg-Marquart method here,
  297. // but because we are very close to the solution alread,
  298. // the simple Gauss-Newton non-linear Least Squares method works well enough.
  299. cntr[0] += h[0];
  300. cntr[1] += h[1];
  301. a1 += h[2];
  302. a2 += h[3];
  303. #ifdef SUPPORT_VERBOSITY
  304. if (verbosity_level >= 20) {
  305. SERIAL_ECHOPGM("iteration: ");
  306. MYSERIAL.print(int(iter));
  307. SERIAL_ECHOPGM("; correction vector: ");
  308. MYSERIAL.print(h[0], 5);
  309. SERIAL_ECHOPGM(", ");
  310. MYSERIAL.print(h[1], 5);
  311. SERIAL_ECHOPGM(", ");
  312. MYSERIAL.print(h[2], 5);
  313. SERIAL_ECHOPGM(", ");
  314. MYSERIAL.print(h[3], 5);
  315. SERIAL_ECHOLNPGM("");
  316. SERIAL_ECHOPGM("corrected x/y: ");
  317. MYSERIAL.print(cntr[0], 5);
  318. SERIAL_ECHOPGM(", ");
  319. MYSERIAL.print(cntr[0], 5);
  320. SERIAL_ECHOLNPGM("");
  321. SERIAL_ECHOPGM("corrected angles: ");
  322. MYSERIAL.print(180.f * a1 / M_PI, 5);
  323. SERIAL_ECHOPGM(", ");
  324. MYSERIAL.print(180.f * a2 / M_PI, 5);
  325. SERIAL_ECHOLNPGM("");
  326. }
  327. #endif // SUPPORT_VERBOSITY
  328. }
  329. vec_x[0] = cos(a1) * MACHINE_AXIS_SCALE_X;
  330. vec_x[1] = sin(a1) * MACHINE_AXIS_SCALE_X;
  331. vec_y[0] = -sin(a2) * MACHINE_AXIS_SCALE_Y;
  332. vec_y[1] = cos(a2) * MACHINE_AXIS_SCALE_Y;
  333. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  334. {
  335. angleDiff = fabs(a2 - a1);
  336. eeprom_update_float((float*)(EEPROM_XYZ_CAL_SKEW), angleDiff); //storing xyz cal. skew to be able to show in support menu later
  337. if (angleDiff > bed_skew_angle_mild)
  338. result = (angleDiff > bed_skew_angle_extreme) ?
  339. BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME :
  340. BED_SKEW_OFFSET_DETECTION_SKEW_MILD;
  341. if (fabs(a1) > bed_skew_angle_extreme ||
  342. fabs(a2) > bed_skew_angle_extreme)
  343. result = BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME;
  344. }
  345. #ifdef SUPPORT_VERBOSITY
  346. if (verbosity_level >= 1) {
  347. SERIAL_ECHOPGM("correction angles: ");
  348. MYSERIAL.print(180.f * a1 / M_PI, 5);
  349. SERIAL_ECHOPGM(", ");
  350. MYSERIAL.print(180.f * a2 / M_PI, 5);
  351. SERIAL_ECHOLNPGM("");
  352. }
  353. if (verbosity_level >= 10) {
  354. // Show the adjusted state, before the fitting.
  355. SERIAL_ECHOPGM("X vector new, inverted: ");
  356. MYSERIAL.print(vec_x[0], 5);
  357. SERIAL_ECHOPGM(", ");
  358. MYSERIAL.print(vec_x[1], 5);
  359. SERIAL_ECHOLNPGM("");
  360. SERIAL_ECHOPGM("Y vector new, inverted: ");
  361. MYSERIAL.print(vec_y[0], 5);
  362. SERIAL_ECHOPGM(", ");
  363. MYSERIAL.print(vec_y[1], 5);
  364. SERIAL_ECHOLNPGM("");
  365. SERIAL_ECHOPGM("center new, inverted: ");
  366. MYSERIAL.print(cntr[0], 5);
  367. SERIAL_ECHOPGM(", ");
  368. MYSERIAL.print(cntr[1], 5);
  369. SERIAL_ECHOLNPGM("");
  370. delay_keep_alive(100);
  371. SERIAL_ECHOLNPGM("Error after correction: ");
  372. }
  373. #endif // SUPPORT_VERBOSITY
  374. // Measure the error after correction.
  375. for (uint8_t i = 0; i < npts; ++i) {
  376. float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1] + cntr[0];
  377. float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1] + cntr[1];
  378. float errX = sqr(pgm_read_float(true_pts + i * 2) - x);
  379. float errY = sqr(pgm_read_float(true_pts + i * 2 + 1) - y);
  380. float err = sqrt(errX + errY);
  381. #ifdef SUPPORT_VERBOSITY
  382. if (verbosity_level >= 10) {
  383. SERIAL_ECHOPGM("point #");
  384. MYSERIAL.print(int(i));
  385. SERIAL_ECHOLNPGM(":");
  386. }
  387. #endif // SUPPORT_VERBOSITY
  388. if (point_on_1st_row(i)) {
  389. #ifdef SUPPORT_VERBOSITY
  390. if(verbosity_level >= 20) SERIAL_ECHOPGM("Point on first row");
  391. #endif // SUPPORT_VERBOSITY
  392. float w = point_weight_y(i, measured_pts[2 * i + 1]);
  393. if (sqrt(errX) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X ||
  394. (w != 0.f && sqrt(errY) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y)) {
  395. result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
  396. #ifdef SUPPORT_VERBOSITY
  397. if (verbosity_level >= 20) {
  398. SERIAL_ECHOPGM(", weigth Y: ");
  399. MYSERIAL.print(w);
  400. if (sqrt(errX) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X) SERIAL_ECHOPGM(", error X > max. error X");
  401. if (w != 0.f && sqrt(errY) > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y) SERIAL_ECHOPGM(", error Y > max. error Y");
  402. }
  403. #endif // SUPPORT_VERBOSITY
  404. }
  405. }
  406. else {
  407. #ifdef SUPPORT_VERBOSITY
  408. if(verbosity_level >=20 ) SERIAL_ECHOPGM("Point not on first row");
  409. #endif // SUPPORT_VERBOSITY
  410. if (err > BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN) {
  411. result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
  412. #ifdef SUPPORT_VERBOSITY
  413. if(verbosity_level >= 20) SERIAL_ECHOPGM(", error > max. error euclidian");
  414. #endif // SUPPORT_VERBOSITY
  415. }
  416. }
  417. #ifdef SUPPORT_VERBOSITY
  418. if (verbosity_level >= 10) {
  419. SERIAL_ECHOLNPGM("");
  420. SERIAL_ECHOPGM("measured: (");
  421. MYSERIAL.print(measured_pts[i * 2], 5);
  422. SERIAL_ECHOPGM(", ");
  423. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  424. SERIAL_ECHOPGM("); corrected: (");
  425. MYSERIAL.print(x, 5);
  426. SERIAL_ECHOPGM(", ");
  427. MYSERIAL.print(y, 5);
  428. SERIAL_ECHOPGM("); target: (");
  429. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  430. SERIAL_ECHOPGM(", ");
  431. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  432. SERIAL_ECHOLNPGM(")");
  433. SERIAL_ECHOPGM("error: ");
  434. MYSERIAL.print(err);
  435. SERIAL_ECHOPGM(", error X: ");
  436. MYSERIAL.print(sqrt(errX));
  437. SERIAL_ECHOPGM(", error Y: ");
  438. MYSERIAL.print(sqrt(errY));
  439. SERIAL_ECHOLNPGM("");
  440. SERIAL_ECHOLNPGM("");
  441. }
  442. #endif // SUPPORT_VERBOSITY
  443. }
  444. #ifdef SUPPORT_VERBOSITY
  445. if (verbosity_level >= 20) {
  446. SERIAL_ECHOLNPGM("Max. errors:");
  447. SERIAL_ECHOPGM("Max. error X:");
  448. MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X);
  449. SERIAL_ECHOPGM("Max. error Y:");
  450. MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y);
  451. SERIAL_ECHOPGM("Max. error euclidian:");
  452. MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN);
  453. SERIAL_ECHOLNPGM("");
  454. }
  455. #endif // SUPPORT_VERBOSITY
  456. #if 0
  457. if (result == BED_SKEW_OFFSET_DETECTION_PERFECT && fabs(a1) < bed_skew_angle_mild && fabs(a2) < bed_skew_angle_mild) {
  458. #ifdef SUPPORT_VERBOSITY
  459. if (verbosity_level > 0)
  460. SERIAL_ECHOLNPGM("Very little skew detected. Disabling skew correction.");
  461. #endif // SUPPORT_VERBOSITY
  462. // Just disable the skew correction.
  463. vec_x[0] = MACHINE_AXIS_SCALE_X;
  464. vec_x[1] = 0.f;
  465. vec_y[0] = 0.f;
  466. vec_y[1] = MACHINE_AXIS_SCALE_Y;
  467. }
  468. #else
  469. if (result == BED_SKEW_OFFSET_DETECTION_PERFECT) {
  470. #ifdef SUPPORT_VERBOSITY
  471. if (verbosity_level > 0)
  472. SERIAL_ECHOLNPGM("Very little skew detected. Orthogonalizing the axes.");
  473. #endif // SUPPORT_VERBOSITY
  474. // Orthogonalize the axes.
  475. a1 = 0.5f * (a1 + a2);
  476. vec_x[0] = cos(a1) * MACHINE_AXIS_SCALE_X;
  477. vec_x[1] = sin(a1) * MACHINE_AXIS_SCALE_X;
  478. vec_y[0] = -sin(a1) * MACHINE_AXIS_SCALE_Y;
  479. vec_y[1] = cos(a1) * MACHINE_AXIS_SCALE_Y;
  480. // Refresh the offset.
  481. cntr[0] = 0.f;
  482. cntr[1] = 0.f;
  483. float wx = 0.f;
  484. float wy = 0.f;
  485. for (int8_t i = 0; i < npts; ++ i) {
  486. float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1];
  487. float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1];
  488. float w = point_weight_x(i, y);
  489. cntr[0] += w * (pgm_read_float(true_pts + i * 2) - x);
  490. wx += w;
  491. #ifdef SUPPORT_VERBOSITY
  492. if (verbosity_level >= 20) {
  493. MYSERIAL.print(i);
  494. SERIAL_ECHOLNPGM("");
  495. SERIAL_ECHOLNPGM("Weight_x:");
  496. MYSERIAL.print(w);
  497. SERIAL_ECHOLNPGM("");
  498. SERIAL_ECHOLNPGM("cntr[0]:");
  499. MYSERIAL.print(cntr[0]);
  500. SERIAL_ECHOLNPGM("");
  501. SERIAL_ECHOLNPGM("wx:");
  502. MYSERIAL.print(wx);
  503. }
  504. #endif // SUPPORT_VERBOSITY
  505. w = point_weight_y(i, y);
  506. cntr[1] += w * (pgm_read_float(true_pts + i * 2 + 1) - y);
  507. wy += w;
  508. #ifdef SUPPORT_VERBOSITY
  509. if (verbosity_level >= 20) {
  510. SERIAL_ECHOLNPGM("");
  511. SERIAL_ECHOLNPGM("Weight_y:");
  512. MYSERIAL.print(w);
  513. SERIAL_ECHOLNPGM("");
  514. SERIAL_ECHOLNPGM("cntr[1]:");
  515. MYSERIAL.print(cntr[1]);
  516. SERIAL_ECHOLNPGM("");
  517. SERIAL_ECHOLNPGM("wy:");
  518. MYSERIAL.print(wy);
  519. SERIAL_ECHOLNPGM("");
  520. SERIAL_ECHOLNPGM("");
  521. }
  522. #endif // SUPPORT_VERBOSITY
  523. }
  524. cntr[0] /= wx;
  525. cntr[1] /= wy;
  526. #ifdef SUPPORT_VERBOSITY
  527. if (verbosity_level >= 20) {
  528. SERIAL_ECHOLNPGM("");
  529. SERIAL_ECHOLNPGM("Final cntr values:");
  530. SERIAL_ECHOLNPGM("cntr[0]:");
  531. MYSERIAL.print(cntr[0]);
  532. SERIAL_ECHOLNPGM("");
  533. SERIAL_ECHOLNPGM("cntr[1]:");
  534. MYSERIAL.print(cntr[1]);
  535. SERIAL_ECHOLNPGM("");
  536. }
  537. #endif // SUPPORT_VERBOSITY
  538. }
  539. #endif
  540. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  541. {
  542. float d = vec_x[0] * vec_y[1] - vec_x[1] * vec_y[0];
  543. float Ainv[2][2] = {
  544. { vec_y[1] / d, -vec_y[0] / d },
  545. { -vec_x[1] / d, vec_x[0] / d }
  546. };
  547. float cntrInv[2] = {
  548. -Ainv[0][0] * cntr[0] - Ainv[0][1] * cntr[1],
  549. -Ainv[1][0] * cntr[0] - Ainv[1][1] * cntr[1]
  550. };
  551. vec_x[0] = Ainv[0][0];
  552. vec_x[1] = Ainv[1][0];
  553. vec_y[0] = Ainv[0][1];
  554. vec_y[1] = Ainv[1][1];
  555. cntr[0] = cntrInv[0];
  556. cntr[1] = cntrInv[1];
  557. }
  558. #ifdef SUPPORT_VERBOSITY
  559. if (verbosity_level >= 1) {
  560. // Show the adjusted state, before the fitting.
  561. SERIAL_ECHOPGM("X vector, adjusted: ");
  562. MYSERIAL.print(vec_x[0], 5);
  563. SERIAL_ECHOPGM(", ");
  564. MYSERIAL.print(vec_x[1], 5);
  565. SERIAL_ECHOLNPGM("");
  566. SERIAL_ECHOPGM("Y vector, adjusted: ");
  567. MYSERIAL.print(vec_y[0], 5);
  568. SERIAL_ECHOPGM(", ");
  569. MYSERIAL.print(vec_y[1], 5);
  570. SERIAL_ECHOLNPGM("");
  571. SERIAL_ECHOPGM("center, adjusted: ");
  572. MYSERIAL.print(cntr[0], 5);
  573. SERIAL_ECHOPGM(", ");
  574. MYSERIAL.print(cntr[1], 5);
  575. SERIAL_ECHOLNPGM("");
  576. delay_keep_alive(100);
  577. }
  578. if (verbosity_level >= 2) {
  579. SERIAL_ECHOLNPGM("Difference after correction: ");
  580. for (uint8_t i = 0; i < npts; ++i) {
  581. float x = vec_x[0] * pgm_read_float(true_pts + i * 2) + vec_y[0] * pgm_read_float(true_pts + i * 2 + 1) + cntr[0];
  582. float y = vec_x[1] * pgm_read_float(true_pts + i * 2) + vec_y[1] * pgm_read_float(true_pts + i * 2 + 1) + cntr[1];
  583. SERIAL_ECHOPGM("point #");
  584. MYSERIAL.print(int(i));
  585. SERIAL_ECHOPGM("measured: (");
  586. MYSERIAL.print(measured_pts[i * 2], 5);
  587. SERIAL_ECHOPGM(", ");
  588. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  589. SERIAL_ECHOPGM("); measured-corrected: (");
  590. MYSERIAL.print(x, 5);
  591. SERIAL_ECHOPGM(", ");
  592. MYSERIAL.print(y, 5);
  593. SERIAL_ECHOPGM("); target: (");
  594. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  595. SERIAL_ECHOPGM(", ");
  596. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  597. SERIAL_ECHOPGM("), error: ");
  598. MYSERIAL.print(sqrt(sqr(measured_pts[i * 2] - x) + sqr(measured_pts[i * 2 + 1] - y)));
  599. SERIAL_ECHOLNPGM("");
  600. }
  601. if (verbosity_level >= 20) {
  602. SERIAL_ECHOLNPGM("");
  603. SERIAL_ECHOLNPGM("Calculate offset and skew returning result:");
  604. MYSERIAL.print(int(result));
  605. SERIAL_ECHOLNPGM("");
  606. SERIAL_ECHOLNPGM("");
  607. }
  608. delay_keep_alive(100);
  609. }
  610. #endif // SUPPORT_VERBOSITY
  611. return result;
  612. }
  613. /**
  614. * @brief Erase calibration data stored in EEPROM
  615. */
  616. void reset_bed_offset_and_skew()
  617. {
  618. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+0), 0x0FFFFFFFF);
  619. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+4), 0x0FFFFFFFF);
  620. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +0), 0x0FFFFFFFF);
  621. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +4), 0x0FFFFFFFF);
  622. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +0), 0x0FFFFFFFF);
  623. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +4), 0x0FFFFFFFF);
  624. // Reset the 8 16bit offsets.
  625. for (int8_t i = 0; i < 4; ++ i)
  626. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_Z_JITTER+i*4), 0x0FFFFFFFF);
  627. }
  628. bool is_bed_z_jitter_data_valid()
  629. // offsets of the Z heiths of the calibration points from the first point are saved as 16bit signed int, scaled to tenths of microns
  630. // if at least one 16bit integer has different value then -1 (0x0FFFF), data are considered valid and function returns true, otherwise it returns false
  631. {
  632. bool data_valid = false;
  633. for (int8_t i = 0; i < 8; ++i) {
  634. if (eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + i * 2)) != 0x0FFFF) data_valid = true;
  635. }
  636. return data_valid;
  637. }
  638. static void world2machine_update(const float vec_x[2], const float vec_y[2], const float cntr[2])
  639. {
  640. world2machine_rotation_and_skew[0][0] = vec_x[0];
  641. world2machine_rotation_and_skew[1][0] = vec_x[1];
  642. world2machine_rotation_and_skew[0][1] = vec_y[0];
  643. world2machine_rotation_and_skew[1][1] = vec_y[1];
  644. world2machine_shift[0] = cntr[0];
  645. world2machine_shift[1] = cntr[1];
  646. // No correction.
  647. world2machine_correction_mode = WORLD2MACHINE_CORRECTION_NONE;
  648. if (world2machine_shift[0] != 0.f || world2machine_shift[1] != 0.f)
  649. // Shift correction.
  650. world2machine_correction_mode |= WORLD2MACHINE_CORRECTION_SHIFT;
  651. if (world2machine_rotation_and_skew[0][0] != 1.f || world2machine_rotation_and_skew[0][1] != 0.f ||
  652. world2machine_rotation_and_skew[1][0] != 0.f || world2machine_rotation_and_skew[1][1] != 1.f) {
  653. // Rotation & skew correction.
  654. world2machine_correction_mode |= WORLD2MACHINE_CORRECTION_SKEW;
  655. // Invert the world2machine matrix.
  656. float d = world2machine_rotation_and_skew[0][0] * world2machine_rotation_and_skew[1][1] - world2machine_rotation_and_skew[1][0] * world2machine_rotation_and_skew[0][1];
  657. world2machine_rotation_and_skew_inv[0][0] = world2machine_rotation_and_skew[1][1] / d;
  658. world2machine_rotation_and_skew_inv[0][1] = -world2machine_rotation_and_skew[0][1] / d;
  659. world2machine_rotation_and_skew_inv[1][0] = -world2machine_rotation_and_skew[1][0] / d;
  660. world2machine_rotation_and_skew_inv[1][1] = world2machine_rotation_and_skew[0][0] / d;
  661. } else {
  662. world2machine_rotation_and_skew_inv[0][0] = 1.f;
  663. world2machine_rotation_and_skew_inv[0][1] = 0.f;
  664. world2machine_rotation_and_skew_inv[1][0] = 0.f;
  665. world2machine_rotation_and_skew_inv[1][1] = 1.f;
  666. }
  667. }
  668. /**
  669. * @brief Set calibration matrix to identity
  670. *
  671. * In contrast with world2machine_revert_to_uncorrected(), it doesn't wait for finishing moves
  672. * nor updates the current position with the absolute values.
  673. */
  674. void world2machine_reset()
  675. {
  676. const float vx[] = { 1.f, 0.f };
  677. const float vy[] = { 0.f, 1.f };
  678. const float cntr[] = { 0.f, 0.f };
  679. world2machine_update(vx, vy, cntr);
  680. }
  681. /**
  682. * @brief Get calibration matrix default value
  683. *
  684. * This is used if no valid calibration data can be read from EEPROM.
  685. * @param [out] vec_x axis x vector
  686. * @param [out] vec_y axis y vector
  687. * @param [out] cntr offset vector
  688. */
  689. static void world2machine_default(float vec_x[2], float vec_y[2], float cntr[2])
  690. {
  691. vec_x[0] = 1.f;
  692. vec_x[1] = 0.f;
  693. vec_y[0] = 0.f;
  694. vec_y[1] = 1.f;
  695. cntr[0] = 0.f;
  696. #ifdef DEFAULT_Y_OFFSET
  697. cntr[1] = DEFAULT_Y_OFFSET;
  698. #else
  699. cntr[1] = 0.f;
  700. #endif
  701. }
  702. /**
  703. * @brief Set calibration matrix to identity and update current position with absolute position
  704. *
  705. * Wait for the motors to stop and then update the current position with the absolute values.
  706. */
  707. void world2machine_revert_to_uncorrected()
  708. {
  709. if (world2machine_correction_mode != WORLD2MACHINE_CORRECTION_NONE) {
  710. world2machine_reset();
  711. st_synchronize();
  712. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  713. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  714. }
  715. }
  716. static inline bool vec_undef(const float v[2])
  717. {
  718. const uint32_t *vx = (const uint32_t*)v;
  719. return vx[0] == 0x0FFFFFFFF || vx[1] == 0x0FFFFFFFF;
  720. }
  721. /**
  722. * @brief Read calibration data from EEPROM
  723. *
  724. * If no calibration data has been stored in EEPROM or invalid,
  725. * world2machine_default() is used.
  726. *
  727. * If stored calibration data is invalid, EEPROM storage is cleared.
  728. * @param [out] vec_x axis x vector
  729. * @param [out] vec_y axis y vector
  730. * @param [out] cntr offset vector
  731. */
  732. void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2])
  733. {
  734. vec_x[0] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0));
  735. vec_x[1] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4));
  736. vec_y[0] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0));
  737. vec_y[1] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4));
  738. cntr[0] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0));
  739. cntr[1] = eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4));
  740. bool reset = false;
  741. if (vec_undef(cntr) || vec_undef(vec_x) || vec_undef(vec_y))
  742. {
  743. #if 0
  744. SERIAL_ECHOLNPGM("Undefined bed correction matrix.");
  745. #endif
  746. reset = true;
  747. }
  748. else
  749. {
  750. // Length of the vec_x shall be close to unity.
  751. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  752. if (l < 0.9 || l > 1.1)
  753. {
  754. #if 0
  755. SERIAL_ECHOLNPGM("X vector length:");
  756. MYSERIAL.println(l);
  757. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  758. #endif
  759. reset = true;
  760. }
  761. // Length of the vec_y shall be close to unity.
  762. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  763. if (l < 0.9 || l > 1.1)
  764. {
  765. #if 0
  766. SERIAL_ECHOLNPGM("Y vector length:");
  767. MYSERIAL.println(l);
  768. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the Y vector out of range.");
  769. #endif
  770. reset = true;
  771. }
  772. // Correction of the zero point shall be reasonably small.
  773. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  774. if (l > 15.f)
  775. {
  776. #if 0
  777. SERIAL_ECHOLNPGM("Zero point correction:");
  778. MYSERIAL.println(l);
  779. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Shift out of range.");
  780. #endif
  781. reset = true;
  782. }
  783. // vec_x and vec_y shall be nearly perpendicular.
  784. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  785. if (fabs(l) > 0.1f)
  786. {
  787. #if 0
  788. SERIAL_ECHOLNPGM("Invalid bed correction matrix. X/Y axes are far from being perpendicular.");
  789. #endif
  790. reset = true;
  791. }
  792. }
  793. if (reset)
  794. {
  795. #if 0
  796. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Resetting to identity.");
  797. #endif
  798. reset_bed_offset_and_skew();
  799. world2machine_default(vec_x, vec_y, cntr);
  800. }
  801. }
  802. /**
  803. * @brief Read and apply validated calibration data from EEPROM
  804. */
  805. void world2machine_initialize()
  806. {
  807. #if 0
  808. SERIAL_ECHOLNPGM("world2machine_initialize");
  809. #endif
  810. float vec_x[2];
  811. float vec_y[2];
  812. float cntr[2];
  813. world2machine_read_valid(vec_x, vec_y, cntr);
  814. world2machine_update(vec_x, vec_y, cntr);
  815. #if 0
  816. SERIAL_ECHOPGM("world2machine_initialize() loaded: ");
  817. MYSERIAL.print(world2machine_rotation_and_skew[0][0], 5);
  818. SERIAL_ECHOPGM(", ");
  819. MYSERIAL.print(world2machine_rotation_and_skew[0][1], 5);
  820. SERIAL_ECHOPGM(", ");
  821. MYSERIAL.print(world2machine_rotation_and_skew[1][0], 5);
  822. SERIAL_ECHOPGM(", ");
  823. MYSERIAL.print(world2machine_rotation_and_skew[1][1], 5);
  824. SERIAL_ECHOPGM(", offset ");
  825. MYSERIAL.print(world2machine_shift[0], 5);
  826. SERIAL_ECHOPGM(", ");
  827. MYSERIAL.print(world2machine_shift[1], 5);
  828. SERIAL_ECHOLNPGM("");
  829. #endif
  830. }
  831. /**
  832. * @brief Update current position after switching to corrected coordinates
  833. *
  834. * When switching from absolute to corrected coordinates,
  835. * this will get the absolute coordinates from the servos,
  836. * applies the inverse world2machine transformation
  837. * and stores the result into current_position[x,y].
  838. */
  839. void world2machine_update_current()
  840. {
  841. float x = current_position[X_AXIS] - world2machine_shift[0];
  842. float y = current_position[Y_AXIS] - world2machine_shift[1];
  843. current_position[X_AXIS] = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  844. current_position[Y_AXIS] = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  845. }
  846. static inline void go_xyz(float x, float y, float z, float fr)
  847. {
  848. plan_buffer_line(x, y, z, current_position[E_AXIS], fr, active_extruder);
  849. st_synchronize();
  850. }
  851. static inline void go_xy(float x, float y, float fr)
  852. {
  853. plan_buffer_line(x, y, current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  854. st_synchronize();
  855. }
  856. static inline void go_to_current(float fr)
  857. {
  858. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  859. st_synchronize();
  860. }
  861. static inline void update_current_position_xyz()
  862. {
  863. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  864. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  865. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  866. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  867. }
  868. static inline void update_current_position_z()
  869. {
  870. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  871. plan_set_z_position(current_position[Z_AXIS]);
  872. }
  873. // At the current position, find the Z stop.
  874. inline bool find_bed_induction_sensor_point_z(float minimum_z, uint8_t n_iter, int
  875. #ifdef SUPPORT_VERBOSITY
  876. verbosity_level
  877. #endif //SUPPORT_VERBOSITY
  878. )
  879. {
  880. #ifdef TMC2130
  881. FORCE_HIGH_POWER_START;
  882. #endif
  883. printf_P(PSTR("Min. Z: %f\n"), minimum_z);
  884. #ifdef SUPPORT_VERBOSITY
  885. if(verbosity_level >= 10) SERIAL_ECHOLNPGM("find bed induction sensor point z");
  886. #endif // SUPPORT_VERBOSITY
  887. bool endstops_enabled = enable_endstops(true);
  888. bool endstop_z_enabled = enable_z_endstop(false);
  889. float z = 0.f;
  890. endstop_z_hit_on_purpose();
  891. // move down until you find the bed
  892. current_position[Z_AXIS] = minimum_z;
  893. go_to_current(homing_feedrate[Z_AXIS]/60);
  894. // we have to let the planner know where we are right now as it is not where we said to go.
  895. update_current_position_z();
  896. if (! endstop_z_hit_on_purpose())
  897. {
  898. printf_P(PSTR("endstop not hit 1, current_pos[Z]: %f \n"), current_position[Z_AXIS]);
  899. goto error;
  900. }
  901. #ifdef TMC2130
  902. if (READ(Z_TMC2130_DIAG) != 0)
  903. {
  904. printf_P(PSTR("crash detected 1, current_pos[Z]: %f \n"), current_position[Z_AXIS]);
  905. goto error; //crash Z detected
  906. }
  907. #endif //TMC2130
  908. for (uint8_t i = 0; i < n_iter; ++ i)
  909. {
  910. current_position[Z_AXIS] += 0.2;
  911. float z_bckp = current_position[Z_AXIS];
  912. go_to_current(homing_feedrate[Z_AXIS]/60);
  913. // Move back down slowly to find bed.
  914. current_position[Z_AXIS] = minimum_z;
  915. printf_P(PSTR("init Z = %f, min_z = %f\n"), z_bckp, minimum_z);
  916. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  917. // we have to let the planner know where we are right now as it is not where we said to go.
  918. update_current_position_z();
  919. //printf_P(PSTR("Zs: %f, Z: %f, delta Z: %f"), z_bckp, current_position[Z_AXIS], (z_bckp - current_position[Z_AXIS]));
  920. if (abs(current_position[Z_AXIS] - z_bckp) < 0.025) {
  921. printf_P(PSTR("PINDA triggered immediately, move Z higher and repeat measurement\n"));
  922. current_position[Z_AXIS] += 0.5;
  923. go_to_current(homing_feedrate[Z_AXIS]/60);
  924. current_position[Z_AXIS] = minimum_z;
  925. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  926. // we have to let the planner know where we are right now as it is not where we said to go.
  927. update_current_position_z();
  928. }
  929. if (!endstop_z_hit_on_purpose())
  930. {
  931. printf_P(PSTR("i = %d, endstop not hit 2, current_pos[Z]: %f \n"), i, current_position[Z_AXIS]);
  932. goto error;
  933. }
  934. #ifdef TMC2130
  935. if (READ(Z_TMC2130_DIAG) != 0) {
  936. printf_P(PSTR("crash detected 2, current_pos[Z]: %f \n"), current_position[Z_AXIS]);
  937. goto error; //crash Z detected
  938. }
  939. #endif //TMC2130
  940. // SERIAL_ECHOPGM("Bed find_bed_induction_sensor_point_z low, height: ");
  941. // MYSERIAL.print(current_position[Z_AXIS], 5);
  942. // SERIAL_ECHOLNPGM("");
  943. float dz = i?abs(current_position[Z_AXIS] - (z / i)):0;
  944. z += current_position[Z_AXIS];
  945. //printf_P(PSTR("Z[%d] = %d, dz=%d\n"), i, (int)(current_position[Z_AXIS] * 1000), (int)(dz * 1000));
  946. if (dz > 0.05) {
  947. printf_P(PSTR("big deviation \n"));
  948. goto error;//deviation > 50um
  949. }
  950. printf_P(PSTR("PINDA triggered at %f\n"), current_position[Z_AXIS]);
  951. }
  952. current_position[Z_AXIS] = z;
  953. if (n_iter > 1)
  954. current_position[Z_AXIS] /= float(n_iter);
  955. enable_endstops(endstops_enabled);
  956. enable_z_endstop(endstop_z_enabled);
  957. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 3");
  958. #ifdef TMC2130
  959. FORCE_HIGH_POWER_END;
  960. #endif
  961. return true;
  962. error:
  963. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 4");
  964. enable_endstops(endstops_enabled);
  965. enable_z_endstop(endstop_z_enabled);
  966. #ifdef TMC2130
  967. FORCE_HIGH_POWER_END;
  968. #endif
  969. return false;
  970. }
  971. #ifdef NEW_XYZCAL
  972. extern bool xyzcal_find_bed_induction_sensor_point_xy();
  973. #endif //NEW_XYZCAL
  974. // Search around the current_position[X,Y],
  975. // look for the induction sensor response.
  976. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  977. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  978. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (4.f)
  979. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  980. #ifdef HEATBED_V2
  981. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (2.f)
  982. #define FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR (0.03f)
  983. #else //HEATBED_V2
  984. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.2f)
  985. #endif //HEATBED_V2
  986. #ifdef HEATBED_V2
  987. inline bool find_bed_induction_sensor_point_xy(int
  988. #if !defined (NEW_XYZCAL) && defined (SUPPORT_VERBOSITY)
  989. verbosity_level
  990. #endif
  991. )
  992. {
  993. #ifdef NEW_XYZCAL
  994. return xyzcal_find_bed_induction_sensor_point_xy();
  995. #else //NEW_XYZCAL
  996. #ifdef SUPPORT_VERBOSITY
  997. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  998. #endif // SUPPORT_VERBOSITY
  999. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1000. bool found = false;
  1001. {
  1002. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1003. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1004. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1005. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1006. uint8_t nsteps_y;
  1007. uint8_t i;
  1008. if (x0 < X_MIN_POS) {
  1009. x0 = X_MIN_POS;
  1010. #ifdef SUPPORT_VERBOSITY
  1011. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  1012. #endif // SUPPORT_VERBOSITY
  1013. }
  1014. if (x1 > X_MAX_POS) {
  1015. x1 = X_MAX_POS;
  1016. #ifdef SUPPORT_VERBOSITY
  1017. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  1018. #endif // SUPPORT_VERBOSITY
  1019. }
  1020. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1021. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1022. #ifdef SUPPORT_VERBOSITY
  1023. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1024. #endif // SUPPORT_VERBOSITY
  1025. }
  1026. if (y1 > Y_MAX_POS) {
  1027. y1 = Y_MAX_POS;
  1028. #ifdef SUPPORT_VERBOSITY
  1029. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1030. #endif // SUPPORT_VERBOSITY
  1031. }
  1032. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1033. enable_endstops(false);
  1034. bool dir_positive = true;
  1035. float z_error = 2 * FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  1036. float find_bed_induction_sensor_point_z_step = FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  1037. float initial_z_position = current_position[Z_AXIS];
  1038. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1039. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1040. // Continously lower the Z axis.
  1041. endstops_hit_on_purpose();
  1042. enable_z_endstop(true);
  1043. bool direction = false;
  1044. while (current_position[Z_AXIS] > -10.f && z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1045. // Do nsteps_y zig-zag movements.
  1046. SERIAL_ECHOPGM("z_error: ");
  1047. MYSERIAL.println(z_error);
  1048. current_position[Y_AXIS] = direction ? y1 : y0;
  1049. initial_z_position = current_position[Z_AXIS];
  1050. for (i = 0; i < (nsteps_y - 1); (direction == false) ? (current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1)) : (current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1)), ++i) {
  1051. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1052. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  1053. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1054. dir_positive = !dir_positive;
  1055. if (endstop_z_hit_on_purpose()) {
  1056. update_current_position_xyz();
  1057. z_error = initial_z_position - current_position[Z_AXIS] + find_bed_induction_sensor_point_z_step;
  1058. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1059. find_bed_induction_sensor_point_z_step = z_error / 2;
  1060. current_position[Z_AXIS] += z_error;
  1061. enable_z_endstop(false);
  1062. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  1063. enable_z_endstop(true);
  1064. }
  1065. goto endloop;
  1066. }
  1067. }
  1068. for (i = 0; i < (nsteps_y - 1); (direction == false) ? (current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1)) : (current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1)), ++i) {
  1069. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1070. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  1071. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1072. dir_positive = !dir_positive;
  1073. if (endstop_z_hit_on_purpose()) {
  1074. update_current_position_xyz();
  1075. z_error = initial_z_position - current_position[Z_AXIS];
  1076. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1077. find_bed_induction_sensor_point_z_step = z_error / 2;
  1078. current_position[Z_AXIS] += z_error;
  1079. enable_z_endstop(false);
  1080. direction = !direction;
  1081. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  1082. enable_z_endstop(true);
  1083. }
  1084. goto endloop;
  1085. }
  1086. }
  1087. endloop:;
  1088. }
  1089. #ifdef SUPPORT_VERBOSITY
  1090. if (verbosity_level >= 20) {
  1091. SERIAL_ECHO("First hit");
  1092. SERIAL_ECHO("- X: ");
  1093. MYSERIAL.print(current_position[X_AXIS]);
  1094. SERIAL_ECHO("; Y: ");
  1095. MYSERIAL.print(current_position[Y_AXIS]);
  1096. SERIAL_ECHO("; Z: ");
  1097. MYSERIAL.println(current_position[Z_AXIS]);
  1098. }
  1099. #endif //SUPPORT_VERBOSITY
  1100. //lcd_show_fullscreen_message_and_wait_P(PSTR("First hit"));
  1101. //lcd_update_enable(true);
  1102. float init_x_position = current_position[X_AXIS];
  1103. float init_y_position = current_position[Y_AXIS];
  1104. // we have to let the planner know where we are right now as it is not where we said to go.
  1105. update_current_position_xyz();
  1106. enable_z_endstop(false);
  1107. for (int8_t iter = 0; iter < 2; ++iter) {
  1108. /*SERIAL_ECHOPGM("iter: ");
  1109. MYSERIAL.println(iter);
  1110. SERIAL_ECHOPGM("1 - current_position[Z_AXIS]: ");
  1111. MYSERIAL.println(current_position[Z_AXIS]);*/
  1112. // Slightly lower the Z axis to get a reliable trigger.
  1113. current_position[Z_AXIS] -= 0.1f;
  1114. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], homing_feedrate[Z_AXIS] / (60 * 10));
  1115. SERIAL_ECHOPGM("2 - current_position[Z_AXIS]: ");
  1116. MYSERIAL.println(current_position[Z_AXIS]);
  1117. // Do nsteps_y zig-zag movements.
  1118. float a, b;
  1119. float avg[2] = { 0,0 };
  1120. invert_z_endstop(true);
  1121. for (int iteration = 0; iteration < 8; iteration++) {
  1122. found = false;
  1123. enable_z_endstop(true);
  1124. go_xy(init_x_position + 16.0f, current_position[Y_AXIS], feedrate / 5);
  1125. update_current_position_xyz();
  1126. if (!endstop_z_hit_on_purpose()) {
  1127. // SERIAL_ECHOLN("Search X span 0 - not found");
  1128. continue;
  1129. }
  1130. // SERIAL_ECHOLN("Search X span 0 - found");
  1131. a = current_position[X_AXIS];
  1132. enable_z_endstop(false);
  1133. go_xy(init_x_position, current_position[Y_AXIS], feedrate / 5);
  1134. enable_z_endstop(true);
  1135. go_xy(init_x_position - 16.0f, current_position[Y_AXIS], feedrate / 5);
  1136. update_current_position_xyz();
  1137. if (!endstop_z_hit_on_purpose()) {
  1138. // SERIAL_ECHOLN("Search X span 1 - not found");
  1139. continue;
  1140. }
  1141. // SERIAL_ECHOLN("Search X span 1 - found");
  1142. b = current_position[X_AXIS];
  1143. // Go to the center.
  1144. enable_z_endstop(false);
  1145. current_position[X_AXIS] = 0.5f * (a + b);
  1146. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1147. found = true;
  1148. // Search in the Y direction along a cross.
  1149. found = false;
  1150. enable_z_endstop(true);
  1151. go_xy(current_position[X_AXIS], init_y_position + 16.0f, feedrate / 5);
  1152. update_current_position_xyz();
  1153. if (!endstop_z_hit_on_purpose()) {
  1154. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1155. continue;
  1156. }
  1157. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1158. a = current_position[Y_AXIS];
  1159. enable_z_endstop(false);
  1160. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1161. enable_z_endstop(true);
  1162. go_xy(current_position[X_AXIS], init_y_position - 16.0f, feedrate / 5);
  1163. update_current_position_xyz();
  1164. if (!endstop_z_hit_on_purpose()) {
  1165. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1166. continue;
  1167. }
  1168. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1169. b = current_position[Y_AXIS];
  1170. // Go to the center.
  1171. enable_z_endstop(false);
  1172. current_position[Y_AXIS] = 0.5f * (a + b);
  1173. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate / 5);
  1174. #ifdef SUPPORT_VERBOSITY
  1175. if (verbosity_level >= 20) {
  1176. SERIAL_ECHOPGM("ITERATION: ");
  1177. MYSERIAL.println(iteration);
  1178. SERIAL_ECHOPGM("CURRENT POSITION X: ");
  1179. MYSERIAL.println(current_position[X_AXIS]);
  1180. SERIAL_ECHOPGM("CURRENT POSITION Y: ");
  1181. MYSERIAL.println(current_position[Y_AXIS]);
  1182. }
  1183. #endif //SUPPORT_VERBOSITY
  1184. if (iteration > 0) {
  1185. // Average the last 7 measurements.
  1186. avg[X_AXIS] += current_position[X_AXIS];
  1187. avg[Y_AXIS] += current_position[Y_AXIS];
  1188. }
  1189. init_x_position = current_position[X_AXIS];
  1190. init_y_position = current_position[Y_AXIS];
  1191. found = true;
  1192. }
  1193. invert_z_endstop(false);
  1194. avg[X_AXIS] *= (1.f / 7.f);
  1195. avg[Y_AXIS] *= (1.f / 7.f);
  1196. current_position[X_AXIS] = avg[X_AXIS];
  1197. current_position[Y_AXIS] = avg[Y_AXIS];
  1198. #ifdef SUPPORT_VERBOSITY
  1199. if (verbosity_level >= 20) {
  1200. SERIAL_ECHOPGM("AVG CURRENT POSITION X: ");
  1201. MYSERIAL.println(current_position[X_AXIS]);
  1202. SERIAL_ECHOPGM("AVG CURRENT POSITION Y: ");
  1203. MYSERIAL.println(current_position[Y_AXIS]);
  1204. }
  1205. #endif // SUPPORT_VERBOSITY
  1206. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1207. #ifdef SUPPORT_VERBOSITY
  1208. if (verbosity_level >= 20) {
  1209. lcd_show_fullscreen_message_and_wait_P(PSTR("Final position"));
  1210. lcd_update_enable(true);
  1211. }
  1212. #endif //SUPPORT_VERBOSITY
  1213. break;
  1214. }
  1215. }
  1216. enable_z_endstop(false);
  1217. invert_z_endstop(false);
  1218. return found;
  1219. #endif //NEW_XYZCAL
  1220. }
  1221. #else //HEATBED_V2
  1222. inline bool find_bed_induction_sensor_point_xy(int verbosity_level)
  1223. {
  1224. #ifdef NEW_XYZCAL
  1225. return xyzcal_find_bed_induction_sensor_point_xy();
  1226. #else //NEW_XYZCAL
  1227. #ifdef SUPPORT_VERBOSITY
  1228. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  1229. #endif // SUPPORT_VERBOSITY
  1230. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1231. bool found = false;
  1232. {
  1233. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1234. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1235. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1236. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1237. uint8_t nsteps_y;
  1238. uint8_t i;
  1239. if (x0 < X_MIN_POS) {
  1240. x0 = X_MIN_POS;
  1241. #ifdef SUPPORT_VERBOSITY
  1242. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  1243. #endif // SUPPORT_VERBOSITY
  1244. }
  1245. if (x1 > X_MAX_POS) {
  1246. x1 = X_MAX_POS;
  1247. #ifdef SUPPORT_VERBOSITY
  1248. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  1249. #endif // SUPPORT_VERBOSITY
  1250. }
  1251. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1252. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1253. #ifdef SUPPORT_VERBOSITY
  1254. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1255. #endif // SUPPORT_VERBOSITY
  1256. }
  1257. if (y1 > Y_MAX_POS) {
  1258. y1 = Y_MAX_POS;
  1259. #ifdef SUPPORT_VERBOSITY
  1260. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1261. #endif // SUPPORT_VERBOSITY
  1262. }
  1263. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1264. enable_endstops(false);
  1265. bool dir_positive = true;
  1266. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1267. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1268. // Continously lower the Z axis.
  1269. endstops_hit_on_purpose();
  1270. enable_z_endstop(true);
  1271. while (current_position[Z_AXIS] > -10.f) {
  1272. // Do nsteps_y zig-zag movements.
  1273. current_position[Y_AXIS] = y0;
  1274. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i) {
  1275. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1276. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1277. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1278. dir_positive = !dir_positive;
  1279. if (endstop_z_hit_on_purpose())
  1280. goto endloop;
  1281. }
  1282. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i) {
  1283. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1284. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1285. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1286. dir_positive = !dir_positive;
  1287. if (endstop_z_hit_on_purpose())
  1288. goto endloop;
  1289. }
  1290. }
  1291. endloop:
  1292. // SERIAL_ECHOLN("First hit");
  1293. // we have to let the planner know where we are right now as it is not where we said to go.
  1294. update_current_position_xyz();
  1295. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  1296. for (int8_t iter = 0; iter < 3; ++iter) {
  1297. if (iter > 0) {
  1298. // Slightly lower the Z axis to get a reliable trigger.
  1299. current_position[Z_AXIS] -= 0.02f;
  1300. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS] / 60);
  1301. }
  1302. // Do nsteps_y zig-zag movements.
  1303. float a, b;
  1304. enable_endstops(false);
  1305. enable_z_endstop(false);
  1306. current_position[Y_AXIS] = y0;
  1307. go_xy(x0, current_position[Y_AXIS], feedrate);
  1308. enable_z_endstop(true);
  1309. found = false;
  1310. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1311. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1312. if (endstop_z_hit_on_purpose()) {
  1313. found = true;
  1314. break;
  1315. }
  1316. }
  1317. update_current_position_xyz();
  1318. if (!found) {
  1319. // SERIAL_ECHOLN("Search in Y - not found");
  1320. continue;
  1321. }
  1322. // SERIAL_ECHOLN("Search in Y - found");
  1323. a = current_position[Y_AXIS];
  1324. enable_z_endstop(false);
  1325. current_position[Y_AXIS] = y1;
  1326. go_xy(x0, current_position[Y_AXIS], feedrate);
  1327. enable_z_endstop(true);
  1328. found = false;
  1329. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1330. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1331. if (endstop_z_hit_on_purpose()) {
  1332. found = true;
  1333. break;
  1334. }
  1335. }
  1336. update_current_position_xyz();
  1337. if (!found) {
  1338. // SERIAL_ECHOLN("Search in Y2 - not found");
  1339. continue;
  1340. }
  1341. // SERIAL_ECHOLN("Search in Y2 - found");
  1342. b = current_position[Y_AXIS];
  1343. current_position[Y_AXIS] = 0.5f * (a + b);
  1344. // Search in the X direction along a cross.
  1345. found = false;
  1346. enable_z_endstop(false);
  1347. go_xy(x0, current_position[Y_AXIS], feedrate);
  1348. enable_z_endstop(true);
  1349. go_xy(x1, current_position[Y_AXIS], feedrate);
  1350. update_current_position_xyz();
  1351. if (!endstop_z_hit_on_purpose()) {
  1352. // SERIAL_ECHOLN("Search X span 0 - not found");
  1353. continue;
  1354. }
  1355. // SERIAL_ECHOLN("Search X span 0 - found");
  1356. a = current_position[X_AXIS];
  1357. enable_z_endstop(false);
  1358. go_xy(x1, current_position[Y_AXIS], feedrate);
  1359. enable_z_endstop(true);
  1360. go_xy(x0, current_position[Y_AXIS], feedrate);
  1361. update_current_position_xyz();
  1362. if (!endstop_z_hit_on_purpose()) {
  1363. // SERIAL_ECHOLN("Search X span 1 - not found");
  1364. continue;
  1365. }
  1366. // SERIAL_ECHOLN("Search X span 1 - found");
  1367. b = current_position[X_AXIS];
  1368. // Go to the center.
  1369. enable_z_endstop(false);
  1370. current_position[X_AXIS] = 0.5f * (a + b);
  1371. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1372. found = true;
  1373. #if 1
  1374. // Search in the Y direction along a cross.
  1375. found = false;
  1376. enable_z_endstop(false);
  1377. go_xy(current_position[X_AXIS], y0, feedrate);
  1378. enable_z_endstop(true);
  1379. go_xy(current_position[X_AXIS], y1, feedrate);
  1380. update_current_position_xyz();
  1381. if (!endstop_z_hit_on_purpose()) {
  1382. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1383. continue;
  1384. }
  1385. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1386. a = current_position[Y_AXIS];
  1387. enable_z_endstop(false);
  1388. go_xy(current_position[X_AXIS], y1, feedrate);
  1389. enable_z_endstop(true);
  1390. go_xy(current_position[X_AXIS], y0, feedrate);
  1391. update_current_position_xyz();
  1392. if (!endstop_z_hit_on_purpose()) {
  1393. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1394. continue;
  1395. }
  1396. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1397. b = current_position[Y_AXIS];
  1398. // Go to the center.
  1399. enable_z_endstop(false);
  1400. current_position[Y_AXIS] = 0.5f * (a + b);
  1401. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1402. found = true;
  1403. #endif
  1404. break;
  1405. }
  1406. }
  1407. enable_z_endstop(false);
  1408. return found;
  1409. #endif //NEW_XYZCAL
  1410. }
  1411. #endif //HEATBED_V2
  1412. #ifndef NEW_XYZCAL
  1413. // Search around the current_position[X,Y,Z].
  1414. // It is expected, that the induction sensor is switched on at the current position.
  1415. // Look around this center point by painting a star around the point.
  1416. inline bool improve_bed_induction_sensor_point()
  1417. {
  1418. static const float search_radius = 8.f;
  1419. bool endstops_enabled = enable_endstops(false);
  1420. bool endstop_z_enabled = enable_z_endstop(false);
  1421. bool found = false;
  1422. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1423. float center_old_x = current_position[X_AXIS];
  1424. float center_old_y = current_position[Y_AXIS];
  1425. float center_x = 0.f;
  1426. float center_y = 0.f;
  1427. for (uint8_t iter = 0; iter < 4; ++ iter) {
  1428. switch (iter) {
  1429. case 0:
  1430. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1431. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1432. break;
  1433. case 1:
  1434. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1435. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1436. break;
  1437. case 2:
  1438. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1439. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1440. break;
  1441. case 3:
  1442. default:
  1443. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1444. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1445. break;
  1446. }
  1447. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  1448. float vx = destination[X_AXIS] - center_old_x;
  1449. float vy = destination[Y_AXIS] - center_old_y;
  1450. float l = sqrt(vx*vx+vy*vy);
  1451. float t;
  1452. if (destination[X_AXIS] < X_MIN_POS) {
  1453. // Exiting the bed at xmin.
  1454. t = (center_x - X_MIN_POS) / l;
  1455. destination[X_AXIS] = X_MIN_POS;
  1456. destination[Y_AXIS] = center_old_y + t * vy;
  1457. } else if (destination[X_AXIS] > X_MAX_POS) {
  1458. // Exiting the bed at xmax.
  1459. t = (X_MAX_POS - center_x) / l;
  1460. destination[X_AXIS] = X_MAX_POS;
  1461. destination[Y_AXIS] = center_old_y + t * vy;
  1462. }
  1463. if (destination[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1464. // Exiting the bed at ymin.
  1465. t = (center_y - Y_MIN_POS_FOR_BED_CALIBRATION) / l;
  1466. destination[X_AXIS] = center_old_x + t * vx;
  1467. destination[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1468. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  1469. // Exiting the bed at xmax.
  1470. t = (Y_MAX_POS - center_y) / l;
  1471. destination[X_AXIS] = center_old_x + t * vx;
  1472. destination[Y_AXIS] = Y_MAX_POS;
  1473. }
  1474. // Move away from the measurement point.
  1475. enable_endstops(false);
  1476. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  1477. // Move towards the measurement point, until the induction sensor triggers.
  1478. enable_endstops(true);
  1479. go_xy(center_old_x, center_old_y, feedrate);
  1480. update_current_position_xyz();
  1481. // if (! endstop_z_hit_on_purpose()) return false;
  1482. center_x += current_position[X_AXIS];
  1483. center_y += current_position[Y_AXIS];
  1484. }
  1485. // Calculate the new center, move to the new center.
  1486. center_x /= 4.f;
  1487. center_y /= 4.f;
  1488. current_position[X_AXIS] = center_x;
  1489. current_position[Y_AXIS] = center_y;
  1490. enable_endstops(false);
  1491. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1492. enable_endstops(endstops_enabled);
  1493. enable_z_endstop(endstop_z_enabled);
  1494. return found;
  1495. }
  1496. #endif //NEW_XYZCAL
  1497. #ifndef NEW_XYZCAL
  1498. static inline void debug_output_point(const char *type, const float &x, const float &y, const float &z)
  1499. {
  1500. SERIAL_ECHOPGM("Measured ");
  1501. SERIAL_ECHORPGM(type);
  1502. SERIAL_ECHOPGM(" ");
  1503. MYSERIAL.print(x, 5);
  1504. SERIAL_ECHOPGM(", ");
  1505. MYSERIAL.print(y, 5);
  1506. SERIAL_ECHOPGM(", ");
  1507. MYSERIAL.print(z, 5);
  1508. SERIAL_ECHOLNPGM("");
  1509. }
  1510. #endif //NEW_XYZCAL
  1511. #ifndef NEW_XYZCAL
  1512. // Search around the current_position[X,Y,Z].
  1513. // It is expected, that the induction sensor is switched on at the current position.
  1514. // Look around this center point by painting a star around the point.
  1515. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  1516. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y, int8_t verbosity_level)
  1517. {
  1518. float center_old_x = current_position[X_AXIS];
  1519. float center_old_y = current_position[Y_AXIS];
  1520. float a, b;
  1521. bool point_small = false;
  1522. enable_endstops(false);
  1523. {
  1524. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1525. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1526. if (x0 < X_MIN_POS)
  1527. x0 = X_MIN_POS;
  1528. if (x1 > X_MAX_POS)
  1529. x1 = X_MAX_POS;
  1530. // Search in the X direction along a cross.
  1531. enable_z_endstop(false);
  1532. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1533. enable_z_endstop(true);
  1534. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1535. update_current_position_xyz();
  1536. if (! endstop_z_hit_on_purpose()) {
  1537. current_position[X_AXIS] = center_old_x;
  1538. goto canceled;
  1539. }
  1540. a = current_position[X_AXIS];
  1541. enable_z_endstop(false);
  1542. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1543. enable_z_endstop(true);
  1544. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1545. update_current_position_xyz();
  1546. if (! endstop_z_hit_on_purpose()) {
  1547. current_position[X_AXIS] = center_old_x;
  1548. goto canceled;
  1549. }
  1550. b = current_position[X_AXIS];
  1551. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1552. #ifdef SUPPORT_VERBOSITY
  1553. if (verbosity_level >= 5) {
  1554. SERIAL_ECHOPGM("Point width too small: ");
  1555. SERIAL_ECHO(b - a);
  1556. SERIAL_ECHOLNPGM("");
  1557. }
  1558. #endif // SUPPORT_VERBOSITY
  1559. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1560. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1561. // Don't use the new X value.
  1562. current_position[X_AXIS] = center_old_x;
  1563. goto canceled;
  1564. } else {
  1565. // Use the new value, but force the Z axis to go a bit lower.
  1566. point_small = true;
  1567. }
  1568. }
  1569. #ifdef SUPPORT_VERBOSITY
  1570. if (verbosity_level >= 5) {
  1571. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1572. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1573. }
  1574. #endif // SUPPORT_VERBOSITY
  1575. // Go to the center.
  1576. enable_z_endstop(false);
  1577. current_position[X_AXIS] = 0.5f * (a + b);
  1578. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1579. }
  1580. {
  1581. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1582. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1583. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1584. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1585. if (y1 > Y_MAX_POS)
  1586. y1 = Y_MAX_POS;
  1587. // Search in the Y direction along a cross.
  1588. enable_z_endstop(false);
  1589. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1590. if (lift_z_on_min_y) {
  1591. // The first row of points are very close to the end stop.
  1592. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  1593. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  1594. // and go back.
  1595. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  1596. }
  1597. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  1598. // Already triggering before we started the move.
  1599. // Shift the trigger point slightly outwards.
  1600. // a = current_position[Y_AXIS] - 1.5f;
  1601. a = current_position[Y_AXIS];
  1602. } else {
  1603. enable_z_endstop(true);
  1604. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1605. update_current_position_xyz();
  1606. if (! endstop_z_hit_on_purpose()) {
  1607. current_position[Y_AXIS] = center_old_y;
  1608. goto canceled;
  1609. }
  1610. a = current_position[Y_AXIS];
  1611. }
  1612. enable_z_endstop(false);
  1613. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1614. enable_z_endstop(true);
  1615. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1616. update_current_position_xyz();
  1617. if (! endstop_z_hit_on_purpose()) {
  1618. current_position[Y_AXIS] = center_old_y;
  1619. goto canceled;
  1620. }
  1621. b = current_position[Y_AXIS];
  1622. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1623. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1624. #ifdef SUPPORT_VERBOSITY
  1625. if (verbosity_level >= 5) {
  1626. SERIAL_ECHOPGM("Point height too small: ");
  1627. SERIAL_ECHO(b - a);
  1628. SERIAL_ECHOLNPGM("");
  1629. }
  1630. #endif // SUPPORT_VERBOSITY
  1631. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1632. // Don't use the new Y value.
  1633. current_position[Y_AXIS] = center_old_y;
  1634. goto canceled;
  1635. } else {
  1636. // Use the new value, but force the Z axis to go a bit lower.
  1637. point_small = true;
  1638. }
  1639. }
  1640. #ifdef SUPPORT_VERBOSITY
  1641. if (verbosity_level >= 5) {
  1642. debug_output_point(PSTR("top" ), current_position[X_AXIS], a, current_position[Z_AXIS]);
  1643. debug_output_point(PSTR("bottom"), current_position[X_AXIS], b, current_position[Z_AXIS]);
  1644. }
  1645. #endif // SUPPORT_VERBOSITY
  1646. // Go to the center.
  1647. enable_z_endstop(false);
  1648. current_position[Y_AXIS] = 0.5f * (a + b);
  1649. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1650. }
  1651. // If point is small but not too small, then force the Z axis to be lowered a bit,
  1652. // but use the new value. This is important when the initial position was off in one axis,
  1653. // for example if the initial calibration was shifted in the Y axis systematically.
  1654. // Then this first step will center.
  1655. return ! point_small;
  1656. canceled:
  1657. // Go back to the center.
  1658. enable_z_endstop(false);
  1659. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1660. return false;
  1661. }
  1662. #endif //NEW_XYZCAL
  1663. #ifndef NEW_XYZCAL
  1664. // Searching the front points, where one cannot move the sensor head in front of the sensor point.
  1665. // Searching in a zig-zag movement in a plane for the maximum width of the response.
  1666. // This function may set the current_position[Y_AXIS] below Y_MIN_POS, if the function succeeded.
  1667. // If this function failed, the Y coordinate will never be outside the working space.
  1668. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS (8.f)
  1669. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y (0.1f)
  1670. inline bool improve_bed_induction_sensor_point3(int verbosity_level)
  1671. {
  1672. float center_old_x = current_position[X_AXIS];
  1673. float center_old_y = current_position[Y_AXIS];
  1674. float a, b;
  1675. bool result = true;
  1676. #ifdef SUPPORT_VERBOSITY
  1677. if (verbosity_level >= 20) MYSERIAL.println("Improve bed induction sensor point3");
  1678. #endif // SUPPORT_VERBOSITY
  1679. // Was the sensor point detected too far in the minus Y axis?
  1680. // If yes, the center of the induction point cannot be reached by the machine.
  1681. {
  1682. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1683. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1684. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1685. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1686. float y = y0;
  1687. if (x0 < X_MIN_POS)
  1688. x0 = X_MIN_POS;
  1689. if (x1 > X_MAX_POS)
  1690. x1 = X_MAX_POS;
  1691. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1692. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1693. if (y1 > Y_MAX_POS)
  1694. y1 = Y_MAX_POS;
  1695. #ifdef SUPPORT_VERBOSITY
  1696. if (verbosity_level >= 20) {
  1697. SERIAL_ECHOPGM("Initial position: ");
  1698. SERIAL_ECHO(center_old_x);
  1699. SERIAL_ECHOPGM(", ");
  1700. SERIAL_ECHO(center_old_y);
  1701. SERIAL_ECHOLNPGM("");
  1702. }
  1703. #endif // SUPPORT_VERBOSITY
  1704. // Search in the positive Y direction, until a maximum diameter is found.
  1705. // (the next diameter is smaller than the current one.)
  1706. float dmax = 0.f;
  1707. float xmax1 = 0.f;
  1708. float xmax2 = 0.f;
  1709. for (y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1710. enable_z_endstop(false);
  1711. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1712. enable_z_endstop(true);
  1713. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1714. update_current_position_xyz();
  1715. if (! endstop_z_hit_on_purpose()) {
  1716. continue;
  1717. // SERIAL_PROTOCOLPGM("Failed 1\n");
  1718. // current_position[X_AXIS] = center_old_x;
  1719. // goto canceled;
  1720. }
  1721. a = current_position[X_AXIS];
  1722. enable_z_endstop(false);
  1723. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1724. enable_z_endstop(true);
  1725. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1726. update_current_position_xyz();
  1727. if (! endstop_z_hit_on_purpose()) {
  1728. continue;
  1729. // SERIAL_PROTOCOLPGM("Failed 2\n");
  1730. // current_position[X_AXIS] = center_old_x;
  1731. // goto canceled;
  1732. }
  1733. b = current_position[X_AXIS];
  1734. #ifdef SUPPORT_VERBOSITY
  1735. if (verbosity_level >= 5) {
  1736. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1737. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1738. }
  1739. #endif // SUPPORT_VERBOSITY
  1740. float d = b - a;
  1741. if (d > dmax) {
  1742. xmax1 = 0.5f * (a + b);
  1743. dmax = d;
  1744. } else if (dmax > 0.) {
  1745. y0 = y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1746. break;
  1747. }
  1748. }
  1749. if (dmax == 0.) {
  1750. #ifdef SUPPORT_VERBOSITY
  1751. if (verbosity_level > 0)
  1752. SERIAL_PROTOCOLPGM("failed - not found\n");
  1753. #endif // SUPPORT_VERBOSITY
  1754. current_position[X_AXIS] = center_old_x;
  1755. current_position[Y_AXIS] = center_old_y;
  1756. goto canceled;
  1757. }
  1758. {
  1759. // Find the positive Y hit. This gives the extreme Y value for the search of the maximum diameter in the -Y direction.
  1760. enable_z_endstop(false);
  1761. go_xy(xmax1, y0 + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1762. enable_z_endstop(true);
  1763. go_xy(xmax1, max(y0 - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1764. update_current_position_xyz();
  1765. if (! endstop_z_hit_on_purpose()) {
  1766. current_position[Y_AXIS] = center_old_y;
  1767. goto canceled;
  1768. }
  1769. #ifdef SUPPORT_VERBOSITY
  1770. if (verbosity_level >= 5)
  1771. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1772. #endif // SUPPORT_VERBOSITY
  1773. y1 = current_position[Y_AXIS];
  1774. }
  1775. if (y1 <= y0) {
  1776. // Either the induction sensor is too high, or the induction sensor target is out of reach.
  1777. current_position[Y_AXIS] = center_old_y;
  1778. goto canceled;
  1779. }
  1780. // Search in the negative Y direction, until a maximum diameter is found.
  1781. dmax = 0.f;
  1782. // if (y0 + 1.f < y1)
  1783. // y1 = y0 + 1.f;
  1784. for (y = y1; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1785. enable_z_endstop(false);
  1786. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1787. enable_z_endstop(true);
  1788. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1789. update_current_position_xyz();
  1790. if (! endstop_z_hit_on_purpose()) {
  1791. continue;
  1792. /*
  1793. current_position[X_AXIS] = center_old_x;
  1794. SERIAL_PROTOCOLPGM("Failed 3\n");
  1795. goto canceled;
  1796. */
  1797. }
  1798. a = current_position[X_AXIS];
  1799. enable_z_endstop(false);
  1800. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1801. enable_z_endstop(true);
  1802. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1803. update_current_position_xyz();
  1804. if (! endstop_z_hit_on_purpose()) {
  1805. continue;
  1806. /*
  1807. current_position[X_AXIS] = center_old_x;
  1808. SERIAL_PROTOCOLPGM("Failed 4\n");
  1809. goto canceled;
  1810. */
  1811. }
  1812. b = current_position[X_AXIS];
  1813. #ifdef SUPPORT_VERBOSITY
  1814. if (verbosity_level >= 5) {
  1815. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1816. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1817. }
  1818. #endif // SUPPORT_VERBOSITY
  1819. float d = b - a;
  1820. if (d > dmax) {
  1821. xmax2 = 0.5f * (a + b);
  1822. dmax = d;
  1823. } else if (dmax > 0.) {
  1824. y1 = y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1825. break;
  1826. }
  1827. }
  1828. float xmax, ymax;
  1829. if (dmax == 0.f) {
  1830. // Only the hit in the positive direction found.
  1831. xmax = xmax1;
  1832. ymax = y0;
  1833. } else {
  1834. // Both positive and negative directions found.
  1835. xmax = xmax2;
  1836. ymax = 0.5f * (y0 + y1);
  1837. for (; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1838. enable_z_endstop(false);
  1839. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1840. enable_z_endstop(true);
  1841. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1842. update_current_position_xyz();
  1843. if (! endstop_z_hit_on_purpose()) {
  1844. continue;
  1845. /*
  1846. current_position[X_AXIS] = center_old_x;
  1847. SERIAL_PROTOCOLPGM("Failed 3\n");
  1848. goto canceled;
  1849. */
  1850. }
  1851. a = current_position[X_AXIS];
  1852. enable_z_endstop(false);
  1853. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1854. enable_z_endstop(true);
  1855. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1856. update_current_position_xyz();
  1857. if (! endstop_z_hit_on_purpose()) {
  1858. continue;
  1859. /*
  1860. current_position[X_AXIS] = center_old_x;
  1861. SERIAL_PROTOCOLPGM("Failed 4\n");
  1862. goto canceled;
  1863. */
  1864. }
  1865. b = current_position[X_AXIS];
  1866. #ifdef SUPPORT_VERBOSITY
  1867. if (verbosity_level >= 5) {
  1868. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1869. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1870. }
  1871. #endif // SUPPORT_VERBOSITY
  1872. float d = b - a;
  1873. if (d > dmax) {
  1874. xmax = 0.5f * (a + b);
  1875. ymax = y;
  1876. dmax = d;
  1877. }
  1878. }
  1879. }
  1880. {
  1881. // Compare the distance in the Y+ direction with the diameter in the X direction.
  1882. // Find the positive Y hit once again, this time along the Y axis going through the X point with the highest diameter.
  1883. enable_z_endstop(false);
  1884. go_xy(xmax, ymax + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1885. enable_z_endstop(true);
  1886. go_xy(xmax, max(ymax - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1887. update_current_position_xyz();
  1888. if (! endstop_z_hit_on_purpose()) {
  1889. current_position[Y_AXIS] = center_old_y;
  1890. goto canceled;
  1891. }
  1892. #ifdef SUPPORT_VERBOSITY
  1893. if (verbosity_level >= 5)
  1894. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1895. #endif // SUPPORT_VERBOSITY
  1896. if (current_position[Y_AXIS] - Y_MIN_POS_FOR_BED_CALIBRATION < 0.5f * dmax) {
  1897. // Probably not even a half circle was detected. The induction point is likely too far in the minus Y direction.
  1898. // First verify, if the measurement has been done at a sufficient height. If no, lower the Z axis a bit.
  1899. if (current_position[Y_AXIS] < ymax || dmax < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1900. #ifdef SUPPORT_VERBOSITY
  1901. if (verbosity_level >= 5) {
  1902. SERIAL_ECHOPGM("Partial point diameter too small: ");
  1903. SERIAL_ECHO(dmax);
  1904. SERIAL_ECHOLNPGM("");
  1905. }
  1906. #endif // SUPPORT_VERBOSITY
  1907. result = false;
  1908. } else {
  1909. // Estimate the circle radius from the maximum diameter and height:
  1910. float h = current_position[Y_AXIS] - ymax;
  1911. float r = dmax * dmax / (8.f * h) + 0.5f * h;
  1912. if (r < 0.8f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1913. #ifdef SUPPORT_VERBOSITY
  1914. if (verbosity_level >= 5) {
  1915. SERIAL_ECHOPGM("Partial point estimated radius too small: ");
  1916. SERIAL_ECHO(r);
  1917. SERIAL_ECHOPGM(", dmax:");
  1918. SERIAL_ECHO(dmax);
  1919. SERIAL_ECHOPGM(", h:");
  1920. SERIAL_ECHO(h);
  1921. SERIAL_ECHOLNPGM("");
  1922. }
  1923. #endif // SUPPORT_VERBOSITY
  1924. result = false;
  1925. } else {
  1926. // The point may end up outside of the machine working space.
  1927. // That is all right as it helps to improve the accuracy of the measurement point
  1928. // due to averaging.
  1929. // For the y correction, use an average of dmax/2 and the estimated radius.
  1930. r = 0.5f * (0.5f * dmax + r);
  1931. ymax = current_position[Y_AXIS] - r;
  1932. }
  1933. }
  1934. } else {
  1935. // If the diameter of the detected spot was smaller than a minimum allowed,
  1936. // the induction sensor is probably too high. Returning false will force
  1937. // the sensor to be lowered a tiny bit.
  1938. result = xmax >= MIN_BED_SENSOR_POINT_RESPONSE_DMR;
  1939. if (y0 > Y_MIN_POS_FOR_BED_CALIBRATION + 0.2f)
  1940. // Only in case both left and right y tangents are known, use them.
  1941. // If y0 is close to the bed edge, it may not be symmetric to the right tangent.
  1942. ymax = 0.5f * ymax + 0.25f * (y0 + y1);
  1943. }
  1944. }
  1945. // Go to the center.
  1946. enable_z_endstop(false);
  1947. current_position[X_AXIS] = xmax;
  1948. current_position[Y_AXIS] = ymax;
  1949. #ifdef SUPPORT_VERBOSITY
  1950. if (verbosity_level >= 20) {
  1951. SERIAL_ECHOPGM("Adjusted position: ");
  1952. SERIAL_ECHO(current_position[X_AXIS]);
  1953. SERIAL_ECHOPGM(", ");
  1954. SERIAL_ECHO(current_position[Y_AXIS]);
  1955. SERIAL_ECHOLNPGM("");
  1956. }
  1957. #endif // SUPPORT_VERBOSITY
  1958. // Don't clamp current_position[Y_AXIS], because the out-of-reach Y coordinate may actually be true.
  1959. // Only clamp the coordinate to go.
  1960. go_xy(current_position[X_AXIS], max(Y_MIN_POS, current_position[Y_AXIS]), homing_feedrate[X_AXIS] / 60.f);
  1961. // delay_keep_alive(3000);
  1962. }
  1963. if (result)
  1964. return true;
  1965. // otherwise clamp the Y coordinate
  1966. canceled:
  1967. // Go back to the center.
  1968. enable_z_endstop(false);
  1969. if (current_position[Y_AXIS] < Y_MIN_POS)
  1970. current_position[Y_AXIS] = Y_MIN_POS;
  1971. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1972. return false;
  1973. }
  1974. #endif //NEW_XYZCAL
  1975. #ifndef NEW_XYZCAL
  1976. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  1977. // write the trigger coordinates to the serial line.
  1978. // Useful for visualizing the behavior of the bed induction detector.
  1979. inline void scan_bed_induction_sensor_point()
  1980. {
  1981. float center_old_x = current_position[X_AXIS];
  1982. float center_old_y = current_position[Y_AXIS];
  1983. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1984. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1985. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1986. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1987. float y = y0;
  1988. if (x0 < X_MIN_POS)
  1989. x0 = X_MIN_POS;
  1990. if (x1 > X_MAX_POS)
  1991. x1 = X_MAX_POS;
  1992. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1993. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1994. if (y1 > Y_MAX_POS)
  1995. y1 = Y_MAX_POS;
  1996. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1997. enable_z_endstop(false);
  1998. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1999. enable_z_endstop(true);
  2000. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  2001. update_current_position_xyz();
  2002. if (endstop_z_hit_on_purpose())
  2003. debug_output_point(PSTR("left" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  2004. enable_z_endstop(false);
  2005. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  2006. enable_z_endstop(true);
  2007. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  2008. update_current_position_xyz();
  2009. if (endstop_z_hit_on_purpose())
  2010. debug_output_point(PSTR("right"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  2011. }
  2012. enable_z_endstop(false);
  2013. current_position[X_AXIS] = center_old_x;
  2014. current_position[Y_AXIS] = center_old_y;
  2015. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  2016. }
  2017. #endif //NEW_XYZCAL
  2018. #define MESH_BED_CALIBRATION_SHOW_LCD
  2019. BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask)
  2020. {
  2021. // Don't let the manage_inactivity() function remove power from the motors.
  2022. refresh_cmd_timeout();
  2023. // Reusing the z_values memory for the measurement cache.
  2024. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2025. float *pts = &mbl.z_values[0][0];
  2026. float *vec_x = pts + 2 * 4;
  2027. float *vec_y = vec_x + 2;
  2028. float *cntr = vec_y + 2;
  2029. memset(pts, 0, sizeof(float) * 7 * 7);
  2030. uint8_t iteration = 0;
  2031. BedSkewOffsetDetectionResultType result;
  2032. // SERIAL_ECHOLNPGM("find_bed_offset_and_skew verbosity level: ");
  2033. // SERIAL_ECHO(int(verbosity_level));
  2034. // SERIAL_ECHOPGM("");
  2035. #ifdef NEW_XYZCAL
  2036. {
  2037. #else //NEW_XYZCAL
  2038. while (iteration < 3) {
  2039. #endif //NEW_XYZCAL
  2040. SERIAL_ECHOPGM("Iteration: ");
  2041. MYSERIAL.println(int(iteration + 1));
  2042. #ifdef SUPPORT_VERBOSITY
  2043. if (verbosity_level >= 20) {
  2044. SERIAL_ECHOLNPGM("Vectors: ");
  2045. SERIAL_ECHOPGM("vec_x[0]:");
  2046. MYSERIAL.print(vec_x[0], 5);
  2047. SERIAL_ECHOLNPGM("");
  2048. SERIAL_ECHOPGM("vec_x[1]:");
  2049. MYSERIAL.print(vec_x[1], 5);
  2050. SERIAL_ECHOLNPGM("");
  2051. SERIAL_ECHOPGM("vec_y[0]:");
  2052. MYSERIAL.print(vec_y[0], 5);
  2053. SERIAL_ECHOLNPGM("");
  2054. SERIAL_ECHOPGM("vec_y[1]:");
  2055. MYSERIAL.print(vec_y[1], 5);
  2056. SERIAL_ECHOLNPGM("");
  2057. SERIAL_ECHOPGM("cntr[0]:");
  2058. MYSERIAL.print(cntr[0], 5);
  2059. SERIAL_ECHOLNPGM("");
  2060. SERIAL_ECHOPGM("cntr[1]:");
  2061. MYSERIAL.print(cntr[1], 5);
  2062. SERIAL_ECHOLNPGM("");
  2063. }
  2064. #endif // SUPPORT_VERBOSITY
  2065. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2066. uint8_t next_line;
  2067. lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1), next_line);
  2068. if (next_line > 3)
  2069. next_line = 3;
  2070. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2071. // Collect the rear 2x3 points.
  2072. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2073. for (int k = 0; k < 4; ++k) {
  2074. // Don't let the manage_inactivity() function remove power from the motors.
  2075. refresh_cmd_timeout();
  2076. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2077. lcd_set_cursor(0, next_line);
  2078. lcd_print(k + 1);
  2079. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));
  2080. if (iteration > 0) {
  2081. lcd_puts_at_P(0, next_line + 1, _i("Iteration "));////MSG_FIND_BED_OFFSET_AND_SKEW_ITERATION c=20 r=0
  2082. lcd_print(int(iteration + 1));
  2083. }
  2084. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2085. float *pt = pts + k * 2;
  2086. // Go up to z_initial.
  2087. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  2088. #ifdef SUPPORT_VERBOSITY
  2089. if (verbosity_level >= 20) {
  2090. // Go to Y0, wait, then go to Y-4.
  2091. current_position[Y_AXIS] = 0.f;
  2092. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2093. SERIAL_ECHOLNPGM("At Y0");
  2094. delay_keep_alive(5000);
  2095. current_position[Y_AXIS] = Y_MIN_POS;
  2096. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2097. SERIAL_ECHOLNPGM("At Y-4");
  2098. delay_keep_alive(5000);
  2099. }
  2100. #endif // SUPPORT_VERBOSITY
  2101. // Go to the measurement point position.
  2102. //if (iteration == 0) {
  2103. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2);
  2104. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2 + 1);
  2105. /*}
  2106. else {
  2107. // if first iteration failed, count corrected point coordinates as initial
  2108. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2109. current_position[X_AXIS] = vec_x[0] * pgm_read_float(bed_ref_points_4 + k * 2) + vec_y[0] * pgm_read_float(bed_ref_points_4 + k * 2 + 1) + cntr[0];
  2110. current_position[Y_AXIS] = vec_x[1] * pgm_read_float(bed_ref_points_4 + k * 2) + vec_y[1] * pgm_read_float(bed_ref_points_4 + k * 2 + 1) + cntr[1];
  2111. // The calibration points are very close to the min Y.
  2112. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2113. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2114. }*/
  2115. #ifdef SUPPORT_VERBOSITY
  2116. if (verbosity_level >= 20) {
  2117. SERIAL_ECHOPGM("current_position[X_AXIS]:");
  2118. MYSERIAL.print(current_position[X_AXIS], 5);
  2119. SERIAL_ECHOLNPGM("");
  2120. SERIAL_ECHOPGM("current_position[Y_AXIS]:");
  2121. MYSERIAL.print(current_position[Y_AXIS], 5);
  2122. SERIAL_ECHOLNPGM("");
  2123. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  2124. MYSERIAL.print(current_position[Z_AXIS], 5);
  2125. SERIAL_ECHOLNPGM("");
  2126. }
  2127. #endif // SUPPORT_VERBOSITY
  2128. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2129. #ifdef SUPPORT_VERBOSITY
  2130. if (verbosity_level >= 10)
  2131. delay_keep_alive(3000);
  2132. #endif // SUPPORT_VERBOSITY
  2133. if (!find_bed_induction_sensor_point_xy(verbosity_level))
  2134. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2135. #ifndef NEW_XYZCAL
  2136. #ifndef HEATBED_V2
  2137. if (k == 0 || k == 1) {
  2138. // Improve the position of the 1st row sensor points by a zig-zag movement.
  2139. find_bed_induction_sensor_point_z();
  2140. int8_t i = 4;
  2141. for (;;) {
  2142. if (improve_bed_induction_sensor_point3(verbosity_level))
  2143. break;
  2144. if (--i == 0)
  2145. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2146. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2147. current_position[Z_AXIS] -= 0.025f;
  2148. enable_endstops(false);
  2149. enable_z_endstop(false);
  2150. go_to_current(homing_feedrate[Z_AXIS]);
  2151. }
  2152. if (i == 0)
  2153. // not found
  2154. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2155. }
  2156. #endif //HEATBED_V2
  2157. #endif
  2158. #ifdef SUPPORT_VERBOSITY
  2159. if (verbosity_level >= 10)
  2160. delay_keep_alive(3000);
  2161. #endif // SUPPORT_VERBOSITY
  2162. // Save the detected point position and then clamp the Y coordinate, which may have been estimated
  2163. // to lie outside the machine working space.
  2164. #ifdef SUPPORT_VERBOSITY
  2165. if (verbosity_level >= 20) {
  2166. SERIAL_ECHOLNPGM("Measured:");
  2167. MYSERIAL.println(current_position[X_AXIS]);
  2168. MYSERIAL.println(current_position[Y_AXIS]);
  2169. }
  2170. #endif // SUPPORT_VERBOSITY
  2171. pt[0] = (pt[0] * iteration) / (iteration + 1);
  2172. pt[0] += (current_position[X_AXIS]/(iteration + 1)); //count average
  2173. pt[1] = (pt[1] * iteration) / (iteration + 1);
  2174. pt[1] += (current_position[Y_AXIS] / (iteration + 1));
  2175. //pt[0] += current_position[X_AXIS];
  2176. //if(iteration > 0) pt[0] = pt[0] / 2;
  2177. //pt[1] += current_position[Y_AXIS];
  2178. //if (iteration > 0) pt[1] = pt[1] / 2;
  2179. #ifdef SUPPORT_VERBOSITY
  2180. if (verbosity_level >= 20) {
  2181. SERIAL_ECHOLNPGM("");
  2182. SERIAL_ECHOPGM("pt[0]:");
  2183. MYSERIAL.println(pt[0]);
  2184. SERIAL_ECHOPGM("pt[1]:");
  2185. MYSERIAL.println(pt[1]);
  2186. }
  2187. #endif // SUPPORT_VERBOSITY
  2188. if (current_position[Y_AXIS] < Y_MIN_POS)
  2189. current_position[Y_AXIS] = Y_MIN_POS;
  2190. // Start searching for the other points at 3mm above the last point.
  2191. current_position[Z_AXIS] += 3.f + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2192. //cntr[0] += pt[0];
  2193. //cntr[1] += pt[1];
  2194. #ifdef SUPPORT_VERBOSITY
  2195. if (verbosity_level >= 10 && k == 0) {
  2196. // Show the zero. Test, whether the Y motor skipped steps.
  2197. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  2198. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2199. delay_keep_alive(3000);
  2200. }
  2201. #endif // SUPPORT_VERBOSITY
  2202. }
  2203. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2204. #ifdef SUPPORT_VERBOSITY
  2205. if (verbosity_level >= 20) {
  2206. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2207. delay_keep_alive(3000);
  2208. for (int8_t mesh_point = 0; mesh_point < 4; ++mesh_point) {
  2209. // Don't let the manage_inactivity() function remove power from the motors.
  2210. refresh_cmd_timeout();
  2211. // Go to the measurement point.
  2212. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2213. current_position[X_AXIS] = pts[mesh_point * 2];
  2214. current_position[Y_AXIS] = pts[mesh_point * 2 + 1];
  2215. go_to_current(homing_feedrate[X_AXIS] / 60);
  2216. delay_keep_alive(3000);
  2217. }
  2218. }
  2219. #endif // SUPPORT_VERBOSITY
  2220. if (pts[1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  2221. too_far_mask |= 1 << 1; //front center point is out of reach
  2222. SERIAL_ECHOLNPGM("");
  2223. SERIAL_ECHOPGM("WARNING: Front point not reachable. Y coordinate:");
  2224. MYSERIAL.print(pts[1]);
  2225. SERIAL_ECHOPGM(" < ");
  2226. MYSERIAL.println(Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2227. }
  2228. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2229. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2230. if (result >= 0) {
  2231. world2machine_update(vec_x, vec_y, cntr);
  2232. #if 1
  2233. // Fearlessly store the calibration values into the eeprom.
  2234. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0), cntr[0]);
  2235. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4), cntr[1]);
  2236. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0), vec_x[0]);
  2237. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4), vec_x[1]);
  2238. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0), vec_y[0]);
  2239. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4), vec_y[1]);
  2240. #endif
  2241. #ifdef SUPPORT_VERBOSITY
  2242. if (verbosity_level >= 10) {
  2243. // Length of the vec_x
  2244. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  2245. SERIAL_ECHOLNPGM("X vector length:");
  2246. MYSERIAL.println(l);
  2247. // Length of the vec_y
  2248. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  2249. SERIAL_ECHOLNPGM("Y vector length:");
  2250. MYSERIAL.println(l);
  2251. // Zero point correction
  2252. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  2253. SERIAL_ECHOLNPGM("Zero point correction:");
  2254. MYSERIAL.println(l);
  2255. // vec_x and vec_y shall be nearly perpendicular.
  2256. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  2257. SERIAL_ECHOLNPGM("Perpendicularity");
  2258. MYSERIAL.println(fabs(l));
  2259. SERIAL_ECHOLNPGM("Saving bed calibration vectors to EEPROM");
  2260. }
  2261. #endif // SUPPORT_VERBOSITY
  2262. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2263. world2machine_update_current();
  2264. #ifdef SUPPORT_VERBOSITY
  2265. if (verbosity_level >= 20) {
  2266. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2267. delay_keep_alive(3000);
  2268. for (int8_t mesh_point = 0; mesh_point < 9; ++mesh_point) {
  2269. // Don't let the manage_inactivity() function remove power from the motors.
  2270. refresh_cmd_timeout();
  2271. // Go to the measurement point.
  2272. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2273. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  2274. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2275. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix;
  2276. current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2277. current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2278. go_to_current(homing_feedrate[X_AXIS] / 60);
  2279. delay_keep_alive(3000);
  2280. }
  2281. }
  2282. #endif // SUPPORT_VERBOSITY
  2283. return result;
  2284. }
  2285. if (result == BED_SKEW_OFFSET_DETECTION_FITTING_FAILED && too_far_mask == 2) return result; //if fitting failed and front center point is out of reach, terminate calibration and inform user
  2286. iteration++;
  2287. }
  2288. return result;
  2289. }
  2290. #ifndef NEW_XYZCAL
  2291. BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask)
  2292. {
  2293. // Don't let the manage_inactivity() function remove power from the motors.
  2294. refresh_cmd_timeout();
  2295. // Mask of the first three points. Are they too far?
  2296. too_far_mask = 0;
  2297. // Reusing the z_values memory for the measurement cache.
  2298. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2299. float *pts = &mbl.z_values[0][0];
  2300. float *vec_x = pts + 2 * 9;
  2301. float *vec_y = vec_x + 2;
  2302. float *cntr = vec_y + 2;
  2303. memset(pts, 0, sizeof(float) * 7 * 7);
  2304. #ifdef SUPPORT_VERBOSITY
  2305. if (verbosity_level >= 10) SERIAL_ECHOLNPGM("Improving bed offset and skew");
  2306. #endif // SUPPORT_VERBOSITY
  2307. // Cache the current correction matrix.
  2308. world2machine_initialize();
  2309. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2310. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2311. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2312. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2313. cntr[0] = world2machine_shift[0];
  2314. cntr[1] = world2machine_shift[1];
  2315. // and reset the correction matrix, so the planner will not do anything.
  2316. world2machine_reset();
  2317. bool endstops_enabled = enable_endstops(false);
  2318. bool endstop_z_enabled = enable_z_endstop(false);
  2319. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2320. uint8_t next_line;
  2321. lcd_display_message_fullscreen_P(_i("Improving bed calibration point"), next_line);////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1 c=60 r=0
  2322. if (next_line > 3)
  2323. next_line = 3;
  2324. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2325. // Collect a matrix of 9x9 points.
  2326. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  2327. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2328. // Don't let the manage_inactivity() function remove power from the motors.
  2329. refresh_cmd_timeout();
  2330. // Print the decrasing ID of the measurement point.
  2331. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2332. lcd_set_cursor(0, next_line);
  2333. lcd_print(mesh_point+1);
  2334. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2 c=14 r=0
  2335. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2336. // Move up.
  2337. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2338. enable_endstops(false);
  2339. enable_z_endstop(false);
  2340. go_to_current(homing_feedrate[Z_AXIS]/60);
  2341. #ifdef SUPPORT_VERBOSITY
  2342. if (verbosity_level >= 20) {
  2343. // Go to Y0, wait, then go to Y-4.
  2344. current_position[Y_AXIS] = 0.f;
  2345. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2346. SERIAL_ECHOLNPGM("At Y0");
  2347. delay_keep_alive(5000);
  2348. current_position[Y_AXIS] = Y_MIN_POS;
  2349. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2350. SERIAL_ECHOLNPGM("At Y_MIN_POS");
  2351. delay_keep_alive(5000);
  2352. }
  2353. #endif // SUPPORT_VERBOSITY
  2354. // Go to the measurement point.
  2355. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2356. current_position[X_AXIS] = vec_x[0] * pgm_read_float(bed_ref_points_4+mesh_point*2) + vec_y[0] * pgm_read_float(bed_ref_points_4+mesh_point*2+1) + cntr[0];
  2357. current_position[Y_AXIS] = vec_x[1] * pgm_read_float(bed_ref_points_4+mesh_point*2) + vec_y[1] * pgm_read_float(bed_ref_points_4+mesh_point*2+1) + cntr[1];
  2358. // The calibration points are very close to the min Y.
  2359. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION){
  2360. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2361. #ifdef SUPPORT_VERBOSITY
  2362. if (verbosity_level >= 20) {
  2363. SERIAL_ECHOPGM("Calibration point ");
  2364. SERIAL_ECHO(mesh_point);
  2365. SERIAL_ECHOPGM("lower than Ymin. Y coordinate clamping was used.");
  2366. SERIAL_ECHOLNPGM("");
  2367. }
  2368. #endif // SUPPORT_VERBOSITY
  2369. }
  2370. go_to_current(homing_feedrate[X_AXIS]/60);
  2371. // Find its Z position by running the normal vertical search.
  2372. #ifdef SUPPORT_VERBOSITY
  2373. if (verbosity_level >= 10)
  2374. delay_keep_alive(3000);
  2375. #endif // SUPPORT_VERBOSITY
  2376. find_bed_induction_sensor_point_z();
  2377. #ifdef SUPPORT_VERBOSITY
  2378. if (verbosity_level >= 10)
  2379. delay_keep_alive(3000);
  2380. #endif // SUPPORT_VERBOSITY
  2381. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2382. current_position[Z_AXIS] -= 0.025f;
  2383. // Improve the point position by searching its center in a current plane.
  2384. int8_t n_errors = 3;
  2385. for (int8_t iter = 0; iter < 8; ) {
  2386. #ifdef SUPPORT_VERBOSITY
  2387. if (verbosity_level > 20) {
  2388. SERIAL_ECHOPGM("Improving bed point ");
  2389. SERIAL_ECHO(mesh_point);
  2390. SERIAL_ECHOPGM(", iteration ");
  2391. SERIAL_ECHO(iter);
  2392. SERIAL_ECHOPGM(", z");
  2393. MYSERIAL.print(current_position[Z_AXIS], 5);
  2394. SERIAL_ECHOLNPGM("");
  2395. }
  2396. #endif // SUPPORT_VERBOSITY
  2397. bool found = false;
  2398. if (mesh_point < 2) {
  2399. // Because the sensor cannot move in front of the first row
  2400. // of the sensor points, the y position cannot be measured
  2401. // by a cross center method.
  2402. // Use a zig-zag search for the first row of the points.
  2403. found = improve_bed_induction_sensor_point3(verbosity_level);
  2404. } else {
  2405. switch (method) {
  2406. case 0: found = improve_bed_induction_sensor_point(); break;
  2407. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 2, verbosity_level); break;
  2408. default: break;
  2409. }
  2410. }
  2411. if (found) {
  2412. if (iter > 3) {
  2413. // Average the last 4 measurements.
  2414. pts[mesh_point*2 ] += current_position[X_AXIS];
  2415. pts[mesh_point*2+1] += current_position[Y_AXIS];
  2416. }
  2417. if (current_position[Y_AXIS] < Y_MIN_POS)
  2418. current_position[Y_AXIS] = Y_MIN_POS;
  2419. ++ iter;
  2420. } else if (n_errors -- == 0) {
  2421. // Give up.
  2422. result = BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2423. goto canceled;
  2424. } else {
  2425. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2426. current_position[Z_AXIS] -= 0.05f;
  2427. enable_endstops(false);
  2428. enable_z_endstop(false);
  2429. go_to_current(homing_feedrate[Z_AXIS]);
  2430. #ifdef SUPPORT_VERBOSITY
  2431. if (verbosity_level >= 5) {
  2432. SERIAL_ECHOPGM("Improving bed point ");
  2433. SERIAL_ECHO(mesh_point);
  2434. SERIAL_ECHOPGM(", iteration ");
  2435. SERIAL_ECHO(iter);
  2436. SERIAL_ECHOPGM(" failed. Lowering z to ");
  2437. MYSERIAL.print(current_position[Z_AXIS], 5);
  2438. SERIAL_ECHOLNPGM("");
  2439. }
  2440. #endif // SUPPORT_VERBOSITY
  2441. }
  2442. }
  2443. #ifdef SUPPORT_VERBOSITY
  2444. if (verbosity_level >= 10)
  2445. delay_keep_alive(3000);
  2446. #endif // SUPPORT_VERBOSITY
  2447. }
  2448. // Don't let the manage_inactivity() function remove power from the motors.
  2449. refresh_cmd_timeout();
  2450. // Average the last 4 measurements.
  2451. for (int8_t i = 0; i < 8; ++ i)
  2452. pts[i] *= (1.f/4.f);
  2453. enable_endstops(false);
  2454. enable_z_endstop(false);
  2455. #ifdef SUPPORT_VERBOSITY
  2456. if (verbosity_level >= 5) {
  2457. // Test the positions. Are the positions reproducible?
  2458. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2459. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2460. // Don't let the manage_inactivity() function remove power from the motors.
  2461. refresh_cmd_timeout();
  2462. // Go to the measurement point.
  2463. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2464. current_position[X_AXIS] = pts[mesh_point*2];
  2465. current_position[Y_AXIS] = pts[mesh_point*2+1];
  2466. if (verbosity_level >= 10) {
  2467. go_to_current(homing_feedrate[X_AXIS]/60);
  2468. delay_keep_alive(3000);
  2469. }
  2470. SERIAL_ECHOPGM("Final measured bed point ");
  2471. SERIAL_ECHO(mesh_point);
  2472. SERIAL_ECHOPGM(": ");
  2473. MYSERIAL.print(current_position[X_AXIS], 5);
  2474. SERIAL_ECHOPGM(", ");
  2475. MYSERIAL.print(current_position[Y_AXIS], 5);
  2476. SERIAL_ECHOLNPGM("");
  2477. }
  2478. }
  2479. #endif // SUPPORT_VERBOSITY
  2480. {
  2481. // First fill in the too_far_mask from the measured points.
  2482. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point)
  2483. if (pts[mesh_point * 2 + 1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2484. too_far_mask |= 1 << mesh_point;
  2485. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2486. if (result < 0) {
  2487. SERIAL_ECHOLNPGM("Calculation of the machine skew and offset failed.");
  2488. goto canceled;
  2489. }
  2490. // In case of success, update the too_far_mask from the calculated points.
  2491. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point) {
  2492. float y = vec_x[1] * pgm_read_float(bed_ref_points_4+mesh_point*2) + vec_y[1] * pgm_read_float(bed_ref_points_4+mesh_point*2+1) + cntr[1];
  2493. #ifdef SUPPORT_VERBOSITY
  2494. if (verbosity_level >= 20) {
  2495. SERIAL_ECHOLNPGM("");
  2496. SERIAL_ECHOPGM("Distance from min:");
  2497. MYSERIAL.print(y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2498. SERIAL_ECHOLNPGM("");
  2499. SERIAL_ECHOPGM("y:");
  2500. MYSERIAL.print(y);
  2501. SERIAL_ECHOLNPGM("");
  2502. }
  2503. #endif // SUPPORT_VERBOSITY
  2504. if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2505. too_far_mask |= 1 << mesh_point;
  2506. }
  2507. }
  2508. world2machine_update(vec_x, vec_y, cntr);
  2509. #if 1
  2510. // Fearlessly store the calibration values into the eeprom.
  2511. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  2512. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  2513. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  2514. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  2515. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  2516. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  2517. #endif
  2518. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2519. world2machine_update_current();
  2520. enable_endstops(false);
  2521. enable_z_endstop(false);
  2522. #ifdef SUPPORT_VERBOSITY
  2523. if (verbosity_level >= 5) {
  2524. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2525. delay_keep_alive(3000);
  2526. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2527. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2528. // Don't let the manage_inactivity() function remove power from the motors.
  2529. refresh_cmd_timeout();
  2530. // Go to the measurement point.
  2531. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2532. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2);
  2533. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2+1);
  2534. if (verbosity_level >= 10) {
  2535. go_to_current(homing_feedrate[X_AXIS]/60);
  2536. delay_keep_alive(3000);
  2537. }
  2538. {
  2539. float x, y;
  2540. world2machine(current_position[X_AXIS], current_position[Y_AXIS], x, y);
  2541. SERIAL_ECHOPGM("Final calculated bed point ");
  2542. SERIAL_ECHO(mesh_point);
  2543. SERIAL_ECHOPGM(": ");
  2544. MYSERIAL.print(x, 5);
  2545. SERIAL_ECHOPGM(", ");
  2546. MYSERIAL.print(y, 5);
  2547. SERIAL_ECHOLNPGM("");
  2548. }
  2549. }
  2550. }
  2551. #endif // SUPPORT_VERBOSITY
  2552. if(!sample_z())
  2553. goto canceled;
  2554. enable_endstops(endstops_enabled);
  2555. enable_z_endstop(endstop_z_enabled);
  2556. // Don't let the manage_inactivity() function remove power from the motors.
  2557. refresh_cmd_timeout();
  2558. return result;
  2559. canceled:
  2560. // Don't let the manage_inactivity() function remove power from the motors.
  2561. refresh_cmd_timeout();
  2562. // Print head up.
  2563. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2564. go_to_current(homing_feedrate[Z_AXIS]/60);
  2565. // Store the identity matrix to EEPROM.
  2566. reset_bed_offset_and_skew();
  2567. enable_endstops(endstops_enabled);
  2568. enable_z_endstop(endstop_z_enabled);
  2569. return result;
  2570. }
  2571. #endif //NEW_XYZCAL
  2572. bool sample_z() {
  2573. bool sampled = true;
  2574. //make space
  2575. current_position[Z_AXIS] += 150;
  2576. go_to_current(homing_feedrate[Z_AXIS] / 60);
  2577. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate, active_extruder););
  2578. lcd_show_fullscreen_message_and_wait_P(_T(MSG_PLACE_STEEL_SHEET));
  2579. // Sample Z heights for the mesh bed leveling.
  2580. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2581. if (!sample_mesh_and_store_reference()) sampled = false;
  2582. return sampled;
  2583. }
  2584. void go_home_with_z_lift()
  2585. {
  2586. // Don't let the manage_inactivity() function remove power from the motors.
  2587. refresh_cmd_timeout();
  2588. // Go home.
  2589. // First move up to a safe height.
  2590. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2591. go_to_current(homing_feedrate[Z_AXIS]/60);
  2592. // Second move to XY [0, 0].
  2593. current_position[X_AXIS] = X_MIN_POS+0.2;
  2594. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2595. // Clamp to the physical coordinates.
  2596. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2597. go_to_current(homing_feedrate[X_AXIS]/20);
  2598. // Third move up to a safe height.
  2599. current_position[Z_AXIS] = Z_MIN_POS;
  2600. go_to_current(homing_feedrate[Z_AXIS]/60);
  2601. }
  2602. // Sample the 9 points of the bed and store them into the EEPROM as a reference.
  2603. // When calling this function, the X, Y, Z axes should be already homed,
  2604. // and the world2machine correction matrix should be active.
  2605. // Returns false if the reference values are more than 3mm far away.
  2606. bool sample_mesh_and_store_reference()
  2607. {
  2608. bool endstops_enabled = enable_endstops(false);
  2609. bool endstop_z_enabled = enable_z_endstop(false);
  2610. // Don't let the manage_inactivity() function remove power from the motors.
  2611. refresh_cmd_timeout();
  2612. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2613. uint8_t next_line;
  2614. lcd_display_message_fullscreen_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1), next_line);
  2615. if (next_line > 3)
  2616. next_line = 3;
  2617. // display "point xx of yy"
  2618. lcd_set_cursor(0, next_line);
  2619. lcd_print(1);
  2620. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2621. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2622. // Sample Z heights for the mesh bed leveling.
  2623. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2624. {
  2625. // The first point defines the reference.
  2626. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2627. go_to_current(homing_feedrate[Z_AXIS]/60);
  2628. current_position[X_AXIS] = BED_X0;
  2629. current_position[Y_AXIS] = BED_Y0;
  2630. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2631. go_to_current(homing_feedrate[X_AXIS]/60);
  2632. memcpy(destination, current_position, sizeof(destination));
  2633. enable_endstops(true);
  2634. homeaxis(Z_AXIS);
  2635. #ifdef TMC2130
  2636. if (!axis_known_position[Z_AXIS] && (READ(Z_TMC2130_DIAG) != 0)) //Z crash
  2637. {
  2638. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2639. return false;
  2640. }
  2641. #endif //TMC2130
  2642. enable_endstops(false);
  2643. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2644. {
  2645. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2646. return false;
  2647. }
  2648. mbl.set_z(0, 0, current_position[Z_AXIS]);
  2649. }
  2650. for (int8_t mesh_point = 1; mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS; ++ mesh_point) {
  2651. // Don't let the manage_inactivity() function remove power from the motors.
  2652. refresh_cmd_timeout();
  2653. // Print the decrasing ID of the measurement point.
  2654. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2655. go_to_current(homing_feedrate[Z_AXIS]/60);
  2656. int8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2657. int8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2658. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2659. current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2660. current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2661. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2662. go_to_current(homing_feedrate[X_AXIS]/60);
  2663. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2664. // display "point xx of yy"
  2665. lcd_set_cursor(0, next_line);
  2666. lcd_print(mesh_point+1);
  2667. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2668. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2669. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2670. {
  2671. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2672. return false;
  2673. }
  2674. // Get cords of measuring point
  2675. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2676. }
  2677. {
  2678. // Verify the span of the Z values.
  2679. float zmin = mbl.z_values[0][0];
  2680. float zmax = zmin;
  2681. for (int8_t j = 0; j < 3; ++ j)
  2682. for (int8_t i = 0; i < 3; ++ i) {
  2683. zmin = min(zmin, mbl.z_values[j][i]);
  2684. zmax = max(zmax, mbl.z_values[j][i]);
  2685. }
  2686. if (zmax - zmin > 3.f) {
  2687. // The span of the Z offsets is extreme. Give up.
  2688. // Homing failed on some of the points.
  2689. SERIAL_PROTOCOLLNPGM("Exreme span of the Z values!");
  2690. return false;
  2691. }
  2692. }
  2693. // Store the correction values to EEPROM.
  2694. // Offsets of the Z heiths of the calibration points from the first point.
  2695. // The offsets are saved as 16bit signed int, scaled to tenths of microns.
  2696. {
  2697. uint16_t addr = EEPROM_BED_CALIBRATION_Z_JITTER;
  2698. for (int8_t j = 0; j < 3; ++ j)
  2699. for (int8_t i = 0; i < 3; ++ i) {
  2700. if (i == 0 && j == 0)
  2701. continue;
  2702. float dif = mbl.z_values[j][i] - mbl.z_values[0][0];
  2703. int16_t dif_quantized = int16_t(floor(dif * 100.f + 0.5f));
  2704. eeprom_update_word((uint16_t*)addr, *reinterpret_cast<uint16_t*>(&dif_quantized));
  2705. #if 0
  2706. {
  2707. uint16_t z_offset_u = eeprom_read_word((uint16_t*)addr);
  2708. float dif2 = *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2709. SERIAL_ECHOPGM("Bed point ");
  2710. SERIAL_ECHO(i);
  2711. SERIAL_ECHOPGM(",");
  2712. SERIAL_ECHO(j);
  2713. SERIAL_ECHOPGM(", differences: written ");
  2714. MYSERIAL.print(dif, 5);
  2715. SERIAL_ECHOPGM(", read: ");
  2716. MYSERIAL.print(dif2, 5);
  2717. SERIAL_ECHOLNPGM("");
  2718. }
  2719. #endif
  2720. addr += 2;
  2721. }
  2722. }
  2723. mbl.upsample_3x3();
  2724. mbl.active = true;
  2725. go_home_with_z_lift();
  2726. enable_endstops(endstops_enabled);
  2727. enable_z_endstop(endstop_z_enabled);
  2728. return true;
  2729. }
  2730. #ifndef NEW_XYZCAL
  2731. bool scan_bed_induction_points(int8_t verbosity_level)
  2732. {
  2733. // Don't let the manage_inactivity() function remove power from the motors.
  2734. refresh_cmd_timeout();
  2735. // Reusing the z_values memory for the measurement cache.
  2736. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2737. float *pts = &mbl.z_values[0][0];
  2738. float *vec_x = pts + 2 * 9;
  2739. float *vec_y = vec_x + 2;
  2740. float *cntr = vec_y + 2;
  2741. memset(pts, 0, sizeof(float) * 7 * 7);
  2742. // Cache the current correction matrix.
  2743. world2machine_initialize();
  2744. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2745. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2746. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2747. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2748. cntr[0] = world2machine_shift[0];
  2749. cntr[1] = world2machine_shift[1];
  2750. // and reset the correction matrix, so the planner will not do anything.
  2751. world2machine_reset();
  2752. bool endstops_enabled = enable_endstops(false);
  2753. bool endstop_z_enabled = enable_z_endstop(false);
  2754. // Collect a matrix of 9x9 points.
  2755. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  2756. // Don't let the manage_inactivity() function remove power from the motors.
  2757. refresh_cmd_timeout();
  2758. // Move up.
  2759. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2760. enable_endstops(false);
  2761. enable_z_endstop(false);
  2762. go_to_current(homing_feedrate[Z_AXIS]/60);
  2763. // Go to the measurement point.
  2764. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2765. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  2766. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2767. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix;
  2768. float bedX = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2769. float bedY = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2770. current_position[X_AXIS] = vec_x[0] * bedX + vec_y[0] * bedY + cntr[0];
  2771. current_position[Y_AXIS] = vec_x[1] * bedX + vec_y[1] * bedY + cntr[1];
  2772. // The calibration points are very close to the min Y.
  2773. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2774. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2775. go_to_current(homing_feedrate[X_AXIS]/60);
  2776. find_bed_induction_sensor_point_z();
  2777. scan_bed_induction_sensor_point();
  2778. }
  2779. // Don't let the manage_inactivity() function remove power from the motors.
  2780. refresh_cmd_timeout();
  2781. enable_endstops(false);
  2782. enable_z_endstop(false);
  2783. // Don't let the manage_inactivity() function remove power from the motors.
  2784. refresh_cmd_timeout();
  2785. enable_endstops(endstops_enabled);
  2786. enable_z_endstop(endstop_z_enabled);
  2787. return true;
  2788. }
  2789. #endif //NEW_XYZCAL
  2790. // Shift a Z axis by a given delta.
  2791. // To replace loading of the babystep correction.
  2792. static void shift_z(float delta)
  2793. {
  2794. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] - delta, current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
  2795. st_synchronize();
  2796. plan_set_z_position(current_position[Z_AXIS]);
  2797. }
  2798. #define BABYSTEP_LOADZ_BY_PLANNER
  2799. // Number of baby steps applied
  2800. static int babystepLoadZ = 0;
  2801. void babystep_load()
  2802. {
  2803. babystepLoadZ = 0;
  2804. // Apply Z height correction aka baby stepping before mesh bed leveling gets activated.
  2805. if (calibration_status() < CALIBRATION_STATUS_LIVE_ADJUST)
  2806. {
  2807. check_babystep(); //checking if babystep is in allowed range, otherwise setting babystep to 0
  2808. // End of G80: Apply the baby stepping value.
  2809. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepLoadZ);
  2810. #if 0
  2811. SERIAL_ECHO("Z baby step: ");
  2812. SERIAL_ECHO(babystepLoadZ);
  2813. SERIAL_ECHO(", current Z: ");
  2814. SERIAL_ECHO(current_position[Z_AXIS]);
  2815. SERIAL_ECHO("correction: ");
  2816. SERIAL_ECHO(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2817. SERIAL_ECHOLN("");
  2818. #endif
  2819. }
  2820. }
  2821. void babystep_apply()
  2822. {
  2823. babystep_load();
  2824. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2825. shift_z(- float(babystepLoadZ) / float(cs.axis_steps_per_unit[Z_AXIS]));
  2826. #else
  2827. babystepsTodoZadd(babystepLoadZ);
  2828. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2829. }
  2830. void babystep_undo()
  2831. {
  2832. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2833. shift_z(float(babystepLoadZ) / float(cs.axis_steps_per_unit[Z_AXIS]));
  2834. #else
  2835. babystepsTodoZsubtract(babystepLoadZ);
  2836. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2837. babystepLoadZ = 0;
  2838. }
  2839. void babystep_reset()
  2840. {
  2841. babystepLoadZ = 0;
  2842. }
  2843. void count_xyz_details(float (&distanceMin)[2]) {
  2844. float cntr[2] = {
  2845. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0)),
  2846. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4))
  2847. };
  2848. float vec_x[2] = {
  2849. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0)),
  2850. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4))
  2851. };
  2852. float vec_y[2] = {
  2853. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0)),
  2854. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4))
  2855. };
  2856. #if 0
  2857. a2 = -1 * asin(vec_y[0] / MACHINE_AXIS_SCALE_Y);
  2858. a1 = asin(vec_x[1] / MACHINE_AXIS_SCALE_X);
  2859. angleDiff = fabs(a2 - a1);
  2860. #endif
  2861. for (uint8_t mesh_point = 0; mesh_point < 2; ++mesh_point) {
  2862. float y = vec_x[1] * pgm_read_float(bed_ref_points_4 + mesh_point * 2) + vec_y[1] * pgm_read_float(bed_ref_points_4 + mesh_point * 2 + 1) + cntr[1];
  2863. distanceMin[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2864. }
  2865. }
  2866. /*
  2867. e_MBL_TYPE e_mbl_type = e_MBL_OPTIMAL;
  2868. void mbl_mode_set() {
  2869. switch (e_mbl_type) {
  2870. case e_MBL_OPTIMAL: e_mbl_type = e_MBL_PREC; break;
  2871. case e_MBL_PREC: e_mbl_type = e_MBL_FAST; break;
  2872. case e_MBL_FAST: e_mbl_type = e_MBL_OPTIMAL; break;
  2873. default: e_mbl_type = e_MBL_OPTIMAL; break;
  2874. }
  2875. eeprom_update_byte((uint8_t*)EEPROM_MBL_TYPE,(uint8_t)e_mbl_type);
  2876. }
  2877. void mbl_mode_init() {
  2878. uint8_t mbl_type = eeprom_read_byte((uint8_t*)EEPROM_MBL_TYPE);
  2879. if (mbl_type == 0xFF) e_mbl_type = e_MBL_OPTIMAL;
  2880. else e_mbl_type = mbl_type;
  2881. }
  2882. */
  2883. void mbl_settings_init() {
  2884. //3x3 mesh; 3 Z-probes on each point, magnet elimination on
  2885. //magnet elimination: use aaproximate Z-coordinate instead of measured values for points which are near magnets
  2886. if (eeprom_read_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION) == 0xFF) {
  2887. eeprom_update_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION, 1);
  2888. }
  2889. if (eeprom_read_byte((uint8_t*)EEPROM_MBL_POINTS_NR) == 0xFF) {
  2890. eeprom_update_byte((uint8_t*)EEPROM_MBL_POINTS_NR, 3);
  2891. }
  2892. mbl_z_probe_nr = eeprom_read_byte((uint8_t*)EEPROM_MBL_PROBE_NR);
  2893. if (mbl_z_probe_nr == 0xFF) {
  2894. mbl_z_probe_nr = 3;
  2895. eeprom_update_byte((uint8_t*)EEPROM_MBL_PROBE_NR, mbl_z_probe_nr);
  2896. }
  2897. }
  2898. bool mbl_point_measurement_valid(uint8_t ix, uint8_t iy, uint8_t meas_points, bool zigzag) {
  2899. //"human readable" heatbed plan
  2900. //magnet proximity influence Z coordinate measurements significantly (40 - 100 um)
  2901. //0 - measurement point is above magnet and Z coordinate can be influenced negatively
  2902. //1 - we should be in safe distance from magnets, measurement should be accurate
  2903. if ((ix >= meas_points) || (iy >= meas_points)) return false;
  2904. uint8_t valid_points_mask[7] = {
  2905. //[X_MAX,Y_MAX]
  2906. 0b1111111,
  2907. 0b1111111,
  2908. 0b1110111,
  2909. 0b1111011,
  2910. 0b1110111,
  2911. 0b1111111,
  2912. 0b1111111,
  2913. //[0,0]
  2914. };
  2915. if (meas_points == 3) {
  2916. ix *= 3;
  2917. iy *= 3;
  2918. }
  2919. if (zigzag) {
  2920. if ((iy % 2) == 0) return (valid_points_mask[6 - iy] & (1 << (6 - ix)));
  2921. else return (valid_points_mask[6 - iy] & (1 << ix));
  2922. }
  2923. else {
  2924. return (valid_points_mask[6 - iy] & (1 << (6 - ix)));
  2925. }
  2926. }
  2927. void mbl_single_point_interpolation(uint8_t x, uint8_t y, uint8_t meas_points) {
  2928. //printf_P(PSTR("x = %d; y = %d \n"), x, y);
  2929. uint8_t count = 0;
  2930. float z = 0;
  2931. if (mbl_point_measurement_valid(x, y + 1, meas_points, false)) { z += mbl.z_values[y + 1][x]; /*printf_P(PSTR("x; y+1: Z = %f \n"), mbl.z_values[y + 1][x]);*/ count++; }
  2932. if (mbl_point_measurement_valid(x, y - 1, meas_points, false)) { z += mbl.z_values[y - 1][x]; /*printf_P(PSTR("x; y-1: Z = %f \n"), mbl.z_values[y - 1][x]);*/ count++; }
  2933. if (mbl_point_measurement_valid(x + 1, y, meas_points, false)) { z += mbl.z_values[y][x + 1]; /*printf_P(PSTR("x+1; y: Z = %f \n"), mbl.z_values[y][x + 1]);*/ count++; }
  2934. if (mbl_point_measurement_valid(x - 1, y, meas_points, false)) { z += mbl.z_values[y][x - 1]; /*printf_P(PSTR("x-1; y: Z = %f \n"), mbl.z_values[y][x - 1]);*/ count++; }
  2935. if(count != 0) mbl.z_values[y][x] = z / count; //if we have at least one valid point in surrounding area use average value, otherwise use inaccurately measured Z-coordinate
  2936. //printf_P(PSTR("result: Z = %f \n\n"), mbl.z_values[y][x]);
  2937. }
  2938. void mbl_interpolation(uint8_t meas_points) {
  2939. for (uint8_t x = 0; x < meas_points; x++) {
  2940. for (uint8_t y = 0; y < meas_points; y++) {
  2941. if (!mbl_point_measurement_valid(x, y, meas_points, false)) {
  2942. mbl_single_point_interpolation(x, y, meas_points);
  2943. }
  2944. }
  2945. }
  2946. }