mesh_bed_calibration.cpp 104 KB

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