mesh_bed_calibration.cpp 124 KB

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