mesh_bed_calibration.cpp 123 KB

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