mesh_bed_calibration.cpp 95 KB

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