mesh_bed_calibration.cpp 105 KB

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