mesh_bed_calibration.cpp 114 KB

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