mesh_bed_calibration.cpp 44 KB

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