mesh_bed_calibration.cpp 117 KB

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