mesh_bed_calibration.cpp 104 KB

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