mesh_bed_calibration.cpp 113 KB

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