mesh_bed_calibration.cpp 114 KB

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