mesh_bed_calibration.cpp 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055
  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_set_cursor(0, next_line);
  2060. lcd_print(k + 1);
  2061. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));
  2062. if (iteration > 0) {
  2063. lcd_puts_at_P(0, next_line + 1, _i("Iteration "));////MSG_FIND_BED_OFFSET_AND_SKEW_ITERATION c=20 r=0
  2064. lcd_print(int(iteration + 1));
  2065. }
  2066. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2067. float *pt = pts + k * 2;
  2068. // Go up to z_initial.
  2069. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  2070. #ifdef SUPPORT_VERBOSITY
  2071. if (verbosity_level >= 20) {
  2072. // Go to Y0, wait, then go to Y-4.
  2073. current_position[Y_AXIS] = 0.f;
  2074. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2075. SERIAL_ECHOLNPGM("At Y0");
  2076. delay_keep_alive(5000);
  2077. current_position[Y_AXIS] = Y_MIN_POS;
  2078. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2079. SERIAL_ECHOLNPGM("At Y-4");
  2080. delay_keep_alive(5000);
  2081. }
  2082. #endif // SUPPORT_VERBOSITY
  2083. // Go to the measurement point position.
  2084. //if (iteration == 0) {
  2085. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2);
  2086. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2 + 1);
  2087. /*}
  2088. else {
  2089. // if first iteration failed, count corrected point coordinates as initial
  2090. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2091. 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];
  2092. 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];
  2093. // The calibration points are very close to the min Y.
  2094. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2095. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2096. }*/
  2097. #ifdef SUPPORT_VERBOSITY
  2098. if (verbosity_level >= 20) {
  2099. SERIAL_ECHOPGM("current_position[X_AXIS]:");
  2100. MYSERIAL.print(current_position[X_AXIS], 5);
  2101. SERIAL_ECHOLNPGM("");
  2102. SERIAL_ECHOPGM("current_position[Y_AXIS]:");
  2103. MYSERIAL.print(current_position[Y_AXIS], 5);
  2104. SERIAL_ECHOLNPGM("");
  2105. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  2106. MYSERIAL.print(current_position[Z_AXIS], 5);
  2107. SERIAL_ECHOLNPGM("");
  2108. }
  2109. #endif // SUPPORT_VERBOSITY
  2110. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2111. #ifdef SUPPORT_VERBOSITY
  2112. if (verbosity_level >= 10)
  2113. delay_keep_alive(3000);
  2114. #endif // SUPPORT_VERBOSITY
  2115. if (!find_bed_induction_sensor_point_xy(verbosity_level))
  2116. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2117. #ifndef NEW_XYZCAL
  2118. #ifndef HEATBED_V2
  2119. if (k == 0 || k == 1) {
  2120. // Improve the position of the 1st row sensor points by a zig-zag movement.
  2121. find_bed_induction_sensor_point_z();
  2122. int8_t i = 4;
  2123. for (;;) {
  2124. if (improve_bed_induction_sensor_point3(verbosity_level))
  2125. break;
  2126. if (--i == 0)
  2127. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2128. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2129. current_position[Z_AXIS] -= 0.025f;
  2130. enable_endstops(false);
  2131. enable_z_endstop(false);
  2132. go_to_current(homing_feedrate[Z_AXIS]);
  2133. }
  2134. if (i == 0)
  2135. // not found
  2136. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2137. }
  2138. #endif //HEATBED_V2
  2139. #endif
  2140. #ifdef SUPPORT_VERBOSITY
  2141. if (verbosity_level >= 10)
  2142. delay_keep_alive(3000);
  2143. #endif // SUPPORT_VERBOSITY
  2144. // Save the detected point position and then clamp the Y coordinate, which may have been estimated
  2145. // to lie outside the machine working space.
  2146. #ifdef SUPPORT_VERBOSITY
  2147. if (verbosity_level >= 20) {
  2148. SERIAL_ECHOLNPGM("Measured:");
  2149. MYSERIAL.println(current_position[X_AXIS]);
  2150. MYSERIAL.println(current_position[Y_AXIS]);
  2151. }
  2152. #endif // SUPPORT_VERBOSITY
  2153. pt[0] = (pt[0] * iteration) / (iteration + 1);
  2154. pt[0] += (current_position[X_AXIS]/(iteration + 1)); //count average
  2155. pt[1] = (pt[1] * iteration) / (iteration + 1);
  2156. pt[1] += (current_position[Y_AXIS] / (iteration + 1));
  2157. //pt[0] += current_position[X_AXIS];
  2158. //if(iteration > 0) pt[0] = pt[0] / 2;
  2159. //pt[1] += current_position[Y_AXIS];
  2160. //if (iteration > 0) pt[1] = pt[1] / 2;
  2161. #ifdef SUPPORT_VERBOSITY
  2162. if (verbosity_level >= 20) {
  2163. SERIAL_ECHOLNPGM("");
  2164. SERIAL_ECHOPGM("pt[0]:");
  2165. MYSERIAL.println(pt[0]);
  2166. SERIAL_ECHOPGM("pt[1]:");
  2167. MYSERIAL.println(pt[1]);
  2168. }
  2169. #endif // SUPPORT_VERBOSITY
  2170. if (current_position[Y_AXIS] < Y_MIN_POS)
  2171. current_position[Y_AXIS] = Y_MIN_POS;
  2172. // Start searching for the other points at 3mm above the last point.
  2173. current_position[Z_AXIS] += 3.f + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2174. //cntr[0] += pt[0];
  2175. //cntr[1] += pt[1];
  2176. #ifdef SUPPORT_VERBOSITY
  2177. if (verbosity_level >= 10 && k == 0) {
  2178. // Show the zero. Test, whether the Y motor skipped steps.
  2179. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  2180. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2181. delay_keep_alive(3000);
  2182. }
  2183. #endif // SUPPORT_VERBOSITY
  2184. }
  2185. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2186. #ifdef SUPPORT_VERBOSITY
  2187. if (verbosity_level >= 20) {
  2188. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2189. delay_keep_alive(3000);
  2190. for (int8_t mesh_point = 0; mesh_point < 4; ++mesh_point) {
  2191. // Don't let the manage_inactivity() function remove power from the motors.
  2192. refresh_cmd_timeout();
  2193. // Go to the measurement point.
  2194. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2195. current_position[X_AXIS] = pts[mesh_point * 2];
  2196. current_position[Y_AXIS] = pts[mesh_point * 2 + 1];
  2197. go_to_current(homing_feedrate[X_AXIS] / 60);
  2198. delay_keep_alive(3000);
  2199. }
  2200. }
  2201. #endif // SUPPORT_VERBOSITY
  2202. if (pts[1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  2203. too_far_mask |= 1 << 1; //front center point is out of reach
  2204. SERIAL_ECHOLNPGM("");
  2205. SERIAL_ECHOPGM("WARNING: Front point not reachable. Y coordinate:");
  2206. MYSERIAL.print(pts[1]);
  2207. SERIAL_ECHOPGM(" < ");
  2208. MYSERIAL.println(Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2209. }
  2210. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2211. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2212. if (result >= 0) {
  2213. world2machine_update(vec_x, vec_y, cntr);
  2214. #if 1
  2215. // Fearlessly store the calibration values into the eeprom.
  2216. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0), cntr[0]);
  2217. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4), cntr[1]);
  2218. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0), vec_x[0]);
  2219. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4), vec_x[1]);
  2220. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0), vec_y[0]);
  2221. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4), vec_y[1]);
  2222. #endif
  2223. #ifdef SUPPORT_VERBOSITY
  2224. if (verbosity_level >= 10) {
  2225. // Length of the vec_x
  2226. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  2227. SERIAL_ECHOLNPGM("X vector length:");
  2228. MYSERIAL.println(l);
  2229. // Length of the vec_y
  2230. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  2231. SERIAL_ECHOLNPGM("Y vector length:");
  2232. MYSERIAL.println(l);
  2233. // Zero point correction
  2234. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  2235. SERIAL_ECHOLNPGM("Zero point correction:");
  2236. MYSERIAL.println(l);
  2237. // vec_x and vec_y shall be nearly perpendicular.
  2238. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  2239. SERIAL_ECHOLNPGM("Perpendicularity");
  2240. MYSERIAL.println(fabs(l));
  2241. SERIAL_ECHOLNPGM("Saving bed calibration vectors to EEPROM");
  2242. }
  2243. #endif // SUPPORT_VERBOSITY
  2244. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2245. world2machine_update_current();
  2246. #ifdef SUPPORT_VERBOSITY
  2247. if (verbosity_level >= 20) {
  2248. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2249. delay_keep_alive(3000);
  2250. for (int8_t mesh_point = 0; mesh_point < 9; ++mesh_point) {
  2251. // Don't let the manage_inactivity() function remove power from the motors.
  2252. refresh_cmd_timeout();
  2253. // Go to the measurement point.
  2254. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2255. current_position[X_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2);
  2256. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2 + 1);
  2257. go_to_current(homing_feedrate[X_AXIS] / 60);
  2258. delay_keep_alive(3000);
  2259. }
  2260. }
  2261. #endif // SUPPORT_VERBOSITY
  2262. return result;
  2263. }
  2264. 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
  2265. iteration++;
  2266. }
  2267. return result;
  2268. }
  2269. #ifndef NEW_XYZCAL
  2270. BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask)
  2271. {
  2272. // Don't let the manage_inactivity() function remove power from the motors.
  2273. refresh_cmd_timeout();
  2274. // Mask of the first three points. Are they too far?
  2275. too_far_mask = 0;
  2276. // Reusing the z_values memory for the measurement cache.
  2277. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2278. float *pts = &mbl.z_values[0][0];
  2279. float *vec_x = pts + 2 * 9;
  2280. float *vec_y = vec_x + 2;
  2281. float *cntr = vec_y + 2;
  2282. memset(pts, 0, sizeof(float) * 7 * 7);
  2283. #ifdef SUPPORT_VERBOSITY
  2284. if (verbosity_level >= 10) SERIAL_ECHOLNPGM("Improving bed offset and skew");
  2285. #endif // SUPPORT_VERBOSITY
  2286. // Cache the current correction matrix.
  2287. world2machine_initialize();
  2288. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2289. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2290. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2291. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2292. cntr[0] = world2machine_shift[0];
  2293. cntr[1] = world2machine_shift[1];
  2294. // and reset the correction matrix, so the planner will not do anything.
  2295. world2machine_reset();
  2296. bool endstops_enabled = enable_endstops(false);
  2297. bool endstop_z_enabled = enable_z_endstop(false);
  2298. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2299. uint8_t next_line;
  2300. lcd_display_message_fullscreen_P(_i("Improving bed calibration point"), next_line);////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1 c=60 r=0
  2301. if (next_line > 3)
  2302. next_line = 3;
  2303. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2304. // Collect a matrix of 9x9 points.
  2305. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  2306. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2307. // Don't let the manage_inactivity() function remove power from the motors.
  2308. refresh_cmd_timeout();
  2309. // Print the decrasing ID of the measurement point.
  2310. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2311. lcd_print_at(0, next_line, mesh_point+1);
  2312. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2 c=14 r=0
  2313. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2314. // Move up.
  2315. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2316. enable_endstops(false);
  2317. enable_z_endstop(false);
  2318. go_to_current(homing_feedrate[Z_AXIS]/60);
  2319. #ifdef SUPPORT_VERBOSITY
  2320. if (verbosity_level >= 20) {
  2321. // Go to Y0, wait, then go to Y-4.
  2322. current_position[Y_AXIS] = 0.f;
  2323. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2324. SERIAL_ECHOLNPGM("At Y0");
  2325. delay_keep_alive(5000);
  2326. current_position[Y_AXIS] = Y_MIN_POS;
  2327. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2328. SERIAL_ECHOLNPGM("At Y_MIN_POS");
  2329. delay_keep_alive(5000);
  2330. }
  2331. #endif // SUPPORT_VERBOSITY
  2332. // Go to the measurement point.
  2333. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2334. 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];
  2335. 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];
  2336. // The calibration points are very close to the min Y.
  2337. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION){
  2338. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2339. #ifdef SUPPORT_VERBOSITY
  2340. if (verbosity_level >= 20) {
  2341. SERIAL_ECHOPGM("Calibration point ");
  2342. SERIAL_ECHO(mesh_point);
  2343. SERIAL_ECHOPGM("lower than Ymin. Y coordinate clamping was used.");
  2344. SERIAL_ECHOLNPGM("");
  2345. }
  2346. #endif // SUPPORT_VERBOSITY
  2347. }
  2348. go_to_current(homing_feedrate[X_AXIS]/60);
  2349. // Find its Z position by running the normal vertical search.
  2350. #ifdef SUPPORT_VERBOSITY
  2351. if (verbosity_level >= 10)
  2352. delay_keep_alive(3000);
  2353. #endif // SUPPORT_VERBOSITY
  2354. find_bed_induction_sensor_point_z();
  2355. #ifdef SUPPORT_VERBOSITY
  2356. if (verbosity_level >= 10)
  2357. delay_keep_alive(3000);
  2358. #endif // SUPPORT_VERBOSITY
  2359. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2360. current_position[Z_AXIS] -= 0.025f;
  2361. // Improve the point position by searching its center in a current plane.
  2362. int8_t n_errors = 3;
  2363. for (int8_t iter = 0; iter < 8; ) {
  2364. #ifdef SUPPORT_VERBOSITY
  2365. if (verbosity_level > 20) {
  2366. SERIAL_ECHOPGM("Improving bed point ");
  2367. SERIAL_ECHO(mesh_point);
  2368. SERIAL_ECHOPGM(", iteration ");
  2369. SERIAL_ECHO(iter);
  2370. SERIAL_ECHOPGM(", z");
  2371. MYSERIAL.print(current_position[Z_AXIS], 5);
  2372. SERIAL_ECHOLNPGM("");
  2373. }
  2374. #endif // SUPPORT_VERBOSITY
  2375. bool found = false;
  2376. if (mesh_point < 2) {
  2377. // Because the sensor cannot move in front of the first row
  2378. // of the sensor points, the y position cannot be measured
  2379. // by a cross center method.
  2380. // Use a zig-zag search for the first row of the points.
  2381. found = improve_bed_induction_sensor_point3(verbosity_level);
  2382. } else {
  2383. switch (method) {
  2384. case 0: found = improve_bed_induction_sensor_point(); break;
  2385. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 2, verbosity_level); break;
  2386. default: break;
  2387. }
  2388. }
  2389. if (found) {
  2390. if (iter > 3) {
  2391. // Average the last 4 measurements.
  2392. pts[mesh_point*2 ] += current_position[X_AXIS];
  2393. pts[mesh_point*2+1] += current_position[Y_AXIS];
  2394. }
  2395. if (current_position[Y_AXIS] < Y_MIN_POS)
  2396. current_position[Y_AXIS] = Y_MIN_POS;
  2397. ++ iter;
  2398. } else if (n_errors -- == 0) {
  2399. // Give up.
  2400. result = BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2401. goto canceled;
  2402. } else {
  2403. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2404. current_position[Z_AXIS] -= 0.05f;
  2405. enable_endstops(false);
  2406. enable_z_endstop(false);
  2407. go_to_current(homing_feedrate[Z_AXIS]);
  2408. #ifdef SUPPORT_VERBOSITY
  2409. if (verbosity_level >= 5) {
  2410. SERIAL_ECHOPGM("Improving bed point ");
  2411. SERIAL_ECHO(mesh_point);
  2412. SERIAL_ECHOPGM(", iteration ");
  2413. SERIAL_ECHO(iter);
  2414. SERIAL_ECHOPGM(" failed. Lowering z to ");
  2415. MYSERIAL.print(current_position[Z_AXIS], 5);
  2416. SERIAL_ECHOLNPGM("");
  2417. }
  2418. #endif // SUPPORT_VERBOSITY
  2419. }
  2420. }
  2421. #ifdef SUPPORT_VERBOSITY
  2422. if (verbosity_level >= 10)
  2423. delay_keep_alive(3000);
  2424. #endif // SUPPORT_VERBOSITY
  2425. }
  2426. // Don't let the manage_inactivity() function remove power from the motors.
  2427. refresh_cmd_timeout();
  2428. // Average the last 4 measurements.
  2429. for (int8_t i = 0; i < 8; ++ i)
  2430. pts[i] *= (1.f/4.f);
  2431. enable_endstops(false);
  2432. enable_z_endstop(false);
  2433. #ifdef SUPPORT_VERBOSITY
  2434. if (verbosity_level >= 5) {
  2435. // Test the positions. Are the positions reproducible?
  2436. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2437. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2438. // Don't let the manage_inactivity() function remove power from the motors.
  2439. refresh_cmd_timeout();
  2440. // Go to the measurement point.
  2441. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2442. current_position[X_AXIS] = pts[mesh_point*2];
  2443. current_position[Y_AXIS] = pts[mesh_point*2+1];
  2444. if (verbosity_level >= 10) {
  2445. go_to_current(homing_feedrate[X_AXIS]/60);
  2446. delay_keep_alive(3000);
  2447. }
  2448. SERIAL_ECHOPGM("Final measured bed point ");
  2449. SERIAL_ECHO(mesh_point);
  2450. SERIAL_ECHOPGM(": ");
  2451. MYSERIAL.print(current_position[X_AXIS], 5);
  2452. SERIAL_ECHOPGM(", ");
  2453. MYSERIAL.print(current_position[Y_AXIS], 5);
  2454. SERIAL_ECHOLNPGM("");
  2455. }
  2456. }
  2457. #endif // SUPPORT_VERBOSITY
  2458. {
  2459. // First fill in the too_far_mask from the measured points.
  2460. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point)
  2461. if (pts[mesh_point * 2 + 1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2462. too_far_mask |= 1 << mesh_point;
  2463. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2464. if (result < 0) {
  2465. SERIAL_ECHOLNPGM("Calculation of the machine skew and offset failed.");
  2466. goto canceled;
  2467. }
  2468. // In case of success, update the too_far_mask from the calculated points.
  2469. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point) {
  2470. 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];
  2471. #ifdef SUPPORT_VERBOSITY
  2472. if (verbosity_level >= 20) {
  2473. SERIAL_ECHOLNPGM("");
  2474. SERIAL_ECHOPGM("Distance from min:");
  2475. MYSERIAL.print(y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2476. SERIAL_ECHOLNPGM("");
  2477. SERIAL_ECHOPGM("y:");
  2478. MYSERIAL.print(y);
  2479. SERIAL_ECHOLNPGM("");
  2480. }
  2481. #endif // SUPPORT_VERBOSITY
  2482. if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2483. too_far_mask |= 1 << mesh_point;
  2484. }
  2485. }
  2486. world2machine_update(vec_x, vec_y, cntr);
  2487. #if 1
  2488. // Fearlessly store the calibration values into the eeprom.
  2489. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  2490. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  2491. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  2492. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  2493. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  2494. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  2495. #endif
  2496. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2497. world2machine_update_current();
  2498. enable_endstops(false);
  2499. enable_z_endstop(false);
  2500. #ifdef SUPPORT_VERBOSITY
  2501. if (verbosity_level >= 5) {
  2502. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2503. delay_keep_alive(3000);
  2504. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2505. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2506. // Don't let the manage_inactivity() function remove power from the motors.
  2507. refresh_cmd_timeout();
  2508. // Go to the measurement point.
  2509. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2510. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2);
  2511. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2+1);
  2512. if (verbosity_level >= 10) {
  2513. go_to_current(homing_feedrate[X_AXIS]/60);
  2514. delay_keep_alive(3000);
  2515. }
  2516. {
  2517. float x, y;
  2518. world2machine(current_position[X_AXIS], current_position[Y_AXIS], x, y);
  2519. SERIAL_ECHOPGM("Final calculated bed point ");
  2520. SERIAL_ECHO(mesh_point);
  2521. SERIAL_ECHOPGM(": ");
  2522. MYSERIAL.print(x, 5);
  2523. SERIAL_ECHOPGM(", ");
  2524. MYSERIAL.print(y, 5);
  2525. SERIAL_ECHOLNPGM("");
  2526. }
  2527. }
  2528. }
  2529. #endif // SUPPORT_VERBOSITY
  2530. if(!sample_z())
  2531. goto canceled;
  2532. enable_endstops(endstops_enabled);
  2533. enable_z_endstop(endstop_z_enabled);
  2534. // Don't let the manage_inactivity() function remove power from the motors.
  2535. refresh_cmd_timeout();
  2536. return result;
  2537. canceled:
  2538. // Don't let the manage_inactivity() function remove power from the motors.
  2539. refresh_cmd_timeout();
  2540. // Print head up.
  2541. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2542. go_to_current(homing_feedrate[Z_AXIS]/60);
  2543. // Store the identity matrix to EEPROM.
  2544. reset_bed_offset_and_skew();
  2545. enable_endstops(endstops_enabled);
  2546. enable_z_endstop(endstop_z_enabled);
  2547. return result;
  2548. }
  2549. #endif //NEW_XYZCAL
  2550. bool sample_z() {
  2551. bool sampled = true;
  2552. //make space
  2553. current_position[Z_AXIS] += 150;
  2554. go_to_current(homing_feedrate[Z_AXIS] / 60);
  2555. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate, active_extruder););
  2556. lcd_show_fullscreen_message_and_wait_P(_T(MSG_PLACE_STEEL_SHEET));
  2557. // Sample Z heights for the mesh bed leveling.
  2558. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2559. if (!sample_mesh_and_store_reference()) sampled = false;
  2560. return sampled;
  2561. }
  2562. void go_home_with_z_lift()
  2563. {
  2564. // Don't let the manage_inactivity() function remove power from the motors.
  2565. refresh_cmd_timeout();
  2566. // Go home.
  2567. // First move up to a safe height.
  2568. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2569. go_to_current(homing_feedrate[Z_AXIS]/60);
  2570. // Second move to XY [0, 0].
  2571. current_position[X_AXIS] = X_MIN_POS+0.2;
  2572. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2573. // Clamp to the physical coordinates.
  2574. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2575. go_to_current(homing_feedrate[X_AXIS]/60);
  2576. // Third move up to a safe height.
  2577. current_position[Z_AXIS] = Z_MIN_POS;
  2578. go_to_current(homing_feedrate[Z_AXIS]/60);
  2579. }
  2580. // Sample the 9 points of the bed and store them into the EEPROM as a reference.
  2581. // When calling this function, the X, Y, Z axes should be already homed,
  2582. // and the world2machine correction matrix should be active.
  2583. // Returns false if the reference values are more than 3mm far away.
  2584. bool sample_mesh_and_store_reference()
  2585. {
  2586. bool endstops_enabled = enable_endstops(false);
  2587. bool endstop_z_enabled = enable_z_endstop(false);
  2588. // Don't let the manage_inactivity() function remove power from the motors.
  2589. refresh_cmd_timeout();
  2590. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2591. uint8_t next_line;
  2592. lcd_display_message_fullscreen_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1), next_line);
  2593. if (next_line > 3)
  2594. next_line = 3;
  2595. // display "point xx of yy"
  2596. lcd_set_cursor(0, next_line);
  2597. lcd_print(1);
  2598. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2599. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2600. // Sample Z heights for the mesh bed leveling.
  2601. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2602. {
  2603. // The first point defines the reference.
  2604. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2605. go_to_current(homing_feedrate[Z_AXIS]/60);
  2606. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  2607. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+1);
  2608. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2609. go_to_current(homing_feedrate[X_AXIS]/60);
  2610. memcpy(destination, current_position, sizeof(destination));
  2611. enable_endstops(true);
  2612. homeaxis(Z_AXIS);
  2613. #ifdef TMC2130
  2614. if (!axis_known_position[Z_AXIS] && (READ(Z_TMC2130_DIAG) != 0)) //Z crash
  2615. {
  2616. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2617. return false;
  2618. }
  2619. #endif //TMC2130
  2620. enable_endstops(false);
  2621. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2622. {
  2623. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2624. return false;
  2625. }
  2626. mbl.set_z(0, 0, current_position[Z_AXIS]);
  2627. }
  2628. for (int8_t mesh_point = 1; mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS; ++ mesh_point) {
  2629. // Don't let the manage_inactivity() function remove power from the motors.
  2630. refresh_cmd_timeout();
  2631. // Print the decrasing ID of the measurement point.
  2632. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2633. go_to_current(homing_feedrate[Z_AXIS]/60);
  2634. current_position[X_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point);
  2635. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point+1);
  2636. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2637. go_to_current(homing_feedrate[X_AXIS]/60);
  2638. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2639. // display "point xx of yy"
  2640. lcd_set_cursor(0, next_line);
  2641. lcd_print(mesh_point+1);
  2642. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2643. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2644. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2645. {
  2646. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2647. return false;
  2648. }
  2649. // Get cords of measuring point
  2650. int8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2651. int8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2652. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2653. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2654. }
  2655. {
  2656. // Verify the span of the Z values.
  2657. float zmin = mbl.z_values[0][0];
  2658. float zmax = zmin;
  2659. for (int8_t j = 0; j < 3; ++ j)
  2660. for (int8_t i = 0; i < 3; ++ i) {
  2661. zmin = min(zmin, mbl.z_values[j][i]);
  2662. zmax = max(zmax, mbl.z_values[j][i]);
  2663. }
  2664. if (zmax - zmin > 3.f) {
  2665. // The span of the Z offsets is extreme. Give up.
  2666. // Homing failed on some of the points.
  2667. SERIAL_PROTOCOLLNPGM("Exreme span of the Z values!");
  2668. return false;
  2669. }
  2670. }
  2671. // Store the correction values to EEPROM.
  2672. // Offsets of the Z heiths of the calibration points from the first point.
  2673. // The offsets are saved as 16bit signed int, scaled to tenths of microns.
  2674. {
  2675. uint16_t addr = EEPROM_BED_CALIBRATION_Z_JITTER;
  2676. for (int8_t j = 0; j < 3; ++ j)
  2677. for (int8_t i = 0; i < 3; ++ i) {
  2678. if (i == 0 && j == 0)
  2679. continue;
  2680. float dif = mbl.z_values[j][i] - mbl.z_values[0][0];
  2681. int16_t dif_quantized = int16_t(floor(dif * 100.f + 0.5f));
  2682. eeprom_update_word((uint16_t*)addr, *reinterpret_cast<uint16_t*>(&dif_quantized));
  2683. #if 0
  2684. {
  2685. uint16_t z_offset_u = eeprom_read_word((uint16_t*)addr);
  2686. float dif2 = *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2687. SERIAL_ECHOPGM("Bed point ");
  2688. SERIAL_ECHO(i);
  2689. SERIAL_ECHOPGM(",");
  2690. SERIAL_ECHO(j);
  2691. SERIAL_ECHOPGM(", differences: written ");
  2692. MYSERIAL.print(dif, 5);
  2693. SERIAL_ECHOPGM(", read: ");
  2694. MYSERIAL.print(dif2, 5);
  2695. SERIAL_ECHOLNPGM("");
  2696. }
  2697. #endif
  2698. addr += 2;
  2699. }
  2700. }
  2701. mbl.upsample_3x3();
  2702. mbl.active = true;
  2703. go_home_with_z_lift();
  2704. enable_endstops(endstops_enabled);
  2705. enable_z_endstop(endstop_z_enabled);
  2706. return true;
  2707. }
  2708. #ifndef NEW_XYZCAL
  2709. bool scan_bed_induction_points(int8_t verbosity_level)
  2710. {
  2711. // Don't let the manage_inactivity() function remove power from the motors.
  2712. refresh_cmd_timeout();
  2713. // Reusing the z_values memory for the measurement cache.
  2714. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2715. float *pts = &mbl.z_values[0][0];
  2716. float *vec_x = pts + 2 * 9;
  2717. float *vec_y = vec_x + 2;
  2718. float *cntr = vec_y + 2;
  2719. memset(pts, 0, sizeof(float) * 7 * 7);
  2720. // Cache the current correction matrix.
  2721. world2machine_initialize();
  2722. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2723. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2724. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2725. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2726. cntr[0] = world2machine_shift[0];
  2727. cntr[1] = world2machine_shift[1];
  2728. // and reset the correction matrix, so the planner will not do anything.
  2729. world2machine_reset();
  2730. bool endstops_enabled = enable_endstops(false);
  2731. bool endstop_z_enabled = enable_z_endstop(false);
  2732. // Collect a matrix of 9x9 points.
  2733. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  2734. // Don't let the manage_inactivity() function remove power from the motors.
  2735. refresh_cmd_timeout();
  2736. // Move up.
  2737. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2738. enable_endstops(false);
  2739. enable_z_endstop(false);
  2740. go_to_current(homing_feedrate[Z_AXIS]/60);
  2741. // Go to the measurement point.
  2742. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2743. 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];
  2744. 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];
  2745. // The calibration points are very close to the min Y.
  2746. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2747. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2748. go_to_current(homing_feedrate[X_AXIS]/60);
  2749. find_bed_induction_sensor_point_z();
  2750. scan_bed_induction_sensor_point();
  2751. }
  2752. // Don't let the manage_inactivity() function remove power from the motors.
  2753. refresh_cmd_timeout();
  2754. enable_endstops(false);
  2755. enable_z_endstop(false);
  2756. // Don't let the manage_inactivity() function remove power from the motors.
  2757. refresh_cmd_timeout();
  2758. enable_endstops(endstops_enabled);
  2759. enable_z_endstop(endstop_z_enabled);
  2760. return true;
  2761. }
  2762. #endif //NEW_XYZCAL
  2763. // Shift a Z axis by a given delta.
  2764. // To replace loading of the babystep correction.
  2765. static void shift_z(float delta)
  2766. {
  2767. 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);
  2768. st_synchronize();
  2769. plan_set_z_position(current_position[Z_AXIS]);
  2770. }
  2771. #define BABYSTEP_LOADZ_BY_PLANNER
  2772. // Number of baby steps applied
  2773. static int babystepLoadZ = 0;
  2774. void babystep_load()
  2775. {
  2776. // Apply Z height correction aka baby stepping before mesh bed leveling gets activated.
  2777. if(calibration_status() < CALIBRATION_STATUS_LIVE_ADJUST)
  2778. {
  2779. check_babystep(); //checking if babystep is in allowed range, otherwise setting babystep to 0
  2780. // End of G80: Apply the baby stepping value.
  2781. EEPROM_read_B(EEPROM_BABYSTEP_Z,&babystepLoadZ);
  2782. #if 0
  2783. SERIAL_ECHO("Z baby step: ");
  2784. SERIAL_ECHO(babystepLoadZ);
  2785. SERIAL_ECHO(", current Z: ");
  2786. SERIAL_ECHO(current_position[Z_AXIS]);
  2787. SERIAL_ECHO("correction: ");
  2788. SERIAL_ECHO(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2789. SERIAL_ECHOLN("");
  2790. #endif
  2791. }
  2792. }
  2793. void babystep_apply()
  2794. {
  2795. babystep_load();
  2796. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2797. shift_z(- float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2798. #else
  2799. babystepsTodoZadd(babystepLoadZ);
  2800. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2801. }
  2802. void babystep_undo()
  2803. {
  2804. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2805. shift_z(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2806. #else
  2807. babystepsTodoZsubtract(babystepLoadZ);
  2808. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2809. babystepLoadZ = 0;
  2810. }
  2811. void babystep_reset()
  2812. {
  2813. babystepLoadZ = 0;
  2814. }
  2815. void count_xyz_details(float (&distanceMin)[2]) {
  2816. float cntr[2] = {
  2817. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0)),
  2818. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4))
  2819. };
  2820. float vec_x[2] = {
  2821. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0)),
  2822. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4))
  2823. };
  2824. float vec_y[2] = {
  2825. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0)),
  2826. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4))
  2827. };
  2828. #if 0
  2829. a2 = -1 * asin(vec_y[0] / MACHINE_AXIS_SCALE_Y);
  2830. a1 = asin(vec_x[1] / MACHINE_AXIS_SCALE_X);
  2831. angleDiff = fabs(a2 - a1);
  2832. #endif
  2833. for (uint8_t mesh_point = 0; mesh_point < 2; ++mesh_point) {
  2834. 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];
  2835. distanceMin[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2836. }
  2837. }