mesh_bed_calibration.cpp 87 KB

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