mesh_bed_calibration.cpp 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  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) // -0.6 + 5 = 4.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 - 4.f - BED_ZERO_REF_Y,
  49. 221.f - BED_ZERO_REF_X, 10.4f - 4.f - BED_ZERO_REF_Y,
  50. 221.f - BED_ZERO_REF_X, 202.4f - 4.f - BED_ZERO_REF_Y,
  51. 13.f - BED_ZERO_REF_X, 202.4f - 4.f - 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. const float cntr[] = { 0.f, 0.f };
  659. world2machine_update(vx, vy, cntr);
  660. }
  661. void world2machine_revert_to_uncorrected()
  662. {
  663. if (world2machine_correction_mode != WORLD2MACHINE_CORRECTION_NONE) {
  664. // Reset the machine correction matrix.
  665. const float vx[] = { 1.f, 0.f };
  666. const float vy[] = { 0.f, 1.f };
  667. const float cntr[] = { 0.f, 0.f };
  668. world2machine_update(vx, vy, cntr);
  669. // Wait for the motors to stop and update the current position with the absolute values.
  670. st_synchronize();
  671. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  672. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  673. }
  674. }
  675. static inline bool vec_undef(const float v[2])
  676. {
  677. const uint32_t *vx = (const uint32_t*)v;
  678. return vx[0] == 0x0FFFFFFFF || vx[1] == 0x0FFFFFFFF;
  679. }
  680. void world2machine_initialize()
  681. {
  682. //SERIAL_ECHOLNPGM("world2machine_initialize");
  683. float cntr[2] = {
  684. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0)),
  685. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4))
  686. };
  687. float vec_x[2] = {
  688. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0)),
  689. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4))
  690. };
  691. float vec_y[2] = {
  692. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0)),
  693. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4))
  694. };
  695. bool reset = false;
  696. if (vec_undef(cntr) || vec_undef(vec_x) || vec_undef(vec_y)) {
  697. // SERIAL_ECHOLNPGM("Undefined bed correction matrix.");
  698. reset = true;
  699. }
  700. else {
  701. // Length of the vec_x shall be close to unity.
  702. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  703. if (l < 0.9 || l > 1.1) {
  704. // SERIAL_ECHOLNPGM("X vector length:");
  705. // MYSERIAL.println(l);
  706. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
  707. reset = true;
  708. }
  709. // Length of the vec_y shall be close to unity.
  710. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  711. if (l < 0.9 || l > 1.1) {
  712. // SERIAL_ECHOLNPGM("Y vector length:");
  713. // MYSERIAL.println(l);
  714. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the Y vector out of range.");
  715. reset = true;
  716. }
  717. // Correction of the zero point shall be reasonably small.
  718. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  719. if (l > 15.f) {
  720. // SERIAL_ECHOLNPGM("Zero point correction:");
  721. // MYSERIAL.println(l);
  722. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. Shift out of range.");
  723. reset = true;
  724. }
  725. // vec_x and vec_y shall be nearly perpendicular.
  726. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  727. if (fabs(l) > 0.1f) {
  728. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. X/Y axes are far from being perpendicular.");
  729. reset = true;
  730. }
  731. }
  732. if (reset) {
  733. // SERIAL_ECHOLNPGM("Invalid bed correction matrix. Resetting to identity.");
  734. reset_bed_offset_and_skew();
  735. world2machine_reset();
  736. } else {
  737. world2machine_update(vec_x, vec_y, cntr);
  738. /*
  739. SERIAL_ECHOPGM("world2machine_initialize() loaded: ");
  740. MYSERIAL.print(world2machine_rotation_and_skew[0][0], 5);
  741. SERIAL_ECHOPGM(", ");
  742. MYSERIAL.print(world2machine_rotation_and_skew[0][1], 5);
  743. SERIAL_ECHOPGM(", ");
  744. MYSERIAL.print(world2machine_rotation_and_skew[1][0], 5);
  745. SERIAL_ECHOPGM(", ");
  746. MYSERIAL.print(world2machine_rotation_and_skew[1][1], 5);
  747. SERIAL_ECHOPGM(", offset ");
  748. MYSERIAL.print(world2machine_shift[0], 5);
  749. SERIAL_ECHOPGM(", ");
  750. MYSERIAL.print(world2machine_shift[1], 5);
  751. SERIAL_ECHOLNPGM("");
  752. */
  753. }
  754. }
  755. // When switching from absolute to corrected coordinates,
  756. // this will get the absolute coordinates from the servos,
  757. // applies the inverse world2machine transformation
  758. // and stores the result into current_position[x,y].
  759. void world2machine_update_current()
  760. {
  761. float x = current_position[X_AXIS] - world2machine_shift[0];
  762. float y = current_position[Y_AXIS] - world2machine_shift[1];
  763. current_position[X_AXIS] = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  764. current_position[Y_AXIS] = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  765. }
  766. static inline void go_xyz(float x, float y, float z, float fr)
  767. {
  768. plan_buffer_line(x, y, z, current_position[E_AXIS], fr, active_extruder);
  769. st_synchronize();
  770. }
  771. static inline void go_xy(float x, float y, float fr)
  772. {
  773. plan_buffer_line(x, y, current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  774. st_synchronize();
  775. }
  776. static inline void go_to_current(float fr)
  777. {
  778. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr, active_extruder);
  779. st_synchronize();
  780. }
  781. static inline void update_current_position_xyz()
  782. {
  783. current_position[X_AXIS] = st_get_position_mm(X_AXIS);
  784. current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
  785. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  786. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  787. }
  788. static inline void update_current_position_z()
  789. {
  790. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  791. plan_set_z_position(current_position[Z_AXIS]);
  792. }
  793. // At the current position, find the Z stop.
  794. inline bool find_bed_induction_sensor_point_z(float minimum_z, uint8_t n_iter, int verbosity_level)
  795. {
  796. #ifdef SUPPORT_VERBOSITY
  797. if(verbosity_level >= 10) SERIAL_ECHOLNPGM("find bed induction sensor point z");
  798. #endif // SUPPORT_VERBOSITY
  799. bool endstops_enabled = enable_endstops(true);
  800. bool endstop_z_enabled = enable_z_endstop(false);
  801. float z = 0.f;
  802. endstop_z_hit_on_purpose();
  803. // move down until you find the bed
  804. current_position[Z_AXIS] = minimum_z;
  805. go_to_current(homing_feedrate[Z_AXIS]/60);
  806. // we have to let the planner know where we are right now as it is not where we said to go.
  807. update_current_position_z();
  808. if (! endstop_z_hit_on_purpose())
  809. goto error;
  810. for (uint8_t i = 0; i < n_iter; ++ i) {
  811. // Move up the retract distance.
  812. current_position[Z_AXIS] += .5f;
  813. go_to_current(homing_feedrate[Z_AXIS]/60);
  814. // Move back down slowly to find bed.
  815. current_position[Z_AXIS] = minimum_z;
  816. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  817. // we have to let the planner know where we are right now as it is not where we said to go.
  818. update_current_position_z();
  819. if (! endstop_z_hit_on_purpose())
  820. goto error;
  821. // SERIAL_ECHOPGM("Bed find_bed_induction_sensor_point_z low, height: ");
  822. // MYSERIAL.print(current_position[Z_AXIS], 5);
  823. // SERIAL_ECHOLNPGM("");
  824. z += current_position[Z_AXIS];
  825. }
  826. current_position[Z_AXIS] = z;
  827. if (n_iter > 1)
  828. current_position[Z_AXIS] /= float(n_iter);
  829. enable_endstops(endstops_enabled);
  830. enable_z_endstop(endstop_z_enabled);
  831. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 3");
  832. return true;
  833. error:
  834. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 4");
  835. enable_endstops(endstops_enabled);
  836. enable_z_endstop(endstop_z_enabled);
  837. return false;
  838. }
  839. #ifdef NEW_XYZCAL
  840. extern bool xyzcal_find_bed_induction_sensor_point_xy();
  841. #endif //NEW_XYZCAL
  842. // Search around the current_position[X,Y],
  843. // look for the induction sensor response.
  844. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  845. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  846. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (4.f)
  847. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  848. #ifdef HEATBED_V2
  849. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (2.f)
  850. #define FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR (0.03f)
  851. #else //HEATBED_V2
  852. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.2f)
  853. #endif //HEATBED_V2
  854. #ifdef HEATBED_V2
  855. /*inline */bool find_bed_induction_sensor_point_xy(int verbosity_level)
  856. {
  857. #ifdef NEW_XYZCAL
  858. return xyzcal_find_bed_induction_sensor_point_xy();
  859. #else //NEW_XYZCAL
  860. #ifdef SUPPORT_VERBOSITY
  861. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  862. #endif // SUPPORT_VERBOSITY
  863. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  864. bool found = false;
  865. {
  866. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  867. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  868. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  869. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  870. uint8_t nsteps_y;
  871. uint8_t i;
  872. if (x0 < X_MIN_POS) {
  873. x0 = X_MIN_POS;
  874. #ifdef SUPPORT_VERBOSITY
  875. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  876. #endif // SUPPORT_VERBOSITY
  877. }
  878. if (x1 > X_MAX_POS) {
  879. x1 = X_MAX_POS;
  880. #ifdef SUPPORT_VERBOSITY
  881. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  882. #endif // SUPPORT_VERBOSITY
  883. }
  884. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  885. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  886. #ifdef SUPPORT_VERBOSITY
  887. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  888. #endif // SUPPORT_VERBOSITY
  889. }
  890. if (y1 > Y_MAX_POS) {
  891. y1 = Y_MAX_POS;
  892. #ifdef SUPPORT_VERBOSITY
  893. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  894. #endif // SUPPORT_VERBOSITY
  895. }
  896. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  897. enable_endstops(false);
  898. bool dir_positive = true;
  899. float z_error = 2 * FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  900. float find_bed_induction_sensor_point_z_step = FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  901. float initial_z_position = current_position[Z_AXIS];
  902. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  903. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  904. // Continously lower the Z axis.
  905. endstops_hit_on_purpose();
  906. enable_z_endstop(true);
  907. bool direction = false;
  908. while (current_position[Z_AXIS] > -10.f && z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  909. // Do nsteps_y zig-zag movements.
  910. SERIAL_ECHOPGM("z_error: ");
  911. MYSERIAL.println(z_error);
  912. current_position[Y_AXIS] = direction ? y1 : y0;
  913. initial_z_position = current_position[Z_AXIS];
  914. 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) {
  915. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  916. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  917. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  918. dir_positive = !dir_positive;
  919. if (endstop_z_hit_on_purpose()) {
  920. update_current_position_xyz();
  921. z_error = initial_z_position - current_position[Z_AXIS] + find_bed_induction_sensor_point_z_step;
  922. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  923. find_bed_induction_sensor_point_z_step = z_error / 2;
  924. current_position[Z_AXIS] += z_error;
  925. enable_z_endstop(false);
  926. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  927. enable_z_endstop(true);
  928. }
  929. goto endloop;
  930. }
  931. }
  932. 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) {
  933. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  934. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  935. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  936. dir_positive = !dir_positive;
  937. if (endstop_z_hit_on_purpose()) {
  938. update_current_position_xyz();
  939. z_error = initial_z_position - current_position[Z_AXIS];
  940. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  941. find_bed_induction_sensor_point_z_step = z_error / 2;
  942. current_position[Z_AXIS] += z_error;
  943. enable_z_endstop(false);
  944. direction = !direction;
  945. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  946. enable_z_endstop(true);
  947. }
  948. goto endloop;
  949. }
  950. }
  951. endloop:;
  952. }
  953. #ifdef SUPPORT_VERBOSITY
  954. if (verbosity_level >= 20) {
  955. SERIAL_ECHO("First hit");
  956. SERIAL_ECHO("- X: ");
  957. MYSERIAL.print(current_position[X_AXIS]);
  958. SERIAL_ECHO("; Y: ");
  959. MYSERIAL.print(current_position[Y_AXIS]);
  960. SERIAL_ECHO("; Z: ");
  961. MYSERIAL.println(current_position[Z_AXIS]);
  962. }
  963. #endif //SUPPORT_VERBOSITY
  964. //lcd_show_fullscreen_message_and_wait_P(PSTR("First hit"));
  965. //lcd_update_enable(true);
  966. float init_x_position = current_position[X_AXIS];
  967. float init_y_position = current_position[Y_AXIS];
  968. // we have to let the planner know where we are right now as it is not where we said to go.
  969. update_current_position_xyz();
  970. enable_z_endstop(false);
  971. for (int8_t iter = 0; iter < 2; ++iter) {
  972. /*SERIAL_ECHOPGM("iter: ");
  973. MYSERIAL.println(iter);
  974. SERIAL_ECHOPGM("1 - current_position[Z_AXIS]: ");
  975. MYSERIAL.println(current_position[Z_AXIS]);*/
  976. // Slightly lower the Z axis to get a reliable trigger.
  977. current_position[Z_AXIS] -= 0.1f;
  978. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], homing_feedrate[Z_AXIS] / (60 * 10));
  979. SERIAL_ECHOPGM("2 - current_position[Z_AXIS]: ");
  980. MYSERIAL.println(current_position[Z_AXIS]);
  981. // Do nsteps_y zig-zag movements.
  982. float a, b;
  983. float avg[2] = { 0,0 };
  984. invert_z_endstop(true);
  985. for (int iteration = 0; iteration < 8; iteration++) {
  986. found = false;
  987. enable_z_endstop(true);
  988. go_xy(init_x_position + 16.0f, current_position[Y_AXIS], feedrate / 5);
  989. update_current_position_xyz();
  990. if (!endstop_z_hit_on_purpose()) {
  991. // SERIAL_ECHOLN("Search X span 0 - not found");
  992. continue;
  993. }
  994. // SERIAL_ECHOLN("Search X span 0 - found");
  995. a = current_position[X_AXIS];
  996. enable_z_endstop(false);
  997. go_xy(init_x_position, current_position[Y_AXIS], feedrate / 5);
  998. enable_z_endstop(true);
  999. go_xy(init_x_position - 16.0f, current_position[Y_AXIS], feedrate / 5);
  1000. update_current_position_xyz();
  1001. if (!endstop_z_hit_on_purpose()) {
  1002. // SERIAL_ECHOLN("Search X span 1 - not found");
  1003. continue;
  1004. }
  1005. // SERIAL_ECHOLN("Search X span 1 - found");
  1006. b = current_position[X_AXIS];
  1007. // Go to the center.
  1008. enable_z_endstop(false);
  1009. current_position[X_AXIS] = 0.5f * (a + b);
  1010. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1011. found = true;
  1012. // Search in the Y direction along a cross.
  1013. found = false;
  1014. enable_z_endstop(true);
  1015. go_xy(current_position[X_AXIS], init_y_position + 16.0f, feedrate / 5);
  1016. update_current_position_xyz();
  1017. if (!endstop_z_hit_on_purpose()) {
  1018. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1019. continue;
  1020. }
  1021. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1022. a = current_position[Y_AXIS];
  1023. enable_z_endstop(false);
  1024. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1025. enable_z_endstop(true);
  1026. go_xy(current_position[X_AXIS], init_y_position - 16.0f, feedrate / 5);
  1027. update_current_position_xyz();
  1028. if (!endstop_z_hit_on_purpose()) {
  1029. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1030. continue;
  1031. }
  1032. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1033. b = current_position[Y_AXIS];
  1034. // Go to the center.
  1035. enable_z_endstop(false);
  1036. current_position[Y_AXIS] = 0.5f * (a + b);
  1037. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate / 5);
  1038. #ifdef SUPPORT_VERBOSITY
  1039. if (verbosity_level >= 20) {
  1040. SERIAL_ECHOPGM("ITERATION: ");
  1041. MYSERIAL.println(iteration);
  1042. SERIAL_ECHOPGM("CURRENT POSITION X: ");
  1043. MYSERIAL.println(current_position[X_AXIS]);
  1044. SERIAL_ECHOPGM("CURRENT POSITION Y: ");
  1045. MYSERIAL.println(current_position[Y_AXIS]);
  1046. }
  1047. #endif //SUPPORT_VERBOSITY
  1048. if (iteration > 0) {
  1049. // Average the last 7 measurements.
  1050. avg[X_AXIS] += current_position[X_AXIS];
  1051. avg[Y_AXIS] += current_position[Y_AXIS];
  1052. }
  1053. init_x_position = current_position[X_AXIS];
  1054. init_y_position = current_position[Y_AXIS];
  1055. found = true;
  1056. }
  1057. invert_z_endstop(false);
  1058. avg[X_AXIS] *= (1.f / 7.f);
  1059. avg[Y_AXIS] *= (1.f / 7.f);
  1060. current_position[X_AXIS] = avg[X_AXIS];
  1061. current_position[Y_AXIS] = avg[Y_AXIS];
  1062. #ifdef SUPPORT_VERBOSITY
  1063. if (verbosity_level >= 20) {
  1064. SERIAL_ECHOPGM("AVG CURRENT POSITION X: ");
  1065. MYSERIAL.println(current_position[X_AXIS]);
  1066. SERIAL_ECHOPGM("AVG CURRENT POSITION Y: ");
  1067. MYSERIAL.println(current_position[Y_AXIS]);
  1068. }
  1069. #endif // SUPPORT_VERBOSITY
  1070. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1071. #ifdef SUPPORT_VERBOSITY
  1072. if (verbosity_level >= 20) {
  1073. lcd_show_fullscreen_message_and_wait_P(PSTR("Final position"));
  1074. lcd_update_enable(true);
  1075. }
  1076. #endif //SUPPORT_VERBOSITY
  1077. break;
  1078. }
  1079. }
  1080. enable_z_endstop(false);
  1081. invert_z_endstop(false);
  1082. return found;
  1083. #endif //NEW_XYZCAL
  1084. }
  1085. #else //HEATBED_V2
  1086. inline bool find_bed_induction_sensor_point_xy(int verbosity_level)
  1087. {
  1088. #ifdef SUPPORT_VERBOSITY
  1089. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  1090. #endif // SUPPORT_VERBOSITY
  1091. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1092. bool found = false;
  1093. {
  1094. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1095. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1096. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1097. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1098. uint8_t nsteps_y;
  1099. uint8_t i;
  1100. if (x0 < X_MIN_POS) {
  1101. x0 = X_MIN_POS;
  1102. #ifdef SUPPORT_VERBOSITY
  1103. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  1104. #endif // SUPPORT_VERBOSITY
  1105. }
  1106. if (x1 > X_MAX_POS) {
  1107. x1 = X_MAX_POS;
  1108. #ifdef SUPPORT_VERBOSITY
  1109. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  1110. #endif // SUPPORT_VERBOSITY
  1111. }
  1112. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1113. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1114. #ifdef SUPPORT_VERBOSITY
  1115. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1116. #endif // SUPPORT_VERBOSITY
  1117. }
  1118. if (y1 > Y_MAX_POS) {
  1119. y1 = Y_MAX_POS;
  1120. #ifdef SUPPORT_VERBOSITY
  1121. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1122. #endif // SUPPORT_VERBOSITY
  1123. }
  1124. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1125. enable_endstops(false);
  1126. bool dir_positive = true;
  1127. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1128. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1129. // Continously lower the Z axis.
  1130. endstops_hit_on_purpose();
  1131. enable_z_endstop(true);
  1132. while (current_position[Z_AXIS] > -10.f) {
  1133. // Do nsteps_y zig-zag movements.
  1134. current_position[Y_AXIS] = y0;
  1135. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i) {
  1136. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1137. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1138. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1139. dir_positive = !dir_positive;
  1140. if (endstop_z_hit_on_purpose())
  1141. goto endloop;
  1142. }
  1143. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i) {
  1144. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1145. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1146. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1147. dir_positive = !dir_positive;
  1148. if (endstop_z_hit_on_purpose())
  1149. goto endloop;
  1150. }
  1151. }
  1152. endloop:
  1153. // SERIAL_ECHOLN("First hit");
  1154. // we have to let the planner know where we are right now as it is not where we said to go.
  1155. update_current_position_xyz();
  1156. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  1157. for (int8_t iter = 0; iter < 3; ++iter) {
  1158. if (iter > 0) {
  1159. // Slightly lower the Z axis to get a reliable trigger.
  1160. current_position[Z_AXIS] -= 0.02f;
  1161. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS] / 60);
  1162. }
  1163. // Do nsteps_y zig-zag movements.
  1164. float a, b;
  1165. enable_endstops(false);
  1166. enable_z_endstop(false);
  1167. current_position[Y_AXIS] = y0;
  1168. go_xy(x0, current_position[Y_AXIS], feedrate);
  1169. enable_z_endstop(true);
  1170. found = false;
  1171. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1172. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1173. if (endstop_z_hit_on_purpose()) {
  1174. found = true;
  1175. break;
  1176. }
  1177. }
  1178. update_current_position_xyz();
  1179. if (!found) {
  1180. // SERIAL_ECHOLN("Search in Y - not found");
  1181. continue;
  1182. }
  1183. // SERIAL_ECHOLN("Search in Y - found");
  1184. a = current_position[Y_AXIS];
  1185. enable_z_endstop(false);
  1186. current_position[Y_AXIS] = y1;
  1187. go_xy(x0, current_position[Y_AXIS], feedrate);
  1188. enable_z_endstop(true);
  1189. found = false;
  1190. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1191. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1192. if (endstop_z_hit_on_purpose()) {
  1193. found = true;
  1194. break;
  1195. }
  1196. }
  1197. update_current_position_xyz();
  1198. if (!found) {
  1199. // SERIAL_ECHOLN("Search in Y2 - not found");
  1200. continue;
  1201. }
  1202. // SERIAL_ECHOLN("Search in Y2 - found");
  1203. b = current_position[Y_AXIS];
  1204. current_position[Y_AXIS] = 0.5f * (a + b);
  1205. // Search in the X direction along a cross.
  1206. found = false;
  1207. enable_z_endstop(false);
  1208. go_xy(x0, current_position[Y_AXIS], feedrate);
  1209. enable_z_endstop(true);
  1210. go_xy(x1, current_position[Y_AXIS], feedrate);
  1211. update_current_position_xyz();
  1212. if (!endstop_z_hit_on_purpose()) {
  1213. // SERIAL_ECHOLN("Search X span 0 - not found");
  1214. continue;
  1215. }
  1216. // SERIAL_ECHOLN("Search X span 0 - found");
  1217. a = current_position[X_AXIS];
  1218. enable_z_endstop(false);
  1219. go_xy(x1, current_position[Y_AXIS], feedrate);
  1220. enable_z_endstop(true);
  1221. go_xy(x0, current_position[Y_AXIS], feedrate);
  1222. update_current_position_xyz();
  1223. if (!endstop_z_hit_on_purpose()) {
  1224. // SERIAL_ECHOLN("Search X span 1 - not found");
  1225. continue;
  1226. }
  1227. // SERIAL_ECHOLN("Search X span 1 - found");
  1228. b = current_position[X_AXIS];
  1229. // Go to the center.
  1230. enable_z_endstop(false);
  1231. current_position[X_AXIS] = 0.5f * (a + b);
  1232. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1233. found = true;
  1234. #if 1
  1235. // Search in the Y direction along a cross.
  1236. found = false;
  1237. enable_z_endstop(false);
  1238. go_xy(current_position[X_AXIS], y0, feedrate);
  1239. enable_z_endstop(true);
  1240. go_xy(current_position[X_AXIS], y1, feedrate);
  1241. update_current_position_xyz();
  1242. if (!endstop_z_hit_on_purpose()) {
  1243. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1244. continue;
  1245. }
  1246. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1247. a = current_position[Y_AXIS];
  1248. enable_z_endstop(false);
  1249. go_xy(current_position[X_AXIS], y1, feedrate);
  1250. enable_z_endstop(true);
  1251. go_xy(current_position[X_AXIS], y0, feedrate);
  1252. update_current_position_xyz();
  1253. if (!endstop_z_hit_on_purpose()) {
  1254. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1255. continue;
  1256. }
  1257. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1258. b = current_position[Y_AXIS];
  1259. // Go to the center.
  1260. enable_z_endstop(false);
  1261. current_position[Y_AXIS] = 0.5f * (a + b);
  1262. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1263. found = true;
  1264. #endif
  1265. break;
  1266. }
  1267. }
  1268. enable_z_endstop(false);
  1269. return found;
  1270. }
  1271. #endif //HEATBED_V2
  1272. #ifndef NEW_XYZCAL
  1273. // Search around the current_position[X,Y,Z].
  1274. // It is expected, that the induction sensor is switched on at the current position.
  1275. // Look around this center point by painting a star around the point.
  1276. inline bool improve_bed_induction_sensor_point()
  1277. {
  1278. static const float search_radius = 8.f;
  1279. bool endstops_enabled = enable_endstops(false);
  1280. bool endstop_z_enabled = enable_z_endstop(false);
  1281. bool found = false;
  1282. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1283. float center_old_x = current_position[X_AXIS];
  1284. float center_old_y = current_position[Y_AXIS];
  1285. float center_x = 0.f;
  1286. float center_y = 0.f;
  1287. for (uint8_t iter = 0; iter < 4; ++ iter) {
  1288. switch (iter) {
  1289. case 0:
  1290. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1291. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1292. break;
  1293. case 1:
  1294. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1295. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1296. break;
  1297. case 2:
  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 3:
  1302. default:
  1303. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1304. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1305. break;
  1306. }
  1307. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  1308. float vx = destination[X_AXIS] - center_old_x;
  1309. float vy = destination[Y_AXIS] - center_old_y;
  1310. float l = sqrt(vx*vx+vy*vy);
  1311. float t;
  1312. if (destination[X_AXIS] < X_MIN_POS) {
  1313. // Exiting the bed at xmin.
  1314. t = (center_x - X_MIN_POS) / l;
  1315. destination[X_AXIS] = X_MIN_POS;
  1316. destination[Y_AXIS] = center_old_y + t * vy;
  1317. } else if (destination[X_AXIS] > X_MAX_POS) {
  1318. // Exiting the bed at xmax.
  1319. t = (X_MAX_POS - center_x) / l;
  1320. destination[X_AXIS] = X_MAX_POS;
  1321. destination[Y_AXIS] = center_old_y + t * vy;
  1322. }
  1323. if (destination[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1324. // Exiting the bed at ymin.
  1325. t = (center_y - Y_MIN_POS_FOR_BED_CALIBRATION) / l;
  1326. destination[X_AXIS] = center_old_x + t * vx;
  1327. destination[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1328. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  1329. // Exiting the bed at xmax.
  1330. t = (Y_MAX_POS - center_y) / l;
  1331. destination[X_AXIS] = center_old_x + t * vx;
  1332. destination[Y_AXIS] = Y_MAX_POS;
  1333. }
  1334. // Move away from the measurement point.
  1335. enable_endstops(false);
  1336. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  1337. // Move towards the measurement point, until the induction sensor triggers.
  1338. enable_endstops(true);
  1339. go_xy(center_old_x, center_old_y, feedrate);
  1340. update_current_position_xyz();
  1341. // if (! endstop_z_hit_on_purpose()) return false;
  1342. center_x += current_position[X_AXIS];
  1343. center_y += current_position[Y_AXIS];
  1344. }
  1345. // Calculate the new center, move to the new center.
  1346. center_x /= 4.f;
  1347. center_y /= 4.f;
  1348. current_position[X_AXIS] = center_x;
  1349. current_position[Y_AXIS] = center_y;
  1350. enable_endstops(false);
  1351. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1352. enable_endstops(endstops_enabled);
  1353. enable_z_endstop(endstop_z_enabled);
  1354. return found;
  1355. }
  1356. #endif //NEW_XYZCAL
  1357. #ifndef NEW_XYZCAL
  1358. static inline void debug_output_point(const char *type, const float &x, const float &y, const float &z)
  1359. {
  1360. SERIAL_ECHOPGM("Measured ");
  1361. SERIAL_ECHORPGM(type);
  1362. SERIAL_ECHOPGM(" ");
  1363. MYSERIAL.print(x, 5);
  1364. SERIAL_ECHOPGM(", ");
  1365. MYSERIAL.print(y, 5);
  1366. SERIAL_ECHOPGM(", ");
  1367. MYSERIAL.print(z, 5);
  1368. SERIAL_ECHOLNPGM("");
  1369. }
  1370. #endif //NEW_XYZCAL
  1371. #ifndef NEW_XYZCAL
  1372. // Search around the current_position[X,Y,Z].
  1373. // It is expected, that the induction sensor is switched on at the current position.
  1374. // Look around this center point by painting a star around the point.
  1375. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  1376. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y, int8_t verbosity_level)
  1377. {
  1378. float center_old_x = current_position[X_AXIS];
  1379. float center_old_y = current_position[Y_AXIS];
  1380. float a, b;
  1381. bool point_small = false;
  1382. enable_endstops(false);
  1383. {
  1384. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1385. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1386. if (x0 < X_MIN_POS)
  1387. x0 = X_MIN_POS;
  1388. if (x1 > X_MAX_POS)
  1389. x1 = X_MAX_POS;
  1390. // Search in the X direction along a cross.
  1391. enable_z_endstop(false);
  1392. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1393. enable_z_endstop(true);
  1394. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1395. update_current_position_xyz();
  1396. if (! endstop_z_hit_on_purpose()) {
  1397. current_position[X_AXIS] = center_old_x;
  1398. goto canceled;
  1399. }
  1400. a = current_position[X_AXIS];
  1401. enable_z_endstop(false);
  1402. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1403. enable_z_endstop(true);
  1404. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1405. update_current_position_xyz();
  1406. if (! endstop_z_hit_on_purpose()) {
  1407. current_position[X_AXIS] = center_old_x;
  1408. goto canceled;
  1409. }
  1410. b = current_position[X_AXIS];
  1411. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1412. #ifdef SUPPORT_VERBOSITY
  1413. if (verbosity_level >= 5) {
  1414. SERIAL_ECHOPGM("Point width too small: ");
  1415. SERIAL_ECHO(b - a);
  1416. SERIAL_ECHOLNPGM("");
  1417. }
  1418. #endif // SUPPORT_VERBOSITY
  1419. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1420. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1421. // Don't use the new X value.
  1422. current_position[X_AXIS] = center_old_x;
  1423. goto canceled;
  1424. } else {
  1425. // Use the new value, but force the Z axis to go a bit lower.
  1426. point_small = true;
  1427. }
  1428. }
  1429. #ifdef SUPPORT_VERBOSITY
  1430. if (verbosity_level >= 5) {
  1431. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1432. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1433. }
  1434. #endif // SUPPORT_VERBOSITY
  1435. // Go to the center.
  1436. enable_z_endstop(false);
  1437. current_position[X_AXIS] = 0.5f * (a + b);
  1438. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1439. }
  1440. {
  1441. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1442. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1443. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1444. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1445. if (y1 > Y_MAX_POS)
  1446. y1 = Y_MAX_POS;
  1447. // Search in the Y direction along a cross.
  1448. enable_z_endstop(false);
  1449. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1450. if (lift_z_on_min_y) {
  1451. // The first row of points are very close to the end stop.
  1452. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  1453. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  1454. // and go back.
  1455. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  1456. }
  1457. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  1458. // Already triggering before we started the move.
  1459. // Shift the trigger point slightly outwards.
  1460. // a = current_position[Y_AXIS] - 1.5f;
  1461. a = current_position[Y_AXIS];
  1462. } else {
  1463. enable_z_endstop(true);
  1464. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1465. update_current_position_xyz();
  1466. if (! endstop_z_hit_on_purpose()) {
  1467. current_position[Y_AXIS] = center_old_y;
  1468. goto canceled;
  1469. }
  1470. a = current_position[Y_AXIS];
  1471. }
  1472. enable_z_endstop(false);
  1473. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1474. enable_z_endstop(true);
  1475. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1476. update_current_position_xyz();
  1477. if (! endstop_z_hit_on_purpose()) {
  1478. current_position[Y_AXIS] = center_old_y;
  1479. goto canceled;
  1480. }
  1481. b = current_position[Y_AXIS];
  1482. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1483. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1484. #ifdef SUPPORT_VERBOSITY
  1485. if (verbosity_level >= 5) {
  1486. SERIAL_ECHOPGM("Point height too small: ");
  1487. SERIAL_ECHO(b - a);
  1488. SERIAL_ECHOLNPGM("");
  1489. }
  1490. #endif // SUPPORT_VERBOSITY
  1491. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1492. // Don't use the new Y value.
  1493. current_position[Y_AXIS] = center_old_y;
  1494. goto canceled;
  1495. } else {
  1496. // Use the new value, but force the Z axis to go a bit lower.
  1497. point_small = true;
  1498. }
  1499. }
  1500. #ifdef SUPPORT_VERBOSITY
  1501. if (verbosity_level >= 5) {
  1502. debug_output_point(PSTR("top" ), current_position[X_AXIS], a, current_position[Z_AXIS]);
  1503. debug_output_point(PSTR("bottom"), current_position[X_AXIS], b, current_position[Z_AXIS]);
  1504. }
  1505. #endif // SUPPORT_VERBOSITY
  1506. // Go to the center.
  1507. enable_z_endstop(false);
  1508. current_position[Y_AXIS] = 0.5f * (a + b);
  1509. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1510. }
  1511. // If point is small but not too small, then force the Z axis to be lowered a bit,
  1512. // but use the new value. This is important when the initial position was off in one axis,
  1513. // for example if the initial calibration was shifted in the Y axis systematically.
  1514. // Then this first step will center.
  1515. return ! point_small;
  1516. canceled:
  1517. // Go back to the center.
  1518. enable_z_endstop(false);
  1519. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1520. return false;
  1521. }
  1522. #endif //NEW_XYZCAL
  1523. #ifndef NEW_XYZCAL
  1524. // Searching the front points, where one cannot move the sensor head in front of the sensor point.
  1525. // Searching in a zig-zag movement in a plane for the maximum width of the response.
  1526. // This function may set the current_position[Y_AXIS] below Y_MIN_POS, if the function succeeded.
  1527. // If this function failed, the Y coordinate will never be outside the working space.
  1528. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS (8.f)
  1529. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y (0.1f)
  1530. inline bool improve_bed_induction_sensor_point3(int verbosity_level)
  1531. {
  1532. float center_old_x = current_position[X_AXIS];
  1533. float center_old_y = current_position[Y_AXIS];
  1534. float a, b;
  1535. bool result = true;
  1536. #ifdef SUPPORT_VERBOSITY
  1537. if (verbosity_level >= 20) MYSERIAL.println("Improve bed induction sensor point3");
  1538. #endif // SUPPORT_VERBOSITY
  1539. // Was the sensor point detected too far in the minus Y axis?
  1540. // If yes, the center of the induction point cannot be reached by the machine.
  1541. {
  1542. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1543. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1544. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1545. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1546. float y = y0;
  1547. if (x0 < X_MIN_POS)
  1548. x0 = X_MIN_POS;
  1549. if (x1 > X_MAX_POS)
  1550. x1 = X_MAX_POS;
  1551. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1552. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1553. if (y1 > Y_MAX_POS)
  1554. y1 = Y_MAX_POS;
  1555. #ifdef SUPPORT_VERBOSITY
  1556. if (verbosity_level >= 20) {
  1557. SERIAL_ECHOPGM("Initial position: ");
  1558. SERIAL_ECHO(center_old_x);
  1559. SERIAL_ECHOPGM(", ");
  1560. SERIAL_ECHO(center_old_y);
  1561. SERIAL_ECHOLNPGM("");
  1562. }
  1563. #endif // SUPPORT_VERBOSITY
  1564. // Search in the positive Y direction, until a maximum diameter is found.
  1565. // (the next diameter is smaller than the current one.)
  1566. float dmax = 0.f;
  1567. float xmax1 = 0.f;
  1568. float xmax2 = 0.f;
  1569. for (y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1570. enable_z_endstop(false);
  1571. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1572. enable_z_endstop(true);
  1573. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1574. update_current_position_xyz();
  1575. if (! endstop_z_hit_on_purpose()) {
  1576. continue;
  1577. // SERIAL_PROTOCOLPGM("Failed 1\n");
  1578. // current_position[X_AXIS] = center_old_x;
  1579. // goto canceled;
  1580. }
  1581. a = current_position[X_AXIS];
  1582. enable_z_endstop(false);
  1583. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1584. enable_z_endstop(true);
  1585. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1586. update_current_position_xyz();
  1587. if (! endstop_z_hit_on_purpose()) {
  1588. continue;
  1589. // SERIAL_PROTOCOLPGM("Failed 2\n");
  1590. // current_position[X_AXIS] = center_old_x;
  1591. // goto canceled;
  1592. }
  1593. b = current_position[X_AXIS];
  1594. #ifdef SUPPORT_VERBOSITY
  1595. if (verbosity_level >= 5) {
  1596. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1597. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1598. }
  1599. #endif // SUPPORT_VERBOSITY
  1600. float d = b - a;
  1601. if (d > dmax) {
  1602. xmax1 = 0.5f * (a + b);
  1603. dmax = d;
  1604. } else if (dmax > 0.) {
  1605. y0 = y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1606. break;
  1607. }
  1608. }
  1609. if (dmax == 0.) {
  1610. #ifdef SUPPORT_VERBOSITY
  1611. if (verbosity_level > 0)
  1612. SERIAL_PROTOCOLPGM("failed - not found\n");
  1613. #endif // SUPPORT_VERBOSITY
  1614. current_position[X_AXIS] = center_old_x;
  1615. current_position[Y_AXIS] = center_old_y;
  1616. goto canceled;
  1617. }
  1618. {
  1619. // Find the positive Y hit. This gives the extreme Y value for the search of the maximum diameter in the -Y direction.
  1620. enable_z_endstop(false);
  1621. go_xy(xmax1, y0 + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1622. enable_z_endstop(true);
  1623. go_xy(xmax1, max(y0 - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1624. update_current_position_xyz();
  1625. if (! endstop_z_hit_on_purpose()) {
  1626. current_position[Y_AXIS] = center_old_y;
  1627. goto canceled;
  1628. }
  1629. #ifdef SUPPORT_VERBOSITY
  1630. if (verbosity_level >= 5)
  1631. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1632. #endif // SUPPORT_VERBOSITY
  1633. y1 = current_position[Y_AXIS];
  1634. }
  1635. if (y1 <= y0) {
  1636. // Either the induction sensor is too high, or the induction sensor target is out of reach.
  1637. current_position[Y_AXIS] = center_old_y;
  1638. goto canceled;
  1639. }
  1640. // Search in the negative Y direction, until a maximum diameter is found.
  1641. dmax = 0.f;
  1642. // if (y0 + 1.f < y1)
  1643. // y1 = y0 + 1.f;
  1644. for (y = y1; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1645. enable_z_endstop(false);
  1646. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1647. enable_z_endstop(true);
  1648. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1649. update_current_position_xyz();
  1650. if (! endstop_z_hit_on_purpose()) {
  1651. continue;
  1652. /*
  1653. current_position[X_AXIS] = center_old_x;
  1654. SERIAL_PROTOCOLPGM("Failed 3\n");
  1655. goto canceled;
  1656. */
  1657. }
  1658. a = current_position[X_AXIS];
  1659. enable_z_endstop(false);
  1660. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1661. enable_z_endstop(true);
  1662. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1663. update_current_position_xyz();
  1664. if (! endstop_z_hit_on_purpose()) {
  1665. continue;
  1666. /*
  1667. current_position[X_AXIS] = center_old_x;
  1668. SERIAL_PROTOCOLPGM("Failed 4\n");
  1669. goto canceled;
  1670. */
  1671. }
  1672. b = current_position[X_AXIS];
  1673. #ifdef SUPPORT_VERBOSITY
  1674. if (verbosity_level >= 5) {
  1675. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1676. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1677. }
  1678. #endif // SUPPORT_VERBOSITY
  1679. float d = b - a;
  1680. if (d > dmax) {
  1681. xmax2 = 0.5f * (a + b);
  1682. dmax = d;
  1683. } else if (dmax > 0.) {
  1684. y1 = y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1685. break;
  1686. }
  1687. }
  1688. float xmax, ymax;
  1689. if (dmax == 0.f) {
  1690. // Only the hit in the positive direction found.
  1691. xmax = xmax1;
  1692. ymax = y0;
  1693. } else {
  1694. // Both positive and negative directions found.
  1695. xmax = xmax2;
  1696. ymax = 0.5f * (y0 + y1);
  1697. for (; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1698. enable_z_endstop(false);
  1699. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1700. enable_z_endstop(true);
  1701. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1702. update_current_position_xyz();
  1703. if (! endstop_z_hit_on_purpose()) {
  1704. continue;
  1705. /*
  1706. current_position[X_AXIS] = center_old_x;
  1707. SERIAL_PROTOCOLPGM("Failed 3\n");
  1708. goto canceled;
  1709. */
  1710. }
  1711. a = current_position[X_AXIS];
  1712. enable_z_endstop(false);
  1713. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1714. enable_z_endstop(true);
  1715. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1716. update_current_position_xyz();
  1717. if (! endstop_z_hit_on_purpose()) {
  1718. continue;
  1719. /*
  1720. current_position[X_AXIS] = center_old_x;
  1721. SERIAL_PROTOCOLPGM("Failed 4\n");
  1722. goto canceled;
  1723. */
  1724. }
  1725. b = current_position[X_AXIS];
  1726. #ifdef SUPPORT_VERBOSITY
  1727. if (verbosity_level >= 5) {
  1728. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1729. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1730. }
  1731. #endif // SUPPORT_VERBOSITY
  1732. float d = b - a;
  1733. if (d > dmax) {
  1734. xmax = 0.5f * (a + b);
  1735. ymax = y;
  1736. dmax = d;
  1737. }
  1738. }
  1739. }
  1740. {
  1741. // Compare the distance in the Y+ direction with the diameter in the X direction.
  1742. // Find the positive Y hit once again, this time along the Y axis going through the X point with the highest diameter.
  1743. enable_z_endstop(false);
  1744. go_xy(xmax, ymax + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1745. enable_z_endstop(true);
  1746. go_xy(xmax, max(ymax - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1747. update_current_position_xyz();
  1748. if (! endstop_z_hit_on_purpose()) {
  1749. current_position[Y_AXIS] = center_old_y;
  1750. goto canceled;
  1751. }
  1752. #ifdef SUPPORT_VERBOSITY
  1753. if (verbosity_level >= 5)
  1754. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1755. #endif // SUPPORT_VERBOSITY
  1756. if (current_position[Y_AXIS] - Y_MIN_POS_FOR_BED_CALIBRATION < 0.5f * dmax) {
  1757. // Probably not even a half circle was detected. The induction point is likely too far in the minus Y direction.
  1758. // First verify, if the measurement has been done at a sufficient height. If no, lower the Z axis a bit.
  1759. if (current_position[Y_AXIS] < ymax || dmax < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1760. #ifdef SUPPORT_VERBOSITY
  1761. if (verbosity_level >= 5) {
  1762. SERIAL_ECHOPGM("Partial point diameter too small: ");
  1763. SERIAL_ECHO(dmax);
  1764. SERIAL_ECHOLNPGM("");
  1765. }
  1766. #endif // SUPPORT_VERBOSITY
  1767. result = false;
  1768. } else {
  1769. // Estimate the circle radius from the maximum diameter and height:
  1770. float h = current_position[Y_AXIS] - ymax;
  1771. float r = dmax * dmax / (8.f * h) + 0.5f * h;
  1772. if (r < 0.8f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1773. #ifdef SUPPORT_VERBOSITY
  1774. if (verbosity_level >= 5) {
  1775. SERIAL_ECHOPGM("Partial point estimated radius too small: ");
  1776. SERIAL_ECHO(r);
  1777. SERIAL_ECHOPGM(", dmax:");
  1778. SERIAL_ECHO(dmax);
  1779. SERIAL_ECHOPGM(", h:");
  1780. SERIAL_ECHO(h);
  1781. SERIAL_ECHOLNPGM("");
  1782. }
  1783. #endif // SUPPORT_VERBOSITY
  1784. result = false;
  1785. } else {
  1786. // The point may end up outside of the machine working space.
  1787. // That is all right as it helps to improve the accuracy of the measurement point
  1788. // due to averaging.
  1789. // For the y correction, use an average of dmax/2 and the estimated radius.
  1790. r = 0.5f * (0.5f * dmax + r);
  1791. ymax = current_position[Y_AXIS] - r;
  1792. }
  1793. }
  1794. } else {
  1795. // If the diameter of the detected spot was smaller than a minimum allowed,
  1796. // the induction sensor is probably too high. Returning false will force
  1797. // the sensor to be lowered a tiny bit.
  1798. result = xmax >= MIN_BED_SENSOR_POINT_RESPONSE_DMR;
  1799. if (y0 > Y_MIN_POS_FOR_BED_CALIBRATION + 0.2f)
  1800. // Only in case both left and right y tangents are known, use them.
  1801. // If y0 is close to the bed edge, it may not be symmetric to the right tangent.
  1802. ymax = 0.5f * ymax + 0.25f * (y0 + y1);
  1803. }
  1804. }
  1805. // Go to the center.
  1806. enable_z_endstop(false);
  1807. current_position[X_AXIS] = xmax;
  1808. current_position[Y_AXIS] = ymax;
  1809. #ifdef SUPPORT_VERBOSITY
  1810. if (verbosity_level >= 20) {
  1811. SERIAL_ECHOPGM("Adjusted position: ");
  1812. SERIAL_ECHO(current_position[X_AXIS]);
  1813. SERIAL_ECHOPGM(", ");
  1814. SERIAL_ECHO(current_position[Y_AXIS]);
  1815. SERIAL_ECHOLNPGM("");
  1816. }
  1817. #endif // SUPPORT_VERBOSITY
  1818. // Don't clamp current_position[Y_AXIS], because the out-of-reach Y coordinate may actually be true.
  1819. // Only clamp the coordinate to go.
  1820. go_xy(current_position[X_AXIS], max(Y_MIN_POS, current_position[Y_AXIS]), homing_feedrate[X_AXIS] / 60.f);
  1821. // delay_keep_alive(3000);
  1822. }
  1823. if (result)
  1824. return true;
  1825. // otherwise clamp the Y coordinate
  1826. canceled:
  1827. // Go back to the center.
  1828. enable_z_endstop(false);
  1829. if (current_position[Y_AXIS] < Y_MIN_POS)
  1830. current_position[Y_AXIS] = Y_MIN_POS;
  1831. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1832. return false;
  1833. }
  1834. #endif //NEW_XYZCAL
  1835. #ifndef NEW_XYZCAL
  1836. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  1837. // write the trigger coordinates to the serial line.
  1838. // Useful for visualizing the behavior of the bed induction detector.
  1839. inline void scan_bed_induction_sensor_point()
  1840. {
  1841. float center_old_x = current_position[X_AXIS];
  1842. float center_old_y = current_position[Y_AXIS];
  1843. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1844. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1845. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1846. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1847. float y = y0;
  1848. if (x0 < X_MIN_POS)
  1849. x0 = X_MIN_POS;
  1850. if (x1 > X_MAX_POS)
  1851. x1 = X_MAX_POS;
  1852. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1853. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1854. if (y1 > Y_MAX_POS)
  1855. y1 = Y_MAX_POS;
  1856. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1857. enable_z_endstop(false);
  1858. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1859. enable_z_endstop(true);
  1860. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1861. update_current_position_xyz();
  1862. if (endstop_z_hit_on_purpose())
  1863. debug_output_point(PSTR("left" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1864. enable_z_endstop(false);
  1865. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1866. enable_z_endstop(true);
  1867. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1868. update_current_position_xyz();
  1869. if (endstop_z_hit_on_purpose())
  1870. debug_output_point(PSTR("right"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1871. }
  1872. enable_z_endstop(false);
  1873. current_position[X_AXIS] = center_old_x;
  1874. current_position[Y_AXIS] = center_old_y;
  1875. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1876. }
  1877. #endif //NEW_XYZCAL
  1878. #define MESH_BED_CALIBRATION_SHOW_LCD
  1879. BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask)
  1880. {
  1881. // Don't let the manage_inactivity() function remove power from the motors.
  1882. refresh_cmd_timeout();
  1883. // Reusing the z_values memory for the measurement cache.
  1884. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  1885. float *pts = &mbl.z_values[0][0];
  1886. float *vec_x = pts + 2 * 4;
  1887. float *vec_y = vec_x + 2;
  1888. float *cntr = vec_y + 2;
  1889. memset(pts, 0, sizeof(float) * 7 * 7);
  1890. uint8_t iteration = 0;
  1891. BedSkewOffsetDetectionResultType result;
  1892. // SERIAL_ECHOLNPGM("find_bed_offset_and_skew verbosity level: ");
  1893. // SERIAL_ECHO(int(verbosity_level));
  1894. // SERIAL_ECHOPGM("");
  1895. while (iteration < 3) {
  1896. SERIAL_ECHOPGM("Iteration: ");
  1897. MYSERIAL.println(int(iteration + 1));
  1898. #ifdef SUPPORT_VERBOSITY
  1899. if (verbosity_level >= 20) {
  1900. SERIAL_ECHOLNPGM("Vectors: ");
  1901. SERIAL_ECHOPGM("vec_x[0]:");
  1902. MYSERIAL.print(vec_x[0], 5);
  1903. SERIAL_ECHOLNPGM("");
  1904. SERIAL_ECHOPGM("vec_x[1]:");
  1905. MYSERIAL.print(vec_x[1], 5);
  1906. SERIAL_ECHOLNPGM("");
  1907. SERIAL_ECHOPGM("vec_y[0]:");
  1908. MYSERIAL.print(vec_y[0], 5);
  1909. SERIAL_ECHOLNPGM("");
  1910. SERIAL_ECHOPGM("vec_y[1]:");
  1911. MYSERIAL.print(vec_y[1], 5);
  1912. SERIAL_ECHOLNPGM("");
  1913. SERIAL_ECHOPGM("cntr[0]:");
  1914. MYSERIAL.print(cntr[0], 5);
  1915. SERIAL_ECHOLNPGM("");
  1916. SERIAL_ECHOPGM("cntr[1]:");
  1917. MYSERIAL.print(cntr[1], 5);
  1918. SERIAL_ECHOLNPGM("");
  1919. }
  1920. #endif // SUPPORT_VERBOSITY
  1921. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1922. uint8_t next_line;
  1923. lcd_display_message_fullscreen_P(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1, next_line);
  1924. if (next_line > 3)
  1925. next_line = 3;
  1926. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1927. // Collect the rear 2x3 points.
  1928. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  1929. for (int k = 0; k < 4; ++k) {
  1930. // Don't let the manage_inactivity() function remove power from the motors.
  1931. refresh_cmd_timeout();
  1932. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  1933. lcd_implementation_print_at(0, next_line, k + 1);
  1934. lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
  1935. if (iteration > 0) {
  1936. lcd_print_at_PGM(0, next_line + 1, MSG_FIND_BED_OFFSET_AND_SKEW_ITERATION);
  1937. lcd_implementation_print(int(iteration + 1));
  1938. }
  1939. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  1940. float *pt = pts + k * 2;
  1941. // Go up to z_initial.
  1942. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  1943. #ifdef SUPPORT_VERBOSITY
  1944. if (verbosity_level >= 20) {
  1945. // Go to Y0, wait, then go to Y-4.
  1946. current_position[Y_AXIS] = 0.f;
  1947. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1948. SERIAL_ECHOLNPGM("At Y0");
  1949. delay_keep_alive(5000);
  1950. current_position[Y_AXIS] = Y_MIN_POS;
  1951. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1952. SERIAL_ECHOLNPGM("At Y-4");
  1953. delay_keep_alive(5000);
  1954. }
  1955. #endif // SUPPORT_VERBOSITY
  1956. // Go to the measurement point position.
  1957. //if (iteration == 0) {
  1958. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2);
  1959. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2 + 1);
  1960. /*}
  1961. else {
  1962. // if first iteration failed, count corrected point coordinates as initial
  1963. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  1964. 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];
  1965. 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];
  1966. // The calibration points are very close to the min Y.
  1967. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  1968. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1969. }*/
  1970. #ifdef SUPPORT_VERBOSITY
  1971. if (verbosity_level >= 20) {
  1972. SERIAL_ECHOPGM("current_position[X_AXIS]:");
  1973. MYSERIAL.print(current_position[X_AXIS], 5);
  1974. SERIAL_ECHOLNPGM("");
  1975. SERIAL_ECHOPGM("current_position[Y_AXIS]:");
  1976. MYSERIAL.print(current_position[Y_AXIS], 5);
  1977. SERIAL_ECHOLNPGM("");
  1978. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  1979. MYSERIAL.print(current_position[Z_AXIS], 5);
  1980. SERIAL_ECHOLNPGM("");
  1981. }
  1982. #endif // SUPPORT_VERBOSITY
  1983. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  1984. #ifdef SUPPORT_VERBOSITY
  1985. if (verbosity_level >= 10)
  1986. delay_keep_alive(3000);
  1987. #endif // SUPPORT_VERBOSITY
  1988. if (!find_bed_induction_sensor_point_xy(verbosity_level))
  1989. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  1990. #ifndef NEW_XYZCAL
  1991. #ifndef HEATBED_V2
  1992. if (k == 0 || k == 1) {
  1993. // Improve the position of the 1st row sensor points by a zig-zag movement.
  1994. find_bed_induction_sensor_point_z();
  1995. int8_t i = 4;
  1996. for (;;) {
  1997. if (improve_bed_induction_sensor_point3(verbosity_level))
  1998. break;
  1999. if (--i == 0)
  2000. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2001. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2002. current_position[Z_AXIS] -= 0.025f;
  2003. enable_endstops(false);
  2004. enable_z_endstop(false);
  2005. go_to_current(homing_feedrate[Z_AXIS]);
  2006. }
  2007. if (i == 0)
  2008. // not found
  2009. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2010. }
  2011. #endif //HEATBED_V2
  2012. #endif
  2013. #ifdef SUPPORT_VERBOSITY
  2014. if (verbosity_level >= 10)
  2015. delay_keep_alive(3000);
  2016. #endif // SUPPORT_VERBOSITY
  2017. // Save the detected point position and then clamp the Y coordinate, which may have been estimated
  2018. // to lie outside the machine working space.
  2019. #ifdef SUPPORT_VERBOSITY
  2020. if (verbosity_level >= 20) {
  2021. SERIAL_ECHOLNPGM("Measured:");
  2022. MYSERIAL.println(current_position[X_AXIS]);
  2023. MYSERIAL.println(current_position[Y_AXIS]);
  2024. }
  2025. #endif // SUPPORT_VERBOSITY
  2026. pt[0] = (pt[0] * iteration) / (iteration + 1);
  2027. pt[0] += (current_position[X_AXIS]/(iteration + 1)); //count average
  2028. pt[1] = (pt[1] * iteration) / (iteration + 1);
  2029. pt[1] += (current_position[Y_AXIS] / (iteration + 1));
  2030. //pt[0] += current_position[X_AXIS];
  2031. //if(iteration > 0) pt[0] = pt[0] / 2;
  2032. //pt[1] += current_position[Y_AXIS];
  2033. //if (iteration > 0) pt[1] = pt[1] / 2;
  2034. #ifdef SUPPORT_VERBOSITY
  2035. if (verbosity_level >= 20) {
  2036. SERIAL_ECHOLNPGM("");
  2037. SERIAL_ECHOPGM("pt[0]:");
  2038. MYSERIAL.println(pt[0]);
  2039. SERIAL_ECHOPGM("pt[1]:");
  2040. MYSERIAL.println(pt[1]);
  2041. }
  2042. #endif // SUPPORT_VERBOSITY
  2043. if (current_position[Y_AXIS] < Y_MIN_POS)
  2044. current_position[Y_AXIS] = Y_MIN_POS;
  2045. // Start searching for the other points at 3mm above the last point.
  2046. current_position[Z_AXIS] += 3.f + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2047. //cntr[0] += pt[0];
  2048. //cntr[1] += pt[1];
  2049. #ifdef SUPPORT_VERBOSITY
  2050. if (verbosity_level >= 10 && k == 0) {
  2051. // Show the zero. Test, whether the Y motor skipped steps.
  2052. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  2053. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2054. delay_keep_alive(3000);
  2055. }
  2056. #endif // SUPPORT_VERBOSITY
  2057. }
  2058. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2059. #ifdef SUPPORT_VERBOSITY
  2060. if (verbosity_level >= 20) {
  2061. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2062. delay_keep_alive(3000);
  2063. for (int8_t mesh_point = 0; mesh_point < 4; ++mesh_point) {
  2064. // Don't let the manage_inactivity() function remove power from the motors.
  2065. refresh_cmd_timeout();
  2066. // Go to the measurement point.
  2067. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2068. current_position[X_AXIS] = pts[mesh_point * 2];
  2069. current_position[Y_AXIS] = pts[mesh_point * 2 + 1];
  2070. go_to_current(homing_feedrate[X_AXIS] / 60);
  2071. delay_keep_alive(3000);
  2072. }
  2073. }
  2074. #endif // SUPPORT_VERBOSITY
  2075. if (pts[1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  2076. too_far_mask |= 1 << 1; //front center point is out of reach
  2077. SERIAL_ECHOLNPGM("");
  2078. SERIAL_ECHOPGM("WARNING: Front point not reachable. Y coordinate:");
  2079. MYSERIAL.print(pts[1]);
  2080. SERIAL_ECHOPGM(" < ");
  2081. MYSERIAL.println(Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2082. }
  2083. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2084. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2085. if (result >= 0) {
  2086. world2machine_update(vec_x, vec_y, cntr);
  2087. #if 1
  2088. // Fearlessly store the calibration values into the eeprom.
  2089. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0), cntr[0]);
  2090. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4), cntr[1]);
  2091. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0), vec_x[0]);
  2092. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4), vec_x[1]);
  2093. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0), vec_y[0]);
  2094. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4), vec_y[1]);
  2095. #endif
  2096. #ifdef SUPPORT_VERBOSITY
  2097. if (verbosity_level >= 10) {
  2098. // Length of the vec_x
  2099. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  2100. SERIAL_ECHOLNPGM("X vector length:");
  2101. MYSERIAL.println(l);
  2102. // Length of the vec_y
  2103. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  2104. SERIAL_ECHOLNPGM("Y vector length:");
  2105. MYSERIAL.println(l);
  2106. // Zero point correction
  2107. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  2108. SERIAL_ECHOLNPGM("Zero point correction:");
  2109. MYSERIAL.println(l);
  2110. // vec_x and vec_y shall be nearly perpendicular.
  2111. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  2112. SERIAL_ECHOLNPGM("Perpendicularity");
  2113. MYSERIAL.println(fabs(l));
  2114. SERIAL_ECHOLNPGM("Saving bed calibration vectors to EEPROM");
  2115. }
  2116. #endif // SUPPORT_VERBOSITY
  2117. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2118. world2machine_update_current();
  2119. #ifdef SUPPORT_VERBOSITY
  2120. if (verbosity_level >= 20) {
  2121. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2122. delay_keep_alive(3000);
  2123. for (int8_t mesh_point = 0; mesh_point < 9; ++mesh_point) {
  2124. // Don't let the manage_inactivity() function remove power from the motors.
  2125. refresh_cmd_timeout();
  2126. // Go to the measurement point.
  2127. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2128. current_position[X_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2);
  2129. current_position[Y_AXIS] = pgm_read_float(bed_ref_points + mesh_point * 2 + 1);
  2130. go_to_current(homing_feedrate[X_AXIS] / 60);
  2131. delay_keep_alive(3000);
  2132. }
  2133. }
  2134. #endif // SUPPORT_VERBOSITY
  2135. return result;
  2136. }
  2137. 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
  2138. iteration++;
  2139. }
  2140. return result;
  2141. }
  2142. #ifndef NEW_XYZCAL
  2143. BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask)
  2144. {
  2145. // Don't let the manage_inactivity() function remove power from the motors.
  2146. refresh_cmd_timeout();
  2147. // Mask of the first three points. Are they too far?
  2148. too_far_mask = 0;
  2149. // Reusing the z_values memory for the measurement cache.
  2150. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2151. float *pts = &mbl.z_values[0][0];
  2152. float *vec_x = pts + 2 * 9;
  2153. float *vec_y = vec_x + 2;
  2154. float *cntr = vec_y + 2;
  2155. memset(pts, 0, sizeof(float) * 7 * 7);
  2156. #ifdef SUPPORT_VERBOSITY
  2157. if (verbosity_level >= 10) SERIAL_ECHOLNPGM("Improving bed offset and skew");
  2158. #endif // SUPPORT_VERBOSITY
  2159. // Cache the current correction matrix.
  2160. world2machine_initialize();
  2161. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2162. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2163. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2164. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2165. cntr[0] = world2machine_shift[0];
  2166. cntr[1] = world2machine_shift[1];
  2167. // and reset the correction matrix, so the planner will not do anything.
  2168. world2machine_reset();
  2169. bool endstops_enabled = enable_endstops(false);
  2170. bool endstop_z_enabled = enable_z_endstop(false);
  2171. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2172. uint8_t next_line;
  2173. lcd_display_message_fullscreen_P(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1, next_line);
  2174. if (next_line > 3)
  2175. next_line = 3;
  2176. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2177. // Collect a matrix of 9x9 points.
  2178. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  2179. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2180. // Don't let the manage_inactivity() function remove power from the motors.
  2181. refresh_cmd_timeout();
  2182. // Print the decrasing ID of the measurement point.
  2183. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2184. lcd_implementation_print_at(0, next_line, mesh_point+1);
  2185. lcd_printPGM(MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2);
  2186. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2187. // Move up.
  2188. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2189. enable_endstops(false);
  2190. enable_z_endstop(false);
  2191. go_to_current(homing_feedrate[Z_AXIS]/60);
  2192. #ifdef SUPPORT_VERBOSITY
  2193. if (verbosity_level >= 20) {
  2194. // Go to Y0, wait, then go to Y-4.
  2195. current_position[Y_AXIS] = 0.f;
  2196. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2197. SERIAL_ECHOLNPGM("At Y0");
  2198. delay_keep_alive(5000);
  2199. current_position[Y_AXIS] = Y_MIN_POS;
  2200. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2201. SERIAL_ECHOLNPGM("At Y_MIN_POS");
  2202. delay_keep_alive(5000);
  2203. }
  2204. #endif // SUPPORT_VERBOSITY
  2205. // Go to the measurement point.
  2206. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2207. 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];
  2208. 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];
  2209. // The calibration points are very close to the min Y.
  2210. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION){
  2211. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2212. #ifdef SUPPORT_VERBOSITY
  2213. if (verbosity_level >= 20) {
  2214. SERIAL_ECHOPGM("Calibration point ");
  2215. SERIAL_ECHO(mesh_point);
  2216. SERIAL_ECHOPGM("lower than Ymin. Y coordinate clamping was used.");
  2217. SERIAL_ECHOLNPGM("");
  2218. }
  2219. #endif // SUPPORT_VERBOSITY
  2220. }
  2221. go_to_current(homing_feedrate[X_AXIS]/60);
  2222. // Find its Z position by running the normal vertical search.
  2223. #ifdef SUPPORT_VERBOSITY
  2224. if (verbosity_level >= 10)
  2225. delay_keep_alive(3000);
  2226. #endif // SUPPORT_VERBOSITY
  2227. find_bed_induction_sensor_point_z();
  2228. #ifdef SUPPORT_VERBOSITY
  2229. if (verbosity_level >= 10)
  2230. delay_keep_alive(3000);
  2231. #endif // SUPPORT_VERBOSITY
  2232. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2233. current_position[Z_AXIS] -= 0.025f;
  2234. // Improve the point position by searching its center in a current plane.
  2235. int8_t n_errors = 3;
  2236. for (int8_t iter = 0; iter < 8; ) {
  2237. #ifdef SUPPORT_VERBOSITY
  2238. if (verbosity_level > 20) {
  2239. SERIAL_ECHOPGM("Improving bed point ");
  2240. SERIAL_ECHO(mesh_point);
  2241. SERIAL_ECHOPGM(", iteration ");
  2242. SERIAL_ECHO(iter);
  2243. SERIAL_ECHOPGM(", z");
  2244. MYSERIAL.print(current_position[Z_AXIS], 5);
  2245. SERIAL_ECHOLNPGM("");
  2246. }
  2247. #endif // SUPPORT_VERBOSITY
  2248. bool found = false;
  2249. if (mesh_point < 2) {
  2250. // Because the sensor cannot move in front of the first row
  2251. // of the sensor points, the y position cannot be measured
  2252. // by a cross center method.
  2253. // Use a zig-zag search for the first row of the points.
  2254. found = improve_bed_induction_sensor_point3(verbosity_level);
  2255. } else {
  2256. switch (method) {
  2257. case 0: found = improve_bed_induction_sensor_point(); break;
  2258. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 2, verbosity_level); break;
  2259. default: break;
  2260. }
  2261. }
  2262. if (found) {
  2263. if (iter > 3) {
  2264. // Average the last 4 measurements.
  2265. pts[mesh_point*2 ] += current_position[X_AXIS];
  2266. pts[mesh_point*2+1] += current_position[Y_AXIS];
  2267. }
  2268. if (current_position[Y_AXIS] < Y_MIN_POS)
  2269. current_position[Y_AXIS] = Y_MIN_POS;
  2270. ++ iter;
  2271. } else if (n_errors -- == 0) {
  2272. // Give up.
  2273. result = BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2274. goto canceled;
  2275. } else {
  2276. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2277. current_position[Z_AXIS] -= 0.05f;
  2278. enable_endstops(false);
  2279. enable_z_endstop(false);
  2280. go_to_current(homing_feedrate[Z_AXIS]);
  2281. #ifdef SUPPORT_VERBOSITY
  2282. if (verbosity_level >= 5) {
  2283. SERIAL_ECHOPGM("Improving bed point ");
  2284. SERIAL_ECHO(mesh_point);
  2285. SERIAL_ECHOPGM(", iteration ");
  2286. SERIAL_ECHO(iter);
  2287. SERIAL_ECHOPGM(" failed. Lowering z to ");
  2288. MYSERIAL.print(current_position[Z_AXIS], 5);
  2289. SERIAL_ECHOLNPGM("");
  2290. }
  2291. #endif // SUPPORT_VERBOSITY
  2292. }
  2293. }
  2294. #ifdef SUPPORT_VERBOSITY
  2295. if (verbosity_level >= 10)
  2296. delay_keep_alive(3000);
  2297. #endif // SUPPORT_VERBOSITY
  2298. }
  2299. // Don't let the manage_inactivity() function remove power from the motors.
  2300. refresh_cmd_timeout();
  2301. // Average the last 4 measurements.
  2302. for (int8_t i = 0; i < 8; ++ i)
  2303. pts[i] *= (1.f/4.f);
  2304. enable_endstops(false);
  2305. enable_z_endstop(false);
  2306. #ifdef SUPPORT_VERBOSITY
  2307. if (verbosity_level >= 5) {
  2308. // Test the positions. Are the positions reproducible?
  2309. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2310. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2311. // Don't let the manage_inactivity() function remove power from the motors.
  2312. refresh_cmd_timeout();
  2313. // Go to the measurement point.
  2314. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2315. current_position[X_AXIS] = pts[mesh_point*2];
  2316. current_position[Y_AXIS] = pts[mesh_point*2+1];
  2317. if (verbosity_level >= 10) {
  2318. go_to_current(homing_feedrate[X_AXIS]/60);
  2319. delay_keep_alive(3000);
  2320. }
  2321. SERIAL_ECHOPGM("Final measured bed point ");
  2322. SERIAL_ECHO(mesh_point);
  2323. SERIAL_ECHOPGM(": ");
  2324. MYSERIAL.print(current_position[X_AXIS], 5);
  2325. SERIAL_ECHOPGM(", ");
  2326. MYSERIAL.print(current_position[Y_AXIS], 5);
  2327. SERIAL_ECHOLNPGM("");
  2328. }
  2329. }
  2330. #endif // SUPPORT_VERBOSITY
  2331. {
  2332. // First fill in the too_far_mask from the measured points.
  2333. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point)
  2334. if (pts[mesh_point * 2 + 1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2335. too_far_mask |= 1 << mesh_point;
  2336. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2337. if (result < 0) {
  2338. SERIAL_ECHOLNPGM("Calculation of the machine skew and offset failed.");
  2339. goto canceled;
  2340. }
  2341. // In case of success, update the too_far_mask from the calculated points.
  2342. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point) {
  2343. 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];
  2344. distance_from_min[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2345. #ifdef SUPPORT_VERBOSITY
  2346. if (verbosity_level >= 20) {
  2347. SERIAL_ECHOLNPGM("");
  2348. SERIAL_ECHOPGM("Distance from min:");
  2349. MYSERIAL.print(distance_from_min[mesh_point]);
  2350. SERIAL_ECHOLNPGM("");
  2351. SERIAL_ECHOPGM("y:");
  2352. MYSERIAL.print(y);
  2353. SERIAL_ECHOLNPGM("");
  2354. }
  2355. #endif // SUPPORT_VERBOSITY
  2356. if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2357. too_far_mask |= 1 << mesh_point;
  2358. }
  2359. }
  2360. world2machine_update(vec_x, vec_y, cntr);
  2361. #if 1
  2362. // Fearlessly store the calibration values into the eeprom.
  2363. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  2364. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  2365. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  2366. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  2367. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  2368. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  2369. #endif
  2370. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2371. world2machine_update_current();
  2372. enable_endstops(false);
  2373. enable_z_endstop(false);
  2374. #ifdef SUPPORT_VERBOSITY
  2375. if (verbosity_level >= 5) {
  2376. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2377. delay_keep_alive(3000);
  2378. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2379. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2380. // Don't let the manage_inactivity() function remove power from the motors.
  2381. refresh_cmd_timeout();
  2382. // Go to the measurement point.
  2383. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2384. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2);
  2385. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2+1);
  2386. if (verbosity_level >= 10) {
  2387. go_to_current(homing_feedrate[X_AXIS]/60);
  2388. delay_keep_alive(3000);
  2389. }
  2390. {
  2391. float x, y;
  2392. world2machine(current_position[X_AXIS], current_position[Y_AXIS], x, y);
  2393. SERIAL_ECHOPGM("Final calculated bed point ");
  2394. SERIAL_ECHO(mesh_point);
  2395. SERIAL_ECHOPGM(": ");
  2396. MYSERIAL.print(x, 5);
  2397. SERIAL_ECHOPGM(", ");
  2398. MYSERIAL.print(y, 5);
  2399. SERIAL_ECHOLNPGM("");
  2400. }
  2401. }
  2402. }
  2403. #endif // SUPPORT_VERBOSITY
  2404. if(!sample_z())
  2405. goto canceled;
  2406. enable_endstops(endstops_enabled);
  2407. enable_z_endstop(endstop_z_enabled);
  2408. // Don't let the manage_inactivity() function remove power from the motors.
  2409. refresh_cmd_timeout();
  2410. return result;
  2411. canceled:
  2412. // Don't let the manage_inactivity() function remove power from the motors.
  2413. refresh_cmd_timeout();
  2414. // Print head up.
  2415. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2416. go_to_current(homing_feedrate[Z_AXIS]/60);
  2417. // Store the identity matrix to EEPROM.
  2418. reset_bed_offset_and_skew();
  2419. enable_endstops(endstops_enabled);
  2420. enable_z_endstop(endstop_z_enabled);
  2421. return result;
  2422. }
  2423. #endif //NEW_XYZCAL
  2424. bool sample_z() {
  2425. bool sampled = true;
  2426. //make space
  2427. current_position[Z_AXIS] += 150;
  2428. go_to_current(homing_feedrate[Z_AXIS] / 60);
  2429. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate, active_extruder););
  2430. lcd_show_fullscreen_message_and_wait_P(MSG_PLACE_STEEL_SHEET);
  2431. // Sample Z heights for the mesh bed leveling.
  2432. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2433. if (!sample_mesh_and_store_reference()) sampled = false;
  2434. return sampled;
  2435. }
  2436. void go_home_with_z_lift()
  2437. {
  2438. // Don't let the manage_inactivity() function remove power from the motors.
  2439. refresh_cmd_timeout();
  2440. // Go home.
  2441. // First move up to a safe height.
  2442. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2443. go_to_current(homing_feedrate[Z_AXIS]/60);
  2444. // Second move to XY [0, 0].
  2445. current_position[X_AXIS] = X_MIN_POS+0.2;
  2446. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2447. // Clamp to the physical coordinates.
  2448. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2449. go_to_current(homing_feedrate[X_AXIS]/60);
  2450. // Third move up to a safe height.
  2451. current_position[Z_AXIS] = Z_MIN_POS;
  2452. go_to_current(homing_feedrate[Z_AXIS]/60);
  2453. }
  2454. // Sample the 9 points of the bed and store them into the EEPROM as a reference.
  2455. // When calling this function, the X, Y, Z axes should be already homed,
  2456. // and the world2machine correction matrix should be active.
  2457. // Returns false if the reference values are more than 3mm far away.
  2458. bool sample_mesh_and_store_reference()
  2459. {
  2460. bool endstops_enabled = enable_endstops(false);
  2461. bool endstop_z_enabled = enable_z_endstop(false);
  2462. // Don't let the manage_inactivity() function remove power from the motors.
  2463. refresh_cmd_timeout();
  2464. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2465. uint8_t next_line;
  2466. lcd_display_message_fullscreen_P(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1, next_line);
  2467. if (next_line > 3)
  2468. next_line = 3;
  2469. // display "point xx of yy"
  2470. lcd_implementation_print_at(0, next_line, 1);
  2471. lcd_printPGM(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2);
  2472. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2473. // Sample Z heights for the mesh bed leveling.
  2474. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2475. {
  2476. // The first point defines the reference.
  2477. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2478. go_to_current(homing_feedrate[Z_AXIS]/60);
  2479. current_position[X_AXIS] = pgm_read_float(bed_ref_points);
  2480. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+1);
  2481. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2482. go_to_current(homing_feedrate[X_AXIS]/60);
  2483. memcpy(destination, current_position, sizeof(destination));
  2484. enable_endstops(true);
  2485. homeaxis(Z_AXIS);
  2486. enable_endstops(false);
  2487. find_bed_induction_sensor_point_z();
  2488. mbl.set_z(0, 0, current_position[Z_AXIS]);
  2489. }
  2490. for (int8_t mesh_point = 1; mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS; ++ mesh_point) {
  2491. // Don't let the manage_inactivity() function remove power from the motors.
  2492. refresh_cmd_timeout();
  2493. // Print the decrasing ID of the measurement point.
  2494. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2495. go_to_current(homing_feedrate[Z_AXIS]/60);
  2496. current_position[X_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point);
  2497. current_position[Y_AXIS] = pgm_read_float(bed_ref_points+2*mesh_point+1);
  2498. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2499. go_to_current(homing_feedrate[X_AXIS]/60);
  2500. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2501. // display "point xx of yy"
  2502. lcd_implementation_print_at(0, next_line, mesh_point+1);
  2503. lcd_printPGM(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2);
  2504. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2505. find_bed_induction_sensor_point_z();
  2506. // Get cords of measuring point
  2507. int8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2508. int8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2509. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2510. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2511. }
  2512. {
  2513. // Verify the span of the Z values.
  2514. float zmin = mbl.z_values[0][0];
  2515. float zmax = zmax;
  2516. for (int8_t j = 0; j < 3; ++ j)
  2517. for (int8_t i = 0; i < 3; ++ i) {
  2518. zmin = min(zmin, mbl.z_values[j][i]);
  2519. zmax = min(zmax, mbl.z_values[j][i]);
  2520. }
  2521. if (zmax - zmin > 3.f) {
  2522. // The span of the Z offsets is extreme. Give up.
  2523. // Homing failed on some of the points.
  2524. SERIAL_PROTOCOLLNPGM("Exreme span of the Z values!");
  2525. return false;
  2526. }
  2527. }
  2528. // Store the correction values to EEPROM.
  2529. // Offsets of the Z heiths of the calibration points from the first point.
  2530. // The offsets are saved as 16bit signed int, scaled to tenths of microns.
  2531. {
  2532. uint16_t addr = EEPROM_BED_CALIBRATION_Z_JITTER;
  2533. for (int8_t j = 0; j < 3; ++ j)
  2534. for (int8_t i = 0; i < 3; ++ i) {
  2535. if (i == 0 && j == 0)
  2536. continue;
  2537. float dif = mbl.z_values[j][i] - mbl.z_values[0][0];
  2538. int16_t dif_quantized = int16_t(floor(dif * 100.f + 0.5f));
  2539. eeprom_update_word((uint16_t*)addr, *reinterpret_cast<uint16_t*>(&dif_quantized));
  2540. #if 0
  2541. {
  2542. uint16_t z_offset_u = eeprom_read_word((uint16_t*)addr);
  2543. float dif2 = *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2544. SERIAL_ECHOPGM("Bed point ");
  2545. SERIAL_ECHO(i);
  2546. SERIAL_ECHOPGM(",");
  2547. SERIAL_ECHO(j);
  2548. SERIAL_ECHOPGM(", differences: written ");
  2549. MYSERIAL.print(dif, 5);
  2550. SERIAL_ECHOPGM(", read: ");
  2551. MYSERIAL.print(dif2, 5);
  2552. SERIAL_ECHOLNPGM("");
  2553. }
  2554. #endif
  2555. addr += 2;
  2556. }
  2557. }
  2558. mbl.upsample_3x3();
  2559. mbl.active = true;
  2560. go_home_with_z_lift();
  2561. enable_endstops(endstops_enabled);
  2562. enable_z_endstop(endstop_z_enabled);
  2563. return true;
  2564. }
  2565. #ifndef NEW_XYZCAL
  2566. bool scan_bed_induction_points(int8_t verbosity_level)
  2567. {
  2568. // Don't let the manage_inactivity() function remove power from the motors.
  2569. refresh_cmd_timeout();
  2570. // Reusing the z_values memory for the measurement cache.
  2571. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2572. float *pts = &mbl.z_values[0][0];
  2573. float *vec_x = pts + 2 * 9;
  2574. float *vec_y = vec_x + 2;
  2575. float *cntr = vec_y + 2;
  2576. memset(pts, 0, sizeof(float) * 7 * 7);
  2577. // Cache the current correction matrix.
  2578. world2machine_initialize();
  2579. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2580. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2581. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2582. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2583. cntr[0] = world2machine_shift[0];
  2584. cntr[1] = world2machine_shift[1];
  2585. // and reset the correction matrix, so the planner will not do anything.
  2586. world2machine_reset();
  2587. bool endstops_enabled = enable_endstops(false);
  2588. bool endstop_z_enabled = enable_z_endstop(false);
  2589. // Collect a matrix of 9x9 points.
  2590. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  2591. // Don't let the manage_inactivity() function remove power from the motors.
  2592. refresh_cmd_timeout();
  2593. // Move up.
  2594. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2595. enable_endstops(false);
  2596. enable_z_endstop(false);
  2597. go_to_current(homing_feedrate[Z_AXIS]/60);
  2598. // Go to the measurement point.
  2599. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2600. 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];
  2601. 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];
  2602. // The calibration points are very close to the min Y.
  2603. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2604. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2605. go_to_current(homing_feedrate[X_AXIS]/60);
  2606. find_bed_induction_sensor_point_z();
  2607. scan_bed_induction_sensor_point();
  2608. }
  2609. // Don't let the manage_inactivity() function remove power from the motors.
  2610. refresh_cmd_timeout();
  2611. enable_endstops(false);
  2612. enable_z_endstop(false);
  2613. // Don't let the manage_inactivity() function remove power from the motors.
  2614. refresh_cmd_timeout();
  2615. enable_endstops(endstops_enabled);
  2616. enable_z_endstop(endstop_z_enabled);
  2617. return true;
  2618. }
  2619. #endif //NEW_XYZCAL
  2620. // Shift a Z axis by a given delta.
  2621. // To replace loading of the babystep correction.
  2622. static void shift_z(float delta)
  2623. {
  2624. 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);
  2625. st_synchronize();
  2626. plan_set_z_position(current_position[Z_AXIS]);
  2627. }
  2628. #define BABYSTEP_LOADZ_BY_PLANNER
  2629. // Number of baby steps applied
  2630. static int babystepLoadZ = 0;
  2631. void babystep_load()
  2632. {
  2633. // Apply Z height correction aka baby stepping before mesh bed leveling gets activated.
  2634. if(calibration_status() < CALIBRATION_STATUS_LIVE_ADJUST)
  2635. {
  2636. check_babystep(); //checking if babystep is in allowed range, otherwise setting babystep to 0
  2637. // End of G80: Apply the baby stepping value.
  2638. EEPROM_read_B(EEPROM_BABYSTEP_Z,&babystepLoadZ);
  2639. #if 0
  2640. SERIAL_ECHO("Z baby step: ");
  2641. SERIAL_ECHO(babystepLoadZ);
  2642. SERIAL_ECHO(", current Z: ");
  2643. SERIAL_ECHO(current_position[Z_AXIS]);
  2644. SERIAL_ECHO("correction: ");
  2645. SERIAL_ECHO(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2646. SERIAL_ECHOLN("");
  2647. #endif
  2648. }
  2649. }
  2650. void babystep_apply()
  2651. {
  2652. babystep_load();
  2653. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2654. shift_z(- float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2655. #else
  2656. babystepsTodoZadd(babystepLoadZ);
  2657. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2658. }
  2659. void babystep_undo()
  2660. {
  2661. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2662. shift_z(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2663. #else
  2664. babystepsTodoZsubtract(babystepLoadZ);
  2665. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2666. babystepLoadZ = 0;
  2667. }
  2668. void babystep_reset()
  2669. {
  2670. babystepLoadZ = 0;
  2671. }
  2672. void count_xyz_details() {
  2673. float a1, a2;
  2674. float cntr[2] = {
  2675. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0)),
  2676. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4))
  2677. };
  2678. float vec_x[2] = {
  2679. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0)),
  2680. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4))
  2681. };
  2682. float vec_y[2] = {
  2683. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0)),
  2684. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4))
  2685. };
  2686. a2 = -1 * asin(vec_y[0] / MACHINE_AXIS_SCALE_Y);
  2687. a1 = asin(vec_x[1] / MACHINE_AXIS_SCALE_X);
  2688. //angleDiff = fabs(a2 - a1);
  2689. for (uint8_t mesh_point = 0; mesh_point < 2; ++mesh_point) {
  2690. 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];
  2691. distance_from_min[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2692. }
  2693. }