mesh_bed_calibration.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. #define BED_ZERO_REF_X (- 22.f + X_PROBE_OFFSET_FROM_EXTRUDER)
  13. #define BED_ZERO_REF_Y (- 0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER)
  14. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  15. // The points are ordered in a zig-zag fashion to speed up the calibration.
  16. const float bed_ref_points[] PROGMEM = {
  17. 13.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  18. 115.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  19. 216.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  20. 216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  21. 115.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  22. 13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  23. 13.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  24. 115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  25. 216.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y
  26. };
  27. // Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
  28. // The points are the following: center front, center right, center rear, center left.
  29. const float bed_ref_points_4[] PROGMEM = {
  30. 115.f - BED_ZERO_REF_X, 6.4f - BED_ZERO_REF_Y,
  31. 216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
  32. 115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
  33. 13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y
  34. };
  35. static inline float sqr(float x) { return x * x; }
  36. bool calculate_machine_skew_and_offset_LS(
  37. // Matrix of maximum 9 2D points (18 floats)
  38. const float *measured_pts,
  39. uint8_t npts,
  40. const float *true_pts,
  41. // Resulting correction matrix.
  42. float *vec_x,
  43. float *vec_y,
  44. float *cntr
  45. // Temporary values, 49-18-(2*3)=25 floats
  46. // , float *temp
  47. )
  48. {
  49. SERIAL_ECHOPGM("X vector, initial: ");
  50. MYSERIAL.print(vec_x[0], 5);
  51. SERIAL_ECHOPGM(", ");
  52. MYSERIAL.print(vec_x[1], 5);
  53. SERIAL_ECHOLNPGM("");
  54. SERIAL_ECHOPGM("Y vector, initial: ");
  55. MYSERIAL.print(vec_y[0], 5);
  56. SERIAL_ECHOPGM(", ");
  57. MYSERIAL.print(vec_y[1], 5);
  58. SERIAL_ECHOLNPGM("");
  59. SERIAL_ECHOPGM("center, initial: ");
  60. MYSERIAL.print(cntr[0], 5);
  61. SERIAL_ECHOPGM(", ");
  62. MYSERIAL.print(cntr[1], 5);
  63. SERIAL_ECHOLNPGM("");
  64. for (uint8_t i = 0; i < npts; ++ i) {
  65. SERIAL_ECHOPGM("point #");
  66. MYSERIAL.print(int(i));
  67. SERIAL_ECHOPGM(" measured: (");
  68. MYSERIAL.print(measured_pts[i*2], 5);
  69. SERIAL_ECHOPGM(", ");
  70. MYSERIAL.print(measured_pts[i*2+1], 5);
  71. SERIAL_ECHOPGM("); target: (");
  72. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  73. SERIAL_ECHOPGM(", ");
  74. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  75. SERIAL_ECHOPGM("), error: ");
  76. MYSERIAL.print(sqrt(
  77. sqr(pgm_read_float(true_pts+i*2 ) - measured_pts[i*2 ]) +
  78. sqr(pgm_read_float(true_pts+i*2+1) - measured_pts[i*2+1])), 5);
  79. SERIAL_ECHOLNPGM("");
  80. }
  81. delay_keep_alive(100);
  82. {
  83. // Create covariance matrix for A, collect the right hand side b.
  84. float A[3][3] = { 0.f };
  85. float b[3] = { 0.f };
  86. float acc;
  87. for (uint8_t r = 0; r < 3; ++ r) {
  88. for (uint8_t c = 0; c < 3; ++ c) {
  89. acc = 0;
  90. for (uint8_t i = 0; i < npts; ++ i) {
  91. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  92. float b = (c == 2) ? 1.f : measured_pts[2 * i + c];
  93. acc += a * b;
  94. }
  95. A[r][c] = acc;
  96. }
  97. acc = 0.f;
  98. for (uint8_t i = 0; i < npts; ++ i) {
  99. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  100. float b = pgm_read_float(true_pts+i*2);
  101. acc += a * b;
  102. }
  103. b[r] = acc;
  104. }
  105. // Solve the linear equation for ax, bx, cx.
  106. float x[3] = { 0.f };
  107. for (uint8_t iter = 0; iter < 100; ++ iter) {
  108. x[0] = (b[0] - A[0][1] * x[1] - A[0][2] * x[2]) / A[0][0];
  109. x[1] = (b[1] - A[1][0] * x[0] - A[1][2] * x[2]) / A[1][1];
  110. x[2] = (b[2] - A[2][0] * x[0] - A[2][1] * x[1]) / A[2][2];
  111. }
  112. // Store the result to the output variables.
  113. vec_x[0] = x[0];
  114. vec_y[0] = x[1];
  115. cntr[0] = x[2];
  116. // Recalculate A and b for the y values.
  117. // Note the weighting of the first row of values.
  118. // const float weight_1st_row = 0.5f;
  119. const float weight_1st_row = 0.2f;
  120. for (uint8_t r = 0; r < 3; ++ r) {
  121. for (uint8_t c = 0; c < 3; ++ c) {
  122. acc = 0;
  123. for (uint8_t i = 0; i < npts; ++ i) {
  124. float w = (i < 3) ? weight_1st_row : 1.f;
  125. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  126. float b = (c == 2) ? 1.f : measured_pts[2 * i + c];
  127. acc += a * b * w;
  128. }
  129. A[r][c] = acc;
  130. }
  131. acc = 0.f;
  132. for (uint8_t i = 0; i < npts; ++ i) {
  133. float w = (i < 3) ? weight_1st_row : 1.f;
  134. float a = (r == 2) ? 1.f : measured_pts[2 * i + r];
  135. float b = pgm_read_float(true_pts+i*2+1);
  136. acc += w * a * b;
  137. }
  138. b[r] = acc;
  139. }
  140. // Solve the linear equation for ay, by, cy.
  141. x[0] = 0.f, x[1] = 0.f; x[2] = 0.f;
  142. for (uint8_t iter = 0; iter < 100; ++ iter) {
  143. x[0] = (b[0] - A[0][1] * x[1] - A[0][2] * x[2]) / A[0][0];
  144. x[1] = (b[1] - A[1][0] * x[0] - A[1][2] * x[2]) / A[1][1];
  145. x[2] = (b[2] - A[2][0] * x[0] - A[2][1] * x[1]) / A[2][2];
  146. }
  147. // Store the result to the output variables.
  148. vec_x[1] = x[0];
  149. vec_y[1] = x[1];
  150. cntr[1] = x[2];
  151. }
  152. SERIAL_ECHOLNPGM("Error after correction: ");
  153. for (uint8_t i = 0; i < npts; ++ i) {
  154. float x = vec_x[0] * measured_pts[i*2] + vec_y[0] * measured_pts[i*2+1] + cntr[0];
  155. float y = vec_x[1] * measured_pts[i*2] + vec_y[1] * measured_pts[i*2+1] + cntr[1];
  156. SERIAL_ECHOPGM("point #");
  157. MYSERIAL.print(int(i));
  158. SERIAL_ECHOPGM(" measured: (");
  159. MYSERIAL.print(measured_pts[i*2], 5);
  160. SERIAL_ECHOPGM(", ");
  161. MYSERIAL.print(measured_pts[i*2+1], 5);
  162. SERIAL_ECHOPGM("); corrected: (");
  163. MYSERIAL.print(x, 5);
  164. SERIAL_ECHOPGM(", ");
  165. MYSERIAL.print(y, 5);
  166. SERIAL_ECHOPGM("); target: (");
  167. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  168. SERIAL_ECHOPGM(", ");
  169. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  170. SERIAL_ECHOPGM("), error: ");
  171. MYSERIAL.print(sqrt(sqr(pgm_read_float(true_pts+i*2)-x)+sqr(pgm_read_float(true_pts+i*2+1)-y)));
  172. SERIAL_ECHOLNPGM("");
  173. }
  174. SERIAL_ECHOPGM("X vector new, inverted: ");
  175. MYSERIAL.print(vec_x[0], 5);
  176. SERIAL_ECHOPGM(", ");
  177. MYSERIAL.print(vec_x[1], 5);
  178. SERIAL_ECHOLNPGM("");
  179. SERIAL_ECHOPGM("Y vector new, inverted: ");
  180. MYSERIAL.print(vec_y[0], 5);
  181. SERIAL_ECHOPGM(", ");
  182. MYSERIAL.print(vec_y[1], 5);
  183. SERIAL_ECHOLNPGM("");
  184. SERIAL_ECHOPGM("center new, inverted: ");
  185. MYSERIAL.print(cntr[0], 5);
  186. SERIAL_ECHOPGM(", ");
  187. MYSERIAL.print(cntr[1], 5);
  188. SERIAL_ECHOLNPGM("");
  189. delay_keep_alive(100);
  190. #if 0
  191. // Normalize the vectors. We expect, that the machine axes may be skewed a bit, but the distances are correct.
  192. // l shall be very close to 1 already.
  193. float l = sqrt(vec_x[0]*vec_x[0] + vec_x[1] * vec_x[1]);
  194. vec_x[0] /= l;
  195. vec_x[1] /= l;
  196. SERIAL_ECHOPGM("Length of the X vector: ");
  197. MYSERIAL.print(l, 5);
  198. SERIAL_ECHOLNPGM("");
  199. l = sqrt(vec_y[0]*vec_y[0] + vec_y[1] * vec_y[1]);
  200. vec_y[0] /= l;
  201. vec_y[1] /= l;
  202. SERIAL_ECHOPGM("Length of the Y vector: ");
  203. MYSERIAL.print(l, 5);
  204. SERIAL_ECHOLNPGM("");
  205. // Recalculate the center using the adjusted vec_x/vec_y
  206. {
  207. cntr[0] = 0.f;
  208. cntr[1] = 0.f;
  209. for (uint8_t i = 0; i < npts; ++ i) {
  210. 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];
  211. 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];
  212. }
  213. cntr[0] /= float(npts);
  214. cntr[1] /= float(npts);
  215. }
  216. SERIAL_ECHOPGM("X vector new, inverted, normalized: ");
  217. MYSERIAL.print(vec_x[0], 5);
  218. SERIAL_ECHOPGM(", ");
  219. MYSERIAL.print(vec_x[1], 5);
  220. SERIAL_ECHOLNPGM("");
  221. SERIAL_ECHOPGM("Y vector new, inverted, normalized: ");
  222. MYSERIAL.print(vec_y[0], 5);
  223. SERIAL_ECHOPGM(", ");
  224. MYSERIAL.print(vec_y[1], 5);
  225. SERIAL_ECHOLNPGM("");
  226. SERIAL_ECHOPGM("center new, inverted, normalized: ");
  227. MYSERIAL.print(cntr[0], 5);
  228. SERIAL_ECHOPGM(", ");
  229. MYSERIAL.print(cntr[1], 5);
  230. SERIAL_ECHOLNPGM("");
  231. #endif
  232. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  233. {
  234. float d = vec_x[0] * vec_y[1] - vec_x[1] * vec_y[0];
  235. float Ainv[2][2] = {
  236. { vec_y[1] / d, - vec_y[0] / d },
  237. { - vec_x[1] / d, vec_x[0] / d }
  238. };
  239. float cntrInv[2] = {
  240. - Ainv[0][0] * cntr[0] - Ainv[0][1] * cntr[1],
  241. - Ainv[1][0] * cntr[0] - Ainv[1][1] * cntr[1]
  242. };
  243. vec_x[0] = Ainv[0][0];
  244. vec_x[1] = Ainv[1][0];
  245. vec_y[0] = Ainv[0][1];
  246. vec_y[1] = Ainv[1][1];
  247. cntr[0] = cntrInv[0];
  248. cntr[1] = cntrInv[1];
  249. }
  250. SERIAL_ECHOPGM("X vector, adjusted: ");
  251. MYSERIAL.print(vec_x[0], 5);
  252. SERIAL_ECHOPGM(", ");
  253. MYSERIAL.print(vec_x[1], 5);
  254. SERIAL_ECHOLNPGM("");
  255. SERIAL_ECHOPGM("Y vector, adjusted: ");
  256. MYSERIAL.print(vec_y[0], 5);
  257. SERIAL_ECHOPGM(", ");
  258. MYSERIAL.print(vec_y[1], 5);
  259. SERIAL_ECHOLNPGM("");
  260. SERIAL_ECHOPGM("center, adjusted: ");
  261. MYSERIAL.print(cntr[0], 5);
  262. SERIAL_ECHOPGM(", ");
  263. MYSERIAL.print(cntr[1], 5);
  264. SERIAL_ECHOLNPGM("");
  265. SERIAL_ECHOLNPGM("Difference after correction: ");
  266. for (uint8_t i = 0; i < npts; ++ i) {
  267. 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];
  268. 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];
  269. SERIAL_ECHOPGM("point #");
  270. MYSERIAL.print(int(i));
  271. SERIAL_ECHOPGM("measured: (");
  272. MYSERIAL.print(measured_pts[i*2], 5);
  273. SERIAL_ECHOPGM(", ");
  274. MYSERIAL.print(measured_pts[i*2+1], 5);
  275. SERIAL_ECHOPGM("); measured-corrected: (");
  276. MYSERIAL.print(x, 5);
  277. SERIAL_ECHOPGM(", ");
  278. MYSERIAL.print(y, 5);
  279. SERIAL_ECHOPGM("); target: (");
  280. MYSERIAL.print(pgm_read_float(true_pts+i*2 ), 5);
  281. SERIAL_ECHOPGM(", ");
  282. MYSERIAL.print(pgm_read_float(true_pts+i*2+1), 5);
  283. SERIAL_ECHOPGM("), error: ");
  284. MYSERIAL.print(sqrt(sqr(measured_pts[i*2]-x)+sqr(measured_pts[i*2+1]-y)));
  285. SERIAL_ECHOLNPGM("");
  286. }
  287. delay_keep_alive(100);
  288. return true;
  289. }
  290. void reset_bed_offset_and_skew()
  291. {
  292. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+0), 0x0FFFFFFFF);
  293. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+4), 0x0FFFFFFFF);
  294. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +0), 0x0FFFFFFFF);
  295. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +4), 0x0FFFFFFFF);
  296. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +0), 0x0FFFFFFFF);
  297. eeprom_update_dword((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +4), 0x0FFFFFFFF);
  298. }
  299. void world2machine_reset()
  300. {
  301. // Identity transformation.
  302. world2machine_rotation_and_skew[0][0] = 1.f;
  303. world2machine_rotation_and_skew[0][1] = 0.f;
  304. world2machine_rotation_and_skew[1][0] = 0.f;
  305. world2machine_rotation_and_skew[1][1] = 1.f;
  306. // Zero shift.
  307. world2machine_shift[0] = 0.f;
  308. world2machine_shift[1] = 0.f;
  309. }
  310. static inline bool vec_undef(const float v[2])
  311. {
  312. const uint32_t *vx = (const uint32_t*)v;
  313. return vx[0] == 0x0FFFFFFFF || vx[1] == 0x0FFFFFFFF;
  314. }
  315. void world2machine_initialize()
  316. {
  317. float cntr[2] = {
  318. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0)),
  319. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4))
  320. };
  321. float vec_x[2] = {
  322. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0)),
  323. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4))
  324. };
  325. float vec_y[2] = {
  326. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0)),
  327. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4))
  328. };
  329. bool reset = false;
  330. if (vec_undef(cntr) || vec_undef(vec_x) || vec_undef(vec_y))
  331. reset = true;
  332. else {
  333. // Length of the vec_x shall be close to unity.
  334. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  335. if (l < 0.9 || l > 1.1) {
  336. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  337. reset = true;
  338. }
  339. // Length of the vec_y shall be close to unity.
  340. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  341. if (l < 0.9 || l > 1.1) {
  342. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  343. reset = true;
  344. }
  345. // Correction of the zero point shall be reasonably small.
  346. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  347. if (l > 15.f) {
  348. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Shift out of range.");
  349. reset = true;
  350. }
  351. // vec_x and vec_y shall be nearly perpendicular.
  352. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  353. if (fabs(l) > 0.1f) {
  354. SERIAL_ECHOLNPGM("Invalid bed correction matrix. X/Y axes are far from being perpendicular.");
  355. reset = true;
  356. }
  357. }
  358. if (reset) {
  359. SERIAL_ECHOLNPGM("Invalid bed correction matrix. Resetting to identity.");
  360. reset_bed_offset_and_skew();
  361. world2machine_reset();
  362. } else {
  363. world2machine_rotation_and_skew[0][0] = vec_x[0];
  364. world2machine_rotation_and_skew[1][0] = vec_x[1];
  365. world2machine_rotation_and_skew[0][1] = vec_y[0];
  366. world2machine_rotation_and_skew[1][1] = vec_y[1];
  367. world2machine_shift[0] = cntr[0];
  368. world2machine_shift[1] = cntr[1];
  369. }
  370. }
  371. // When switching from absolute to corrected coordinates,
  372. // this will get the absolute coordinates from the servos,
  373. // applies the inverse world2machine transformation
  374. // and stores the result into current_position[x,y].
  375. void world2machine_update_current()
  376. {
  377. // Invert the transformation matrix made of vec_x, vec_y and cntr.
  378. 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];
  379. float Ainv[2][2] = {
  380. { world2machine_rotation_and_skew[1][1] / d, - world2machine_rotation_and_skew[0][1] / d },
  381. { - world2machine_rotation_and_skew[1][0] / d, world2machine_rotation_and_skew[0][0] / d }
  382. };
  383. float x = current_position[X_AXIS] - world2machine_shift[0];
  384. float y = current_position[Y_AXIS] - world2machine_shift[1];
  385. current_position[X_AXIS] = Ainv[0][0] * x + Ainv[0][1] * y;
  386. current_position[Y_AXIS] = Ainv[1][0] * x + Ainv[1][1] * y;
  387. }
  388. static inline void go_xyz(float x, float y, float z, float fr)
  389. {
  390. plan_buffer_line(x, y, z, current_position[E_AXIS], fr, active_extruder);
  391. st_synchronize();
  392. }
  393. static inline void go_xy(float x, float y, float fr)
  394. {
  395. plan_buffer_line(x, y, current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  396. st_synchronize();
  397. }
  398. static inline void go_to_current(float fr)
  399. {
  400. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  401. st_synchronize();
  402. }
  403. static inline void update_current_position_xyz()
  404. {
  405. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  406. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  407. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  408. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  409. }
  410. static inline void update_current_position_z()
  411. {
  412. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  413. plan_set_z_position(current_position[Z_AXIS]);
  414. }
  415. // At the current position, find the Z stop.
  416. inline void find_bed_induction_sensor_point_z()
  417. {
  418. bool endstops_enabled = enable_endstops(true);
  419. bool endstop_z_enabled = enable_z_endstop(false);
  420. // move down until you find the bed
  421. current_position[Z_AXIS] = -10;
  422. go_to_current(homing_feedrate[Z_AXIS]/60);
  423. // we have to let the planner know where we are right now as it is not where we said to go.
  424. update_current_position_z();
  425. // move up the retract distance
  426. current_position[Z_AXIS] += home_retract_mm_ext(Z_AXIS);
  427. go_to_current(homing_feedrate[Z_AXIS]/60);
  428. // move back down slowly to find bed
  429. current_position[Z_AXIS] -= home_retract_mm_ext(Z_AXIS) * 2;
  430. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  431. // we have to let the planner know where we are right now as it is not where we said to go.
  432. update_current_position_z();
  433. enable_endstops(endstops_enabled);
  434. enable_z_endstop(endstop_z_enabled);
  435. }
  436. // Search around the current_position[X,Y],
  437. // look for the induction sensor response.
  438. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  439. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  440. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (6.f)
  441. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  442. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.5f)
  443. inline bool find_bed_induction_sensor_point_xy()
  444. {
  445. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  446. bool found = false;
  447. {
  448. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  449. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  450. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  451. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  452. uint8_t nsteps_y;
  453. uint8_t i;
  454. if (x0 < X_MIN_POS)
  455. x0 = X_MIN_POS;
  456. if (x1 > X_MAX_POS)
  457. x1 = X_MAX_POS;
  458. if (y0 < Y_MIN_POS)
  459. y0 = Y_MIN_POS;
  460. if (y1 > Y_MAX_POS)
  461. y1 = Y_MAX_POS;
  462. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  463. enable_endstops(false);
  464. bool dir_positive = true;
  465. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  466. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  467. // Continously lower the Z axis.
  468. endstops_hit_on_purpose();
  469. enable_z_endstop(true);
  470. while (current_position[Z_AXIS] > -10.f) {
  471. // Do nsteps_y zig-zag movements.
  472. current_position[Y_AXIS] = y0;
  473. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++ i) {
  474. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  475. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  476. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  477. dir_positive = ! dir_positive;
  478. if (endstop_z_hit_on_purpose())
  479. goto endloop;
  480. }
  481. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++ i) {
  482. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  483. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  484. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  485. dir_positive = ! dir_positive;
  486. if (endstop_z_hit_on_purpose())
  487. goto endloop;
  488. }
  489. }
  490. endloop:
  491. // SERIAL_ECHOLN("First hit");
  492. // we have to let the planner know where we are right now as it is not where we said to go.
  493. update_current_position_xyz();
  494. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  495. for (int8_t iter = 0; iter < 3; ++ iter) {
  496. if (iter > 0) {
  497. // Slightly lower the Z axis to get a reliable trigger.
  498. current_position[Z_AXIS] -= 0.02f;
  499. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  500. }
  501. // Do nsteps_y zig-zag movements.
  502. float a, b;
  503. enable_endstops(false);
  504. enable_z_endstop(false);
  505. current_position[Y_AXIS] = y0;
  506. go_xy(x0, current_position[Y_AXIS], feedrate);
  507. enable_z_endstop(true);
  508. found = false;
  509. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++ i, dir_positive = ! dir_positive) {
  510. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  511. if (endstop_z_hit_on_purpose()) {
  512. found = true;
  513. break;
  514. }
  515. }
  516. update_current_position_xyz();
  517. if (! found) {
  518. // SERIAL_ECHOLN("Search in Y - not found");
  519. continue;
  520. }
  521. // SERIAL_ECHOLN("Search in Y - found");
  522. a = current_position[Y_AXIS];
  523. enable_z_endstop(false);
  524. current_position[Y_AXIS] = y1;
  525. go_xy(x0, current_position[Y_AXIS], feedrate);
  526. enable_z_endstop(true);
  527. found = false;
  528. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++ i, dir_positive = ! dir_positive) {
  529. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  530. if (endstop_z_hit_on_purpose()) {
  531. found = true;
  532. break;
  533. }
  534. }
  535. update_current_position_xyz();
  536. if (! found) {
  537. // SERIAL_ECHOLN("Search in Y2 - not found");
  538. continue;
  539. }
  540. // SERIAL_ECHOLN("Search in Y2 - found");
  541. b = current_position[Y_AXIS];
  542. current_position[Y_AXIS] = 0.5f * (a + b);
  543. // Search in the X direction along a cross.
  544. found = false;
  545. enable_z_endstop(false);
  546. go_xy(x0, current_position[Y_AXIS], feedrate);
  547. enable_z_endstop(true);
  548. go_xy(x1, current_position[Y_AXIS], feedrate);
  549. update_current_position_xyz();
  550. if (! endstop_z_hit_on_purpose()) {
  551. // SERIAL_ECHOLN("Search X span 0 - not found");
  552. continue;
  553. }
  554. // SERIAL_ECHOLN("Search X span 0 - found");
  555. a = current_position[X_AXIS];
  556. enable_z_endstop(false);
  557. go_xy(x1, current_position[Y_AXIS], feedrate);
  558. enable_z_endstop(true);
  559. go_xy(x0, current_position[Y_AXIS], feedrate);
  560. update_current_position_xyz();
  561. if (! endstop_z_hit_on_purpose()) {
  562. // SERIAL_ECHOLN("Search X span 1 - not found");
  563. continue;
  564. }
  565. // SERIAL_ECHOLN("Search X span 1 - found");
  566. b = current_position[X_AXIS];
  567. // Go to the center.
  568. enable_z_endstop(false);
  569. current_position[X_AXIS] = 0.5f * (a + b);
  570. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  571. found = true;
  572. #if 1
  573. // Search in the Y direction along a cross.
  574. found = false;
  575. enable_z_endstop(false);
  576. go_xy(current_position[X_AXIS], y0, feedrate);
  577. enable_z_endstop(true);
  578. go_xy(current_position[X_AXIS], y1, feedrate);
  579. update_current_position_xyz();
  580. if (! endstop_z_hit_on_purpose()) {
  581. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  582. continue;
  583. }
  584. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  585. a = current_position[Y_AXIS];
  586. enable_z_endstop(false);
  587. go_xy(current_position[X_AXIS], y1, feedrate);
  588. enable_z_endstop(true);
  589. go_xy(current_position[X_AXIS], y0, feedrate);
  590. update_current_position_xyz();
  591. if (! endstop_z_hit_on_purpose()) {
  592. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  593. continue;
  594. }
  595. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  596. b = current_position[Y_AXIS];
  597. // Go to the center.
  598. enable_z_endstop(false);
  599. current_position[Y_AXIS] = 0.5f * (a + b);
  600. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  601. found = true;
  602. #endif
  603. break;
  604. }
  605. }
  606. enable_z_endstop(false);
  607. return found;
  608. }
  609. // Search around the current_position[X,Y,Z].
  610. // It is expected, that the induction sensor is switched on at the current position.
  611. // Look around this center point by painting a star around the point.
  612. inline bool improve_bed_induction_sensor_point()
  613. {
  614. static const float search_radius = 8.f;
  615. bool endstops_enabled = enable_endstops(false);
  616. bool endstop_z_enabled = enable_z_endstop(false);
  617. bool found = false;
  618. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  619. float center_old_x = current_position[X_AXIS];
  620. float center_old_y = current_position[Y_AXIS];
  621. float center_x = 0.f;
  622. float center_y = 0.f;
  623. for (uint8_t iter = 0; iter < 4; ++ iter) {
  624. switch (iter) {
  625. case 0:
  626. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  627. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  628. break;
  629. case 1:
  630. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  631. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  632. break;
  633. case 2:
  634. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  635. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  636. break;
  637. case 3:
  638. default:
  639. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  640. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  641. break;
  642. }
  643. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  644. float vx = destination[X_AXIS] - center_old_x;
  645. float vy = destination[Y_AXIS] - center_old_y;
  646. float l = sqrt(vx*vx+vy*vy);
  647. float t;
  648. if (destination[X_AXIS] < X_MIN_POS) {
  649. // Exiting the bed at xmin.
  650. t = (center_x - X_MIN_POS) / l;
  651. destination[X_AXIS] = X_MIN_POS;
  652. destination[Y_AXIS] = center_old_y + t * vy;
  653. } else if (destination[X_AXIS] > X_MAX_POS) {
  654. // Exiting the bed at xmax.
  655. t = (X_MAX_POS - center_x) / l;
  656. destination[X_AXIS] = X_MAX_POS;
  657. destination[Y_AXIS] = center_old_y + t * vy;
  658. }
  659. if (destination[Y_AXIS] < Y_MIN_POS) {
  660. // Exiting the bed at ymin.
  661. t = (center_y - Y_MIN_POS) / l;
  662. destination[X_AXIS] = center_old_x + t * vx;
  663. destination[Y_AXIS] = Y_MIN_POS;
  664. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  665. // Exiting the bed at xmax.
  666. t = (Y_MAX_POS - center_y) / l;
  667. destination[X_AXIS] = center_old_x + t * vx;
  668. destination[Y_AXIS] = Y_MAX_POS;
  669. }
  670. // Move away from the measurement point.
  671. enable_endstops(false);
  672. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  673. // Move towards the measurement point, until the induction sensor triggers.
  674. enable_endstops(true);
  675. go_xy(center_old_x, center_old_y, feedrate);
  676. update_current_position_xyz();
  677. // if (! endstop_z_hit_on_purpose()) return false;
  678. center_x += current_position[X_AXIS];
  679. center_y += current_position[Y_AXIS];
  680. }
  681. // Calculate the new center, move to the new center.
  682. center_x /= 4.f;
  683. center_y /= 4.f;
  684. current_position[X_AXIS] = center_x;
  685. current_position[Y_AXIS] = center_y;
  686. enable_endstops(false);
  687. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  688. enable_endstops(endstops_enabled);
  689. enable_z_endstop(endstop_z_enabled);
  690. return found;
  691. }
  692. // Search around the current_position[X,Y,Z].
  693. // It is expected, that the induction sensor is switched on at the current position.
  694. // Look around this center point by painting a star around the point.
  695. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  696. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y)
  697. {
  698. float center_old_x = current_position[X_AXIS];
  699. float center_old_y = current_position[Y_AXIS];
  700. float a, b;
  701. enable_endstops(false);
  702. {
  703. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  704. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  705. if (x0 < X_MIN_POS)
  706. x0 = X_MIN_POS;
  707. if (x1 > X_MAX_POS)
  708. x1 = X_MAX_POS;
  709. // Search in the X direction along a cross.
  710. enable_z_endstop(false);
  711. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  712. enable_z_endstop(true);
  713. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  714. update_current_position_xyz();
  715. if (! endstop_z_hit_on_purpose()) {
  716. current_position[X_AXIS] = center_old_x;
  717. goto canceled;
  718. }
  719. a = current_position[X_AXIS];
  720. enable_z_endstop(false);
  721. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  722. enable_z_endstop(true);
  723. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  724. update_current_position_xyz();
  725. if (! endstop_z_hit_on_purpose()) {
  726. current_position[X_AXIS] = center_old_x;
  727. goto canceled;
  728. }
  729. b = current_position[X_AXIS];
  730. // Go to the center.
  731. enable_z_endstop(false);
  732. current_position[X_AXIS] = 0.5f * (a + b);
  733. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  734. }
  735. {
  736. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  737. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  738. if (y0 < Y_MIN_POS)
  739. y0 = Y_MIN_POS;
  740. if (y1 > Y_MAX_POS)
  741. y1 = Y_MAX_POS;
  742. // Search in the Y direction along a cross.
  743. enable_z_endstop(false);
  744. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  745. if (lift_z_on_min_y) {
  746. // The first row of points are very close to the end stop.
  747. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  748. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  749. // and go back.
  750. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  751. }
  752. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  753. // Already triggering before we started the move.
  754. // Shift the trigger point slightly outwards.
  755. // a = current_position[Y_AXIS] - 1.5f;
  756. a = current_position[Y_AXIS];
  757. } else {
  758. enable_z_endstop(true);
  759. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  760. update_current_position_xyz();
  761. if (! endstop_z_hit_on_purpose()) {
  762. current_position[Y_AXIS] = center_old_y;
  763. goto canceled;
  764. }
  765. a = current_position[Y_AXIS];
  766. }
  767. enable_z_endstop(false);
  768. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  769. enable_z_endstop(true);
  770. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  771. update_current_position_xyz();
  772. if (! endstop_z_hit_on_purpose()) {
  773. current_position[Y_AXIS] = center_old_y;
  774. goto canceled;
  775. }
  776. b = current_position[Y_AXIS];
  777. // Go to the center.
  778. enable_z_endstop(false);
  779. current_position[Y_AXIS] = 0.5f * (a + b);
  780. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  781. }
  782. return true;
  783. canceled:
  784. // Go back to the center.
  785. enable_z_endstop(false);
  786. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  787. return false;
  788. }
  789. #define MESH_BED_CALIBRATION_SHOW_LCD
  790. bool find_bed_offset_and_skew()
  791. {
  792. // Reusing the z_values memory for the measurement cache.
  793. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  794. float *pts = &mbl.z_values[0][0];
  795. float *vec_x = pts + 2 * 4;
  796. float *vec_y = vec_x + 2;
  797. float *cntr = vec_y + 2;
  798. memset(pts, 0, sizeof(float) * 7 * 7);
  799. // Let the planner use the uncorrected coordinates.
  800. world2machine_reset();
  801. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  802. lcd_implementation_clear();
  803. lcd_print_at_PGM(0, 0, MSG_FIND_BED_OFFSET_AND_SKEW_LINE1);
  804. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  805. // Collect the rear 2x3 points.
  806. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  807. for (int k = 0; k < 4; ++ k) {
  808. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  809. lcd_print_at_PGM(0, 1, MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
  810. lcd_implementation_print_at(0, 2, k+1);
  811. lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE3);
  812. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  813. float *pt = pts + k * 2;
  814. // Go up to z_initial.
  815. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  816. // Go to the measurement point position.
  817. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+k*2);
  818. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+k*2+1);
  819. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  820. if (! find_bed_induction_sensor_point_xy())
  821. return false;
  822. find_bed_induction_sensor_point_z();
  823. pt[0] = current_position[X_AXIS];
  824. pt[1] = current_position[Y_AXIS];
  825. // Start searching for the other points at 3mm above the last point.
  826. current_position[Z_AXIS] += 3.f;
  827. cntr[0] += pt[0];
  828. cntr[1] += pt[1];
  829. }
  830. #if 0
  831. // Average the X and Y vectors. They may not be perpendicular, if the printer is built incorrectly.
  832. {
  833. float len;
  834. // Average the center point.
  835. cntr[0] *= 1.f/4.f;
  836. cntr[1] *= 1.f/4.f;
  837. cntr[2] *= 1.f/4.f;
  838. // Average the X vector.
  839. vec_x[0] = (pts[2 * 1 + 0] - pts[2 * 3 + 0]) / 2.f;
  840. vec_x[1] = (pts[2 * 1 + 1] - pts[2 * 3 + 1]) / 2.f;
  841. len = sqrt(vec_x[0]*vec_x[0] + vec_x[1]*vec_x[1]);
  842. if (0) {
  843. // if (len < MEAS_NUM_X_DIST) {
  844. // Scale the vector up to MEAS_NUM_X_DIST lenght.
  845. float factor = MEAS_NUM_X_DIST / len;
  846. vec_x[0] *= factor;
  847. vec_x[0] *= factor;
  848. } else {
  849. // The vector is longer than MEAS_NUM_X_DIST. The X/Y axes are skewed.
  850. // Verify the maximum skew?
  851. }
  852. // Average the Y vector.
  853. vec_y[0] = (pts[2 * 2 + 0] - pts[2 * 0 + 0]) / 2.f;
  854. vec_y[1] = (pts[2 * 2 + 1] - pts[2 * 0 + 1]) / 2.f;
  855. len = sqrt(vec_y[0]*vec_y[0] + vec_y[1]*vec_y[1]);
  856. if (0) {
  857. // if (len < MEAS_NUM_Y_DIST) {
  858. // Scale the vector up to MEAS_NUM_X_DIST lenght.
  859. float factor = MEAS_NUM_Y_DIST / len;
  860. vec_y[1] *= factor;
  861. vec_y[1] *= factor;
  862. } else {
  863. // The vector is longer than MEAS_NUM_X_DIST. The X/Y axes are skewed.
  864. // Verify the maximum skew?
  865. }
  866. // Fearlessly store the calibration values into the eeprom.
  867. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  868. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  869. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  870. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  871. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  872. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  873. #if 0
  874. SERIAL_ECHOLN("Calibration done.");
  875. SERIAL_ECHO("Center: ");
  876. SERIAL_ECHO(cntr[0]);
  877. SERIAL_ECHO(",");
  878. SERIAL_ECHO(cntr[1]);
  879. SERIAL_ECHO(", x: ");
  880. SERIAL_ECHO(vec_x[0]);
  881. SERIAL_ECHO(",");
  882. SERIAL_ECHO(vec_x[1]);
  883. SERIAL_ECHO(", y: ");
  884. SERIAL_ECHO(vec_y[0]);
  885. SERIAL_ECHO(",");
  886. SERIAL_ECHO(vec_y[1]);
  887. SERIAL_ECHOLN("");
  888. #endif
  889. }
  890. #endif
  891. calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr);
  892. world2machine_rotation_and_skew[0][0] = vec_x[0];
  893. world2machine_rotation_and_skew[1][0] = vec_x[1];
  894. world2machine_rotation_and_skew[0][1] = vec_y[0];
  895. world2machine_rotation_and_skew[1][1] = vec_y[1];
  896. world2machine_shift[0] = cntr[0];
  897. world2machine_shift[1] = cntr[1];
  898. #if 1
  899. // Fearlessly store the calibration values into the eeprom.
  900. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  901. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  902. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  903. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  904. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  905. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  906. #endif
  907. return true;
  908. }
  909. bool improve_bed_offset_and_skew(int8_t method)
  910. {
  911. // Reusing the z_values memory for the measurement cache.
  912. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  913. float *pts = &mbl.z_values[0][0];
  914. float *vec_x = pts + 2 * 9;
  915. float *vec_y = vec_x + 2;
  916. float *cntr = vec_y + 2;
  917. memset(pts, 0, sizeof(float) * 7 * 7);
  918. // Cache the current correction matrix.
  919. vec_x[0] = world2machine_rotation_and_skew[0][0];
  920. vec_x[1] = world2machine_rotation_and_skew[1][0];
  921. vec_y[0] = world2machine_rotation_and_skew[0][1];
  922. vec_y[1] = world2machine_rotation_and_skew[1][1];
  923. cntr[0] = world2machine_shift[0];
  924. cntr[1] = world2machine_shift[1];
  925. // and reset the correction matrix, so the planner will not do anything.
  926. world2machine_reset();
  927. bool endstops_enabled = enable_endstops(false);
  928. bool endstop_z_enabled = enable_z_endstop(false);
  929. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  930. lcd_implementation_clear();
  931. lcd_print_at_PGM(0, 0, MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1);
  932. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  933. // Collect a matrix of 9x9 points.
  934. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  935. // Print the decrasing ID of the measurement point.
  936. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  937. lcd_print_at_PGM(0, 1, MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2);
  938. lcd_implementation_print_at(0, 2, mesh_point+1);
  939. lcd_printPGM(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE3);
  940. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  941. // Move up.
  942. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  943. enable_endstops(false);
  944. enable_z_endstop(false);
  945. go_to_current(homing_feedrate[Z_AXIS]/60);
  946. // Go to the measurement point.
  947. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  948. 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];
  949. 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];
  950. // The calibration points are very close to the min Y.
  951. if (current_position[Y_AXIS] < Y_MIN_POS)
  952. current_position[Y_AXIS] = Y_MIN_POS;
  953. go_to_current(homing_feedrate[X_AXIS]/60);
  954. // Find its Z position by running the normal vertical search.
  955. // delay_keep_alive(3000);
  956. find_bed_induction_sensor_point_z();
  957. // delay_keep_alive(3000);
  958. // Improve the point position by searching its center in a current plane.
  959. int8_t n_errors = 3;
  960. for (int8_t iter = 0; iter < 8; ) {
  961. bool found = false;
  962. switch (method) {
  963. case 0: found = improve_bed_induction_sensor_point(); break;
  964. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 3); break;
  965. default: break;
  966. }
  967. if (found) {
  968. if (iter > 3) {
  969. // Average the last 4 measurements.
  970. pts[mesh_point*2 ] += current_position[X_AXIS];
  971. pts[mesh_point*2+1] += current_position[Y_AXIS];
  972. }
  973. ++ iter;
  974. } else if (n_errors -- == 0) {
  975. // Give up.
  976. goto canceled;
  977. } else {
  978. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  979. current_position[Z_AXIS] -= 0.025f;
  980. enable_endstops(false);
  981. enable_z_endstop(false);
  982. go_to_current(homing_feedrate[Z_AXIS]);
  983. }
  984. }
  985. delay_keep_alive(3000);
  986. }
  987. // Average the last 4 measurements.
  988. for (int8_t i = 0; i < 18; ++ i)
  989. pts[i] *= (1.f/4.f);
  990. // Test the positions. Are the positions reproducible?
  991. #if 1
  992. enable_endstops(false);
  993. enable_z_endstop(false);
  994. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  995. // Go to the measurement point.
  996. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  997. current_position[X_AXIS] = pts[mesh_point*2];
  998. current_position[Y_AXIS] = pts[mesh_point*2+1];
  999. go_to_current(homing_feedrate[X_AXIS]/60);
  1000. delay_keep_alive(3000);
  1001. }
  1002. #endif
  1003. #if 0
  1004. // Average the X and Y vectors. They may not be perpendicular, if the printer is built incorrectly.
  1005. // Average the center point.
  1006. cntr[0] *= 1.f/9.f;
  1007. cntr[1] *= 1.f/9.f;
  1008. // Average the X vector.
  1009. vec_x[0] = (pts[2 * 2 + 0] - pts[2 * 0 + 0] + pts[2 * 5 + 0] - pts[2 * 3 + 0] + pts[2 * 8 + 0] - pts[2 * 6 + 0]) / 6.f;
  1010. vec_x[1] = (pts[2 * 2 + 1] - pts[2 * 0 + 1] + pts[2 * 5 + 1] - pts[2 * 3 + 1] + pts[2 * 8 + 1] - pts[2 * 6 + 1]) / 6.f;
  1011. // Average the Y vector.
  1012. vec_y[0] = (pts[2 * 6 + 0] - pts[2 * 0 + 0] + pts[2 * 7 + 0] - pts[2 * 1 + 0] + pts[2 * 8 + 0] - pts[2 * 2 + 0]) / 6.f;
  1013. vec_y[1] = (pts[2 * 6 + 1] - pts[2 * 0 + 1] + pts[2 * 7 + 1] - pts[2 * 1 + 1] + pts[2 * 8 + 1] - pts[2 * 2 + 1]) / 6.f;
  1014. #if 1
  1015. // Fearlessly store the calibration values into the eeprom.
  1016. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  1017. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  1018. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  1019. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  1020. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  1021. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  1022. #endif
  1023. #else
  1024. calculate_machine_skew_and_offset_LS(pts, 9, bed_ref_points, vec_x, vec_y, cntr);
  1025. world2machine_rotation_and_skew[0][0] = vec_x[0];
  1026. world2machine_rotation_and_skew[1][0] = vec_x[1];
  1027. world2machine_rotation_and_skew[0][1] = vec_y[0];
  1028. world2machine_rotation_and_skew[1][1] = vec_y[1];
  1029. world2machine_shift[0] = cntr[0];
  1030. world2machine_shift[1] = cntr[1];
  1031. #if 1
  1032. // Fearlessly store the calibration values into the eeprom.
  1033. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  1034. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  1035. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  1036. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  1037. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  1038. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  1039. #endif
  1040. #endif
  1041. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  1042. #if 1
  1043. enable_endstops(false);
  1044. enable_z_endstop(false);
  1045. delay_keep_alive(3000);
  1046. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  1047. // Go to the measurement point.
  1048. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1049. current_position[X_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2);
  1050. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+mesh_point*2+1);
  1051. go_to_current(homing_feedrate[X_AXIS]/60);
  1052. delay_keep_alive(3000);
  1053. }
  1054. #endif
  1055. #if 0
  1056. // and let us know the result.
  1057. SERIAL_ECHOLN("Calibration done.");
  1058. SERIAL_ECHO("Center: ");
  1059. SERIAL_ECHO(cntr[0]);
  1060. SERIAL_ECHO(",");
  1061. SERIAL_ECHO(cntr[1]);
  1062. SERIAL_ECHO(", x: ");
  1063. SERIAL_ECHO(vec_x[0]);
  1064. SERIAL_ECHO(",");
  1065. SERIAL_ECHO(vec_x[1]);
  1066. SERIAL_ECHO(", y: ");
  1067. SERIAL_ECHO(vec_y[0]);
  1068. SERIAL_ECHO(",");
  1069. SERIAL_ECHO(vec_y[1]);
  1070. SERIAL_ECHOLN("");
  1071. #endif
  1072. enable_endstops(endstops_enabled);
  1073. enable_z_endstop(endstop_z_enabled);
  1074. return true;
  1075. canceled:
  1076. // Store the identity matrix to EEPROM.
  1077. reset_bed_offset_and_skew();
  1078. enable_endstops(endstops_enabled);
  1079. enable_z_endstop(endstop_z_enabled);
  1080. return false;
  1081. }