mesh_bed_calibration.cpp 115 KB

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