mesh_bed_calibration.cpp 117 KB

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