mesh_bed_calibration.cpp 105 KB

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