mesh_bed_calibration.cpp 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  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. // Search around the current_position[X,Y],
  840. // look for the induction sensor response.
  841. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  842. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  843. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (4.f)
  844. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  845. #ifdef HEATBED_V2
  846. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (2.f)
  847. #define FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR (0.01f)
  848. #else //HEATBED_V2
  849. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.2f)
  850. #endif //HEATBED_V2
  851. #ifdef HEATBED_V2
  852. inline bool find_bed_induction_sensor_point_xy(int verbosity_level)
  853. {
  854. #ifdef SUPPORT_VERBOSITY
  855. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  856. #endif // SUPPORT_VERBOSITY
  857. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  858. bool found = false;
  859. {
  860. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  861. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  862. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  863. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  864. uint8_t nsteps_y;
  865. uint8_t i;
  866. if (x0 < X_MIN_POS) {
  867. x0 = X_MIN_POS;
  868. #ifdef SUPPORT_VERBOSITY
  869. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  870. #endif // SUPPORT_VERBOSITY
  871. }
  872. if (x1 > X_MAX_POS) {
  873. x1 = X_MAX_POS;
  874. #ifdef SUPPORT_VERBOSITY
  875. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  876. #endif // SUPPORT_VERBOSITY
  877. }
  878. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  879. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  880. #ifdef SUPPORT_VERBOSITY
  881. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  882. #endif // SUPPORT_VERBOSITY
  883. }
  884. if (y1 > Y_MAX_POS) {
  885. y1 = Y_MAX_POS;
  886. #ifdef SUPPORT_VERBOSITY
  887. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  888. #endif // SUPPORT_VERBOSITY
  889. }
  890. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  891. enable_endstops(false);
  892. bool dir_positive = true;
  893. float z_error = 2 * FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  894. float find_bed_induction_sensor_point_z_step = FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  895. float initial_z_position = current_position[Z_AXIS];
  896. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  897. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  898. // Continously lower the Z axis.
  899. endstops_hit_on_purpose();
  900. enable_z_endstop(true);
  901. while (current_position[Z_AXIS] > -10.f && z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  902. // Do nsteps_y zig-zag movements.
  903. //SERIAL_ECHOPGM("z_error: ");
  904. //MYSERIAL.println(z_error);
  905. current_position[Y_AXIS] = y0;
  906. initial_z_position = current_position[Z_AXIS];
  907. for (i = 0; i < (nsteps_y - 1); current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i) {
  908. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  909. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  910. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  911. dir_positive = !dir_positive;
  912. if (endstop_z_hit_on_purpose()) {
  913. update_current_position_xyz();
  914. z_error = 2 * (initial_z_position - current_position[Z_AXIS]);
  915. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  916. find_bed_induction_sensor_point_z_step = z_error / 2;
  917. current_position[Z_AXIS] += z_error;
  918. enable_z_endstop(false);
  919. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  920. enable_z_endstop(true);
  921. }
  922. goto endloop;
  923. }
  924. }
  925. initial_z_position = current_position[Z_AXIS];
  926. for (i = 0; i < (nsteps_y - 1); current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i) {
  927. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  928. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  929. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  930. dir_positive = !dir_positive;
  931. if (endstop_z_hit_on_purpose()) {
  932. update_current_position_xyz();
  933. z_error = 2 * (initial_z_position - current_position[Z_AXIS]);
  934. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  935. find_bed_induction_sensor_point_z_step = z_error / 2;
  936. current_position[Z_AXIS] += z_error;
  937. enable_z_endstop(false);
  938. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  939. enable_z_endstop(true);
  940. }
  941. goto endloop;
  942. }
  943. }
  944. endloop:;
  945. }
  946. #ifdef SUPPORT_VERBOSITY
  947. if (verbosity_level >= 20) {
  948. SERIAL_ECHO("First hit");
  949. SERIAL_ECHO("- X: ");
  950. MYSERIAL.print(current_position[X_AXIS]);
  951. SERIAL_ECHO("; Y: ");
  952. MYSERIAL.print(current_position[Y_AXIS]);
  953. SERIAL_ECHO("; Z: ");
  954. MYSERIAL.println(current_position[Z_AXIS]);
  955. }
  956. #endif //SUPPORT_VERBOSITY
  957. //lcd_show_fullscreen_message_and_wait_P(PSTR("First hit"));
  958. //lcd_update_enable(true);
  959. float init_x_position = current_position[X_AXIS];
  960. float init_y_position = current_position[Y_AXIS];
  961. // we have to let the planner know where we are right now as it is not where we said to go.
  962. update_current_position_xyz();
  963. enable_z_endstop(false);
  964. for (int8_t iter = 0; iter < 2; ++iter) {
  965. /*SERIAL_ECHOPGM("iter: ");
  966. MYSERIAL.println(iter);
  967. SERIAL_ECHOPGM("1 - current_position[Z_AXIS]: ");
  968. MYSERIAL.println(current_position[Z_AXIS]);*/
  969. // Slightly lower the Z axis to get a reliable trigger.
  970. current_position[Z_AXIS] -= 0.1f;
  971. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], homing_feedrate[Z_AXIS] / (60 * 10));
  972. SERIAL_ECHOPGM("2 - current_position[Z_AXIS]: ");
  973. MYSERIAL.println(current_position[Z_AXIS]);
  974. // Do nsteps_y zig-zag movements.
  975. float a, b;
  976. float avg[2] = { 0,0 };
  977. for (int iteration = 0; iteration < 8; iteration++) {
  978. found = false;
  979. invert_z_endstop(true);
  980. enable_z_endstop(true);
  981. go_xy(x0, current_position[Y_AXIS], feedrate / 5);
  982. update_current_position_xyz();
  983. if (!endstop_z_hit_on_purpose()) {
  984. // SERIAL_ECHOLN("Search X span 0 - not found");
  985. continue;
  986. }
  987. //lcd_show_fullscreen_message_and_wait_P(PSTR("X1 found"));
  988. //lcd_update_enable(true);
  989. // SERIAL_ECHOLN("Search X span 0 - found");
  990. a = current_position[X_AXIS];
  991. enable_z_endstop(false);
  992. go_xy(init_x_position, current_position[Y_AXIS], feedrate / 5);
  993. enable_z_endstop(true);
  994. go_xy(x1, current_position[Y_AXIS], feedrate / 5);
  995. update_current_position_xyz();
  996. if (!endstop_z_hit_on_purpose()) {
  997. // SERIAL_ECHOLN("Search X span 1 - not found");
  998. continue;
  999. }
  1000. //lcd_show_fullscreen_message_and_wait_P(PSTR("X2 found"));
  1001. //lcd_update_enable(true);
  1002. // SERIAL_ECHOLN("Search X span 1 - found");
  1003. b = current_position[X_AXIS];
  1004. // Go to the center.
  1005. enable_z_endstop(false);
  1006. current_position[X_AXIS] = 0.5f * (a + b);
  1007. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate / 5);
  1008. found = true;
  1009. // Search in the Y direction along a cross.
  1010. found = false;
  1011. enable_z_endstop(true);
  1012. go_xy(current_position[X_AXIS], y0, feedrate / 5);
  1013. update_current_position_xyz();
  1014. if (!endstop_z_hit_on_purpose()) {
  1015. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1016. continue;
  1017. }
  1018. //lcd_show_fullscreen_message_and_wait_P(PSTR("Y1 found"));
  1019. //lcd_update_enable(true);
  1020. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1021. a = current_position[Y_AXIS];
  1022. enable_z_endstop(false);
  1023. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1024. enable_z_endstop(true);
  1025. go_xy(current_position[X_AXIS], y1, feedrate / 5);
  1026. update_current_position_xyz();
  1027. if (!endstop_z_hit_on_purpose()) {
  1028. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1029. continue;
  1030. }
  1031. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1032. b = current_position[Y_AXIS];
  1033. //lcd_show_fullscreen_message_and_wait_P(PSTR("Y2 found"));
  1034. //lcd_update_enable(true);
  1035. // Go to the center.
  1036. enable_z_endstop(false);
  1037. invert_z_endstop(false);
  1038. current_position[Y_AXIS] = 0.5f * (a + b);
  1039. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate / 5);
  1040. #ifdef SUPPORT_VERBOSITY
  1041. if (verbosity_level >= 20) {
  1042. SERIAL_ECHOPGM("ITERATION: ");
  1043. MYSERIAL.println(iteration);
  1044. SERIAL_ECHOPGM("CURRENT POSITION X: ");
  1045. MYSERIAL.println(current_position[X_AXIS]);
  1046. SERIAL_ECHOPGM("CURRENT POSITION Y: ");
  1047. MYSERIAL.println(current_position[Y_AXIS]);
  1048. }
  1049. #endif //SUPPORT_VERBOSITY
  1050. if (iteration > 0) {
  1051. // Average the last 7 measurements.
  1052. avg[X_AXIS] += current_position[X_AXIS];
  1053. avg[Y_AXIS] += current_position[Y_AXIS];
  1054. }
  1055. init_x_position = current_position[X_AXIS];
  1056. init_y_position = current_position[Y_AXIS];
  1057. found = true;
  1058. }
  1059. avg[X_AXIS] *= (1.f / 7.f);
  1060. avg[Y_AXIS] *= (1.f / 7.f);
  1061. current_position[X_AXIS] = avg[X_AXIS];
  1062. current_position[Y_AXIS] = avg[Y_AXIS];
  1063. #ifdef SUPPORT_VERBOSITY
  1064. if (verbosity_level >= 20) {
  1065. SERIAL_ECHOPGM("AVG CURRENT POSITION X: ");
  1066. MYSERIAL.println(current_position[X_AXIS]);
  1067. SERIAL_ECHOPGM("AVG CURRENT POSITION Y: ");
  1068. MYSERIAL.println(current_position[Y_AXIS]);
  1069. }
  1070. #endif // SUPPORT_VERBOSITY
  1071. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1072. #ifdef SUPPORT_VERBOSITY
  1073. if (verbosity_level >= 20) {
  1074. lcd_show_fullscreen_message_and_wait_P(PSTR("Final position"));
  1075. lcd_update_enable(true);
  1076. }
  1077. #endif //SUPPORT_VERBOSITY
  1078. break;
  1079. }
  1080. }
  1081. enable_z_endstop(false);
  1082. return found;
  1083. }
  1084. #else //HEATBED_V2
  1085. inline bool find_bed_induction_sensor_point_xy(int verbosity_level)
  1086. {
  1087. #ifdef SUPPORT_VERBOSITY
  1088. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  1089. #endif // SUPPORT_VERBOSITY
  1090. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1091. bool found = false;
  1092. {
  1093. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1094. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1095. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1096. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1097. uint8_t nsteps_y;
  1098. uint8_t i;
  1099. if (x0 < X_MIN_POS) {
  1100. x0 = X_MIN_POS;
  1101. #ifdef SUPPORT_VERBOSITY
  1102. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  1103. #endif // SUPPORT_VERBOSITY
  1104. }
  1105. if (x1 > X_MAX_POS) {
  1106. x1 = X_MAX_POS;
  1107. #ifdef SUPPORT_VERBOSITY
  1108. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  1109. #endif // SUPPORT_VERBOSITY
  1110. }
  1111. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1112. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1113. #ifdef SUPPORT_VERBOSITY
  1114. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1115. #endif // SUPPORT_VERBOSITY
  1116. }
  1117. if (y1 > Y_MAX_POS) {
  1118. y1 = Y_MAX_POS;
  1119. #ifdef SUPPORT_VERBOSITY
  1120. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1121. #endif // SUPPORT_VERBOSITY
  1122. }
  1123. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1124. enable_endstops(false);
  1125. bool dir_positive = true;
  1126. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1127. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1128. // Continously lower the Z axis.
  1129. endstops_hit_on_purpose();
  1130. enable_z_endstop(true);
  1131. while (current_position[Z_AXIS] > -10.f) {
  1132. // Do nsteps_y zig-zag movements.
  1133. current_position[Y_AXIS] = y0;
  1134. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i) {
  1135. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1136. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1137. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1138. dir_positive = !dir_positive;
  1139. if (endstop_z_hit_on_purpose())
  1140. goto endloop;
  1141. }
  1142. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i) {
  1143. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1144. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1145. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1146. dir_positive = !dir_positive;
  1147. if (endstop_z_hit_on_purpose())
  1148. goto endloop;
  1149. }
  1150. }
  1151. endloop:
  1152. // SERIAL_ECHOLN("First hit");
  1153. // we have to let the planner know where we are right now as it is not where we said to go.
  1154. update_current_position_xyz();
  1155. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  1156. for (int8_t iter = 0; iter < 3; ++iter) {
  1157. if (iter > 0) {
  1158. // Slightly lower the Z axis to get a reliable trigger.
  1159. current_position[Z_AXIS] -= 0.02f;
  1160. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS] / 60);
  1161. }
  1162. // Do nsteps_y zig-zag movements.
  1163. float a, b;
  1164. enable_endstops(false);
  1165. enable_z_endstop(false);
  1166. current_position[Y_AXIS] = y0;
  1167. go_xy(x0, current_position[Y_AXIS], feedrate);
  1168. enable_z_endstop(true);
  1169. found = false;
  1170. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1171. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1172. if (endstop_z_hit_on_purpose()) {
  1173. found = true;
  1174. break;
  1175. }
  1176. }
  1177. update_current_position_xyz();
  1178. if (!found) {
  1179. // SERIAL_ECHOLN("Search in Y - not found");
  1180. continue;
  1181. }
  1182. // SERIAL_ECHOLN("Search in Y - found");
  1183. a = current_position[Y_AXIS];
  1184. enable_z_endstop(false);
  1185. current_position[Y_AXIS] = y1;
  1186. go_xy(x0, current_position[Y_AXIS], feedrate);
  1187. enable_z_endstop(true);
  1188. found = false;
  1189. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1190. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1191. if (endstop_z_hit_on_purpose()) {
  1192. found = true;
  1193. break;
  1194. }
  1195. }
  1196. update_current_position_xyz();
  1197. if (!found) {
  1198. // SERIAL_ECHOLN("Search in Y2 - not found");
  1199. continue;
  1200. }
  1201. // SERIAL_ECHOLN("Search in Y2 - found");
  1202. b = current_position[Y_AXIS];
  1203. current_position[Y_AXIS] = 0.5f * (a + b);
  1204. // Search in the X direction along a cross.
  1205. found = false;
  1206. enable_z_endstop(false);
  1207. go_xy(x0, current_position[Y_AXIS], feedrate);
  1208. enable_z_endstop(true);
  1209. go_xy(x1, current_position[Y_AXIS], feedrate);
  1210. update_current_position_xyz();
  1211. if (!endstop_z_hit_on_purpose()) {
  1212. // SERIAL_ECHOLN("Search X span 0 - not found");
  1213. continue;
  1214. }
  1215. // SERIAL_ECHOLN("Search X span 0 - found");
  1216. a = current_position[X_AXIS];
  1217. enable_z_endstop(false);
  1218. go_xy(x1, current_position[Y_AXIS], feedrate);
  1219. enable_z_endstop(true);
  1220. go_xy(x0, current_position[Y_AXIS], feedrate);
  1221. update_current_position_xyz();
  1222. if (!endstop_z_hit_on_purpose()) {
  1223. // SERIAL_ECHOLN("Search X span 1 - not found");
  1224. continue;
  1225. }
  1226. // SERIAL_ECHOLN("Search X span 1 - found");
  1227. b = current_position[X_AXIS];
  1228. // Go to the center.
  1229. enable_z_endstop(false);
  1230. current_position[X_AXIS] = 0.5f * (a + b);
  1231. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1232. found = true;
  1233. #if 1
  1234. // Search in the Y direction along a cross.
  1235. found = false;
  1236. enable_z_endstop(false);
  1237. go_xy(current_position[X_AXIS], y0, feedrate);
  1238. enable_z_endstop(true);
  1239. go_xy(current_position[X_AXIS], y1, feedrate);
  1240. update_current_position_xyz();
  1241. if (!endstop_z_hit_on_purpose()) {
  1242. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1243. continue;
  1244. }
  1245. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1246. a = current_position[Y_AXIS];
  1247. enable_z_endstop(false);
  1248. go_xy(current_position[X_AXIS], y1, feedrate);
  1249. enable_z_endstop(true);
  1250. go_xy(current_position[X_AXIS], y0, feedrate);
  1251. update_current_position_xyz();
  1252. if (!endstop_z_hit_on_purpose()) {
  1253. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1254. continue;
  1255. }
  1256. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1257. b = current_position[Y_AXIS];
  1258. // Go to the center.
  1259. enable_z_endstop(false);
  1260. current_position[Y_AXIS] = 0.5f * (a + b);
  1261. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1262. found = true;
  1263. #endif
  1264. break;
  1265. }
  1266. }
  1267. enable_z_endstop(false);
  1268. return found;
  1269. }
  1270. #endif //HEATBED_V2
  1271. // Search around the current_position[X,Y,Z].
  1272. // It is expected, that the induction sensor is switched on at the current position.
  1273. // Look around this center point by painting a star around the point.
  1274. inline bool improve_bed_induction_sensor_point()
  1275. {
  1276. static const float search_radius = 8.f;
  1277. bool endstops_enabled = enable_endstops(false);
  1278. bool endstop_z_enabled = enable_z_endstop(false);
  1279. bool found = false;
  1280. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1281. float center_old_x = current_position[X_AXIS];
  1282. float center_old_y = current_position[Y_AXIS];
  1283. float center_x = 0.f;
  1284. float center_y = 0.f;
  1285. for (uint8_t iter = 0; iter < 4; ++ iter) {
  1286. switch (iter) {
  1287. case 0:
  1288. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1289. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1290. break;
  1291. case 1:
  1292. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1293. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1294. break;
  1295. case 2:
  1296. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1297. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1298. break;
  1299. case 3:
  1300. default:
  1301. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1302. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1303. break;
  1304. }
  1305. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  1306. float vx = destination[X_AXIS] - center_old_x;
  1307. float vy = destination[Y_AXIS] - center_old_y;
  1308. float l = sqrt(vx*vx+vy*vy);
  1309. float t;
  1310. if (destination[X_AXIS] < X_MIN_POS) {
  1311. // Exiting the bed at xmin.
  1312. t = (center_x - X_MIN_POS) / l;
  1313. destination[X_AXIS] = X_MIN_POS;
  1314. destination[Y_AXIS] = center_old_y + t * vy;
  1315. } else if (destination[X_AXIS] > X_MAX_POS) {
  1316. // Exiting the bed at xmax.
  1317. t = (X_MAX_POS - center_x) / l;
  1318. destination[X_AXIS] = X_MAX_POS;
  1319. destination[Y_AXIS] = center_old_y + t * vy;
  1320. }
  1321. if (destination[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1322. // Exiting the bed at ymin.
  1323. t = (center_y - Y_MIN_POS_FOR_BED_CALIBRATION) / l;
  1324. destination[X_AXIS] = center_old_x + t * vx;
  1325. destination[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1326. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  1327. // Exiting the bed at xmax.
  1328. t = (Y_MAX_POS - center_y) / l;
  1329. destination[X_AXIS] = center_old_x + t * vx;
  1330. destination[Y_AXIS] = Y_MAX_POS;
  1331. }
  1332. // Move away from the measurement point.
  1333. enable_endstops(false);
  1334. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  1335. // Move towards the measurement point, until the induction sensor triggers.
  1336. enable_endstops(true);
  1337. go_xy(center_old_x, center_old_y, feedrate);
  1338. update_current_position_xyz();
  1339. // if (! endstop_z_hit_on_purpose()) return false;
  1340. center_x += current_position[X_AXIS];
  1341. center_y += current_position[Y_AXIS];
  1342. }
  1343. // Calculate the new center, move to the new center.
  1344. center_x /= 4.f;
  1345. center_y /= 4.f;
  1346. current_position[X_AXIS] = center_x;
  1347. current_position[Y_AXIS] = center_y;
  1348. enable_endstops(false);
  1349. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1350. enable_endstops(endstops_enabled);
  1351. enable_z_endstop(endstop_z_enabled);
  1352. return found;
  1353. }
  1354. static inline void debug_output_point(const char *type, const float &x, const float &y, const float &z)
  1355. {
  1356. SERIAL_ECHOPGM("Measured ");
  1357. SERIAL_ECHORPGM(type);
  1358. SERIAL_ECHOPGM(" ");
  1359. MYSERIAL.print(x, 5);
  1360. SERIAL_ECHOPGM(", ");
  1361. MYSERIAL.print(y, 5);
  1362. SERIAL_ECHOPGM(", ");
  1363. MYSERIAL.print(z, 5);
  1364. SERIAL_ECHOLNPGM("");
  1365. }
  1366. // Search around the current_position[X,Y,Z].
  1367. // It is expected, that the induction sensor is switched on at the current position.
  1368. // Look around this center point by painting a star around the point.
  1369. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  1370. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y, int8_t verbosity_level)
  1371. {
  1372. float center_old_x = current_position[X_AXIS];
  1373. float center_old_y = current_position[Y_AXIS];
  1374. float a, b;
  1375. bool point_small = false;
  1376. enable_endstops(false);
  1377. {
  1378. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1379. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1380. if (x0 < X_MIN_POS)
  1381. x0 = X_MIN_POS;
  1382. if (x1 > X_MAX_POS)
  1383. x1 = X_MAX_POS;
  1384. // Search in the X direction along a cross.
  1385. enable_z_endstop(false);
  1386. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1387. enable_z_endstop(true);
  1388. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1389. update_current_position_xyz();
  1390. if (! endstop_z_hit_on_purpose()) {
  1391. current_position[X_AXIS] = center_old_x;
  1392. goto canceled;
  1393. }
  1394. a = current_position[X_AXIS];
  1395. enable_z_endstop(false);
  1396. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1397. enable_z_endstop(true);
  1398. go_xy(x0, 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. b = current_position[X_AXIS];
  1405. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1406. #ifdef SUPPORT_VERBOSITY
  1407. if (verbosity_level >= 5) {
  1408. SERIAL_ECHOPGM("Point width too small: ");
  1409. SERIAL_ECHO(b - a);
  1410. SERIAL_ECHOLNPGM("");
  1411. }
  1412. #endif // SUPPORT_VERBOSITY
  1413. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1414. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1415. // Don't use the new X value.
  1416. current_position[X_AXIS] = center_old_x;
  1417. goto canceled;
  1418. } else {
  1419. // Use the new value, but force the Z axis to go a bit lower.
  1420. point_small = true;
  1421. }
  1422. }
  1423. #ifdef SUPPORT_VERBOSITY
  1424. if (verbosity_level >= 5) {
  1425. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1426. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1427. }
  1428. #endif // SUPPORT_VERBOSITY
  1429. // Go to the center.
  1430. enable_z_endstop(false);
  1431. current_position[X_AXIS] = 0.5f * (a + b);
  1432. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1433. }
  1434. {
  1435. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1436. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1437. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1438. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1439. if (y1 > Y_MAX_POS)
  1440. y1 = Y_MAX_POS;
  1441. // Search in the Y direction along a cross.
  1442. enable_z_endstop(false);
  1443. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1444. if (lift_z_on_min_y) {
  1445. // The first row of points are very close to the end stop.
  1446. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  1447. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  1448. // and go back.
  1449. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  1450. }
  1451. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  1452. // Already triggering before we started the move.
  1453. // Shift the trigger point slightly outwards.
  1454. // a = current_position[Y_AXIS] - 1.5f;
  1455. a = current_position[Y_AXIS];
  1456. } else {
  1457. enable_z_endstop(true);
  1458. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1459. update_current_position_xyz();
  1460. if (! endstop_z_hit_on_purpose()) {
  1461. current_position[Y_AXIS] = center_old_y;
  1462. goto canceled;
  1463. }
  1464. a = current_position[Y_AXIS];
  1465. }
  1466. enable_z_endstop(false);
  1467. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1468. enable_z_endstop(true);
  1469. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1470. update_current_position_xyz();
  1471. if (! endstop_z_hit_on_purpose()) {
  1472. current_position[Y_AXIS] = center_old_y;
  1473. goto canceled;
  1474. }
  1475. b = current_position[Y_AXIS];
  1476. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1477. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1478. #ifdef SUPPORT_VERBOSITY
  1479. if (verbosity_level >= 5) {
  1480. SERIAL_ECHOPGM("Point height too small: ");
  1481. SERIAL_ECHO(b - a);
  1482. SERIAL_ECHOLNPGM("");
  1483. }
  1484. #endif // SUPPORT_VERBOSITY
  1485. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1486. // Don't use the new Y value.
  1487. current_position[Y_AXIS] = center_old_y;
  1488. goto canceled;
  1489. } else {
  1490. // Use the new value, but force the Z axis to go a bit lower.
  1491. point_small = true;
  1492. }
  1493. }
  1494. #ifdef SUPPORT_VERBOSITY
  1495. if (verbosity_level >= 5) {
  1496. debug_output_point(PSTR("top" ), current_position[X_AXIS], a, current_position[Z_AXIS]);
  1497. debug_output_point(PSTR("bottom"), current_position[X_AXIS], b, current_position[Z_AXIS]);
  1498. }
  1499. #endif // SUPPORT_VERBOSITY
  1500. // Go to the center.
  1501. enable_z_endstop(false);
  1502. current_position[Y_AXIS] = 0.5f * (a + b);
  1503. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1504. }
  1505. // If point is small but not too small, then force the Z axis to be lowered a bit,
  1506. // but use the new value. This is important when the initial position was off in one axis,
  1507. // for example if the initial calibration was shifted in the Y axis systematically.
  1508. // Then this first step will center.
  1509. return ! point_small;
  1510. canceled:
  1511. // Go back to the center.
  1512. enable_z_endstop(false);
  1513. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1514. return false;
  1515. }
  1516. // Searching the front points, where one cannot move the sensor head in front of the sensor point.
  1517. // Searching in a zig-zag movement in a plane for the maximum width of the response.
  1518. // This function may set the current_position[Y_AXIS] below Y_MIN_POS, if the function succeeded.
  1519. // If this function failed, the Y coordinate will never be outside the working space.
  1520. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS (8.f)
  1521. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y (0.1f)
  1522. inline bool improve_bed_induction_sensor_point3(int verbosity_level)
  1523. {
  1524. float center_old_x = current_position[X_AXIS];
  1525. float center_old_y = current_position[Y_AXIS];
  1526. float a, b;
  1527. bool result = true;
  1528. #ifdef SUPPORT_VERBOSITY
  1529. if (verbosity_level >= 20) MYSERIAL.println("Improve bed induction sensor point3");
  1530. #endif // SUPPORT_VERBOSITY
  1531. // Was the sensor point detected too far in the minus Y axis?
  1532. // If yes, the center of the induction point cannot be reached by the machine.
  1533. {
  1534. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1535. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1536. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1537. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1538. float y = y0;
  1539. if (x0 < X_MIN_POS)
  1540. x0 = X_MIN_POS;
  1541. if (x1 > X_MAX_POS)
  1542. x1 = X_MAX_POS;
  1543. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1544. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1545. if (y1 > Y_MAX_POS)
  1546. y1 = Y_MAX_POS;
  1547. #ifdef SUPPORT_VERBOSITY
  1548. if (verbosity_level >= 20) {
  1549. SERIAL_ECHOPGM("Initial position: ");
  1550. SERIAL_ECHO(center_old_x);
  1551. SERIAL_ECHOPGM(", ");
  1552. SERIAL_ECHO(center_old_y);
  1553. SERIAL_ECHOLNPGM("");
  1554. }
  1555. #endif // SUPPORT_VERBOSITY
  1556. // Search in the positive Y direction, until a maximum diameter is found.
  1557. // (the next diameter is smaller than the current one.)
  1558. float dmax = 0.f;
  1559. float xmax1 = 0.f;
  1560. float xmax2 = 0.f;
  1561. for (y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1562. enable_z_endstop(false);
  1563. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1564. enable_z_endstop(true);
  1565. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1566. update_current_position_xyz();
  1567. if (! endstop_z_hit_on_purpose()) {
  1568. continue;
  1569. // SERIAL_PROTOCOLPGM("Failed 1\n");
  1570. // current_position[X_AXIS] = center_old_x;
  1571. // goto canceled;
  1572. }
  1573. a = current_position[X_AXIS];
  1574. enable_z_endstop(false);
  1575. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1576. enable_z_endstop(true);
  1577. go_xy(x0, 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 2\n");
  1582. // current_position[X_AXIS] = center_old_x;
  1583. // goto canceled;
  1584. }
  1585. b = current_position[X_AXIS];
  1586. #ifdef SUPPORT_VERBOSITY
  1587. if (verbosity_level >= 5) {
  1588. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1589. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1590. }
  1591. #endif // SUPPORT_VERBOSITY
  1592. float d = b - a;
  1593. if (d > dmax) {
  1594. xmax1 = 0.5f * (a + b);
  1595. dmax = d;
  1596. } else if (dmax > 0.) {
  1597. y0 = y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1598. break;
  1599. }
  1600. }
  1601. if (dmax == 0.) {
  1602. #ifdef SUPPORT_VERBOSITY
  1603. if (verbosity_level > 0)
  1604. SERIAL_PROTOCOLPGM("failed - not found\n");
  1605. #endif // SUPPORT_VERBOSITY
  1606. current_position[X_AXIS] = center_old_x;
  1607. current_position[Y_AXIS] = center_old_y;
  1608. goto canceled;
  1609. }
  1610. {
  1611. // Find the positive Y hit. This gives the extreme Y value for the search of the maximum diameter in the -Y direction.
  1612. enable_z_endstop(false);
  1613. go_xy(xmax1, y0 + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1614. enable_z_endstop(true);
  1615. go_xy(xmax1, max(y0 - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1616. update_current_position_xyz();
  1617. if (! endstop_z_hit_on_purpose()) {
  1618. current_position[Y_AXIS] = center_old_y;
  1619. goto canceled;
  1620. }
  1621. #ifdef SUPPORT_VERBOSITY
  1622. if (verbosity_level >= 5)
  1623. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1624. #endif // SUPPORT_VERBOSITY
  1625. y1 = current_position[Y_AXIS];
  1626. }
  1627. if (y1 <= y0) {
  1628. // Either the induction sensor is too high, or the induction sensor target is out of reach.
  1629. current_position[Y_AXIS] = center_old_y;
  1630. goto canceled;
  1631. }
  1632. // Search in the negative Y direction, until a maximum diameter is found.
  1633. dmax = 0.f;
  1634. // if (y0 + 1.f < y1)
  1635. // y1 = y0 + 1.f;
  1636. for (y = y1; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1637. enable_z_endstop(false);
  1638. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1639. enable_z_endstop(true);
  1640. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1641. update_current_position_xyz();
  1642. if (! endstop_z_hit_on_purpose()) {
  1643. continue;
  1644. /*
  1645. current_position[X_AXIS] = center_old_x;
  1646. SERIAL_PROTOCOLPGM("Failed 3\n");
  1647. goto canceled;
  1648. */
  1649. }
  1650. a = current_position[X_AXIS];
  1651. enable_z_endstop(false);
  1652. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1653. enable_z_endstop(true);
  1654. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1655. update_current_position_xyz();
  1656. if (! endstop_z_hit_on_purpose()) {
  1657. continue;
  1658. /*
  1659. current_position[X_AXIS] = center_old_x;
  1660. SERIAL_PROTOCOLPGM("Failed 4\n");
  1661. goto canceled;
  1662. */
  1663. }
  1664. b = current_position[X_AXIS];
  1665. #ifdef SUPPORT_VERBOSITY
  1666. if (verbosity_level >= 5) {
  1667. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1668. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1669. }
  1670. #endif // SUPPORT_VERBOSITY
  1671. float d = b - a;
  1672. if (d > dmax) {
  1673. xmax2 = 0.5f * (a + b);
  1674. dmax = d;
  1675. } else if (dmax > 0.) {
  1676. y1 = y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1677. break;
  1678. }
  1679. }
  1680. float xmax, ymax;
  1681. if (dmax == 0.f) {
  1682. // Only the hit in the positive direction found.
  1683. xmax = xmax1;
  1684. ymax = y0;
  1685. } else {
  1686. // Both positive and negative directions found.
  1687. xmax = xmax2;
  1688. ymax = 0.5f * (y0 + y1);
  1689. for (; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1690. enable_z_endstop(false);
  1691. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1692. enable_z_endstop(true);
  1693. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1694. update_current_position_xyz();
  1695. if (! endstop_z_hit_on_purpose()) {
  1696. continue;
  1697. /*
  1698. current_position[X_AXIS] = center_old_x;
  1699. SERIAL_PROTOCOLPGM("Failed 3\n");
  1700. goto canceled;
  1701. */
  1702. }
  1703. a = current_position[X_AXIS];
  1704. enable_z_endstop(false);
  1705. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1706. enable_z_endstop(true);
  1707. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1708. update_current_position_xyz();
  1709. if (! endstop_z_hit_on_purpose()) {
  1710. continue;
  1711. /*
  1712. current_position[X_AXIS] = center_old_x;
  1713. SERIAL_PROTOCOLPGM("Failed 4\n");
  1714. goto canceled;
  1715. */
  1716. }
  1717. b = current_position[X_AXIS];
  1718. #ifdef SUPPORT_VERBOSITY
  1719. if (verbosity_level >= 5) {
  1720. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1721. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1722. }
  1723. #endif // SUPPORT_VERBOSITY
  1724. float d = b - a;
  1725. if (d > dmax) {
  1726. xmax = 0.5f * (a + b);
  1727. ymax = y;
  1728. dmax = d;
  1729. }
  1730. }
  1731. }
  1732. {
  1733. // Compare the distance in the Y+ direction with the diameter in the X direction.
  1734. // Find the positive Y hit once again, this time along the Y axis going through the X point with the highest diameter.
  1735. enable_z_endstop(false);
  1736. go_xy(xmax, ymax + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1737. enable_z_endstop(true);
  1738. go_xy(xmax, max(ymax - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1739. update_current_position_xyz();
  1740. if (! endstop_z_hit_on_purpose()) {
  1741. current_position[Y_AXIS] = center_old_y;
  1742. goto canceled;
  1743. }
  1744. #ifdef SUPPORT_VERBOSITY
  1745. if (verbosity_level >= 5)
  1746. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1747. #endif // SUPPORT_VERBOSITY
  1748. if (current_position[Y_AXIS] - Y_MIN_POS_FOR_BED_CALIBRATION < 0.5f * dmax) {
  1749. // Probably not even a half circle was detected. The induction point is likely too far in the minus Y direction.
  1750. // First verify, if the measurement has been done at a sufficient height. If no, lower the Z axis a bit.
  1751. if (current_position[Y_AXIS] < ymax || dmax < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1752. #ifdef SUPPORT_VERBOSITY
  1753. if (verbosity_level >= 5) {
  1754. SERIAL_ECHOPGM("Partial point diameter too small: ");
  1755. SERIAL_ECHO(dmax);
  1756. SERIAL_ECHOLNPGM("");
  1757. }
  1758. #endif // SUPPORT_VERBOSITY
  1759. result = false;
  1760. } else {
  1761. // Estimate the circle radius from the maximum diameter and height:
  1762. float h = current_position[Y_AXIS] - ymax;
  1763. float r = dmax * dmax / (8.f * h) + 0.5f * h;
  1764. if (r < 0.8f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1765. #ifdef SUPPORT_VERBOSITY
  1766. if (verbosity_level >= 5) {
  1767. SERIAL_ECHOPGM("Partial point estimated radius too small: ");
  1768. SERIAL_ECHO(r);
  1769. SERIAL_ECHOPGM(", dmax:");
  1770. SERIAL_ECHO(dmax);
  1771. SERIAL_ECHOPGM(", h:");
  1772. SERIAL_ECHO(h);
  1773. SERIAL_ECHOLNPGM("");
  1774. }
  1775. #endif // SUPPORT_VERBOSITY
  1776. result = false;
  1777. } else {
  1778. // The point may end up outside of the machine working space.
  1779. // That is all right as it helps to improve the accuracy of the measurement point
  1780. // due to averaging.
  1781. // For the y correction, use an average of dmax/2 and the estimated radius.
  1782. r = 0.5f * (0.5f * dmax + r);
  1783. ymax = current_position[Y_AXIS] - r;
  1784. }
  1785. }
  1786. } else {
  1787. // If the diameter of the detected spot was smaller than a minimum allowed,
  1788. // the induction sensor is probably too high. Returning false will force
  1789. // the sensor to be lowered a tiny bit.
  1790. result = xmax >= MIN_BED_SENSOR_POINT_RESPONSE_DMR;
  1791. if (y0 > Y_MIN_POS_FOR_BED_CALIBRATION + 0.2f)
  1792. // Only in case both left and right y tangents are known, use them.
  1793. // If y0 is close to the bed edge, it may not be symmetric to the right tangent.
  1794. ymax = 0.5f * ymax + 0.25f * (y0 + y1);
  1795. }
  1796. }
  1797. // Go to the center.
  1798. enable_z_endstop(false);
  1799. current_position[X_AXIS] = xmax;
  1800. current_position[Y_AXIS] = ymax;
  1801. #ifdef SUPPORT_VERBOSITY
  1802. if (verbosity_level >= 20) {
  1803. SERIAL_ECHOPGM("Adjusted position: ");
  1804. SERIAL_ECHO(current_position[X_AXIS]);
  1805. SERIAL_ECHOPGM(", ");
  1806. SERIAL_ECHO(current_position[Y_AXIS]);
  1807. SERIAL_ECHOLNPGM("");
  1808. }
  1809. #endif // SUPPORT_VERBOSITY
  1810. // Don't clamp current_position[Y_AXIS], because the out-of-reach Y coordinate may actually be true.
  1811. // Only clamp the coordinate to go.
  1812. go_xy(current_position[X_AXIS], max(Y_MIN_POS, current_position[Y_AXIS]), homing_feedrate[X_AXIS] / 60.f);
  1813. // delay_keep_alive(3000);
  1814. }
  1815. if (result)
  1816. return true;
  1817. // otherwise clamp the Y coordinate
  1818. canceled:
  1819. // Go back to the center.
  1820. enable_z_endstop(false);
  1821. if (current_position[Y_AXIS] < Y_MIN_POS)
  1822. current_position[Y_AXIS] = Y_MIN_POS;
  1823. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1824. return false;
  1825. }
  1826. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  1827. // write the trigger coordinates to the serial line.
  1828. // Useful for visualizing the behavior of the bed induction detector.
  1829. inline void scan_bed_induction_sensor_point()
  1830. {
  1831. float center_old_x = current_position[X_AXIS];
  1832. float center_old_y = current_position[Y_AXIS];
  1833. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1834. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1835. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1836. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1837. float y = y0;
  1838. if (x0 < X_MIN_POS)
  1839. x0 = X_MIN_POS;
  1840. if (x1 > X_MAX_POS)
  1841. x1 = X_MAX_POS;
  1842. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1843. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1844. if (y1 > Y_MAX_POS)
  1845. y1 = Y_MAX_POS;
  1846. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1847. enable_z_endstop(false);
  1848. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1849. enable_z_endstop(true);
  1850. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1851. update_current_position_xyz();
  1852. if (endstop_z_hit_on_purpose())
  1853. debug_output_point(PSTR("left" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1854. enable_z_endstop(false);
  1855. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1856. enable_z_endstop(true);
  1857. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1858. update_current_position_xyz();
  1859. if (endstop_z_hit_on_purpose())
  1860. debug_output_point(PSTR("right"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1861. }
  1862. enable_z_endstop(false);
  1863. current_position[X_AXIS] = center_old_x;
  1864. current_position[Y_AXIS] = center_old_y;
  1865. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1866. }
  1867. #define MESH_BED_CALIBRATION_SHOW_LCD
  1868. BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask)
  1869. {
  1870. // Don't let the manage_inactivity() function remove power from the motors.
  1871. refresh_cmd_timeout();
  1872. // Reusing the z_values memory for the measurement cache.
  1873. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  1874. float *pts = &mbl.z_values[0][0];
  1875. float *vec_x = pts + 2 * 4;
  1876. float *vec_y = vec_x + 2;
  1877. float *cntr = vec_y + 2;
  1878. memset(pts, 0, sizeof(float) * 7 * 7);
  1879. uint8_t iteration = 0;
  1880. BedSkewOffsetDetectionResultType result;
  1881. // SERIAL_ECHOLNPGM("find_bed_offset_and_skew verbosity level: ");
  1882. // SERIAL_ECHO(int(verbosity_level));
  1883. // SERIAL_ECHOPGM("");
  1884. while (iteration < 3) {
  1885. SERIAL_ECHOPGM("Iteration: ");
  1886. MYSERIAL.println(int(iteration + 1));
  1887. #ifdef SUPPORT_VERBOSITY
  1888. if (verbosity_level >= 20) {
  1889. SERIAL_ECHOLNPGM("Vectors: ");
  1890. SERIAL_ECHOPGM("vec_x[0]:");
  1891. MYSERIAL.print(vec_x[0], 5);
  1892. SERIAL_ECHOLNPGM("");
  1893. SERIAL_ECHOPGM("vec_x[1]:");
  1894. MYSERIAL.print(vec_x[1], 5);
  1895. SERIAL_ECHOLNPGM("");
  1896. SERIAL_ECHOPGM("vec_y[0]:");
  1897. MYSERIAL.print(vec_y[0], 5);
  1898. SERIAL_ECHOLNPGM("");
  1899. SERIAL_ECHOPGM("vec_y[1]:");
  1900. MYSERIAL.print(vec_y[1], 5);
  1901. SERIAL_ECHOLNPGM("");
  1902. SERIAL_ECHOPGM("cntr[0]:");
  1903. MYSERIAL.print(cntr[0], 5);
  1904. SERIAL_ECHOLNPGM("");
  1905. SERIAL_ECHOPGM("cntr[1]:");
  1906. MYSERIAL.print(cntr[1], 5);
  1907. SERIAL_ECHOLNPGM("");
  1908. }
  1909. #endif // SUPPORT_VERBOSITY
  1910. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1911. uint8_t next_line;
  1912. lcd_display_message_fullscreen_P(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1, next_line);
  1913. if (next_line > 3)
  1914. next_line = 3;
  1915. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1916. // Collect the rear 2x3 points.
  1917. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  1918. for (int k = 0; k < 4; ++k) {
  1919. // Don't let the manage_inactivity() function remove power from the motors.
  1920. refresh_cmd_timeout();
  1921. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1922. lcd_implementation_print_at(0, next_line, k + 1);
  1923. lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
  1924. if (iteration > 0) {
  1925. lcd_print_at_PGM(0, next_line + 1, MSG_FIND_BED_OFFSET_AND_SKEW_ITERATION);
  1926. lcd_implementation_print(int(iteration + 1));
  1927. }
  1928. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1929. float *pt = pts + k * 2;
  1930. // Go up to z_initial.
  1931. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  1932. #ifdef SUPPORT_VERBOSITY
  1933. if (verbosity_level >= 20) {
  1934. // Go to Y0, wait, then go to Y-4.
  1935. current_position[Y_AXIS] = 0.f;
  1936. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1937. SERIAL_ECHOLNPGM("At Y0");
  1938. delay_keep_alive(5000);
  1939. current_position[Y_AXIS] = Y_MIN_POS;
  1940. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1941. SERIAL_ECHOLNPGM("At Y-4");
  1942. delay_keep_alive(5000);
  1943. }
  1944. #endif // SUPPORT_VERBOSITY
  1945. // Go to the measurement point position.
  1946. //if (iteration == 0) {
  1947. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2);
  1948. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2 + 1);
  1949. /*}
  1950. else {
  1951. // if first iteration failed, count corrected point coordinates as initial
  1952. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1953. 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];
  1954. 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];
  1955. // The calibration points are very close to the min Y.
  1956. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  1957. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1958. }*/
  1959. #ifdef SUPPORT_VERBOSITY
  1960. if (verbosity_level >= 20) {
  1961. SERIAL_ECHOPGM("current_position[X_AXIS]:");
  1962. MYSERIAL.print(current_position[X_AXIS], 5);
  1963. SERIAL_ECHOLNPGM("");
  1964. SERIAL_ECHOPGM("current_position[Y_AXIS]:");
  1965. MYSERIAL.print(current_position[Y_AXIS], 5);
  1966. SERIAL_ECHOLNPGM("");
  1967. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  1968. MYSERIAL.print(current_position[Z_AXIS], 5);
  1969. SERIAL_ECHOLNPGM("");
  1970. }
  1971. #endif // SUPPORT_VERBOSITY
  1972. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1973. #ifdef SUPPORT_VERBOSITY
  1974. if (verbosity_level >= 10)
  1975. delay_keep_alive(3000);
  1976. #endif // SUPPORT_VERBOSITY
  1977. if (!find_bed_induction_sensor_point_xy(verbosity_level))
  1978. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  1979. #ifndef HEATBED_V2
  1980. if (k == 0 || k == 1) {
  1981. // Improve the position of the 1st row sensor points by a zig-zag movement.
  1982. find_bed_induction_sensor_point_z();
  1983. int8_t i = 4;
  1984. for (;;) {
  1985. if (improve_bed_induction_sensor_point3(verbosity_level))
  1986. break;
  1987. if (--i == 0)
  1988. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  1989. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  1990. current_position[Z_AXIS] -= 0.025f;
  1991. enable_endstops(false);
  1992. enable_z_endstop(false);
  1993. go_to_current(homing_feedrate[Z_AXIS]);
  1994. }
  1995. if (i == 0)
  1996. // not found
  1997. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  1998. }
  1999. #endif //HEATBED_V2
  2000. #ifdef SUPPORT_VERBOSITY
  2001. if (verbosity_level >= 10)
  2002. delay_keep_alive(3000);
  2003. #endif // SUPPORT_VERBOSITY
  2004. // Save the detected point position and then clamp the Y coordinate, which may have been estimated
  2005. // to lie outside the machine working space.
  2006. #ifdef SUPPORT_VERBOSITY
  2007. if (verbosity_level >= 20) {
  2008. SERIAL_ECHOLNPGM("Measured:");
  2009. MYSERIAL.println(current_position[X_AXIS]);
  2010. MYSERIAL.println(current_position[Y_AXIS]);
  2011. }
  2012. #endif // SUPPORT_VERBOSITY
  2013. pt[0] = (pt[0] * iteration) / (iteration + 1);
  2014. pt[0] += (current_position[X_AXIS]/(iteration + 1)); //count average
  2015. pt[1] = (pt[1] * iteration) / (iteration + 1);
  2016. pt[1] += (current_position[Y_AXIS] / (iteration + 1));
  2017. //pt[0] += current_position[X_AXIS];
  2018. //if(iteration > 0) pt[0] = pt[0] / 2;
  2019. //pt[1] += current_position[Y_AXIS];
  2020. //if (iteration > 0) pt[1] = pt[1] / 2;
  2021. #ifdef SUPPORT_VERBOSITY
  2022. if (verbosity_level >= 20) {
  2023. SERIAL_ECHOLNPGM("");
  2024. SERIAL_ECHOPGM("pt[0]:");
  2025. MYSERIAL.println(pt[0]);
  2026. SERIAL_ECHOPGM("pt[1]:");
  2027. MYSERIAL.println(pt[1]);
  2028. }
  2029. #endif // SUPPORT_VERBOSITY
  2030. if (current_position[Y_AXIS] < Y_MIN_POS)
  2031. current_position[Y_AXIS] = Y_MIN_POS;
  2032. // Start searching for the other points at 3mm above the last point.
  2033. current_position[Z_AXIS] += 3.f + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2034. //cntr[0] += pt[0];
  2035. //cntr[1] += pt[1];
  2036. #ifdef SUPPORT_VERBOSITY
  2037. if (verbosity_level >= 10 && k == 0) {
  2038. // Show the zero. Test, whether the Y motor skipped steps.
  2039. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  2040. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2041. delay_keep_alive(3000);
  2042. }
  2043. #endif // SUPPORT_VERBOSITY
  2044. }
  2045. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2046. #ifdef SUPPORT_VERBOSITY
  2047. if (verbosity_level >= 20) {
  2048. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2049. delay_keep_alive(3000);
  2050. for (int8_t mesh_point = 0; mesh_point < 4; ++mesh_point) {
  2051. // Don't let the manage_inactivity() function remove power from the motors.
  2052. refresh_cmd_timeout();
  2053. // Go to the measurement point.
  2054. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2055. current_position[X_AXIS] = pts[mesh_point * 2];
  2056. current_position[Y_AXIS] = pts[mesh_point * 2 + 1];
  2057. go_to_current(homing_feedrate[X_AXIS] / 60);
  2058. delay_keep_alive(3000);
  2059. }
  2060. }
  2061. #endif // SUPPORT_VERBOSITY
  2062. if (pts[1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  2063. too_far_mask |= 1 << 1; //front center point is out of reach
  2064. SERIAL_ECHOLNPGM("");
  2065. SERIAL_ECHOPGM("WARNING: Front point not reachable. Y coordinate:");
  2066. MYSERIAL.print(pts[1]);
  2067. SERIAL_ECHOPGM(" < ");
  2068. MYSERIAL.println(Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2069. }
  2070. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2071. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2072. if (result >= 0) {
  2073. world2machine_update(vec_x, vec_y, cntr);
  2074. #if 1
  2075. // Fearlessly store the calibration values into the eeprom.
  2076. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0), cntr[0]);
  2077. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4), cntr[1]);
  2078. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0), vec_x[0]);
  2079. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4), vec_x[1]);
  2080. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0), vec_y[0]);
  2081. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4), vec_y[1]);
  2082. #endif
  2083. #ifdef SUPPORT_VERBOSITY
  2084. if (verbosity_level >= 10) {
  2085. // Length of the vec_x
  2086. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  2087. SERIAL_ECHOLNPGM("X vector length:");
  2088. MYSERIAL.println(l);
  2089. // Length of the vec_y
  2090. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  2091. SERIAL_ECHOLNPGM("Y vector length:");
  2092. MYSERIAL.println(l);
  2093. // Zero point correction
  2094. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  2095. SERIAL_ECHOLNPGM("Zero point correction:");
  2096. MYSERIAL.println(l);
  2097. // vec_x and vec_y shall be nearly perpendicular.
  2098. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  2099. SERIAL_ECHOLNPGM("Perpendicularity");
  2100. MYSERIAL.println(fabs(l));
  2101. SERIAL_ECHOLNPGM("Saving bed calibration vectors to EEPROM");
  2102. }
  2103. #endif // SUPPORT_VERBOSITY
  2104. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2105. world2machine_update_current();
  2106. #ifdef SUPPORT_VERBOSITY
  2107. if (verbosity_level >= 20) {
  2108. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2109. delay_keep_alive(3000);
  2110. for (int8_t mesh_point = 0; mesh_point < 9; ++mesh_point) {
  2111. // Don't let the manage_inactivity() function remove power from the motors.
  2112. refresh_cmd_timeout();
  2113. // Go to the measurement point.
  2114. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2115. current_position[X_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2);
  2116. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2 + 1);
  2117. go_to_current(homing_feedrate[X_AXIS] / 60);
  2118. delay_keep_alive(3000);
  2119. }
  2120. }
  2121. #endif // SUPPORT_VERBOSITY
  2122. return result;
  2123. }
  2124. 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
  2125. iteration++;
  2126. }
  2127. return result;
  2128. }
  2129. BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask)
  2130. {
  2131. // Don't let the manage_inactivity() function remove power from the motors.
  2132. refresh_cmd_timeout();
  2133. // Mask of the first three points. Are they too far?
  2134. too_far_mask = 0;
  2135. // Reusing the z_values memory for the measurement cache.
  2136. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2137. float *pts = &mbl.z_values[0][0];
  2138. float *vec_x = pts + 2 * 9;
  2139. float *vec_y = vec_x + 2;
  2140. float *cntr = vec_y + 2;
  2141. memset(pts, 0, sizeof(float) * 7 * 7);
  2142. #ifdef SUPPORT_VERBOSITY
  2143. if (verbosity_level >= 10) SERIAL_ECHOLNPGM("Improving bed offset and skew");
  2144. #endif // SUPPORT_VERBOSITY
  2145. // Cache the current correction matrix.
  2146. world2machine_initialize();
  2147. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2148. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2149. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2150. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2151. cntr[0] = world2machine_shift[0];
  2152. cntr[1] = world2machine_shift[1];
  2153. // and reset the correction matrix, so the planner will not do anything.
  2154. world2machine_reset();
  2155. bool endstops_enabled = enable_endstops(false);
  2156. bool endstop_z_enabled = enable_z_endstop(false);
  2157. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2158. uint8_t next_line;
  2159. lcd_display_message_fullscreen_P(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1, next_line);
  2160. if (next_line > 3)
  2161. next_line = 3;
  2162. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2163. // Collect a matrix of 9x9 points.
  2164. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  2165. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2166. // Don't let the manage_inactivity() function remove power from the motors.
  2167. refresh_cmd_timeout();
  2168. // Print the decrasing ID of the measurement point.
  2169. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2170. lcd_implementation_print_at(0, next_line, mesh_point+1);
  2171. lcd_printPGM(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2);
  2172. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2173. // Move up.
  2174. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2175. enable_endstops(false);
  2176. enable_z_endstop(false);
  2177. go_to_current(homing_feedrate[Z_AXIS]/60);
  2178. #ifdef SUPPORT_VERBOSITY
  2179. if (verbosity_level >= 20) {
  2180. // Go to Y0, wait, then go to Y-4.
  2181. current_position[Y_AXIS] = 0.f;
  2182. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2183. SERIAL_ECHOLNPGM("At Y0");
  2184. delay_keep_alive(5000);
  2185. current_position[Y_AXIS] = Y_MIN_POS;
  2186. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2187. SERIAL_ECHOLNPGM("At Y_MIN_POS");
  2188. delay_keep_alive(5000);
  2189. }
  2190. #endif // SUPPORT_VERBOSITY
  2191. // Go to the measurement point.
  2192. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2193. 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];
  2194. 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];
  2195. // The calibration points are very close to the min Y.
  2196. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION){
  2197. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2198. #ifdef SUPPORT_VERBOSITY
  2199. if (verbosity_level >= 20) {
  2200. SERIAL_ECHOPGM("Calibration point ");
  2201. SERIAL_ECHO(mesh_point);
  2202. SERIAL_ECHOPGM("lower than Ymin. Y coordinate clamping was used.");
  2203. SERIAL_ECHOLNPGM("");
  2204. }
  2205. #endif // SUPPORT_VERBOSITY
  2206. }
  2207. go_to_current(homing_feedrate[X_AXIS]/60);
  2208. // Find its Z position by running the normal vertical search.
  2209. #ifdef SUPPORT_VERBOSITY
  2210. if (verbosity_level >= 10)
  2211. delay_keep_alive(3000);
  2212. #endif // SUPPORT_VERBOSITY
  2213. find_bed_induction_sensor_point_z();
  2214. #ifdef SUPPORT_VERBOSITY
  2215. if (verbosity_level >= 10)
  2216. delay_keep_alive(3000);
  2217. #endif // SUPPORT_VERBOSITY
  2218. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2219. current_position[Z_AXIS] -= 0.025f;
  2220. // Improve the point position by searching its center in a current plane.
  2221. int8_t n_errors = 3;
  2222. for (int8_t iter = 0; iter < 8; ) {
  2223. #ifdef SUPPORT_VERBOSITY
  2224. if (verbosity_level > 20) {
  2225. SERIAL_ECHOPGM("Improving bed point ");
  2226. SERIAL_ECHO(mesh_point);
  2227. SERIAL_ECHOPGM(", iteration ");
  2228. SERIAL_ECHO(iter);
  2229. SERIAL_ECHOPGM(", z");
  2230. MYSERIAL.print(current_position[Z_AXIS], 5);
  2231. SERIAL_ECHOLNPGM("");
  2232. }
  2233. #endif // SUPPORT_VERBOSITY
  2234. bool found = false;
  2235. if (mesh_point < 2) {
  2236. // Because the sensor cannot move in front of the first row
  2237. // of the sensor points, the y position cannot be measured
  2238. // by a cross center method.
  2239. // Use a zig-zag search for the first row of the points.
  2240. found = improve_bed_induction_sensor_point3(verbosity_level);
  2241. } else {
  2242. switch (method) {
  2243. case 0: found = improve_bed_induction_sensor_point(); break;
  2244. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 2, verbosity_level); break;
  2245. default: break;
  2246. }
  2247. }
  2248. if (found) {
  2249. if (iter > 3) {
  2250. // Average the last 4 measurements.
  2251. pts[mesh_point*2 ] += current_position[X_AXIS];
  2252. pts[mesh_point*2+1] += current_position[Y_AXIS];
  2253. }
  2254. if (current_position[Y_AXIS] < Y_MIN_POS)
  2255. current_position[Y_AXIS] = Y_MIN_POS;
  2256. ++ iter;
  2257. } else if (n_errors -- == 0) {
  2258. // Give up.
  2259. result = BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2260. goto canceled;
  2261. } else {
  2262. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2263. current_position[Z_AXIS] -= 0.05f;
  2264. enable_endstops(false);
  2265. enable_z_endstop(false);
  2266. go_to_current(homing_feedrate[Z_AXIS]);
  2267. #ifdef SUPPORT_VERBOSITY
  2268. if (verbosity_level >= 5) {
  2269. SERIAL_ECHOPGM("Improving bed point ");
  2270. SERIAL_ECHO(mesh_point);
  2271. SERIAL_ECHOPGM(", iteration ");
  2272. SERIAL_ECHO(iter);
  2273. SERIAL_ECHOPGM(" failed. Lowering z to ");
  2274. MYSERIAL.print(current_position[Z_AXIS], 5);
  2275. SERIAL_ECHOLNPGM("");
  2276. }
  2277. #endif // SUPPORT_VERBOSITY
  2278. }
  2279. }
  2280. #ifdef SUPPORT_VERBOSITY
  2281. if (verbosity_level >= 10)
  2282. delay_keep_alive(3000);
  2283. #endif // SUPPORT_VERBOSITY
  2284. }
  2285. // Don't let the manage_inactivity() function remove power from the motors.
  2286. refresh_cmd_timeout();
  2287. // Average the last 4 measurements.
  2288. for (int8_t i = 0; i < 8; ++ i)
  2289. pts[i] *= (1.f/4.f);
  2290. enable_endstops(false);
  2291. enable_z_endstop(false);
  2292. #ifdef SUPPORT_VERBOSITY
  2293. if (verbosity_level >= 5) {
  2294. // Test the positions. Are the positions reproducible?
  2295. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2296. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2297. // Don't let the manage_inactivity() function remove power from the motors.
  2298. refresh_cmd_timeout();
  2299. // Go to the measurement point.
  2300. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2301. current_position[X_AXIS] = pts[mesh_point*2];
  2302. current_position[Y_AXIS] = pts[mesh_point*2+1];
  2303. if (verbosity_level >= 10) {
  2304. go_to_current(homing_feedrate[X_AXIS]/60);
  2305. delay_keep_alive(3000);
  2306. }
  2307. SERIAL_ECHOPGM("Final measured bed point ");
  2308. SERIAL_ECHO(mesh_point);
  2309. SERIAL_ECHOPGM(": ");
  2310. MYSERIAL.print(current_position[X_AXIS], 5);
  2311. SERIAL_ECHOPGM(", ");
  2312. MYSERIAL.print(current_position[Y_AXIS], 5);
  2313. SERIAL_ECHOLNPGM("");
  2314. }
  2315. }
  2316. #endif // SUPPORT_VERBOSITY
  2317. {
  2318. // First fill in the too_far_mask from the measured points.
  2319. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point)
  2320. if (pts[mesh_point * 2 + 1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2321. too_far_mask |= 1 << mesh_point;
  2322. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2323. if (result < 0) {
  2324. SERIAL_ECHOLNPGM("Calculation of the machine skew and offset failed.");
  2325. goto canceled;
  2326. }
  2327. // In case of success, update the too_far_mask from the calculated points.
  2328. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point) {
  2329. 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];
  2330. distance_from_min[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2331. #ifdef SUPPORT_VERBOSITY
  2332. if (verbosity_level >= 20) {
  2333. SERIAL_ECHOLNPGM("");
  2334. SERIAL_ECHOPGM("Distance from min:");
  2335. MYSERIAL.print(distance_from_min[mesh_point]);
  2336. SERIAL_ECHOLNPGM("");
  2337. SERIAL_ECHOPGM("y:");
  2338. MYSERIAL.print(y);
  2339. SERIAL_ECHOLNPGM("");
  2340. }
  2341. #endif // SUPPORT_VERBOSITY
  2342. if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2343. too_far_mask |= 1 << mesh_point;
  2344. }
  2345. }
  2346. world2machine_update(vec_x, vec_y, cntr);
  2347. #if 1
  2348. // Fearlessly store the calibration values into the eeprom.
  2349. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  2350. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  2351. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  2352. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  2353. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  2354. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  2355. #endif
  2356. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2357. world2machine_update_current();
  2358. enable_endstops(false);
  2359. enable_z_endstop(false);
  2360. #ifdef SUPPORT_VERBOSITY
  2361. if (verbosity_level >= 5) {
  2362. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2363. delay_keep_alive(3000);
  2364. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2365. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2366. // Don't let the manage_inactivity() function remove power from the motors.
  2367. refresh_cmd_timeout();
  2368. // Go to the measurement point.
  2369. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2370. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2);
  2371. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2+1);
  2372. if (verbosity_level >= 10) {
  2373. go_to_current(homing_feedrate[X_AXIS]/60);
  2374. delay_keep_alive(3000);
  2375. }
  2376. {
  2377. float x, y;
  2378. world2machine(current_position[X_AXIS], current_position[Y_AXIS], x, y);
  2379. SERIAL_ECHOPGM("Final calculated bed point ");
  2380. SERIAL_ECHO(mesh_point);
  2381. SERIAL_ECHOPGM(": ");
  2382. MYSERIAL.print(x, 5);
  2383. SERIAL_ECHOPGM(", ");
  2384. MYSERIAL.print(y, 5);
  2385. SERIAL_ECHOLNPGM("");
  2386. }
  2387. }
  2388. }
  2389. #endif // SUPPORT_VERBOSITY
  2390. if(!sample_z())
  2391. goto canceled;
  2392. enable_endstops(endstops_enabled);
  2393. enable_z_endstop(endstop_z_enabled);
  2394. // Don't let the manage_inactivity() function remove power from the motors.
  2395. refresh_cmd_timeout();
  2396. return result;
  2397. canceled:
  2398. // Don't let the manage_inactivity() function remove power from the motors.
  2399. refresh_cmd_timeout();
  2400. // Print head up.
  2401. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2402. go_to_current(homing_feedrate[Z_AXIS]/60);
  2403. // Store the identity matrix to EEPROM.
  2404. reset_bed_offset_and_skew();
  2405. enable_endstops(endstops_enabled);
  2406. enable_z_endstop(endstop_z_enabled);
  2407. return result;
  2408. }
  2409. bool sample_z() {
  2410. bool sampled = true;
  2411. //make space
  2412. current_position[Z_AXIS] += 150;
  2413. go_to_current(homing_feedrate[Z_AXIS] / 60);
  2414. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate, active_extruder););
  2415. lcd_show_fullscreen_message_and_wait_P(MSG_PLACE_STEEL_SHEET);
  2416. // Sample Z heights for the mesh bed leveling.
  2417. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2418. if (!sample_mesh_and_store_reference()) sampled = false;
  2419. return sampled;
  2420. }
  2421. void go_home_with_z_lift()
  2422. {
  2423. // Don't let the manage_inactivity() function remove power from the motors.
  2424. refresh_cmd_timeout();
  2425. // Go home.
  2426. // First move up to a safe height.
  2427. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2428. go_to_current(homing_feedrate[Z_AXIS]/60);
  2429. // Second move to XY [0, 0].
  2430. current_position[X_AXIS] = X_MIN_POS+0.2;
  2431. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2432. // Clamp to the physical coordinates.
  2433. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2434. go_to_current(homing_feedrate[X_AXIS]/60);
  2435. // Third move up to a safe height.
  2436. current_position[Z_AXIS] = Z_MIN_POS;
  2437. go_to_current(homing_feedrate[Z_AXIS]/60);
  2438. }
  2439. // Sample the 9 points of the bed and store them into the EEPROM as a reference.
  2440. // When calling this function, the X, Y, Z axes should be already homed,
  2441. // and the world2machine correction matrix should be active.
  2442. // Returns false if the reference values are more than 3mm far away.
  2443. bool sample_mesh_and_store_reference()
  2444. {
  2445. bool endstops_enabled = enable_endstops(false);
  2446. bool endstop_z_enabled = enable_z_endstop(false);
  2447. // Don't let the manage_inactivity() function remove power from the motors.
  2448. refresh_cmd_timeout();
  2449. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2450. uint8_t next_line;
  2451. lcd_display_message_fullscreen_P(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1, next_line);
  2452. if (next_line > 3)
  2453. next_line = 3;
  2454. // display "point xx of yy"
  2455. lcd_implementation_print_at(0, next_line, 1);
  2456. lcd_printPGM(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2);
  2457. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2458. // Sample Z heights for the mesh bed leveling.
  2459. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2460. {
  2461. // The first point defines the reference.
  2462. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2463. go_to_current(homing_feedrate[Z_AXIS]/60);
  2464. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  2465. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+1);
  2466. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2467. go_to_current(homing_feedrate[X_AXIS]/60);
  2468. memcpy(destination, current_position, sizeof(destination));
  2469. enable_endstops(true);
  2470. homeaxis(Z_AXIS);
  2471. enable_endstops(false);
  2472. find_bed_induction_sensor_point_z();
  2473. mbl.set_z(0, 0, current_position[Z_AXIS]);
  2474. }
  2475. for (int8_t mesh_point = 1; mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS; ++ mesh_point) {
  2476. // Don't let the manage_inactivity() function remove power from the motors.
  2477. refresh_cmd_timeout();
  2478. // Print the decrasing ID of the measurement point.
  2479. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2480. go_to_current(homing_feedrate[Z_AXIS]/60);
  2481. current_position[X_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point);
  2482. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point+1);
  2483. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2484. go_to_current(homing_feedrate[X_AXIS]/60);
  2485. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2486. // display "point xx of yy"
  2487. lcd_implementation_print_at(0, next_line, mesh_point+1);
  2488. lcd_printPGM(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2);
  2489. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2490. find_bed_induction_sensor_point_z();
  2491. // Get cords of measuring point
  2492. int8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2493. int8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2494. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2495. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2496. }
  2497. {
  2498. // Verify the span of the Z values.
  2499. float zmin = mbl.z_values[0][0];
  2500. float zmax = zmax;
  2501. for (int8_t j = 0; j < 3; ++ j)
  2502. for (int8_t i = 0; i < 3; ++ i) {
  2503. zmin = min(zmin, mbl.z_values[j][i]);
  2504. zmax = min(zmax, mbl.z_values[j][i]);
  2505. }
  2506. if (zmax - zmin > 3.f) {
  2507. // The span of the Z offsets is extreme. Give up.
  2508. // Homing failed on some of the points.
  2509. SERIAL_PROTOCOLLNPGM("Exreme span of the Z values!");
  2510. return false;
  2511. }
  2512. }
  2513. // Store the correction values to EEPROM.
  2514. // Offsets of the Z heiths of the calibration points from the first point.
  2515. // The offsets are saved as 16bit signed int, scaled to tenths of microns.
  2516. {
  2517. uint16_t addr = EEPROM_BED_CALIBRATION_Z_JITTER;
  2518. for (int8_t j = 0; j < 3; ++ j)
  2519. for (int8_t i = 0; i < 3; ++ i) {
  2520. if (i == 0 && j == 0)
  2521. continue;
  2522. float dif = mbl.z_values[j][i] - mbl.z_values[0][0];
  2523. int16_t dif_quantized = int16_t(floor(dif * 100.f + 0.5f));
  2524. eeprom_update_word((uint16_t*)addr, *reinterpret_cast<uint16_t*>(&dif_quantized));
  2525. #if 0
  2526. {
  2527. uint16_t z_offset_u = eeprom_read_word((uint16_t*)addr);
  2528. float dif2 = *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2529. SERIAL_ECHOPGM("Bed point ");
  2530. SERIAL_ECHO(i);
  2531. SERIAL_ECHOPGM(",");
  2532. SERIAL_ECHO(j);
  2533. SERIAL_ECHOPGM(", differences: written ");
  2534. MYSERIAL.print(dif, 5);
  2535. SERIAL_ECHOPGM(", read: ");
  2536. MYSERIAL.print(dif2, 5);
  2537. SERIAL_ECHOLNPGM("");
  2538. }
  2539. #endif
  2540. addr += 2;
  2541. }
  2542. }
  2543. mbl.upsample_3x3();
  2544. mbl.active = true;
  2545. go_home_with_z_lift();
  2546. enable_endstops(endstops_enabled);
  2547. enable_z_endstop(endstop_z_enabled);
  2548. return true;
  2549. }
  2550. bool scan_bed_induction_points(int8_t verbosity_level)
  2551. {
  2552. // Don't let the manage_inactivity() function remove power from the motors.
  2553. refresh_cmd_timeout();
  2554. // Reusing the z_values memory for the measurement cache.
  2555. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2556. float *pts = &mbl.z_values[0][0];
  2557. float *vec_x = pts + 2 * 9;
  2558. float *vec_y = vec_x + 2;
  2559. float *cntr = vec_y + 2;
  2560. memset(pts, 0, sizeof(float) * 7 * 7);
  2561. // Cache the current correction matrix.
  2562. world2machine_initialize();
  2563. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2564. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2565. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2566. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2567. cntr[0] = world2machine_shift[0];
  2568. cntr[1] = world2machine_shift[1];
  2569. // and reset the correction matrix, so the planner will not do anything.
  2570. world2machine_reset();
  2571. bool endstops_enabled = enable_endstops(false);
  2572. bool endstop_z_enabled = enable_z_endstop(false);
  2573. // Collect a matrix of 9x9 points.
  2574. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  2575. // Don't let the manage_inactivity() function remove power from the motors.
  2576. refresh_cmd_timeout();
  2577. // Move up.
  2578. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2579. enable_endstops(false);
  2580. enable_z_endstop(false);
  2581. go_to_current(homing_feedrate[Z_AXIS]/60);
  2582. // Go to the measurement point.
  2583. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2584. 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];
  2585. 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];
  2586. // The calibration points are very close to the min Y.
  2587. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2588. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2589. go_to_current(homing_feedrate[X_AXIS]/60);
  2590. find_bed_induction_sensor_point_z();
  2591. scan_bed_induction_sensor_point();
  2592. }
  2593. // Don't let the manage_inactivity() function remove power from the motors.
  2594. refresh_cmd_timeout();
  2595. enable_endstops(false);
  2596. enable_z_endstop(false);
  2597. // Don't let the manage_inactivity() function remove power from the motors.
  2598. refresh_cmd_timeout();
  2599. enable_endstops(endstops_enabled);
  2600. enable_z_endstop(endstop_z_enabled);
  2601. return true;
  2602. }
  2603. // Shift a Z axis by a given delta.
  2604. // To replace loading of the babystep correction.
  2605. static void shift_z(float delta)
  2606. {
  2607. 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);
  2608. st_synchronize();
  2609. plan_set_z_position(current_position[Z_AXIS]);
  2610. }
  2611. #define BABYSTEP_LOADZ_BY_PLANNER
  2612. // Number of baby steps applied
  2613. static int babystepLoadZ = 0;
  2614. void babystep_load()
  2615. {
  2616. // Apply Z height correction aka baby stepping before mesh bed leveling gets activated.
  2617. if(calibration_status() < CALIBRATION_STATUS_LIVE_ADJUST)
  2618. {
  2619. check_babystep(); //checking if babystep is in allowed range, otherwise setting babystep to 0
  2620. // End of G80: Apply the baby stepping value.
  2621. EEPROM_read_B(EEPROM_BABYSTEP_Z,&babystepLoadZ);
  2622. #if 0
  2623. SERIAL_ECHO("Z baby step: ");
  2624. SERIAL_ECHO(babystepLoadZ);
  2625. SERIAL_ECHO(", current Z: ");
  2626. SERIAL_ECHO(current_position[Z_AXIS]);
  2627. SERIAL_ECHO("correction: ");
  2628. SERIAL_ECHO(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2629. SERIAL_ECHOLN("");
  2630. #endif
  2631. }
  2632. }
  2633. void babystep_apply()
  2634. {
  2635. babystep_load();
  2636. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2637. shift_z(- float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2638. #else
  2639. babystepsTodoZadd(babystepLoadZ);
  2640. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2641. }
  2642. void babystep_undo()
  2643. {
  2644. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2645. shift_z(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2646. #else
  2647. babystepsTodoZsubtract(babystepLoadZ);
  2648. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2649. babystepLoadZ = 0;
  2650. }
  2651. void babystep_reset()
  2652. {
  2653. babystepLoadZ = 0;
  2654. }
  2655. void count_xyz_details() {
  2656. float a1, a2;
  2657. float cntr[2] = {
  2658. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0)),
  2659. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4))
  2660. };
  2661. float vec_x[2] = {
  2662. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0)),
  2663. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4))
  2664. };
  2665. float vec_y[2] = {
  2666. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0)),
  2667. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4))
  2668. };
  2669. a2 = -1 * asin(vec_y[0] / MACHINE_AXIS_SCALE_Y);
  2670. a1 = asin(vec_x[1] / MACHINE_AXIS_SCALE_X);
  2671. //angleDiff = fabs(a2 - a1);
  2672. for (uint8_t mesh_point = 0; mesh_point < 2; ++mesh_point) {
  2673. 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];
  2674. distance_from_min[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2675. }
  2676. }
  2677. /*countDistanceFromMin() {
  2678. }*/