mesh_bed_calibration.cpp 113 KB

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