mesh_bed_calibration.cpp 117 KB

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