mesh_bed_calibration.cpp 93 KB

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