mesh_bed_calibration.cpp 104 KB

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