mesh_bed_calibration.cpp 93 KB

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