mesh_bed_calibration.cpp 105 KB

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