mesh_bed_calibration.cpp 122 KB

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