mesh_bed_calibration.cpp 116 KB

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