mesh_bed_calibration.cpp 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. #include "Marlin.h"
  2. #include "Configuration.h"
  3. #include "language_all.h"
  4. #include "mesh_bed_calibration.h"
  5. #include "mesh_bed_leveling.h"
  6. #include "stepper.h"
  7. #include "ultralcd.h"
  8. // #include "qr_solve.h"
  9. extern float home_retract_mm_ext(int axis);
  10. float world2machine_rotation_and_skew[2][2];
  11. float world2machine_shift[2];
  12. // Weight of the Y coordinate for the least squares fitting of the bed induction sensor targets.
  13. // Only used for the first row of the points, which may not befully in reach of the sensor.
  14. #define WEIGHT_FIRST_ROW (0.2f)
  15. #define BED_ZERO_REF_X (- 22.f + X_PROBE_OFFSET_FROM_EXTRUDER)
  16. #define BED_ZERO_REF_Y (- 0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER)
  17. // Scaling of the real machine axes against the programmed dimensions in the firmware.
  18. // The correction is tiny, here around 0.5mm on 250mm length.
  19. #define MACHINE_AXIS_SCALE_X ((250.f + 0.5f) / 250.f)
  20. #define MACHINE_AXIS_SCALE_Y ((250.f + 0.5f) / 250.f)
  21. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  22. // The points are ordered in a zig-zag fashion to speed up the calibration.
  23. const float bed_ref_points[] PROGMEM = {
  24. 13.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  25. 115.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  26. 216.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  27. 216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  28. 115.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  29. 13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  30. 13.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  31. 115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  32. 216.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y
  33. };
  34. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  35. // The points are the following: center front, center right, center rear, center left.
  36. const float bed_ref_points_4[] PROGMEM = {
  37. 115.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  38. 216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  39. 115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  40. 13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y
  41. };
  42. //#define Y_MIN_POS_FOR_BED_CALIBRATION (MANUAL_Y_HOME_POS-0.2f)
  43. #define Y_MIN_POS_FOR_BED_CALIBRATION (Y_MIN_POS)
  44. static inline float sqr(float x) { return x * x; }
  45. #if 0
  46. // Linear Least Squares fitting of the bed to the measured induction points.
  47. // This method will not maintain a unity length of the machine axes.
  48. // This may be all right if the sensor points are measured precisely,
  49. // but it will stretch or shorten the machine axes if the measured data is not precise enough.
  50. bool calculate_machine_skew_and_offset_LS(
  51. // Matrix of maximum 9 2D points (18 floats)
  52. const float *measured_pts,
  53. uint8_t npts,
  54. const float *true_pts,
  55. // Resulting correction matrix.
  56. float *vec_x,
  57. float *vec_y,
  58. float *cntr,
  59. // Temporary values, 49-18-(2*3)=25 floats
  60. // , float *temp
  61. int8_t verbosity_level
  62. )
  63. {
  64. if (verbosity_level >= 10) {
  65. // Show the initial state, before the fitting.
  66. SERIAL_ECHOPGM("X vector, initial: ");
  67. MYSERIAL.print(vec_x[0], 5);
  68. SERIAL_ECHOPGM(", ");
  69. MYSERIAL.print(vec_x[1], 5);
  70. SERIAL_ECHOLNPGM("");
  71. SERIAL_ECHOPGM("Y vector, initial: ");
  72. MYSERIAL.print(vec_y[0], 5);
  73. SERIAL_ECHOPGM(", ");
  74. MYSERIAL.print(vec_y[1], 5);
  75. SERIAL_ECHOLNPGM("");
  76. SERIAL_ECHOPGM("center, initial: ");
  77. MYSERIAL.print(cntr[0], 5);
  78. SERIAL_ECHOPGM(", ");
  79. MYSERIAL.print(cntr[1], 5);
  80. SERIAL_ECHOLNPGM("");
  81. for (uint8_t i = 0; i < npts; ++ i) {
  82. SERIAL_ECHOPGM("point #");
  83. MYSERIAL.print(int(i));
  84. SERIAL_ECHOPGM(" measured: (");
  85. MYSERIAL.print(measured_pts[i*2], 5);
  86. SERIAL_ECHOPGM(", ");
  87. MYSERIAL.print(measured_pts[i*2+1], 5);
  88. SERIAL_ECHOPGM("); target: (");
  89. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  90. SERIAL_ECHOPGM(", ");
  91. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  92. SERIAL_ECHOPGM("), error: ");
  93. MYSERIAL.print(sqrt(
  94. sqr(pgm_read_float(true_pts+i*2 ) - measured_pts[i*2 ]) +
  95. sqr(pgm_read_float(true_pts+i*2+1) - measured_pts[i*2+1])), 5);
  96. SERIAL_ECHOLNPGM("");
  97. }
  98. delay_keep_alive(100);
  99. }
  100. {
  101. // Create covariance matrix for A, collect the right hand side b.
  102. float A[3][3] = { 0.f };
  103. float b[3] = { 0.f };
  104. float acc;
  105. for (uint8_t r = 0; r < 3; ++ r) {
  106. for (uint8_t c = 0; c < 3; ++ c) {
  107. acc = 0;
  108. for (uint8_t i = 0; i < npts; ++ i) {
  109. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  110. float b = (c == 2) ? 1.f : measured_pts[2 * i + c];
  111. acc += a * b;
  112. }
  113. A[r][c] = acc;
  114. }
  115. acc = 0.f;
  116. for (uint8_t i = 0; i < npts; ++ i) {
  117. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  118. float b = pgm_read_float(true_pts+i*2);
  119. acc += a * b;
  120. }
  121. b[r] = acc;
  122. }
  123. // Solve the linear equation for ax, bx, cx.
  124. float x[3] = { 0.f };
  125. for (uint8_t iter = 0; iter < 100; ++ iter) {
  126. x[0] = (b[0] - A[0][1] * x[1] - A[0][2] * x[2]) / A[0][0];
  127. x[1] = (b[1] - A[1][0] * x[0] - A[1][2] * x[2]) / A[1][1];
  128. x[2] = (b[2] - A[2][0] * x[0] - A[2][1] * x[1]) / A[2][2];
  129. }
  130. // Store the result to the output variables.
  131. vec_x[0] = x[0];
  132. vec_y[0] = x[1];
  133. cntr[0] = x[2];
  134. // Recalculate A and b for the y values.
  135. // Note the weighting of the first row of values.
  136. for (uint8_t r = 0; r < 3; ++ r) {
  137. for (uint8_t c = 0; c < 3; ++ c) {
  138. acc = 0;
  139. for (uint8_t i = 0; i < npts; ++ i) {
  140. float w = (i < 3) ? WEIGHT_FIRST_ROW : 1.f;
  141. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  142. float b = (c == 2) ? 1.f : measured_pts[2 * i + c];
  143. acc += a * b * w;
  144. }
  145. A[r][c] = acc;
  146. }
  147. acc = 0.f;
  148. for (uint8_t i = 0; i < npts; ++ i) {
  149. float w = (i < 3) ? WEIGHT_FIRST_ROW : 1.f;
  150. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  151. float b = pgm_read_float(true_pts+i*2+1);
  152. acc += w * a * b;
  153. }
  154. b[r] = acc;
  155. }
  156. // Solve the linear equation for ay, by, cy.
  157. x[0] = 0.f, x[1] = 0.f; x[2] = 0.f;
  158. for (uint8_t iter = 0; iter < 100; ++ iter) {
  159. x[0] = (b[0] - A[0][1] * x[1] - A[0][2] * x[2]) / A[0][0];
  160. x[1] = (b[1] - A[1][0] * x[0] - A[1][2] * x[2]) / A[1][1];
  161. x[2] = (b[2] - A[2][0] * x[0] - A[2][1] * x[1]) / A[2][2];
  162. }
  163. // Store the result to the output variables.
  164. vec_x[1] = x[0];
  165. vec_y[1] = x[1];
  166. cntr[1] = x[2];
  167. }
  168. if (verbosity_level >= 10) {
  169. // Show the adjusted state, before the fitting.
  170. SERIAL_ECHOPGM("X vector new, inverted: ");
  171. MYSERIAL.print(vec_x[0], 5);
  172. SERIAL_ECHOPGM(", ");
  173. MYSERIAL.print(vec_x[1], 5);
  174. SERIAL_ECHOLNPGM("");
  175. SERIAL_ECHOPGM("Y vector new, inverted: ");
  176. MYSERIAL.print(vec_y[0], 5);
  177. SERIAL_ECHOPGM(", ");
  178. MYSERIAL.print(vec_y[1], 5);
  179. SERIAL_ECHOLNPGM("");
  180. SERIAL_ECHOPGM("center new, inverted: ");
  181. MYSERIAL.print(cntr[0], 5);
  182. SERIAL_ECHOPGM(", ");
  183. MYSERIAL.print(cntr[1], 5);
  184. SERIAL_ECHOLNPGM("");
  185. delay_keep_alive(100);
  186. SERIAL_ECHOLNPGM("Error after correction: ");
  187. for (uint8_t i = 0; i < npts; ++ i) {
  188. float x = vec_x[0] * measured_pts[i*2] + vec_y[0] * measured_pts[i*2+1] + cntr[0];
  189. float y = vec_x[1] * measured_pts[i*2] + vec_y[1] * measured_pts[i*2+1] + cntr[1];
  190. SERIAL_ECHOPGM("point #");
  191. MYSERIAL.print(int(i));
  192. SERIAL_ECHOPGM(" measured: (");
  193. MYSERIAL.print(measured_pts[i*2], 5);
  194. SERIAL_ECHOPGM(", ");
  195. MYSERIAL.print(measured_pts[i*2+1], 5);
  196. SERIAL_ECHOPGM("); corrected: (");
  197. MYSERIAL.print(x, 5);
  198. SERIAL_ECHOPGM(", ");
  199. MYSERIAL.print(y, 5);
  200. SERIAL_ECHOPGM("); target: (");
  201. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  202. SERIAL_ECHOPGM(", ");
  203. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  204. SERIAL_ECHOPGM("), error: ");
  205. MYSERIAL.print(sqrt(sqr(pgm_read_float(true_pts+i*2)-x)+sqr(pgm_read_float(true_pts+i*2+1)-y)));
  206. SERIAL_ECHOLNPGM("");
  207. }
  208. }
  209. #if 0
  210. // Normalize the vectors. We expect, that the machine axes may be skewed a bit, but the distances are correct.
  211. // l shall be very close to 1 already.
  212. float l = sqrt(vec_x[0]*vec_x[0] + vec_x[1] * vec_x[1]);
  213. vec_x[0] /= l;
  214. vec_x[1] /= l;
  215. SERIAL_ECHOPGM("Length of the X vector: ");
  216. MYSERIAL.print(l, 5);
  217. SERIAL_ECHOLNPGM("");
  218. l = sqrt(vec_y[0]*vec_y[0] + vec_y[1] * vec_y[1]);
  219. vec_y[0] /= l;
  220. vec_y[1] /= l;
  221. SERIAL_ECHOPGM("Length of the Y vector: ");
  222. MYSERIAL.print(l, 5);
  223. SERIAL_ECHOLNPGM("");
  224. // Recalculate the center using the adjusted vec_x/vec_y
  225. {
  226. cntr[0] = 0.f;
  227. cntr[1] = 0.f;
  228. for (uint8_t i = 0; i < npts; ++ i) {
  229. cntr[0] += measured_pts[2 * i ] - pgm_read_float(true_pts+i*2) * vec_x[0] - pgm_read_float(true_pts+i*2+1) * vec_y[0];
  230. cntr[1] += measured_pts[2 * i + 1] - pgm_read_float(true_pts+i*2) * vec_x[1] - pgm_read_float(true_pts+i*2+1) * vec_y[1];
  231. }
  232. cntr[0] /= float(npts);
  233. cntr[1] /= float(npts);
  234. }
  235. SERIAL_ECHOPGM("X vector new, inverted, normalized: ");
  236. MYSERIAL.print(vec_x[0], 5);
  237. SERIAL_ECHOPGM(", ");
  238. MYSERIAL.print(vec_x[1], 5);
  239. SERIAL_ECHOLNPGM("");
  240. SERIAL_ECHOPGM("Y vector new, inverted, normalized: ");
  241. MYSERIAL.print(vec_y[0], 5);
  242. SERIAL_ECHOPGM(", ");
  243. MYSERIAL.print(vec_y[1], 5);
  244. SERIAL_ECHOLNPGM("");
  245. SERIAL_ECHOPGM("center new, inverted, normalized: ");
  246. MYSERIAL.print(cntr[0], 5);
  247. SERIAL_ECHOPGM(", ");
  248. MYSERIAL.print(cntr[1], 5);
  249. SERIAL_ECHOLNPGM("");
  250. #endif
  251. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  252. {
  253. float d = vec_x[0] * vec_y[1] - vec_x[1] * vec_y[0];
  254. float Ainv[2][2] = {
  255. { vec_y[1] / d, - vec_y[0] / d },
  256. { - vec_x[1] / d, vec_x[0] / d }
  257. };
  258. float cntrInv[2] = {
  259. - Ainv[0][0] * cntr[0] - Ainv[0][1] * cntr[1],
  260. - Ainv[1][0] * cntr[0] - Ainv[1][1] * cntr[1]
  261. };
  262. vec_x[0] = Ainv[0][0];
  263. vec_x[1] = Ainv[1][0];
  264. vec_y[0] = Ainv[0][1];
  265. vec_y[1] = Ainv[1][1];
  266. cntr[0] = cntrInv[0];
  267. cntr[1] = cntrInv[1];
  268. }
  269. if (verbosity_level >= 1) {
  270. // Show the adjusted state, before the fitting.
  271. SERIAL_ECHOPGM("X vector, adjusted: ");
  272. MYSERIAL.print(vec_x[0], 5);
  273. SERIAL_ECHOPGM(", ");
  274. MYSERIAL.print(vec_x[1], 5);
  275. SERIAL_ECHOLNPGM("");
  276. SERIAL_ECHOPGM("Y vector, adjusted: ");
  277. MYSERIAL.print(vec_y[0], 5);
  278. SERIAL_ECHOPGM(", ");
  279. MYSERIAL.print(vec_y[1], 5);
  280. SERIAL_ECHOLNPGM("");
  281. SERIAL_ECHOPGM("center, adjusted: ");
  282. MYSERIAL.print(cntr[0], 5);
  283. SERIAL_ECHOPGM(", ");
  284. MYSERIAL.print(cntr[1], 5);
  285. SERIAL_ECHOLNPGM("");
  286. delay_keep_alive(100);
  287. }
  288. if (verbosity_level >= 2) {
  289. SERIAL_ECHOLNPGM("Difference after correction: ");
  290. for (uint8_t i = 0; i < npts; ++ i) {
  291. 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];
  292. 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];
  293. SERIAL_ECHOPGM("point #");
  294. MYSERIAL.print(int(i));
  295. SERIAL_ECHOPGM("measured: (");
  296. MYSERIAL.print(measured_pts[i*2], 5);
  297. SERIAL_ECHOPGM(", ");
  298. MYSERIAL.print(measured_pts[i*2+1], 5);
  299. SERIAL_ECHOPGM("); measured-corrected: (");
  300. MYSERIAL.print(x, 5);
  301. SERIAL_ECHOPGM(", ");
  302. MYSERIAL.print(y, 5);
  303. SERIAL_ECHOPGM("); target: (");
  304. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  305. SERIAL_ECHOPGM(", ");
  306. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  307. SERIAL_ECHOPGM("), error: ");
  308. MYSERIAL.print(sqrt(sqr(measured_pts[i*2]-x)+sqr(measured_pts[i*2+1]-y)));
  309. SERIAL_ECHOLNPGM("");
  310. }
  311. delay_keep_alive(100);
  312. }
  313. return true;
  314. }
  315. #else
  316. // Non-Linear Least Squares fitting of the bed to the measured induction points
  317. // using the Gauss-Newton method.
  318. // This method will maintain a unity length of the machine axes,
  319. // which is the correct approach if the sensor points are not measured precisely.
  320. bool calculate_machine_skew_and_offset_LS(
  321. // Matrix of maximum 9 2D points (18 floats)
  322. const float *measured_pts,
  323. uint8_t npts,
  324. const float *true_pts,
  325. // Resulting correction matrix.
  326. float *vec_x,
  327. float *vec_y,
  328. float *cntr,
  329. // Temporary values, 49-18-(2*3)=25 floats
  330. // , float *temp
  331. int8_t verbosity_level
  332. )
  333. {
  334. if (verbosity_level >= 10) {
  335. // Show the initial state, before the fitting.
  336. SERIAL_ECHOPGM("X vector, initial: ");
  337. MYSERIAL.print(vec_x[0], 5);
  338. SERIAL_ECHOPGM(", ");
  339. MYSERIAL.print(vec_x[1], 5);
  340. SERIAL_ECHOLNPGM("");
  341. SERIAL_ECHOPGM("Y vector, initial: ");
  342. MYSERIAL.print(vec_y[0], 5);
  343. SERIAL_ECHOPGM(", ");
  344. MYSERIAL.print(vec_y[1], 5);
  345. SERIAL_ECHOLNPGM("");
  346. SERIAL_ECHOPGM("center, initial: ");
  347. MYSERIAL.print(cntr[0], 5);
  348. SERIAL_ECHOPGM(", ");
  349. MYSERIAL.print(cntr[1], 5);
  350. SERIAL_ECHOLNPGM("");
  351. for (uint8_t i = 0; i < npts; ++i) {
  352. SERIAL_ECHOPGM("point #");
  353. MYSERIAL.print(int(i));
  354. SERIAL_ECHOPGM(" measured: (");
  355. MYSERIAL.print(measured_pts[i * 2], 5);
  356. SERIAL_ECHOPGM(", ");
  357. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  358. SERIAL_ECHOPGM("); target: (");
  359. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  360. SERIAL_ECHOPGM(", ");
  361. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  362. SERIAL_ECHOPGM("), error: ");
  363. MYSERIAL.print(sqrt(
  364. sqr(pgm_read_float(true_pts + i * 2) - measured_pts[i * 2]) +
  365. sqr(pgm_read_float(true_pts + i * 2 + 1) - measured_pts[i * 2 + 1])), 5);
  366. SERIAL_ECHOLNPGM("");
  367. }
  368. delay_keep_alive(100);
  369. }
  370. // Run some iterations of the Gauss-Newton method of non-linear least squares.
  371. // Initial set of parameters:
  372. // X,Y offset
  373. cntr[0] = 0.f;
  374. cntr[1] = 0.f;
  375. // Rotation of the machine X axis from the bed X axis.
  376. float a1 = 0;
  377. // Rotation of the machine Y axis from the bed Y axis.
  378. float a2 = 0;
  379. for (int8_t iter = 0; iter < 100; ++iter) {
  380. float c1 = cos(a1) * MACHINE_AXIS_SCALE_X;
  381. float s1 = sin(a1) * MACHINE_AXIS_SCALE_X;
  382. float c2 = cos(a2) * MACHINE_AXIS_SCALE_Y;
  383. float s2 = sin(a2) * MACHINE_AXIS_SCALE_Y;
  384. // Prepare the Normal equation for the Gauss-Newton method.
  385. float A[4][4] = { 0.f };
  386. float b[4] = { 0.f };
  387. float acc;
  388. for (uint8_t r = 0; r < 4; ++r) {
  389. for (uint8_t c = 0; c < 4; ++c) {
  390. acc = 0;
  391. // J^T times J
  392. for (uint8_t i = 0; i < npts; ++i) {
  393. // First for the residuum in the x axis:
  394. if (r != 1 && c != 1) {
  395. float a =
  396. (r == 0) ? 1.f :
  397. ((r == 2) ? (-s1 * measured_pts[2 * i]) :
  398. (-c2 * measured_pts[2 * i + 1]));
  399. float b =
  400. (c == 0) ? 1.f :
  401. ((c == 2) ? (-s1 * measured_pts[2 * i]) :
  402. (-c2 * measured_pts[2 * i + 1]));
  403. acc += a * b;
  404. }
  405. // Second for the residuum in the y axis.
  406. // The first row of the points have a low weight, because their position may not be known
  407. // with a sufficient accuracy.
  408. if (r != 0 && c != 0) {
  409. float a =
  410. (r == 1) ? 1.f :
  411. ((r == 2) ? ( c1 * measured_pts[2 * i]) :
  412. (-s2 * measured_pts[2 * i + 1]));
  413. float b =
  414. (c == 1) ? 1.f :
  415. ((c == 2) ? ( c1 * measured_pts[2 * i]) :
  416. (-s2 * measured_pts[2 * i + 1]));
  417. float w = (i < 3) ? WEIGHT_FIRST_ROW : 1.f;
  418. acc += a * b * w;
  419. }
  420. }
  421. A[r][c] = acc;
  422. }
  423. // J^T times f(x)
  424. acc = 0.f;
  425. for (uint8_t i = 0; i < npts; ++i) {
  426. {
  427. float j =
  428. (r == 0) ? 1.f :
  429. ((r == 1) ? 0.f :
  430. ((r == 2) ? (-s1 * measured_pts[2 * i]) :
  431. (-c2 * measured_pts[2 * i + 1])));
  432. float fx = c1 * measured_pts[2 * i] - s2 * measured_pts[2 * i + 1] + cntr[0] - pgm_read_float(true_pts + i * 2);
  433. acc += j * fx;
  434. }
  435. {
  436. float j =
  437. (r == 0) ? 0.f :
  438. ((r == 1) ? 1.f :
  439. ((r == 2) ? ( c1 * measured_pts[2 * i]) :
  440. (-s2 * measured_pts[2 * i + 1])));
  441. float fy = s1 * measured_pts[2 * i] + c2 * measured_pts[2 * i + 1] + cntr[1] - pgm_read_float(true_pts + i * 2 + 1);
  442. float w = (i < 3) ? WEIGHT_FIRST_ROW : 1.f;
  443. acc += j * fy * w;
  444. }
  445. }
  446. b[r] = -acc;
  447. }
  448. // Solve for h by a Gauss iteration method.
  449. float h[4] = { 0.f };
  450. for (uint8_t gauss_iter = 0; gauss_iter < 100; ++gauss_iter) {
  451. h[0] = (b[0] - A[0][1] * h[1] - A[0][2] * h[2] - A[0][3] * h[3]) / A[0][0];
  452. h[1] = (b[1] - A[1][0] * h[0] - A[1][2] * h[2] - A[1][3] * h[3]) / A[1][1];
  453. h[2] = (b[2] - A[2][0] * h[0] - A[2][1] * h[1] - A[2][3] * h[3]) / A[2][2];
  454. h[3] = (b[3] - A[3][0] * h[0] - A[3][1] * h[1] - A[3][2] * h[2]) / A[3][3];
  455. }
  456. // and update the current position with h.
  457. // It may be better to use the Levenberg-Marquart method here,
  458. // but because we are very close to the solution alread,
  459. // the simple Gauss-Newton non-linear Least Squares method works well enough.
  460. cntr[0] += h[0];
  461. cntr[1] += h[1];
  462. a1 += h[2];
  463. a2 += h[3];
  464. if (verbosity_level >= 20) {
  465. SERIAL_ECHOPGM("iteration: ");
  466. MYSERIAL.print(iter, 0);
  467. SERIAL_ECHOPGM("correction vector: ");
  468. MYSERIAL.print(h[0], 5);
  469. SERIAL_ECHOPGM(", ");
  470. MYSERIAL.print(h[1], 5);
  471. SERIAL_ECHOPGM(", ");
  472. MYSERIAL.print(h[2], 5);
  473. SERIAL_ECHOPGM(", ");
  474. MYSERIAL.print(h[3], 5);
  475. SERIAL_ECHOLNPGM("");
  476. SERIAL_ECHOPGM("corrected x/y: ");
  477. MYSERIAL.print(cntr[0], 5);
  478. SERIAL_ECHOPGM(", ");
  479. MYSERIAL.print(cntr[0], 5);
  480. SERIAL_ECHOLNPGM("");
  481. SERIAL_ECHOPGM("corrected angles: ");
  482. MYSERIAL.print(180.f * a1 / M_PI, 5);
  483. SERIAL_ECHOPGM(", ");
  484. MYSERIAL.print(180.f * a2 / M_PI, 5);
  485. SERIAL_ECHOLNPGM("");
  486. }
  487. }
  488. vec_x[0] = cos(a1) * MACHINE_AXIS_SCALE_X;
  489. vec_x[1] = sin(a1) * MACHINE_AXIS_SCALE_X;
  490. vec_y[0] = -sin(a2) * MACHINE_AXIS_SCALE_Y;
  491. vec_y[1] = cos(a2) * MACHINE_AXIS_SCALE_Y;
  492. if (verbosity_level >= 1) {
  493. SERIAL_ECHOPGM("correction angles: ");
  494. MYSERIAL.print(180.f * a1 / M_PI, 5);
  495. SERIAL_ECHOPGM(", ");
  496. MYSERIAL.print(180.f * a2 / M_PI, 5);
  497. SERIAL_ECHOLNPGM("");
  498. }
  499. if (verbosity_level >= 10) {
  500. // Show the adjusted state, before the fitting.
  501. SERIAL_ECHOPGM("X vector new, inverted: ");
  502. MYSERIAL.print(vec_x[0], 5);
  503. SERIAL_ECHOPGM(", ");
  504. MYSERIAL.print(vec_x[1], 5);
  505. SERIAL_ECHOLNPGM("");
  506. SERIAL_ECHOPGM("Y vector new, inverted: ");
  507. MYSERIAL.print(vec_y[0], 5);
  508. SERIAL_ECHOPGM(", ");
  509. MYSERIAL.print(vec_y[1], 5);
  510. SERIAL_ECHOLNPGM("");
  511. SERIAL_ECHOPGM("center new, inverted: ");
  512. MYSERIAL.print(cntr[0], 5);
  513. SERIAL_ECHOPGM(", ");
  514. MYSERIAL.print(cntr[1], 5);
  515. SERIAL_ECHOLNPGM("");
  516. delay_keep_alive(100);
  517. SERIAL_ECHOLNPGM("Error after correction: ");
  518. for (uint8_t i = 0; i < npts; ++i) {
  519. float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1] + cntr[0];
  520. float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1] + cntr[1];
  521. SERIAL_ECHOPGM("point #");
  522. MYSERIAL.print(int(i));
  523. SERIAL_ECHOPGM(" measured: (");
  524. MYSERIAL.print(measured_pts[i * 2], 5);
  525. SERIAL_ECHOPGM(", ");
  526. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  527. SERIAL_ECHOPGM("); corrected: (");
  528. MYSERIAL.print(x, 5);
  529. SERIAL_ECHOPGM(", ");
  530. MYSERIAL.print(y, 5);
  531. SERIAL_ECHOPGM("); target: (");
  532. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  533. SERIAL_ECHOPGM(", ");
  534. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  535. SERIAL_ECHOPGM("), error: ");
  536. MYSERIAL.print(sqrt(sqr(pgm_read_float(true_pts + i * 2) - x) + sqr(pgm_read_float(true_pts + i * 2 + 1) - y)));
  537. SERIAL_ECHOLNPGM("");
  538. }
  539. }
  540. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  541. {
  542. float d = vec_x[0] * vec_y[1] - vec_x[1] * vec_y[0];
  543. float Ainv[2][2] = {
  544. { vec_y[1] / d, -vec_y[0] / d },
  545. { -vec_x[1] / d, vec_x[0] / d }
  546. };
  547. float cntrInv[2] = {
  548. -Ainv[0][0] * cntr[0] - Ainv[0][1] * cntr[1],
  549. -Ainv[1][0] * cntr[0] - Ainv[1][1] * cntr[1]
  550. };
  551. vec_x[0] = Ainv[0][0];
  552. vec_x[1] = Ainv[1][0];
  553. vec_y[0] = Ainv[0][1];
  554. vec_y[1] = Ainv[1][1];
  555. cntr[0] = cntrInv[0];
  556. cntr[1] = cntrInv[1];
  557. }
  558. if (verbosity_level >= 1) {
  559. // Show the adjusted state, before the fitting.
  560. SERIAL_ECHOPGM("X vector, adjusted: ");
  561. MYSERIAL.print(vec_x[0], 5);
  562. SERIAL_ECHOPGM(", ");
  563. MYSERIAL.print(vec_x[1], 5);
  564. SERIAL_ECHOLNPGM("");
  565. SERIAL_ECHOPGM("Y vector, adjusted: ");
  566. MYSERIAL.print(vec_y[0], 5);
  567. SERIAL_ECHOPGM(", ");
  568. MYSERIAL.print(vec_y[1], 5);
  569. SERIAL_ECHOLNPGM("");
  570. SERIAL_ECHOPGM("center, adjusted: ");
  571. MYSERIAL.print(cntr[0], 5);
  572. SERIAL_ECHOPGM(", ");
  573. MYSERIAL.print(cntr[1], 5);
  574. SERIAL_ECHOLNPGM("");
  575. delay_keep_alive(100);
  576. }
  577. if (verbosity_level >= 2) {
  578. SERIAL_ECHOLNPGM("Difference after correction: ");
  579. for (uint8_t i = 0; i < npts; ++i) {
  580. 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];
  581. 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];
  582. SERIAL_ECHOPGM("point #");
  583. MYSERIAL.print(int(i));
  584. SERIAL_ECHOPGM("measured: (");
  585. MYSERIAL.print(measured_pts[i * 2], 5);
  586. SERIAL_ECHOPGM(", ");
  587. MYSERIAL.print(measured_pts[i * 2 + 1], 5);
  588. SERIAL_ECHOPGM("); measured-corrected: (");
  589. MYSERIAL.print(x, 5);
  590. SERIAL_ECHOPGM(", ");
  591. MYSERIAL.print(y, 5);
  592. SERIAL_ECHOPGM("); target: (");
  593. MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
  594. SERIAL_ECHOPGM(", ");
  595. MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
  596. SERIAL_ECHOPGM("), error: ");
  597. MYSERIAL.print(sqrt(sqr(measured_pts[i * 2] - x) + sqr(measured_pts[i * 2 + 1] - y)));
  598. SERIAL_ECHOLNPGM("");
  599. }
  600. delay_keep_alive(100);
  601. }
  602. return true;
  603. }
  604. #endif
  605. void reset_bed_offset_and_skew()
  606. {
  607. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+0), 0x0FFFFFFFF);
  608. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+4), 0x0FFFFFFFF);
  609. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +0), 0x0FFFFFFFF);
  610. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +4), 0x0FFFFFFFF);
  611. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +0), 0x0FFFFFFFF);
  612. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +4), 0x0FFFFFFFF);
  613. }
  614. void world2machine_reset()
  615. {
  616. // Identity transformation.
  617. world2machine_rotation_and_skew[0][0] = 1.f;
  618. world2machine_rotation_and_skew[0][1] = 0.f;
  619. world2machine_rotation_and_skew[1][0] = 0.f;
  620. world2machine_rotation_and_skew[1][1] = 1.f;
  621. // Zero shift.
  622. world2machine_shift[0] = 0.f;
  623. world2machine_shift[1] = 0.f;
  624. }
  625. static inline bool vec_undef(const float v[2])
  626. {
  627. const uint32_t *vx = (const uint32_t*)v;
  628. return vx[0] == 0x0FFFFFFFF || vx[1] == 0x0FFFFFFFF;
  629. }
  630. void world2machine_initialize()
  631. {
  632. float cntr[2] = {
  633. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0)),
  634. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4))
  635. };
  636. float vec_x[2] = {
  637. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0)),
  638. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4))
  639. };
  640. float vec_y[2] = {
  641. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0)),
  642. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4))
  643. };
  644. bool reset = false;
  645. if (vec_undef(cntr) || vec_undef(vec_x) || vec_undef(vec_y)) {
  646. SERIAL_ECHOLNPGM("Undefined bed correction matrix.");
  647. reset = true;
  648. }
  649. else {
  650. // Length of the vec_x shall be close to unity.
  651. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  652. if (l < 0.9 || l > 1.1) {
  653. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  654. reset = true;
  655. }
  656. // Length of the vec_y shall be close to unity.
  657. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  658. if (l < 0.9 || l > 1.1) {
  659. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  660. reset = true;
  661. }
  662. // Correction of the zero point shall be reasonably small.
  663. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  664. if (l > 15.f) {
  665. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Shift out of range.");
  666. reset = true;
  667. }
  668. // vec_x and vec_y shall be nearly perpendicular.
  669. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  670. if (fabs(l) > 0.1f) {
  671. SERIAL_ECHOLNPGM("Invalid bed correction matrix. X/Y axes are far from being perpendicular.");
  672. reset = true;
  673. }
  674. }
  675. if (reset) {
  676. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. Resetting to identity.");
  677. reset_bed_offset_and_skew();
  678. world2machine_reset();
  679. } else {
  680. world2machine_rotation_and_skew[0][0] = vec_x[0];
  681. world2machine_rotation_and_skew[1][0] = vec_x[1];
  682. world2machine_rotation_and_skew[0][1] = vec_y[0];
  683. world2machine_rotation_and_skew[1][1] = vec_y[1];
  684. world2machine_shift[0] = cntr[0];
  685. world2machine_shift[1] = cntr[1];
  686. }
  687. }
  688. // When switching from absolute to corrected coordinates,
  689. // this will get the absolute coordinates from the servos,
  690. // applies the inverse world2machine transformation
  691. // and stores the result into current_position[x,y].
  692. void world2machine_update_current()
  693. {
  694. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  695. 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];
  696. float Ainv[2][2] = {
  697. { world2machine_rotation_and_skew[1][1] / d, - world2machine_rotation_and_skew[0][1] / d },
  698. { - world2machine_rotation_and_skew[1][0] / d, world2machine_rotation_and_skew[0][0] / d }
  699. };
  700. float x = current_position[X_AXIS] - world2machine_shift[0];
  701. float y = current_position[Y_AXIS] - world2machine_shift[1];
  702. current_position[X_AXIS] = Ainv[0][0] * x + Ainv[0][1] * y;
  703. current_position[Y_AXIS] = Ainv[1][0] * x + Ainv[1][1] * y;
  704. }
  705. static inline void go_xyz(float x, float y, float z, float fr)
  706. {
  707. plan_buffer_line(x, y, z, current_position[E_AXIS], fr, active_extruder);
  708. st_synchronize();
  709. }
  710. static inline void go_xy(float x, float y, float fr)
  711. {
  712. plan_buffer_line(x, y, current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  713. st_synchronize();
  714. }
  715. static inline void go_to_current(float fr)
  716. {
  717. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  718. st_synchronize();
  719. }
  720. static inline void update_current_position_xyz()
  721. {
  722. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  723. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  724. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  725. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  726. }
  727. static inline void update_current_position_z()
  728. {
  729. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  730. plan_set_z_position(current_position[Z_AXIS]);
  731. }
  732. // At the current position, find the Z stop.
  733. inline void find_bed_induction_sensor_point_z()
  734. {
  735. bool endstops_enabled = enable_endstops(true);
  736. bool endstop_z_enabled = enable_z_endstop(false);
  737. // move down until you find the bed
  738. current_position[Z_AXIS] = -10;
  739. go_to_current(homing_feedrate[Z_AXIS]/60);
  740. // we have to let the planner know where we are right now as it is not where we said to go.
  741. update_current_position_z();
  742. // move up the retract distance
  743. current_position[Z_AXIS] += home_retract_mm_ext(Z_AXIS);
  744. go_to_current(homing_feedrate[Z_AXIS]/60);
  745. // move back down slowly to find bed
  746. current_position[Z_AXIS] -= home_retract_mm_ext(Z_AXIS) * 2;
  747. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  748. // we have to let the planner know where we are right now as it is not where we said to go.
  749. update_current_position_z();
  750. enable_endstops(endstops_enabled);
  751. enable_z_endstop(endstop_z_enabled);
  752. }
  753. // Search around the current_position[X,Y],
  754. // look for the induction sensor response.
  755. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  756. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  757. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (6.f)
  758. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  759. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.5f)
  760. inline bool find_bed_induction_sensor_point_xy()
  761. {
  762. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  763. bool found = false;
  764. {
  765. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  766. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  767. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  768. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  769. uint8_t nsteps_y;
  770. uint8_t i;
  771. if (x0 < X_MIN_POS)
  772. x0 = X_MIN_POS;
  773. if (x1 > X_MAX_POS)
  774. x1 = X_MAX_POS;
  775. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  776. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  777. if (y1 > Y_MAX_POS)
  778. y1 = Y_MAX_POS;
  779. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  780. enable_endstops(false);
  781. bool dir_positive = true;
  782. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  783. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  784. // Continously lower the Z axis.
  785. endstops_hit_on_purpose();
  786. enable_z_endstop(true);
  787. while (current_position[Z_AXIS] > -10.f) {
  788. // Do nsteps_y zig-zag movements.
  789. current_position[Y_AXIS] = y0;
  790. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++ i) {
  791. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  792. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  793. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  794. dir_positive = ! dir_positive;
  795. if (endstop_z_hit_on_purpose())
  796. goto endloop;
  797. }
  798. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++ i) {
  799. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  800. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  801. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  802. dir_positive = ! dir_positive;
  803. if (endstop_z_hit_on_purpose())
  804. goto endloop;
  805. }
  806. }
  807. endloop:
  808. // SERIAL_ECHOLN("First hit");
  809. // we have to let the planner know where we are right now as it is not where we said to go.
  810. update_current_position_xyz();
  811. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  812. for (int8_t iter = 0; iter < 3; ++ iter) {
  813. if (iter > 0) {
  814. // Slightly lower the Z axis to get a reliable trigger.
  815. current_position[Z_AXIS] -= 0.02f;
  816. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  817. }
  818. // Do nsteps_y zig-zag movements.
  819. float a, b;
  820. enable_endstops(false);
  821. enable_z_endstop(false);
  822. current_position[Y_AXIS] = y0;
  823. go_xy(x0, current_position[Y_AXIS], feedrate);
  824. enable_z_endstop(true);
  825. found = false;
  826. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++ i, dir_positive = ! dir_positive) {
  827. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  828. if (endstop_z_hit_on_purpose()) {
  829. found = true;
  830. break;
  831. }
  832. }
  833. update_current_position_xyz();
  834. if (! found) {
  835. // SERIAL_ECHOLN("Search in Y - not found");
  836. continue;
  837. }
  838. // SERIAL_ECHOLN("Search in Y - found");
  839. a = current_position[Y_AXIS];
  840. enable_z_endstop(false);
  841. current_position[Y_AXIS] = y1;
  842. go_xy(x0, current_position[Y_AXIS], feedrate);
  843. enable_z_endstop(true);
  844. found = false;
  845. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++ i, dir_positive = ! dir_positive) {
  846. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  847. if (endstop_z_hit_on_purpose()) {
  848. found = true;
  849. break;
  850. }
  851. }
  852. update_current_position_xyz();
  853. if (! found) {
  854. // SERIAL_ECHOLN("Search in Y2 - not found");
  855. continue;
  856. }
  857. // SERIAL_ECHOLN("Search in Y2 - found");
  858. b = current_position[Y_AXIS];
  859. current_position[Y_AXIS] = 0.5f * (a + b);
  860. // Search in the X direction along a cross.
  861. found = false;
  862. enable_z_endstop(false);
  863. go_xy(x0, current_position[Y_AXIS], feedrate);
  864. enable_z_endstop(true);
  865. go_xy(x1, current_position[Y_AXIS], feedrate);
  866. update_current_position_xyz();
  867. if (! endstop_z_hit_on_purpose()) {
  868. // SERIAL_ECHOLN("Search X span 0 - not found");
  869. continue;
  870. }
  871. // SERIAL_ECHOLN("Search X span 0 - found");
  872. a = current_position[X_AXIS];
  873. enable_z_endstop(false);
  874. go_xy(x1, current_position[Y_AXIS], feedrate);
  875. enable_z_endstop(true);
  876. go_xy(x0, current_position[Y_AXIS], feedrate);
  877. update_current_position_xyz();
  878. if (! endstop_z_hit_on_purpose()) {
  879. // SERIAL_ECHOLN("Search X span 1 - not found");
  880. continue;
  881. }
  882. // SERIAL_ECHOLN("Search X span 1 - found");
  883. b = current_position[X_AXIS];
  884. // Go to the center.
  885. enable_z_endstop(false);
  886. current_position[X_AXIS] = 0.5f * (a + b);
  887. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  888. found = true;
  889. #if 1
  890. // Search in the Y direction along a cross.
  891. found = false;
  892. enable_z_endstop(false);
  893. go_xy(current_position[X_AXIS], y0, feedrate);
  894. enable_z_endstop(true);
  895. go_xy(current_position[X_AXIS], y1, feedrate);
  896. update_current_position_xyz();
  897. if (! endstop_z_hit_on_purpose()) {
  898. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  899. continue;
  900. }
  901. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  902. a = current_position[Y_AXIS];
  903. enable_z_endstop(false);
  904. go_xy(current_position[X_AXIS], y1, feedrate);
  905. enable_z_endstop(true);
  906. go_xy(current_position[X_AXIS], y0, feedrate);
  907. update_current_position_xyz();
  908. if (! endstop_z_hit_on_purpose()) {
  909. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  910. continue;
  911. }
  912. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  913. b = current_position[Y_AXIS];
  914. // Go to the center.
  915. enable_z_endstop(false);
  916. current_position[Y_AXIS] = 0.5f * (a + b);
  917. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  918. found = true;
  919. #endif
  920. break;
  921. }
  922. }
  923. enable_z_endstop(false);
  924. return found;
  925. }
  926. // Search around the current_position[X,Y,Z].
  927. // It is expected, that the induction sensor is switched on at the current position.
  928. // Look around this center point by painting a star around the point.
  929. inline bool improve_bed_induction_sensor_point()
  930. {
  931. static const float search_radius = 8.f;
  932. bool endstops_enabled = enable_endstops(false);
  933. bool endstop_z_enabled = enable_z_endstop(false);
  934. bool found = false;
  935. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  936. float center_old_x = current_position[X_AXIS];
  937. float center_old_y = current_position[Y_AXIS];
  938. float center_x = 0.f;
  939. float center_y = 0.f;
  940. for (uint8_t iter = 0; iter < 4; ++ iter) {
  941. switch (iter) {
  942. case 0:
  943. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  944. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  945. break;
  946. case 1:
  947. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  948. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  949. break;
  950. case 2:
  951. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  952. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  953. break;
  954. case 3:
  955. default:
  956. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  957. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  958. break;
  959. }
  960. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  961. float vx = destination[X_AXIS] - center_old_x;
  962. float vy = destination[Y_AXIS] - center_old_y;
  963. float l = sqrt(vx*vx+vy*vy);
  964. float t;
  965. if (destination[X_AXIS] < X_MIN_POS) {
  966. // Exiting the bed at xmin.
  967. t = (center_x - X_MIN_POS) / l;
  968. destination[X_AXIS] = X_MIN_POS;
  969. destination[Y_AXIS] = center_old_y + t * vy;
  970. } else if (destination[X_AXIS] > X_MAX_POS) {
  971. // Exiting the bed at xmax.
  972. t = (X_MAX_POS - center_x) / l;
  973. destination[X_AXIS] = X_MAX_POS;
  974. destination[Y_AXIS] = center_old_y + t * vy;
  975. }
  976. if (destination[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION) {
  977. // Exiting the bed at ymin.
  978. t = (center_y - Y_MIN_POS_FOR_BED_CALIBRATION) / l;
  979. destination[X_AXIS] = center_old_x + t * vx;
  980. destination[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  981. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  982. // Exiting the bed at xmax.
  983. t = (Y_MAX_POS - center_y) / l;
  984. destination[X_AXIS] = center_old_x + t * vx;
  985. destination[Y_AXIS] = Y_MAX_POS;
  986. }
  987. // Move away from the measurement point.
  988. enable_endstops(false);
  989. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  990. // Move towards the measurement point, until the induction sensor triggers.
  991. enable_endstops(true);
  992. go_xy(center_old_x, center_old_y, feedrate);
  993. update_current_position_xyz();
  994. // if (! endstop_z_hit_on_purpose()) return false;
  995. center_x += current_position[X_AXIS];
  996. center_y += current_position[Y_AXIS];
  997. }
  998. // Calculate the new center, move to the new center.
  999. center_x /= 4.f;
  1000. center_y /= 4.f;
  1001. current_position[X_AXIS] = center_x;
  1002. current_position[Y_AXIS] = center_y;
  1003. enable_endstops(false);
  1004. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1005. enable_endstops(endstops_enabled);
  1006. enable_z_endstop(endstop_z_enabled);
  1007. return found;
  1008. }
  1009. // Search around the current_position[X,Y,Z].
  1010. // It is expected, that the induction sensor is switched on at the current position.
  1011. // Look around this center point by painting a star around the point.
  1012. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  1013. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y)
  1014. {
  1015. float center_old_x = current_position[X_AXIS];
  1016. float center_old_y = current_position[Y_AXIS];
  1017. float a, b;
  1018. enable_endstops(false);
  1019. {
  1020. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1021. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1022. if (x0 < X_MIN_POS)
  1023. x0 = X_MIN_POS;
  1024. if (x1 > X_MAX_POS)
  1025. x1 = X_MAX_POS;
  1026. // Search in the X direction along a cross.
  1027. enable_z_endstop(false);
  1028. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1029. enable_z_endstop(true);
  1030. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1031. update_current_position_xyz();
  1032. if (! endstop_z_hit_on_purpose()) {
  1033. current_position[X_AXIS] = center_old_x;
  1034. goto canceled;
  1035. }
  1036. a = current_position[X_AXIS];
  1037. enable_z_endstop(false);
  1038. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1039. enable_z_endstop(true);
  1040. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1041. update_current_position_xyz();
  1042. if (! endstop_z_hit_on_purpose()) {
  1043. current_position[X_AXIS] = center_old_x;
  1044. goto canceled;
  1045. }
  1046. b = current_position[X_AXIS];
  1047. // Go to the center.
  1048. enable_z_endstop(false);
  1049. current_position[X_AXIS] = 0.5f * (a + b);
  1050. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1051. }
  1052. {
  1053. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1054. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1055. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1056. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1057. if (y1 > Y_MAX_POS)
  1058. y1 = Y_MAX_POS;
  1059. // Search in the Y direction along a cross.
  1060. enable_z_endstop(false);
  1061. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1062. if (lift_z_on_min_y) {
  1063. // The first row of points are very close to the end stop.
  1064. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  1065. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  1066. // and go back.
  1067. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  1068. }
  1069. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  1070. // Already triggering before we started the move.
  1071. // Shift the trigger point slightly outwards.
  1072. // a = current_position[Y_AXIS] - 1.5f;
  1073. a = current_position[Y_AXIS];
  1074. } else {
  1075. enable_z_endstop(true);
  1076. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1077. update_current_position_xyz();
  1078. if (! endstop_z_hit_on_purpose()) {
  1079. current_position[Y_AXIS] = center_old_y;
  1080. goto canceled;
  1081. }
  1082. a = current_position[Y_AXIS];
  1083. }
  1084. enable_z_endstop(false);
  1085. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1086. enable_z_endstop(true);
  1087. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1088. update_current_position_xyz();
  1089. if (! endstop_z_hit_on_purpose()) {
  1090. current_position[Y_AXIS] = center_old_y;
  1091. goto canceled;
  1092. }
  1093. b = current_position[Y_AXIS];
  1094. // Go to the center.
  1095. enable_z_endstop(false);
  1096. current_position[Y_AXIS] = 0.5f * (a + b);
  1097. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1098. }
  1099. return true;
  1100. canceled:
  1101. // Go back to the center.
  1102. enable_z_endstop(false);
  1103. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1104. return false;
  1105. }
  1106. // Searching the front points, where one cannot move the sensor head in front of the sensor point.
  1107. // Searching in a zig-zag movement in a plane for the maximum width of the response.
  1108. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS (4.f)
  1109. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y (0.1f)
  1110. inline bool improve_bed_induction_sensor_point3(int verbosity_level)
  1111. {
  1112. float center_old_x = current_position[X_AXIS];
  1113. float center_old_y = current_position[Y_AXIS];
  1114. float a, b;
  1115. {
  1116. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1117. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1118. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1119. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1120. float y = y0;
  1121. if (x0 < X_MIN_POS)
  1122. x0 = X_MIN_POS;
  1123. if (x1 > X_MAX_POS)
  1124. x1 = X_MAX_POS;
  1125. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1126. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1127. if (y1 > Y_MAX_POS)
  1128. y1 = Y_MAX_POS;
  1129. if (verbosity_level >= 20) {
  1130. SERIAL_ECHOPGM("Initial position: ");
  1131. SERIAL_ECHO(center_old_x);
  1132. SERIAL_ECHOPGM(", ");
  1133. SERIAL_ECHO(center_old_y);
  1134. SERIAL_ECHOLNPGM("");
  1135. }
  1136. // Search in the positive Y direction, until a maximum diameter is found.
  1137. float dmax = 0.f;
  1138. float xmax1 = 0.f;
  1139. float xmax2 = 0.f;
  1140. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1141. enable_z_endstop(false);
  1142. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1143. enable_z_endstop(true);
  1144. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1145. update_current_position_xyz();
  1146. if (! endstop_z_hit_on_purpose()) {
  1147. continue;
  1148. // SERIAL_PROTOCOLPGM("Failed 1\n");
  1149. // current_position[X_AXIS] = center_old_x;
  1150. // goto canceled;
  1151. }
  1152. a = current_position[X_AXIS];
  1153. enable_z_endstop(false);
  1154. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1155. enable_z_endstop(true);
  1156. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1157. update_current_position_xyz();
  1158. if (! endstop_z_hit_on_purpose()) {
  1159. continue;
  1160. // SERIAL_PROTOCOLPGM("Failed 2\n");
  1161. // current_position[X_AXIS] = center_old_x;
  1162. // goto canceled;
  1163. }
  1164. b = current_position[X_AXIS];
  1165. if (verbosity_level > 20) {
  1166. SERIAL_ECHOPGM("Measured left ");
  1167. MYSERIAL.print(a, 5);
  1168. SERIAL_ECHOPGM("right ");
  1169. MYSERIAL.print(b, 5);
  1170. SERIAL_ECHOPGM(", ");
  1171. MYSERIAL.print(y, 5);
  1172. SERIAL_ECHOPGM(", ");
  1173. MYSERIAL.print(current_position[Z_AXIS], 5);
  1174. SERIAL_ECHOLNPGM("");
  1175. }
  1176. float d = b - a;
  1177. if (d > dmax) {
  1178. xmax1 = 0.5f * (a + b);
  1179. dmax = d;
  1180. } else if (dmax > 0.) {
  1181. y0 = y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1182. break;
  1183. }
  1184. }
  1185. if (dmax == 0.) {
  1186. SERIAL_PROTOCOLPGM("failed - not found\n");
  1187. goto canceled;
  1188. }
  1189. SERIAL_PROTOCOLPGM("ok 1\n");
  1190. // Search in the negative Y direction, until a maximum diameter is found.
  1191. dmax = 0.;
  1192. if (y0 + 1.f < y1)
  1193. y1 = y0 + 1.f;
  1194. for (float y = y1; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1195. enable_z_endstop(false);
  1196. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1197. enable_z_endstop(true);
  1198. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1199. update_current_position_xyz();
  1200. if (! endstop_z_hit_on_purpose()) {
  1201. continue;
  1202. /*
  1203. current_position[X_AXIS] = center_old_x;
  1204. SERIAL_PROTOCOLPGM("Failed 3\n");
  1205. goto canceled;
  1206. */
  1207. }
  1208. a = current_position[X_AXIS];
  1209. enable_z_endstop(false);
  1210. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1211. enable_z_endstop(true);
  1212. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1213. update_current_position_xyz();
  1214. if (! endstop_z_hit_on_purpose()) {
  1215. continue;
  1216. /*
  1217. current_position[X_AXIS] = center_old_x;
  1218. SERIAL_PROTOCOLPGM("Failed 4\n");
  1219. goto canceled;
  1220. */
  1221. }
  1222. b = current_position[X_AXIS];
  1223. if (verbosity_level > 20) {
  1224. SERIAL_ECHOPGM("Measured left ");
  1225. MYSERIAL.print(a, 5);
  1226. SERIAL_ECHOPGM("right ");
  1227. MYSERIAL.print(b, 5);
  1228. SERIAL_ECHOPGM(", ");
  1229. MYSERIAL.print(y, 5);
  1230. SERIAL_ECHOPGM(", ");
  1231. MYSERIAL.print(current_position[Z_AXIS], 5);
  1232. SERIAL_ECHOLNPGM("");
  1233. }
  1234. float d = b - a;
  1235. if (d > dmax) {
  1236. xmax2 = 0.5f * (a + b);
  1237. dmax = d;
  1238. } else if (dmax > 0.) {
  1239. y1 = y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1240. break;
  1241. }
  1242. }
  1243. // SERIAL_PROTOCOLPGM("ok 2\n");
  1244. // Go to the center.
  1245. enable_z_endstop(false);
  1246. if (dmax == 0.f) {
  1247. // Found only the point going from ymin to ymax.
  1248. current_position[X_AXIS] = xmax1;
  1249. current_position[Y_AXIS] = y0;
  1250. } else {
  1251. // Both points found (from ymin to ymax and from ymax to ymin).
  1252. float p = 0.5f;
  1253. // If the first hit was on the machine boundary,
  1254. // give it a higher weight.
  1255. if (y0 == Y_MIN_POS_FOR_BED_CALIBRATION)
  1256. p = 0.75f;
  1257. current_position[X_AXIS] = p * xmax1 + (1.f - p) * xmax2;
  1258. current_position[Y_AXIS] = p * y0 + (1.f - p) * y1;
  1259. }
  1260. if (verbosity_level >= 20) {
  1261. SERIAL_ECHOPGM("Adjusted position: ");
  1262. SERIAL_ECHO(current_position[X_AXIS]);
  1263. SERIAL_ECHOPGM(", ");
  1264. SERIAL_ECHO(current_position[Y_AXIS]);
  1265. SERIAL_ECHOLNPGM("");
  1266. }
  1267. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1268. // delay_keep_alive(3000);
  1269. }
  1270. // Try yet to improve the X position.
  1271. {
  1272. float x0 = current_position[X_AXIS] - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1273. float x1 = current_position[X_AXIS] + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1274. if (x0 < X_MIN_POS)
  1275. x0 = X_MIN_POS;
  1276. if (x1 > X_MAX_POS)
  1277. x1 = X_MAX_POS;
  1278. // Search in the X direction along a cross.
  1279. enable_z_endstop(false);
  1280. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1281. enable_z_endstop(true);
  1282. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1283. update_current_position_xyz();
  1284. if (! endstop_z_hit_on_purpose()) {
  1285. current_position[X_AXIS] = center_old_x;
  1286. goto canceled;
  1287. }
  1288. a = current_position[X_AXIS];
  1289. enable_z_endstop(false);
  1290. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1291. enable_z_endstop(true);
  1292. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1293. update_current_position_xyz();
  1294. if (! endstop_z_hit_on_purpose()) {
  1295. current_position[X_AXIS] = center_old_x;
  1296. goto canceled;
  1297. }
  1298. b = current_position[X_AXIS];
  1299. // Go to the center.
  1300. enable_z_endstop(false);
  1301. current_position[X_AXIS] = 0.5f * (a + b);
  1302. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1303. }
  1304. return true;
  1305. canceled:
  1306. // Go back to the center.
  1307. enable_z_endstop(false);
  1308. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1309. return false;
  1310. }
  1311. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  1312. // write the trigger coordinates to the serial line.
  1313. // Useful for visualizing the behavior of the bed induction detector.
  1314. inline void scan_bed_induction_sensor_point()
  1315. {
  1316. float center_old_x = current_position[X_AXIS];
  1317. float center_old_y = current_position[Y_AXIS];
  1318. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1319. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1320. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1321. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1322. float y = y0;
  1323. if (x0 < X_MIN_POS)
  1324. x0 = X_MIN_POS;
  1325. if (x1 > X_MAX_POS)
  1326. x1 = X_MAX_POS;
  1327. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1328. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1329. if (y1 > Y_MAX_POS)
  1330. y1 = Y_MAX_POS;
  1331. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1332. enable_z_endstop(false);
  1333. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1334. enable_z_endstop(true);
  1335. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1336. update_current_position_xyz();
  1337. if (endstop_z_hit_on_purpose()) {
  1338. SERIAL_ECHOPGM("Measured left: ");
  1339. MYSERIAL.print(current_position[X_AXIS], 5);
  1340. SERIAL_ECHOPGM(", ");
  1341. MYSERIAL.print(y, 5);
  1342. SERIAL_ECHOPGM(", ");
  1343. MYSERIAL.print(current_position[Z_AXIS], 5);
  1344. SERIAL_ECHOLNPGM("");
  1345. }
  1346. enable_z_endstop(false);
  1347. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1348. enable_z_endstop(true);
  1349. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1350. update_current_position_xyz();
  1351. if (endstop_z_hit_on_purpose()) {
  1352. SERIAL_ECHOPGM("Measured right: ");
  1353. MYSERIAL.print(current_position[X_AXIS], 5);
  1354. SERIAL_ECHOPGM(", ");
  1355. MYSERIAL.print(y, 5);
  1356. SERIAL_ECHOPGM(", ");
  1357. MYSERIAL.print(current_position[Z_AXIS], 5);
  1358. SERIAL_ECHOLNPGM("");
  1359. }
  1360. }
  1361. enable_z_endstop(false);
  1362. current_position[X_AXIS] = center_old_x;
  1363. current_position[Y_AXIS] = center_old_y;
  1364. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1365. }
  1366. #define MESH_BED_CALIBRATION_SHOW_LCD
  1367. bool find_bed_offset_and_skew(int8_t verbosity_level)
  1368. {
  1369. // Don't let the manage_inactivity() function remove power from the motors.
  1370. refresh_cmd_timeout();
  1371. // Reusing the z_values memory for the measurement cache.
  1372. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  1373. float *pts = &mbl.z_values[0][0];
  1374. float *vec_x = pts + 2 * 4;
  1375. float *vec_y = vec_x + 2;
  1376. float *cntr = vec_y + 2;
  1377. memset(pts, 0, sizeof(float) * 7 * 7);
  1378. // SERIAL_ECHOLNPGM("find_bed_offset_and_skew verbosity level: ");
  1379. // SERIAL_ECHO(int(verbosity_level));
  1380. // SERIAL_ECHOPGM("");
  1381. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1382. lcd_implementation_clear();
  1383. lcd_print_at_PGM(0, 0, MSG_FIND_BED_OFFSET_AND_SKEW_LINE1);
  1384. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1385. // Collect the rear 2x3 points.
  1386. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  1387. for (int k = 0; k < 4; ++ k) {
  1388. // Don't let the manage_inactivity() function remove power from the motors.
  1389. refresh_cmd_timeout();
  1390. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1391. lcd_print_at_PGM(0, 1, MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
  1392. lcd_implementation_print_at(0, 2, k+1);
  1393. lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE3);
  1394. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1395. float *pt = pts + k * 2;
  1396. // Go up to z_initial.
  1397. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  1398. if (verbosity_level >= 20) {
  1399. // Go to Y0, wait, then go to Y-4.
  1400. current_position[Y_AXIS] = 0.f;
  1401. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1402. SERIAL_ECHOLNPGM("At Y0");
  1403. delay_keep_alive(5000);
  1404. current_position[Y_AXIS] = Y_MIN_POS;
  1405. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1406. SERIAL_ECHOLNPGM("At Y-4");
  1407. delay_keep_alive(5000);
  1408. }
  1409. // Go to the measurement point position.
  1410. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+k*2);
  1411. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+k*2+1);
  1412. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1413. if (verbosity_level >= 10)
  1414. delay_keep_alive(3000);
  1415. if (! find_bed_induction_sensor_point_xy())
  1416. return false;
  1417. find_bed_induction_sensor_point_z();
  1418. #if 1
  1419. if (k == 0) {
  1420. // Improve the position of the 1st row sensor points by a zig-zag movement.
  1421. int8_t i = 4;
  1422. for (;;) {
  1423. if (improve_bed_induction_sensor_point3(verbosity_level))
  1424. break;
  1425. if (-- i == 0)
  1426. return false;
  1427. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  1428. current_position[Z_AXIS] -= 0.025f;
  1429. enable_endstops(false);
  1430. enable_z_endstop(false);
  1431. go_to_current(homing_feedrate[Z_AXIS]);
  1432. }
  1433. if (i == 0)
  1434. // not found
  1435. return false;
  1436. }
  1437. #endif
  1438. if (verbosity_level >= 10)
  1439. delay_keep_alive(3000);
  1440. pt[0] = current_position[X_AXIS];
  1441. pt[1] = current_position[Y_AXIS];
  1442. // Start searching for the other points at 3mm above the last point.
  1443. current_position[Z_AXIS] += 3.f;
  1444. cntr[0] += pt[0];
  1445. cntr[1] += pt[1];
  1446. if (verbosity_level >= 10 && k == 0) {
  1447. // Show the zero. Test, whether the Y motor skipped steps.
  1448. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  1449. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1450. delay_keep_alive(3000);
  1451. }
  1452. }
  1453. if (verbosity_level >= 20) {
  1454. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  1455. delay_keep_alive(3000);
  1456. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  1457. // Don't let the manage_inactivity() function remove power from the motors.
  1458. refresh_cmd_timeout();
  1459. // Go to the measurement point.
  1460. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1461. current_position[X_AXIS] = pts[mesh_point*2];
  1462. current_position[Y_AXIS] = pts[mesh_point*2+1];
  1463. go_to_current(homing_feedrate[X_AXIS]/60);
  1464. delay_keep_alive(3000);
  1465. }
  1466. }
  1467. calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  1468. world2machine_rotation_and_skew[0][0] = vec_x[0];
  1469. world2machine_rotation_and_skew[1][0] = vec_x[1];
  1470. world2machine_rotation_and_skew[0][1] = vec_y[0];
  1471. world2machine_rotation_and_skew[1][1] = vec_y[1];
  1472. world2machine_shift[0] = cntr[0];
  1473. world2machine_shift[1] = cntr[1];
  1474. #if 1
  1475. // Fearlessly store the calibration values into the eeprom.
  1476. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  1477. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  1478. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  1479. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  1480. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  1481. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  1482. #endif
  1483. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  1484. world2machine_update_current();
  1485. if (verbosity_level >= 20) {
  1486. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  1487. delay_keep_alive(3000);
  1488. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1489. // Don't let the manage_inactivity() function remove power from the motors.
  1490. refresh_cmd_timeout();
  1491. // Go to the measurement point.
  1492. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1493. current_position[X_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2);
  1494. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2+1);
  1495. go_to_current(homing_feedrate[X_AXIS]/60);
  1496. delay_keep_alive(3000);
  1497. }
  1498. }
  1499. return true;
  1500. }
  1501. bool improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level)
  1502. {
  1503. // Don't let the manage_inactivity() function remove power from the motors.
  1504. refresh_cmd_timeout();
  1505. // Reusing the z_values memory for the measurement cache.
  1506. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  1507. float *pts = &mbl.z_values[0][0];
  1508. float *vec_x = pts + 2 * 9;
  1509. float *vec_y = vec_x + 2;
  1510. float *cntr = vec_y + 2;
  1511. memset(pts, 0, sizeof(float) * 7 * 7);
  1512. // Cache the current correction matrix.
  1513. world2machine_initialize();
  1514. vec_x[0] = world2machine_rotation_and_skew[0][0];
  1515. vec_x[1] = world2machine_rotation_and_skew[1][0];
  1516. vec_y[0] = world2machine_rotation_and_skew[0][1];
  1517. vec_y[1] = world2machine_rotation_and_skew[1][1];
  1518. cntr[0] = world2machine_shift[0];
  1519. cntr[1] = world2machine_shift[1];
  1520. // and reset the correction matrix, so the planner will not do anything.
  1521. world2machine_reset();
  1522. bool endstops_enabled = enable_endstops(false);
  1523. bool endstop_z_enabled = enable_z_endstop(false);
  1524. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1525. lcd_implementation_clear();
  1526. lcd_print_at_PGM(0, 0, MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1);
  1527. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1528. // Collect a matrix of 9x9 points.
  1529. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1530. // Don't let the manage_inactivity() function remove power from the motors.
  1531. refresh_cmd_timeout();
  1532. // Print the decrasing ID of the measurement point.
  1533. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1534. lcd_print_at_PGM(0, 1, MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2);
  1535. lcd_implementation_print_at(0, 2, mesh_point+1);
  1536. lcd_printPGM(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE3);
  1537. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1538. // Move up.
  1539. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  1540. enable_endstops(false);
  1541. enable_z_endstop(false);
  1542. go_to_current(homing_feedrate[Z_AXIS]/60);
  1543. if (verbosity_level >= 20) {
  1544. // Go to Y0, wait, then go to Y-4.
  1545. current_position[Y_AXIS] = 0.f;
  1546. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1547. SERIAL_ECHOLNPGM("At Y0");
  1548. delay_keep_alive(5000);
  1549. current_position[Y_AXIS] = Y_MIN_POS;
  1550. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1551. SERIAL_ECHOLNPGM("At Y-4");
  1552. delay_keep_alive(5000);
  1553. }
  1554. // Go to the measurement point.
  1555. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1556. 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];
  1557. 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];
  1558. // The calibration points are very close to the min Y.
  1559. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  1560. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1561. go_to_current(homing_feedrate[X_AXIS]/60);
  1562. // Find its Z position by running the normal vertical search.
  1563. if (verbosity_level >= 10)
  1564. delay_keep_alive(3000);
  1565. find_bed_induction_sensor_point_z();
  1566. if (verbosity_level >= 10)
  1567. delay_keep_alive(3000);
  1568. // Improve the point position by searching its center in a current plane.
  1569. int8_t n_errors = 3;
  1570. for (int8_t iter = 0; iter < 8; ) {
  1571. if (verbosity_level > 20) {
  1572. SERIAL_ECHOPGM("Improving bed point ");
  1573. SERIAL_ECHO(mesh_point);
  1574. SERIAL_ECHOPGM(", iteration ");
  1575. SERIAL_ECHO(iter);
  1576. SERIAL_ECHOPGM(", z");
  1577. MYSERIAL.print(current_position[Z_AXIS], 5);
  1578. SERIAL_ECHOLNPGM("");
  1579. }
  1580. bool found = false;
  1581. if (mesh_point < 3) {
  1582. // Because the sensor cannot move in front of the first row
  1583. // of the sensor points, the y position cannot be measured
  1584. // by a cross center method.
  1585. // Use a zig-zag search for the first row of the points.
  1586. found = improve_bed_induction_sensor_point3(verbosity_level);
  1587. } else {
  1588. switch (method) {
  1589. case 0: found = improve_bed_induction_sensor_point(); break;
  1590. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 3); break;
  1591. default: break;
  1592. }
  1593. }
  1594. if (found) {
  1595. if (iter > 3) {
  1596. // Average the last 4 measurements.
  1597. pts[mesh_point*2 ] += current_position[X_AXIS];
  1598. pts[mesh_point*2+1] += current_position[Y_AXIS];
  1599. }
  1600. ++ iter;
  1601. } else if (n_errors -- == 0) {
  1602. // Give up.
  1603. goto canceled;
  1604. } else {
  1605. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  1606. current_position[Z_AXIS] -= 0.025f;
  1607. enable_endstops(false);
  1608. enable_z_endstop(false);
  1609. go_to_current(homing_feedrate[Z_AXIS]);
  1610. if (verbosity_level > 20) {
  1611. SERIAL_ECHOPGM("Improving bed point ");
  1612. SERIAL_ECHO(mesh_point);
  1613. SERIAL_ECHOPGM(", iteration ");
  1614. SERIAL_ECHO(iter);
  1615. SERIAL_ECHOPGM(" failed. Lowering z to ");
  1616. MYSERIAL.print(current_position[Z_AXIS], 5);
  1617. SERIAL_ECHOLNPGM("");
  1618. }
  1619. }
  1620. }
  1621. if (verbosity_level >= 10)
  1622. delay_keep_alive(3000);
  1623. }
  1624. // Don't let the manage_inactivity() function remove power from the motors.
  1625. refresh_cmd_timeout();
  1626. // Average the last 4 measurements.
  1627. for (int8_t i = 0; i < 18; ++ i)
  1628. pts[i] *= (1.f/4.f);
  1629. enable_endstops(false);
  1630. enable_z_endstop(false);
  1631. if (verbosity_level >= 10) {
  1632. // Test the positions. Are the positions reproducible?
  1633. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1634. // Don't let the manage_inactivity() function remove power from the motors.
  1635. refresh_cmd_timeout();
  1636. // Go to the measurement point.
  1637. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1638. current_position[X_AXIS] = pts[mesh_point*2];
  1639. current_position[Y_AXIS] = pts[mesh_point*2+1];
  1640. go_to_current(homing_feedrate[X_AXIS]/60);
  1641. delay_keep_alive(3000);
  1642. #if 0
  1643. if (verbosity_level > 20) {
  1644. SERIAL_ECHOPGM("Final measured bed point ");
  1645. SERIAL_ECHO(mesh_point);
  1646. SERIAL_ECHOPGM(": ");
  1647. MYSERIAL.print(current_position[X_AXIS], 5);
  1648. SERIAL_ECHOPGM(", ");
  1649. MYSERIAL.print(current_position[Y_AXIS], 5);
  1650. SERIAL_ECHOLNPGM("");
  1651. }
  1652. #endif
  1653. }
  1654. }
  1655. calculate_machine_skew_and_offset_LS(pts, 9, bed_ref_points, vec_x, vec_y, cntr, verbosity_level);
  1656. world2machine_rotation_and_skew[0][0] = vec_x[0];
  1657. world2machine_rotation_and_skew[1][0] = vec_x[1];
  1658. world2machine_rotation_and_skew[0][1] = vec_y[0];
  1659. world2machine_rotation_and_skew[1][1] = vec_y[1];
  1660. world2machine_shift[0] = cntr[0];
  1661. world2machine_shift[1] = cntr[1];
  1662. #if 1
  1663. // Fearlessly store the calibration values into the eeprom.
  1664. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  1665. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  1666. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  1667. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  1668. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  1669. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  1670. #endif
  1671. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  1672. world2machine_update_current();
  1673. enable_endstops(false);
  1674. enable_z_endstop(false);
  1675. if (verbosity_level >= 10) {
  1676. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  1677. delay_keep_alive(3000);
  1678. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1679. // Don't let the manage_inactivity() function remove power from the motors.
  1680. refresh_cmd_timeout();
  1681. // Go to the measurement point.
  1682. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1683. current_position[X_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2);
  1684. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2+1);
  1685. go_to_current(homing_feedrate[X_AXIS]/60);
  1686. delay_keep_alive(3000);
  1687. #if 0
  1688. if (verbosity_level > 20) {
  1689. SERIAL_ECHOPGM("Final calculated bed point ");
  1690. SERIAL_ECHO(mesh_point);
  1691. SERIAL_ECHOPGM(": ");
  1692. MYSERIAL.print(st_get_position_mm(X_AXIS), 5);
  1693. SERIAL_ECHOPGM(", ");
  1694. MYSERIAL.print(st_get_position_mm(Y_AXIS), 5);
  1695. SERIAL_ECHOLNPGM("");
  1696. }
  1697. #endif
  1698. }
  1699. }
  1700. // Don't let the manage_inactivity() function remove power from the motors.
  1701. refresh_cmd_timeout();
  1702. enable_endstops(endstops_enabled);
  1703. enable_z_endstop(endstop_z_enabled);
  1704. return true;
  1705. canceled:
  1706. // Don't let the manage_inactivity() function remove power from the motors.
  1707. refresh_cmd_timeout();
  1708. // Store the identity matrix to EEPROM.
  1709. reset_bed_offset_and_skew();
  1710. enable_endstops(endstops_enabled);
  1711. enable_z_endstop(endstop_z_enabled);
  1712. return false;
  1713. }
  1714. bool scan_bed_induction_points(int8_t verbosity_level)
  1715. {
  1716. // Don't let the manage_inactivity() function remove power from the motors.
  1717. refresh_cmd_timeout();
  1718. // Reusing the z_values memory for the measurement cache.
  1719. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  1720. float *pts = &mbl.z_values[0][0];
  1721. float *vec_x = pts + 2 * 9;
  1722. float *vec_y = vec_x + 2;
  1723. float *cntr = vec_y + 2;
  1724. memset(pts, 0, sizeof(float) * 7 * 7);
  1725. // Cache the current correction matrix.
  1726. world2machine_initialize();
  1727. vec_x[0] = world2machine_rotation_and_skew[0][0];
  1728. vec_x[1] = world2machine_rotation_and_skew[1][0];
  1729. vec_y[0] = world2machine_rotation_and_skew[0][1];
  1730. vec_y[1] = world2machine_rotation_and_skew[1][1];
  1731. cntr[0] = world2machine_shift[0];
  1732. cntr[1] = world2machine_shift[1];
  1733. // and reset the correction matrix, so the planner will not do anything.
  1734. world2machine_reset();
  1735. bool endstops_enabled = enable_endstops(false);
  1736. bool endstop_z_enabled = enable_z_endstop(false);
  1737. // Collect a matrix of 9x9 points.
  1738. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1739. // Don't let the manage_inactivity() function remove power from the motors.
  1740. refresh_cmd_timeout();
  1741. // Move up.
  1742. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  1743. enable_endstops(false);
  1744. enable_z_endstop(false);
  1745. go_to_current(homing_feedrate[Z_AXIS]/60);
  1746. // Go to the measurement point.
  1747. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1748. current_position[X_AXIS] = 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];
  1749. 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];
  1750. // The calibration points are very close to the min Y.
  1751. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  1752. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1753. go_to_current(homing_feedrate[X_AXIS]/60);
  1754. find_bed_induction_sensor_point_z();
  1755. scan_bed_induction_sensor_point();
  1756. }
  1757. // Don't let the manage_inactivity() function remove power from the motors.
  1758. refresh_cmd_timeout();
  1759. enable_endstops(false);
  1760. enable_z_endstop(false);
  1761. // Don't let the manage_inactivity() function remove power from the motors.
  1762. refresh_cmd_timeout();
  1763. enable_endstops(endstops_enabled);
  1764. enable_z_endstop(endstop_z_enabled);
  1765. return true;
  1766. }