mesh_bed_calibration.cpp 101 KB

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