mesh_bed_calibration.cpp 118 KB

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