mesh_bed_calibration.cpp 101 KB

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