mesh_bed_calibration.cpp 113 KB

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