mesh_bed_calibration.cpp 123 KB

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