mesh_bed_calibration.cpp 123 KB

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