mesh_bed_calibration.cpp 115 KB

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