mesh_bed_calibration.cpp 118 KB

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