mesh_bed_calibration.cpp 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146
  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. current_position[Z_AXIS] += 0.15;
  901. float z_bckp = current_position[Z_AXIS];
  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. //printf_P(PSTR("Zs: %f, Z: %f, delta Z: %f"), z_bckp, current_position[Z_AXIS], (z_bckp - current_position[Z_AXIS]));
  909. if (abs(current_position[Z_AXIS] - z_bckp) < 0.025) {
  910. printf_P(PSTR("PINDA triggered immediately, move Z higher and repeat measurement\n"));
  911. current_position[Z_AXIS] += 0.5;
  912. go_to_current(homing_feedrate[Z_AXIS]/60);
  913. current_position[Z_AXIS] = minimum_z;
  914. go_to_current(homing_feedrate[Z_AXIS]/(4*60));
  915. // we have to let the planner know where we are right now as it is not where we said to go.
  916. update_current_position_z();
  917. }
  918. if (! endstop_z_hit_on_purpose())
  919. goto error;
  920. #ifdef TMC2130
  921. if (READ(Z_TMC2130_DIAG) != 0) goto error; //crash Z detected
  922. #endif //TMC2130
  923. // SERIAL_ECHOPGM("Bed find_bed_induction_sensor_point_z low, height: ");
  924. // MYSERIAL.print(current_position[Z_AXIS], 5);
  925. // SERIAL_ECHOLNPGM("");
  926. float dz = i?abs(current_position[Z_AXIS] - (z / i)):0;
  927. z += current_position[Z_AXIS];
  928. printf_P(PSTR("Z[%d] = %d, dz=%d\n"), i, (int)(current_position[Z_AXIS] * 1000), (int)(dz * 1000));
  929. if (dz > 0.05) goto error;//deviation > 50um
  930. }
  931. current_position[Z_AXIS] = z;
  932. if (n_iter > 1)
  933. current_position[Z_AXIS] /= float(n_iter);
  934. enable_endstops(endstops_enabled);
  935. enable_z_endstop(endstop_z_enabled);
  936. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 3");
  937. #ifdef TMC2130
  938. FORCE_HIGH_POWER_END;
  939. #endif
  940. return true;
  941. error:
  942. // SERIAL_ECHOLNPGM("find_bed_induction_sensor_point_z 4");
  943. enable_endstops(endstops_enabled);
  944. enable_z_endstop(endstop_z_enabled);
  945. #ifdef TMC2130
  946. FORCE_HIGH_POWER_END;
  947. #endif
  948. return false;
  949. }
  950. #ifdef NEW_XYZCAL
  951. extern bool xyzcal_find_bed_induction_sensor_point_xy();
  952. #endif //NEW_XYZCAL
  953. // Search around the current_position[X,Y],
  954. // look for the induction sensor response.
  955. // Adjust the current_position[X,Y,Z] to the center of the target dot and its response Z coordinate.
  956. #define FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS (8.f)
  957. #define FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS (4.f)
  958. #define FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP (1.f)
  959. #ifdef HEATBED_V2
  960. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (2.f)
  961. #define FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR (0.03f)
  962. #else //HEATBED_V2
  963. #define FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP (0.2f)
  964. #endif //HEATBED_V2
  965. #ifdef HEATBED_V2
  966. inline bool find_bed_induction_sensor_point_xy(int
  967. #if !defined (NEW_XYZCAL) && defined (SUPPORT_VERBOSITY)
  968. verbosity_level
  969. #endif
  970. )
  971. {
  972. #ifdef NEW_XYZCAL
  973. return xyzcal_find_bed_induction_sensor_point_xy();
  974. #else //NEW_XYZCAL
  975. #ifdef SUPPORT_VERBOSITY
  976. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  977. #endif // SUPPORT_VERBOSITY
  978. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  979. bool found = false;
  980. {
  981. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  982. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  983. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  984. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  985. uint8_t nsteps_y;
  986. uint8_t i;
  987. if (x0 < X_MIN_POS) {
  988. x0 = X_MIN_POS;
  989. #ifdef SUPPORT_VERBOSITY
  990. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  991. #endif // SUPPORT_VERBOSITY
  992. }
  993. if (x1 > X_MAX_POS) {
  994. x1 = X_MAX_POS;
  995. #ifdef SUPPORT_VERBOSITY
  996. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  997. #endif // SUPPORT_VERBOSITY
  998. }
  999. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1000. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1001. #ifdef SUPPORT_VERBOSITY
  1002. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1003. #endif // SUPPORT_VERBOSITY
  1004. }
  1005. if (y1 > Y_MAX_POS) {
  1006. y1 = Y_MAX_POS;
  1007. #ifdef SUPPORT_VERBOSITY
  1008. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1009. #endif // SUPPORT_VERBOSITY
  1010. }
  1011. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1012. enable_endstops(false);
  1013. bool dir_positive = true;
  1014. float z_error = 2 * FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  1015. float find_bed_induction_sensor_point_z_step = FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP;
  1016. float initial_z_position = current_position[Z_AXIS];
  1017. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1018. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1019. // Continously lower the Z axis.
  1020. endstops_hit_on_purpose();
  1021. enable_z_endstop(true);
  1022. bool direction = false;
  1023. while (current_position[Z_AXIS] > -10.f && z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1024. // Do nsteps_y zig-zag movements.
  1025. SERIAL_ECHOPGM("z_error: ");
  1026. MYSERIAL.println(z_error);
  1027. current_position[Y_AXIS] = direction ? y1 : y0;
  1028. initial_z_position = current_position[Z_AXIS];
  1029. 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) {
  1030. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1031. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  1032. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1033. dir_positive = !dir_positive;
  1034. if (endstop_z_hit_on_purpose()) {
  1035. update_current_position_xyz();
  1036. z_error = initial_z_position - current_position[Z_AXIS] + find_bed_induction_sensor_point_z_step;
  1037. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1038. find_bed_induction_sensor_point_z_step = z_error / 2;
  1039. current_position[Z_AXIS] += z_error;
  1040. enable_z_endstop(false);
  1041. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  1042. enable_z_endstop(true);
  1043. }
  1044. goto endloop;
  1045. }
  1046. }
  1047. 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) {
  1048. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1049. current_position[Z_AXIS] -= find_bed_induction_sensor_point_z_step / float(nsteps_y - 1);
  1050. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1051. dir_positive = !dir_positive;
  1052. if (endstop_z_hit_on_purpose()) {
  1053. update_current_position_xyz();
  1054. z_error = initial_z_position - current_position[Z_AXIS];
  1055. if (z_error > FIND_BED_INDUCTION_SENSOR_POINT_MAX_Z_ERROR) {
  1056. find_bed_induction_sensor_point_z_step = z_error / 2;
  1057. current_position[Z_AXIS] += z_error;
  1058. enable_z_endstop(false);
  1059. direction = !direction;
  1060. (direction == false) ? go_xyz(x0, y0, current_position[Z_AXIS], feedrate) : go_xyz(x0, y1, current_position[Z_AXIS], feedrate);
  1061. enable_z_endstop(true);
  1062. }
  1063. goto endloop;
  1064. }
  1065. }
  1066. endloop:;
  1067. }
  1068. #ifdef SUPPORT_VERBOSITY
  1069. if (verbosity_level >= 20) {
  1070. SERIAL_ECHO("First hit");
  1071. SERIAL_ECHO("- X: ");
  1072. MYSERIAL.print(current_position[X_AXIS]);
  1073. SERIAL_ECHO("; Y: ");
  1074. MYSERIAL.print(current_position[Y_AXIS]);
  1075. SERIAL_ECHO("; Z: ");
  1076. MYSERIAL.println(current_position[Z_AXIS]);
  1077. }
  1078. #endif //SUPPORT_VERBOSITY
  1079. //lcd_show_fullscreen_message_and_wait_P(PSTR("First hit"));
  1080. //lcd_update_enable(true);
  1081. float init_x_position = current_position[X_AXIS];
  1082. float init_y_position = current_position[Y_AXIS];
  1083. // we have to let the planner know where we are right now as it is not where we said to go.
  1084. update_current_position_xyz();
  1085. enable_z_endstop(false);
  1086. for (int8_t iter = 0; iter < 2; ++iter) {
  1087. /*SERIAL_ECHOPGM("iter: ");
  1088. MYSERIAL.println(iter);
  1089. SERIAL_ECHOPGM("1 - current_position[Z_AXIS]: ");
  1090. MYSERIAL.println(current_position[Z_AXIS]);*/
  1091. // Slightly lower the Z axis to get a reliable trigger.
  1092. current_position[Z_AXIS] -= 0.1f;
  1093. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], homing_feedrate[Z_AXIS] / (60 * 10));
  1094. SERIAL_ECHOPGM("2 - current_position[Z_AXIS]: ");
  1095. MYSERIAL.println(current_position[Z_AXIS]);
  1096. // Do nsteps_y zig-zag movements.
  1097. float a, b;
  1098. float avg[2] = { 0,0 };
  1099. invert_z_endstop(true);
  1100. for (int iteration = 0; iteration < 8; iteration++) {
  1101. found = false;
  1102. enable_z_endstop(true);
  1103. go_xy(init_x_position + 16.0f, current_position[Y_AXIS], feedrate / 5);
  1104. update_current_position_xyz();
  1105. if (!endstop_z_hit_on_purpose()) {
  1106. // SERIAL_ECHOLN("Search X span 0 - not found");
  1107. continue;
  1108. }
  1109. // SERIAL_ECHOLN("Search X span 0 - found");
  1110. a = current_position[X_AXIS];
  1111. enable_z_endstop(false);
  1112. go_xy(init_x_position, current_position[Y_AXIS], feedrate / 5);
  1113. enable_z_endstop(true);
  1114. go_xy(init_x_position - 16.0f, current_position[Y_AXIS], feedrate / 5);
  1115. update_current_position_xyz();
  1116. if (!endstop_z_hit_on_purpose()) {
  1117. // SERIAL_ECHOLN("Search X span 1 - not found");
  1118. continue;
  1119. }
  1120. // SERIAL_ECHOLN("Search X span 1 - found");
  1121. b = current_position[X_AXIS];
  1122. // Go to the center.
  1123. enable_z_endstop(false);
  1124. current_position[X_AXIS] = 0.5f * (a + b);
  1125. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1126. found = true;
  1127. // Search in the Y direction along a cross.
  1128. found = false;
  1129. enable_z_endstop(true);
  1130. go_xy(current_position[X_AXIS], init_y_position + 16.0f, feedrate / 5);
  1131. update_current_position_xyz();
  1132. if (!endstop_z_hit_on_purpose()) {
  1133. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1134. continue;
  1135. }
  1136. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1137. a = current_position[Y_AXIS];
  1138. enable_z_endstop(false);
  1139. go_xy(current_position[X_AXIS], init_y_position, feedrate / 5);
  1140. enable_z_endstop(true);
  1141. go_xy(current_position[X_AXIS], init_y_position - 16.0f, feedrate / 5);
  1142. update_current_position_xyz();
  1143. if (!endstop_z_hit_on_purpose()) {
  1144. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1145. continue;
  1146. }
  1147. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1148. b = current_position[Y_AXIS];
  1149. // Go to the center.
  1150. enable_z_endstop(false);
  1151. current_position[Y_AXIS] = 0.5f * (a + b);
  1152. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate / 5);
  1153. #ifdef SUPPORT_VERBOSITY
  1154. if (verbosity_level >= 20) {
  1155. SERIAL_ECHOPGM("ITERATION: ");
  1156. MYSERIAL.println(iteration);
  1157. SERIAL_ECHOPGM("CURRENT POSITION X: ");
  1158. MYSERIAL.println(current_position[X_AXIS]);
  1159. SERIAL_ECHOPGM("CURRENT POSITION Y: ");
  1160. MYSERIAL.println(current_position[Y_AXIS]);
  1161. }
  1162. #endif //SUPPORT_VERBOSITY
  1163. if (iteration > 0) {
  1164. // Average the last 7 measurements.
  1165. avg[X_AXIS] += current_position[X_AXIS];
  1166. avg[Y_AXIS] += current_position[Y_AXIS];
  1167. }
  1168. init_x_position = current_position[X_AXIS];
  1169. init_y_position = current_position[Y_AXIS];
  1170. found = true;
  1171. }
  1172. invert_z_endstop(false);
  1173. avg[X_AXIS] *= (1.f / 7.f);
  1174. avg[Y_AXIS] *= (1.f / 7.f);
  1175. current_position[X_AXIS] = avg[X_AXIS];
  1176. current_position[Y_AXIS] = avg[Y_AXIS];
  1177. #ifdef SUPPORT_VERBOSITY
  1178. if (verbosity_level >= 20) {
  1179. SERIAL_ECHOPGM("AVG CURRENT POSITION X: ");
  1180. MYSERIAL.println(current_position[X_AXIS]);
  1181. SERIAL_ECHOPGM("AVG CURRENT POSITION Y: ");
  1182. MYSERIAL.println(current_position[Y_AXIS]);
  1183. }
  1184. #endif // SUPPORT_VERBOSITY
  1185. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1186. #ifdef SUPPORT_VERBOSITY
  1187. if (verbosity_level >= 20) {
  1188. lcd_show_fullscreen_message_and_wait_P(PSTR("Final position"));
  1189. lcd_update_enable(true);
  1190. }
  1191. #endif //SUPPORT_VERBOSITY
  1192. break;
  1193. }
  1194. }
  1195. enable_z_endstop(false);
  1196. invert_z_endstop(false);
  1197. return found;
  1198. #endif //NEW_XYZCAL
  1199. }
  1200. #else //HEATBED_V2
  1201. inline bool find_bed_induction_sensor_point_xy(int verbosity_level)
  1202. {
  1203. #ifdef NEW_XYZCAL
  1204. return xyzcal_find_bed_induction_sensor_point_xy();
  1205. #else //NEW_XYZCAL
  1206. #ifdef SUPPORT_VERBOSITY
  1207. if (verbosity_level >= 10) MYSERIAL.println("find bed induction sensor point xy");
  1208. #endif // SUPPORT_VERBOSITY
  1209. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1210. bool found = false;
  1211. {
  1212. float x0 = current_position[X_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1213. float x1 = current_position[X_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_X_RADIUS;
  1214. float y0 = current_position[Y_AXIS] - FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1215. float y1 = current_position[Y_AXIS] + FIND_BED_INDUCTION_SENSOR_POINT_Y_RADIUS;
  1216. uint8_t nsteps_y;
  1217. uint8_t i;
  1218. if (x0 < X_MIN_POS) {
  1219. x0 = X_MIN_POS;
  1220. #ifdef SUPPORT_VERBOSITY
  1221. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius lower than X_MIN. Clamping was done.");
  1222. #endif // SUPPORT_VERBOSITY
  1223. }
  1224. if (x1 > X_MAX_POS) {
  1225. x1 = X_MAX_POS;
  1226. #ifdef SUPPORT_VERBOSITY
  1227. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("X searching radius higher than X_MAX. Clamping was done.");
  1228. #endif // SUPPORT_VERBOSITY
  1229. }
  1230. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1231. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1232. #ifdef SUPPORT_VERBOSITY
  1233. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius lower than Y_MIN. Clamping was done.");
  1234. #endif // SUPPORT_VERBOSITY
  1235. }
  1236. if (y1 > Y_MAX_POS) {
  1237. y1 = Y_MAX_POS;
  1238. #ifdef SUPPORT_VERBOSITY
  1239. if (verbosity_level >= 20) SERIAL_ECHOLNPGM("Y searching radius higher than X_MAX. Clamping was done.");
  1240. #endif // SUPPORT_VERBOSITY
  1241. }
  1242. nsteps_y = int(ceil((y1 - y0) / FIND_BED_INDUCTION_SENSOR_POINT_XY_STEP));
  1243. enable_endstops(false);
  1244. bool dir_positive = true;
  1245. // go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS]/60);
  1246. go_xyz(x0, y0, current_position[Z_AXIS], feedrate);
  1247. // Continously lower the Z axis.
  1248. endstops_hit_on_purpose();
  1249. enable_z_endstop(true);
  1250. while (current_position[Z_AXIS] > -10.f) {
  1251. // Do nsteps_y zig-zag movements.
  1252. current_position[Y_AXIS] = y0;
  1253. for (i = 0; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i) {
  1254. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1255. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1256. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1257. dir_positive = !dir_positive;
  1258. if (endstop_z_hit_on_purpose())
  1259. goto endloop;
  1260. }
  1261. for (i = 0; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i) {
  1262. // Run with a slightly decreasing Z axis, zig-zag movement. Stop at the Z end-stop.
  1263. current_position[Z_AXIS] -= FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP / float(nsteps_y);
  1264. go_xyz(dir_positive ? x1 : x0, current_position[Y_AXIS], current_position[Z_AXIS], feedrate);
  1265. dir_positive = !dir_positive;
  1266. if (endstop_z_hit_on_purpose())
  1267. goto endloop;
  1268. }
  1269. }
  1270. endloop:
  1271. // SERIAL_ECHOLN("First hit");
  1272. // we have to let the planner know where we are right now as it is not where we said to go.
  1273. update_current_position_xyz();
  1274. // Search in this plane for the first hit. Zig-zag first in X, then in Y axis.
  1275. for (int8_t iter = 0; iter < 3; ++iter) {
  1276. if (iter > 0) {
  1277. // Slightly lower the Z axis to get a reliable trigger.
  1278. current_position[Z_AXIS] -= 0.02f;
  1279. go_xyz(current_position[X_AXIS], current_position[Y_AXIS], MESH_HOME_Z_SEARCH, homing_feedrate[Z_AXIS] / 60);
  1280. }
  1281. // Do nsteps_y zig-zag movements.
  1282. float a, b;
  1283. enable_endstops(false);
  1284. enable_z_endstop(false);
  1285. current_position[Y_AXIS] = y0;
  1286. go_xy(x0, current_position[Y_AXIS], feedrate);
  1287. enable_z_endstop(true);
  1288. found = false;
  1289. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] += (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1290. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1291. if (endstop_z_hit_on_purpose()) {
  1292. found = true;
  1293. break;
  1294. }
  1295. }
  1296. update_current_position_xyz();
  1297. if (!found) {
  1298. // SERIAL_ECHOLN("Search in Y - not found");
  1299. continue;
  1300. }
  1301. // SERIAL_ECHOLN("Search in Y - found");
  1302. a = current_position[Y_AXIS];
  1303. enable_z_endstop(false);
  1304. current_position[Y_AXIS] = y1;
  1305. go_xy(x0, current_position[Y_AXIS], feedrate);
  1306. enable_z_endstop(true);
  1307. found = false;
  1308. for (i = 0, dir_positive = true; i < nsteps_y; current_position[Y_AXIS] -= (y1 - y0) / float(nsteps_y - 1), ++i, dir_positive = !dir_positive) {
  1309. go_xy(dir_positive ? x1 : x0, current_position[Y_AXIS], feedrate);
  1310. if (endstop_z_hit_on_purpose()) {
  1311. found = true;
  1312. break;
  1313. }
  1314. }
  1315. update_current_position_xyz();
  1316. if (!found) {
  1317. // SERIAL_ECHOLN("Search in Y2 - not found");
  1318. continue;
  1319. }
  1320. // SERIAL_ECHOLN("Search in Y2 - found");
  1321. b = current_position[Y_AXIS];
  1322. current_position[Y_AXIS] = 0.5f * (a + b);
  1323. // Search in the X direction along a cross.
  1324. found = false;
  1325. enable_z_endstop(false);
  1326. go_xy(x0, current_position[Y_AXIS], feedrate);
  1327. enable_z_endstop(true);
  1328. go_xy(x1, current_position[Y_AXIS], feedrate);
  1329. update_current_position_xyz();
  1330. if (!endstop_z_hit_on_purpose()) {
  1331. // SERIAL_ECHOLN("Search X span 0 - not found");
  1332. continue;
  1333. }
  1334. // SERIAL_ECHOLN("Search X span 0 - found");
  1335. a = current_position[X_AXIS];
  1336. enable_z_endstop(false);
  1337. go_xy(x1, current_position[Y_AXIS], feedrate);
  1338. enable_z_endstop(true);
  1339. go_xy(x0, current_position[Y_AXIS], feedrate);
  1340. update_current_position_xyz();
  1341. if (!endstop_z_hit_on_purpose()) {
  1342. // SERIAL_ECHOLN("Search X span 1 - not found");
  1343. continue;
  1344. }
  1345. // SERIAL_ECHOLN("Search X span 1 - found");
  1346. b = current_position[X_AXIS];
  1347. // Go to the center.
  1348. enable_z_endstop(false);
  1349. current_position[X_AXIS] = 0.5f * (a + b);
  1350. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1351. found = true;
  1352. #if 1
  1353. // Search in the Y direction along a cross.
  1354. found = false;
  1355. enable_z_endstop(false);
  1356. go_xy(current_position[X_AXIS], y0, feedrate);
  1357. enable_z_endstop(true);
  1358. go_xy(current_position[X_AXIS], y1, feedrate);
  1359. update_current_position_xyz();
  1360. if (!endstop_z_hit_on_purpose()) {
  1361. // SERIAL_ECHOLN("Search Y2 span 0 - not found");
  1362. continue;
  1363. }
  1364. // SERIAL_ECHOLN("Search Y2 span 0 - found");
  1365. a = current_position[Y_AXIS];
  1366. enable_z_endstop(false);
  1367. go_xy(current_position[X_AXIS], y1, feedrate);
  1368. enable_z_endstop(true);
  1369. go_xy(current_position[X_AXIS], y0, feedrate);
  1370. update_current_position_xyz();
  1371. if (!endstop_z_hit_on_purpose()) {
  1372. // SERIAL_ECHOLN("Search Y2 span 1 - not found");
  1373. continue;
  1374. }
  1375. // SERIAL_ECHOLN("Search Y2 span 1 - found");
  1376. b = current_position[Y_AXIS];
  1377. // Go to the center.
  1378. enable_z_endstop(false);
  1379. current_position[Y_AXIS] = 0.5f * (a + b);
  1380. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1381. found = true;
  1382. #endif
  1383. break;
  1384. }
  1385. }
  1386. enable_z_endstop(false);
  1387. return found;
  1388. #endif //NEW_XYZCAL
  1389. }
  1390. #endif //HEATBED_V2
  1391. #ifndef NEW_XYZCAL
  1392. // Search around the current_position[X,Y,Z].
  1393. // It is expected, that the induction sensor is switched on at the current position.
  1394. // Look around this center point by painting a star around the point.
  1395. inline bool improve_bed_induction_sensor_point()
  1396. {
  1397. static const float search_radius = 8.f;
  1398. bool endstops_enabled = enable_endstops(false);
  1399. bool endstop_z_enabled = enable_z_endstop(false);
  1400. bool found = false;
  1401. float feedrate = homing_feedrate[X_AXIS] / 60.f;
  1402. float center_old_x = current_position[X_AXIS];
  1403. float center_old_y = current_position[Y_AXIS];
  1404. float center_x = 0.f;
  1405. float center_y = 0.f;
  1406. for (uint8_t iter = 0; iter < 4; ++ iter) {
  1407. switch (iter) {
  1408. case 0:
  1409. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1410. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1411. break;
  1412. case 1:
  1413. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1414. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1415. break;
  1416. case 2:
  1417. destination[X_AXIS] = center_old_x + search_radius * 0.707;
  1418. destination[Y_AXIS] = center_old_y - search_radius * 0.707;
  1419. break;
  1420. case 3:
  1421. default:
  1422. destination[X_AXIS] = center_old_x - search_radius * 0.707;
  1423. destination[Y_AXIS] = center_old_y + search_radius * 0.707;
  1424. break;
  1425. }
  1426. // Trim the vector from center_old_[x,y] to destination[x,y] by the bed dimensions.
  1427. float vx = destination[X_AXIS] - center_old_x;
  1428. float vy = destination[Y_AXIS] - center_old_y;
  1429. float l = sqrt(vx*vx+vy*vy);
  1430. float t;
  1431. if (destination[X_AXIS] < X_MIN_POS) {
  1432. // Exiting the bed at xmin.
  1433. t = (center_x - X_MIN_POS) / l;
  1434. destination[X_AXIS] = X_MIN_POS;
  1435. destination[Y_AXIS] = center_old_y + t * vy;
  1436. } else if (destination[X_AXIS] > X_MAX_POS) {
  1437. // Exiting the bed at xmax.
  1438. t = (X_MAX_POS - center_x) / l;
  1439. destination[X_AXIS] = X_MAX_POS;
  1440. destination[Y_AXIS] = center_old_y + t * vy;
  1441. }
  1442. if (destination[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION) {
  1443. // Exiting the bed at ymin.
  1444. t = (center_y - Y_MIN_POS_FOR_BED_CALIBRATION) / l;
  1445. destination[X_AXIS] = center_old_x + t * vx;
  1446. destination[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  1447. } else if (destination[Y_AXIS] > Y_MAX_POS) {
  1448. // Exiting the bed at xmax.
  1449. t = (Y_MAX_POS - center_y) / l;
  1450. destination[X_AXIS] = center_old_x + t * vx;
  1451. destination[Y_AXIS] = Y_MAX_POS;
  1452. }
  1453. // Move away from the measurement point.
  1454. enable_endstops(false);
  1455. go_xy(destination[X_AXIS], destination[Y_AXIS], feedrate);
  1456. // Move towards the measurement point, until the induction sensor triggers.
  1457. enable_endstops(true);
  1458. go_xy(center_old_x, center_old_y, feedrate);
  1459. update_current_position_xyz();
  1460. // if (! endstop_z_hit_on_purpose()) return false;
  1461. center_x += current_position[X_AXIS];
  1462. center_y += current_position[Y_AXIS];
  1463. }
  1464. // Calculate the new center, move to the new center.
  1465. center_x /= 4.f;
  1466. center_y /= 4.f;
  1467. current_position[X_AXIS] = center_x;
  1468. current_position[Y_AXIS] = center_y;
  1469. enable_endstops(false);
  1470. go_xy(current_position[X_AXIS], current_position[Y_AXIS], feedrate);
  1471. enable_endstops(endstops_enabled);
  1472. enable_z_endstop(endstop_z_enabled);
  1473. return found;
  1474. }
  1475. #endif //NEW_XYZCAL
  1476. #ifndef NEW_XYZCAL
  1477. static inline void debug_output_point(const char *type, const float &x, const float &y, const float &z)
  1478. {
  1479. SERIAL_ECHOPGM("Measured ");
  1480. SERIAL_ECHORPGM(type);
  1481. SERIAL_ECHOPGM(" ");
  1482. MYSERIAL.print(x, 5);
  1483. SERIAL_ECHOPGM(", ");
  1484. MYSERIAL.print(y, 5);
  1485. SERIAL_ECHOPGM(", ");
  1486. MYSERIAL.print(z, 5);
  1487. SERIAL_ECHOLNPGM("");
  1488. }
  1489. #endif //NEW_XYZCAL
  1490. #ifndef NEW_XYZCAL
  1491. // Search around the current_position[X,Y,Z].
  1492. // It is expected, that the induction sensor is switched on at the current position.
  1493. // Look around this center point by painting a star around the point.
  1494. #define IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS (8.f)
  1495. inline bool improve_bed_induction_sensor_point2(bool lift_z_on_min_y, int8_t verbosity_level)
  1496. {
  1497. float center_old_x = current_position[X_AXIS];
  1498. float center_old_y = current_position[Y_AXIS];
  1499. float a, b;
  1500. bool point_small = false;
  1501. enable_endstops(false);
  1502. {
  1503. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1504. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1505. if (x0 < X_MIN_POS)
  1506. x0 = X_MIN_POS;
  1507. if (x1 > X_MAX_POS)
  1508. x1 = X_MAX_POS;
  1509. // Search in the X direction along a cross.
  1510. enable_z_endstop(false);
  1511. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1512. enable_z_endstop(true);
  1513. go_xy(x1, 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. a = current_position[X_AXIS];
  1520. enable_z_endstop(false);
  1521. go_xy(x1, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1522. enable_z_endstop(true);
  1523. go_xy(x0, current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1524. update_current_position_xyz();
  1525. if (! endstop_z_hit_on_purpose()) {
  1526. current_position[X_AXIS] = center_old_x;
  1527. goto canceled;
  1528. }
  1529. b = current_position[X_AXIS];
  1530. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1531. #ifdef SUPPORT_VERBOSITY
  1532. if (verbosity_level >= 5) {
  1533. SERIAL_ECHOPGM("Point width too small: ");
  1534. SERIAL_ECHO(b - a);
  1535. SERIAL_ECHOLNPGM("");
  1536. }
  1537. #endif // SUPPORT_VERBOSITY
  1538. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1539. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1540. // Don't use the new X value.
  1541. current_position[X_AXIS] = center_old_x;
  1542. goto canceled;
  1543. } else {
  1544. // Use the new value, but force the Z axis to go a bit lower.
  1545. point_small = true;
  1546. }
  1547. }
  1548. #ifdef SUPPORT_VERBOSITY
  1549. if (verbosity_level >= 5) {
  1550. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1551. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1552. }
  1553. #endif // SUPPORT_VERBOSITY
  1554. // Go to the center.
  1555. enable_z_endstop(false);
  1556. current_position[X_AXIS] = 0.5f * (a + b);
  1557. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1558. }
  1559. {
  1560. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1561. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS;
  1562. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1563. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1564. if (y1 > Y_MAX_POS)
  1565. y1 = Y_MAX_POS;
  1566. // Search in the Y direction along a cross.
  1567. enable_z_endstop(false);
  1568. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1569. if (lift_z_on_min_y) {
  1570. // The first row of points are very close to the end stop.
  1571. // Lift the sensor to disengage the trigger. This is necessary because of the sensor hysteresis.
  1572. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS]+1.5f, homing_feedrate[Z_AXIS] / 60.f);
  1573. // and go back.
  1574. go_xyz(current_position[X_AXIS], y0, current_position[Z_AXIS], homing_feedrate[Z_AXIS] / 60.f);
  1575. }
  1576. if (lift_z_on_min_y && (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING) == 1) {
  1577. // Already triggering before we started the move.
  1578. // Shift the trigger point slightly outwards.
  1579. // a = current_position[Y_AXIS] - 1.5f;
  1580. a = current_position[Y_AXIS];
  1581. } else {
  1582. enable_z_endstop(true);
  1583. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1584. update_current_position_xyz();
  1585. if (! endstop_z_hit_on_purpose()) {
  1586. current_position[Y_AXIS] = center_old_y;
  1587. goto canceled;
  1588. }
  1589. a = current_position[Y_AXIS];
  1590. }
  1591. enable_z_endstop(false);
  1592. go_xy(current_position[X_AXIS], y1, homing_feedrate[X_AXIS] / 60.f);
  1593. enable_z_endstop(true);
  1594. go_xy(current_position[X_AXIS], y0, homing_feedrate[X_AXIS] / 60.f);
  1595. update_current_position_xyz();
  1596. if (! endstop_z_hit_on_purpose()) {
  1597. current_position[Y_AXIS] = center_old_y;
  1598. goto canceled;
  1599. }
  1600. b = current_position[Y_AXIS];
  1601. if (b - a < MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1602. // We force the calibration routine to move the Z axis slightly down to make the response more pronounced.
  1603. #ifdef SUPPORT_VERBOSITY
  1604. if (verbosity_level >= 5) {
  1605. SERIAL_ECHOPGM("Point height too small: ");
  1606. SERIAL_ECHO(b - a);
  1607. SERIAL_ECHOLNPGM("");
  1608. }
  1609. #endif // SUPPORT_VERBOSITY
  1610. if (b - a < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1611. // Don't use the new Y value.
  1612. current_position[Y_AXIS] = center_old_y;
  1613. goto canceled;
  1614. } else {
  1615. // Use the new value, but force the Z axis to go a bit lower.
  1616. point_small = true;
  1617. }
  1618. }
  1619. #ifdef SUPPORT_VERBOSITY
  1620. if (verbosity_level >= 5) {
  1621. debug_output_point(PSTR("top" ), current_position[X_AXIS], a, current_position[Z_AXIS]);
  1622. debug_output_point(PSTR("bottom"), current_position[X_AXIS], b, current_position[Z_AXIS]);
  1623. }
  1624. #endif // SUPPORT_VERBOSITY
  1625. // Go to the center.
  1626. enable_z_endstop(false);
  1627. current_position[Y_AXIS] = 0.5f * (a + b);
  1628. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1629. }
  1630. // If point is small but not too small, then force the Z axis to be lowered a bit,
  1631. // but use the new value. This is important when the initial position was off in one axis,
  1632. // for example if the initial calibration was shifted in the Y axis systematically.
  1633. // Then this first step will center.
  1634. return ! point_small;
  1635. canceled:
  1636. // Go back to the center.
  1637. enable_z_endstop(false);
  1638. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1639. return false;
  1640. }
  1641. #endif //NEW_XYZCAL
  1642. #ifndef NEW_XYZCAL
  1643. // Searching the front points, where one cannot move the sensor head in front of the sensor point.
  1644. // Searching in a zig-zag movement in a plane for the maximum width of the response.
  1645. // This function may set the current_position[Y_AXIS] below Y_MIN_POS, if the function succeeded.
  1646. // If this function failed, the Y coordinate will never be outside the working space.
  1647. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS (8.f)
  1648. #define IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y (0.1f)
  1649. inline bool improve_bed_induction_sensor_point3(int verbosity_level)
  1650. {
  1651. float center_old_x = current_position[X_AXIS];
  1652. float center_old_y = current_position[Y_AXIS];
  1653. float a, b;
  1654. bool result = true;
  1655. #ifdef SUPPORT_VERBOSITY
  1656. if (verbosity_level >= 20) MYSERIAL.println("Improve bed induction sensor point3");
  1657. #endif // SUPPORT_VERBOSITY
  1658. // Was the sensor point detected too far in the minus Y axis?
  1659. // If yes, the center of the induction point cannot be reached by the machine.
  1660. {
  1661. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1662. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1663. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1664. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1665. float y = y0;
  1666. if (x0 < X_MIN_POS)
  1667. x0 = X_MIN_POS;
  1668. if (x1 > X_MAX_POS)
  1669. x1 = X_MAX_POS;
  1670. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1671. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1672. if (y1 > Y_MAX_POS)
  1673. y1 = Y_MAX_POS;
  1674. #ifdef SUPPORT_VERBOSITY
  1675. if (verbosity_level >= 20) {
  1676. SERIAL_ECHOPGM("Initial position: ");
  1677. SERIAL_ECHO(center_old_x);
  1678. SERIAL_ECHOPGM(", ");
  1679. SERIAL_ECHO(center_old_y);
  1680. SERIAL_ECHOLNPGM("");
  1681. }
  1682. #endif // SUPPORT_VERBOSITY
  1683. // Search in the positive Y direction, until a maximum diameter is found.
  1684. // (the next diameter is smaller than the current one.)
  1685. float dmax = 0.f;
  1686. float xmax1 = 0.f;
  1687. float xmax2 = 0.f;
  1688. for (y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1689. enable_z_endstop(false);
  1690. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1691. enable_z_endstop(true);
  1692. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1693. update_current_position_xyz();
  1694. if (! endstop_z_hit_on_purpose()) {
  1695. continue;
  1696. // SERIAL_PROTOCOLPGM("Failed 1\n");
  1697. // current_position[X_AXIS] = center_old_x;
  1698. // goto canceled;
  1699. }
  1700. a = current_position[X_AXIS];
  1701. enable_z_endstop(false);
  1702. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1703. enable_z_endstop(true);
  1704. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1705. update_current_position_xyz();
  1706. if (! endstop_z_hit_on_purpose()) {
  1707. continue;
  1708. // SERIAL_PROTOCOLPGM("Failed 2\n");
  1709. // current_position[X_AXIS] = center_old_x;
  1710. // goto canceled;
  1711. }
  1712. b = current_position[X_AXIS];
  1713. #ifdef SUPPORT_VERBOSITY
  1714. if (verbosity_level >= 5) {
  1715. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1716. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1717. }
  1718. #endif // SUPPORT_VERBOSITY
  1719. float d = b - a;
  1720. if (d > dmax) {
  1721. xmax1 = 0.5f * (a + b);
  1722. dmax = d;
  1723. } else if (dmax > 0.) {
  1724. y0 = y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1725. break;
  1726. }
  1727. }
  1728. if (dmax == 0.) {
  1729. #ifdef SUPPORT_VERBOSITY
  1730. if (verbosity_level > 0)
  1731. SERIAL_PROTOCOLPGM("failed - not found\n");
  1732. #endif // SUPPORT_VERBOSITY
  1733. current_position[X_AXIS] = center_old_x;
  1734. current_position[Y_AXIS] = center_old_y;
  1735. goto canceled;
  1736. }
  1737. {
  1738. // Find the positive Y hit. This gives the extreme Y value for the search of the maximum diameter in the -Y direction.
  1739. enable_z_endstop(false);
  1740. go_xy(xmax1, y0 + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1741. enable_z_endstop(true);
  1742. go_xy(xmax1, max(y0 - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1743. update_current_position_xyz();
  1744. if (! endstop_z_hit_on_purpose()) {
  1745. current_position[Y_AXIS] = center_old_y;
  1746. goto canceled;
  1747. }
  1748. #ifdef SUPPORT_VERBOSITY
  1749. if (verbosity_level >= 5)
  1750. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1751. #endif // SUPPORT_VERBOSITY
  1752. y1 = current_position[Y_AXIS];
  1753. }
  1754. if (y1 <= y0) {
  1755. // Either the induction sensor is too high, or the induction sensor target is out of reach.
  1756. current_position[Y_AXIS] = center_old_y;
  1757. goto canceled;
  1758. }
  1759. // Search in the negative Y direction, until a maximum diameter is found.
  1760. dmax = 0.f;
  1761. // if (y0 + 1.f < y1)
  1762. // y1 = y0 + 1.f;
  1763. for (y = y1; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1764. enable_z_endstop(false);
  1765. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1766. enable_z_endstop(true);
  1767. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1768. update_current_position_xyz();
  1769. if (! endstop_z_hit_on_purpose()) {
  1770. continue;
  1771. /*
  1772. current_position[X_AXIS] = center_old_x;
  1773. SERIAL_PROTOCOLPGM("Failed 3\n");
  1774. goto canceled;
  1775. */
  1776. }
  1777. a = current_position[X_AXIS];
  1778. enable_z_endstop(false);
  1779. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1780. enable_z_endstop(true);
  1781. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1782. update_current_position_xyz();
  1783. if (! endstop_z_hit_on_purpose()) {
  1784. continue;
  1785. /*
  1786. current_position[X_AXIS] = center_old_x;
  1787. SERIAL_PROTOCOLPGM("Failed 4\n");
  1788. goto canceled;
  1789. */
  1790. }
  1791. b = current_position[X_AXIS];
  1792. #ifdef SUPPORT_VERBOSITY
  1793. if (verbosity_level >= 5) {
  1794. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1795. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1796. }
  1797. #endif // SUPPORT_VERBOSITY
  1798. float d = b - a;
  1799. if (d > dmax) {
  1800. xmax2 = 0.5f * (a + b);
  1801. dmax = d;
  1802. } else if (dmax > 0.) {
  1803. y1 = y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y;
  1804. break;
  1805. }
  1806. }
  1807. float xmax, ymax;
  1808. if (dmax == 0.f) {
  1809. // Only the hit in the positive direction found.
  1810. xmax = xmax1;
  1811. ymax = y0;
  1812. } else {
  1813. // Both positive and negative directions found.
  1814. xmax = xmax2;
  1815. ymax = 0.5f * (y0 + y1);
  1816. for (; y >= y0; y -= IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1817. enable_z_endstop(false);
  1818. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1819. enable_z_endstop(true);
  1820. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1821. update_current_position_xyz();
  1822. if (! endstop_z_hit_on_purpose()) {
  1823. continue;
  1824. /*
  1825. current_position[X_AXIS] = center_old_x;
  1826. SERIAL_PROTOCOLPGM("Failed 3\n");
  1827. goto canceled;
  1828. */
  1829. }
  1830. a = current_position[X_AXIS];
  1831. enable_z_endstop(false);
  1832. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1833. enable_z_endstop(true);
  1834. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1835. update_current_position_xyz();
  1836. if (! endstop_z_hit_on_purpose()) {
  1837. continue;
  1838. /*
  1839. current_position[X_AXIS] = center_old_x;
  1840. SERIAL_PROTOCOLPGM("Failed 4\n");
  1841. goto canceled;
  1842. */
  1843. }
  1844. b = current_position[X_AXIS];
  1845. #ifdef SUPPORT_VERBOSITY
  1846. if (verbosity_level >= 5) {
  1847. debug_output_point(PSTR("left" ), a, current_position[Y_AXIS], current_position[Z_AXIS]);
  1848. debug_output_point(PSTR("right"), b, current_position[Y_AXIS], current_position[Z_AXIS]);
  1849. }
  1850. #endif // SUPPORT_VERBOSITY
  1851. float d = b - a;
  1852. if (d > dmax) {
  1853. xmax = 0.5f * (a + b);
  1854. ymax = y;
  1855. dmax = d;
  1856. }
  1857. }
  1858. }
  1859. {
  1860. // Compare the distance in the Y+ direction with the diameter in the X direction.
  1861. // Find the positive Y hit once again, this time along the Y axis going through the X point with the highest diameter.
  1862. enable_z_endstop(false);
  1863. go_xy(xmax, ymax + IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, homing_feedrate[X_AXIS] / 60.f);
  1864. enable_z_endstop(true);
  1865. go_xy(xmax, max(ymax - IMPROVE_BED_INDUCTION_SENSOR_SEARCH_RADIUS, Y_MIN_POS_FOR_BED_CALIBRATION), homing_feedrate[X_AXIS] / 60.f);
  1866. update_current_position_xyz();
  1867. if (! endstop_z_hit_on_purpose()) {
  1868. current_position[Y_AXIS] = center_old_y;
  1869. goto canceled;
  1870. }
  1871. #ifdef SUPPORT_VERBOSITY
  1872. if (verbosity_level >= 5)
  1873. debug_output_point(PSTR("top" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1874. #endif // SUPPORT_VERBOSITY
  1875. if (current_position[Y_AXIS] - Y_MIN_POS_FOR_BED_CALIBRATION < 0.5f * dmax) {
  1876. // Probably not even a half circle was detected. The induction point is likely too far in the minus Y direction.
  1877. // First verify, if the measurement has been done at a sufficient height. If no, lower the Z axis a bit.
  1878. if (current_position[Y_AXIS] < ymax || dmax < 0.5f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1879. #ifdef SUPPORT_VERBOSITY
  1880. if (verbosity_level >= 5) {
  1881. SERIAL_ECHOPGM("Partial point diameter too small: ");
  1882. SERIAL_ECHO(dmax);
  1883. SERIAL_ECHOLNPGM("");
  1884. }
  1885. #endif // SUPPORT_VERBOSITY
  1886. result = false;
  1887. } else {
  1888. // Estimate the circle radius from the maximum diameter and height:
  1889. float h = current_position[Y_AXIS] - ymax;
  1890. float r = dmax * dmax / (8.f * h) + 0.5f * h;
  1891. if (r < 0.8f * MIN_BED_SENSOR_POINT_RESPONSE_DMR) {
  1892. #ifdef SUPPORT_VERBOSITY
  1893. if (verbosity_level >= 5) {
  1894. SERIAL_ECHOPGM("Partial point estimated radius too small: ");
  1895. SERIAL_ECHO(r);
  1896. SERIAL_ECHOPGM(", dmax:");
  1897. SERIAL_ECHO(dmax);
  1898. SERIAL_ECHOPGM(", h:");
  1899. SERIAL_ECHO(h);
  1900. SERIAL_ECHOLNPGM("");
  1901. }
  1902. #endif // SUPPORT_VERBOSITY
  1903. result = false;
  1904. } else {
  1905. // The point may end up outside of the machine working space.
  1906. // That is all right as it helps to improve the accuracy of the measurement point
  1907. // due to averaging.
  1908. // For the y correction, use an average of dmax/2 and the estimated radius.
  1909. r = 0.5f * (0.5f * dmax + r);
  1910. ymax = current_position[Y_AXIS] - r;
  1911. }
  1912. }
  1913. } else {
  1914. // If the diameter of the detected spot was smaller than a minimum allowed,
  1915. // the induction sensor is probably too high. Returning false will force
  1916. // the sensor to be lowered a tiny bit.
  1917. result = xmax >= MIN_BED_SENSOR_POINT_RESPONSE_DMR;
  1918. if (y0 > Y_MIN_POS_FOR_BED_CALIBRATION + 0.2f)
  1919. // Only in case both left and right y tangents are known, use them.
  1920. // If y0 is close to the bed edge, it may not be symmetric to the right tangent.
  1921. ymax = 0.5f * ymax + 0.25f * (y0 + y1);
  1922. }
  1923. }
  1924. // Go to the center.
  1925. enable_z_endstop(false);
  1926. current_position[X_AXIS] = xmax;
  1927. current_position[Y_AXIS] = ymax;
  1928. #ifdef SUPPORT_VERBOSITY
  1929. if (verbosity_level >= 20) {
  1930. SERIAL_ECHOPGM("Adjusted position: ");
  1931. SERIAL_ECHO(current_position[X_AXIS]);
  1932. SERIAL_ECHOPGM(", ");
  1933. SERIAL_ECHO(current_position[Y_AXIS]);
  1934. SERIAL_ECHOLNPGM("");
  1935. }
  1936. #endif // SUPPORT_VERBOSITY
  1937. // Don't clamp current_position[Y_AXIS], because the out-of-reach Y coordinate may actually be true.
  1938. // Only clamp the coordinate to go.
  1939. go_xy(current_position[X_AXIS], max(Y_MIN_POS, current_position[Y_AXIS]), homing_feedrate[X_AXIS] / 60.f);
  1940. // delay_keep_alive(3000);
  1941. }
  1942. if (result)
  1943. return true;
  1944. // otherwise clamp the Y coordinate
  1945. canceled:
  1946. // Go back to the center.
  1947. enable_z_endstop(false);
  1948. if (current_position[Y_AXIS] < Y_MIN_POS)
  1949. current_position[Y_AXIS] = Y_MIN_POS;
  1950. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1951. return false;
  1952. }
  1953. #endif //NEW_XYZCAL
  1954. #ifndef NEW_XYZCAL
  1955. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  1956. // write the trigger coordinates to the serial line.
  1957. // Useful for visualizing the behavior of the bed induction detector.
  1958. inline void scan_bed_induction_sensor_point()
  1959. {
  1960. float center_old_x = current_position[X_AXIS];
  1961. float center_old_y = current_position[Y_AXIS];
  1962. float x0 = center_old_x - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1963. float x1 = center_old_x + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1964. float y0 = center_old_y - IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1965. float y1 = center_old_y + IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_RADIUS;
  1966. float y = y0;
  1967. if (x0 < X_MIN_POS)
  1968. x0 = X_MIN_POS;
  1969. if (x1 > X_MAX_POS)
  1970. x1 = X_MAX_POS;
  1971. if (y0 < Y_MIN_POS_FOR_BED_CALIBRATION)
  1972. y0 = Y_MIN_POS_FOR_BED_CALIBRATION;
  1973. if (y1 > Y_MAX_POS)
  1974. y1 = Y_MAX_POS;
  1975. for (float y = y0; y < y1; y += IMPROVE_BED_INDUCTION_SENSOR_POINT3_SEARCH_STEP_FINE_Y) {
  1976. enable_z_endstop(false);
  1977. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1978. enable_z_endstop(true);
  1979. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1980. update_current_position_xyz();
  1981. if (endstop_z_hit_on_purpose())
  1982. debug_output_point(PSTR("left" ), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1983. enable_z_endstop(false);
  1984. go_xy(x1, y, homing_feedrate[X_AXIS] / 60.f);
  1985. enable_z_endstop(true);
  1986. go_xy(x0, y, homing_feedrate[X_AXIS] / 60.f);
  1987. update_current_position_xyz();
  1988. if (endstop_z_hit_on_purpose())
  1989. debug_output_point(PSTR("right"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
  1990. }
  1991. enable_z_endstop(false);
  1992. current_position[X_AXIS] = center_old_x;
  1993. current_position[Y_AXIS] = center_old_y;
  1994. go_xy(current_position[X_AXIS], current_position[Y_AXIS], homing_feedrate[X_AXIS] / 60.f);
  1995. }
  1996. #endif //NEW_XYZCAL
  1997. #define MESH_BED_CALIBRATION_SHOW_LCD
  1998. BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask)
  1999. {
  2000. // Don't let the manage_inactivity() function remove power from the motors.
  2001. refresh_cmd_timeout();
  2002. // Reusing the z_values memory for the measurement cache.
  2003. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2004. float *pts = &mbl.z_values[0][0];
  2005. float *vec_x = pts + 2 * 4;
  2006. float *vec_y = vec_x + 2;
  2007. float *cntr = vec_y + 2;
  2008. memset(pts, 0, sizeof(float) * 7 * 7);
  2009. uint8_t iteration = 0;
  2010. BedSkewOffsetDetectionResultType result;
  2011. // SERIAL_ECHOLNPGM("find_bed_offset_and_skew verbosity level: ");
  2012. // SERIAL_ECHO(int(verbosity_level));
  2013. // SERIAL_ECHOPGM("");
  2014. #ifdef NEW_XYZCAL
  2015. {
  2016. #else //NEW_XYZCAL
  2017. while (iteration < 3) {
  2018. #endif //NEW_XYZCAL
  2019. SERIAL_ECHOPGM("Iteration: ");
  2020. MYSERIAL.println(int(iteration + 1));
  2021. #ifdef SUPPORT_VERBOSITY
  2022. if (verbosity_level >= 20) {
  2023. SERIAL_ECHOLNPGM("Vectors: ");
  2024. SERIAL_ECHOPGM("vec_x[0]:");
  2025. MYSERIAL.print(vec_x[0], 5);
  2026. SERIAL_ECHOLNPGM("");
  2027. SERIAL_ECHOPGM("vec_x[1]:");
  2028. MYSERIAL.print(vec_x[1], 5);
  2029. SERIAL_ECHOLNPGM("");
  2030. SERIAL_ECHOPGM("vec_y[0]:");
  2031. MYSERIAL.print(vec_y[0], 5);
  2032. SERIAL_ECHOLNPGM("");
  2033. SERIAL_ECHOPGM("vec_y[1]:");
  2034. MYSERIAL.print(vec_y[1], 5);
  2035. SERIAL_ECHOLNPGM("");
  2036. SERIAL_ECHOPGM("cntr[0]:");
  2037. MYSERIAL.print(cntr[0], 5);
  2038. SERIAL_ECHOLNPGM("");
  2039. SERIAL_ECHOPGM("cntr[1]:");
  2040. MYSERIAL.print(cntr[1], 5);
  2041. SERIAL_ECHOLNPGM("");
  2042. }
  2043. #endif // SUPPORT_VERBOSITY
  2044. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2045. uint8_t next_line;
  2046. lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1), next_line);
  2047. if (next_line > 3)
  2048. next_line = 3;
  2049. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2050. // Collect the rear 2x3 points.
  2051. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2052. for (int k = 0; k < 4; ++k) {
  2053. // Don't let the manage_inactivity() function remove power from the motors.
  2054. refresh_cmd_timeout();
  2055. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2056. lcd_set_cursor(0, next_line);
  2057. lcd_print(k + 1);
  2058. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));
  2059. if (iteration > 0) {
  2060. lcd_puts_at_P(0, next_line + 1, _i("Iteration "));////MSG_FIND_BED_OFFSET_AND_SKEW_ITERATION c=20 r=0
  2061. lcd_print(int(iteration + 1));
  2062. }
  2063. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2064. float *pt = pts + k * 2;
  2065. // Go up to z_initial.
  2066. go_to_current(homing_feedrate[Z_AXIS] / 60.f);
  2067. #ifdef SUPPORT_VERBOSITY
  2068. if (verbosity_level >= 20) {
  2069. // Go to Y0, wait, then go to Y-4.
  2070. current_position[Y_AXIS] = 0.f;
  2071. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2072. SERIAL_ECHOLNPGM("At Y0");
  2073. delay_keep_alive(5000);
  2074. current_position[Y_AXIS] = Y_MIN_POS;
  2075. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2076. SERIAL_ECHOLNPGM("At Y-4");
  2077. delay_keep_alive(5000);
  2078. }
  2079. #endif // SUPPORT_VERBOSITY
  2080. // Go to the measurement point position.
  2081. //if (iteration == 0) {
  2082. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2);
  2083. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + k * 2 + 1);
  2084. /*}
  2085. else {
  2086. // if first iteration failed, count corrected point coordinates as initial
  2087. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2088. 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];
  2089. 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];
  2090. // The calibration points are very close to the min Y.
  2091. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2092. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2093. }*/
  2094. #ifdef SUPPORT_VERBOSITY
  2095. if (verbosity_level >= 20) {
  2096. SERIAL_ECHOPGM("current_position[X_AXIS]:");
  2097. MYSERIAL.print(current_position[X_AXIS], 5);
  2098. SERIAL_ECHOLNPGM("");
  2099. SERIAL_ECHOPGM("current_position[Y_AXIS]:");
  2100. MYSERIAL.print(current_position[Y_AXIS], 5);
  2101. SERIAL_ECHOLNPGM("");
  2102. SERIAL_ECHOPGM("current_position[Z_AXIS]:");
  2103. MYSERIAL.print(current_position[Z_AXIS], 5);
  2104. SERIAL_ECHOLNPGM("");
  2105. }
  2106. #endif // SUPPORT_VERBOSITY
  2107. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2108. #ifdef SUPPORT_VERBOSITY
  2109. if (verbosity_level >= 10)
  2110. delay_keep_alive(3000);
  2111. #endif // SUPPORT_VERBOSITY
  2112. if (!find_bed_induction_sensor_point_xy(verbosity_level))
  2113. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2114. #ifndef NEW_XYZCAL
  2115. #ifndef HEATBED_V2
  2116. if (k == 0 || k == 1) {
  2117. // Improve the position of the 1st row sensor points by a zig-zag movement.
  2118. find_bed_induction_sensor_point_z();
  2119. int8_t i = 4;
  2120. for (;;) {
  2121. if (improve_bed_induction_sensor_point3(verbosity_level))
  2122. break;
  2123. if (--i == 0)
  2124. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2125. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2126. current_position[Z_AXIS] -= 0.025f;
  2127. enable_endstops(false);
  2128. enable_z_endstop(false);
  2129. go_to_current(homing_feedrate[Z_AXIS]);
  2130. }
  2131. if (i == 0)
  2132. // not found
  2133. return BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2134. }
  2135. #endif //HEATBED_V2
  2136. #endif
  2137. #ifdef SUPPORT_VERBOSITY
  2138. if (verbosity_level >= 10)
  2139. delay_keep_alive(3000);
  2140. #endif // SUPPORT_VERBOSITY
  2141. // Save the detected point position and then clamp the Y coordinate, which may have been estimated
  2142. // to lie outside the machine working space.
  2143. #ifdef SUPPORT_VERBOSITY
  2144. if (verbosity_level >= 20) {
  2145. SERIAL_ECHOLNPGM("Measured:");
  2146. MYSERIAL.println(current_position[X_AXIS]);
  2147. MYSERIAL.println(current_position[Y_AXIS]);
  2148. }
  2149. #endif // SUPPORT_VERBOSITY
  2150. pt[0] = (pt[0] * iteration) / (iteration + 1);
  2151. pt[0] += (current_position[X_AXIS]/(iteration + 1)); //count average
  2152. pt[1] = (pt[1] * iteration) / (iteration + 1);
  2153. pt[1] += (current_position[Y_AXIS] / (iteration + 1));
  2154. //pt[0] += current_position[X_AXIS];
  2155. //if(iteration > 0) pt[0] = pt[0] / 2;
  2156. //pt[1] += current_position[Y_AXIS];
  2157. //if (iteration > 0) pt[1] = pt[1] / 2;
  2158. #ifdef SUPPORT_VERBOSITY
  2159. if (verbosity_level >= 20) {
  2160. SERIAL_ECHOLNPGM("");
  2161. SERIAL_ECHOPGM("pt[0]:");
  2162. MYSERIAL.println(pt[0]);
  2163. SERIAL_ECHOPGM("pt[1]:");
  2164. MYSERIAL.println(pt[1]);
  2165. }
  2166. #endif // SUPPORT_VERBOSITY
  2167. if (current_position[Y_AXIS] < Y_MIN_POS)
  2168. current_position[Y_AXIS] = Y_MIN_POS;
  2169. // Start searching for the other points at 3mm above the last point.
  2170. current_position[Z_AXIS] += 3.f + FIND_BED_INDUCTION_SENSOR_POINT_Z_STEP * iteration * 0.3;
  2171. //cntr[0] += pt[0];
  2172. //cntr[1] += pt[1];
  2173. #ifdef SUPPORT_VERBOSITY
  2174. if (verbosity_level >= 10 && k == 0) {
  2175. // Show the zero. Test, whether the Y motor skipped steps.
  2176. current_position[Y_AXIS] = MANUAL_Y_HOME_POS;
  2177. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2178. delay_keep_alive(3000);
  2179. }
  2180. #endif // SUPPORT_VERBOSITY
  2181. }
  2182. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2183. #ifdef SUPPORT_VERBOSITY
  2184. if (verbosity_level >= 20) {
  2185. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2186. delay_keep_alive(3000);
  2187. for (int8_t mesh_point = 0; mesh_point < 4; ++mesh_point) {
  2188. // Don't let the manage_inactivity() function remove power from the motors.
  2189. refresh_cmd_timeout();
  2190. // Go to the measurement point.
  2191. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2192. current_position[X_AXIS] = pts[mesh_point * 2];
  2193. current_position[Y_AXIS] = pts[mesh_point * 2 + 1];
  2194. go_to_current(homing_feedrate[X_AXIS] / 60);
  2195. delay_keep_alive(3000);
  2196. }
  2197. }
  2198. #endif // SUPPORT_VERBOSITY
  2199. if (pts[1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
  2200. too_far_mask |= 1 << 1; //front center point is out of reach
  2201. SERIAL_ECHOLNPGM("");
  2202. SERIAL_ECHOPGM("WARNING: Front point not reachable. Y coordinate:");
  2203. MYSERIAL.print(pts[1]);
  2204. SERIAL_ECHOPGM(" < ");
  2205. MYSERIAL.println(Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2206. }
  2207. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2208. delay_keep_alive(0); //manage_heater, reset watchdog, manage inactivity
  2209. if (result >= 0) {
  2210. world2machine_update(vec_x, vec_y, cntr);
  2211. #if 1
  2212. // Fearlessly store the calibration values into the eeprom.
  2213. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0), cntr[0]);
  2214. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4), cntr[1]);
  2215. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0), vec_x[0]);
  2216. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4), vec_x[1]);
  2217. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0), vec_y[0]);
  2218. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4), vec_y[1]);
  2219. #endif
  2220. #ifdef SUPPORT_VERBOSITY
  2221. if (verbosity_level >= 10) {
  2222. // Length of the vec_x
  2223. float l = sqrt(vec_x[0] * vec_x[0] + vec_x[1] * vec_x[1]);
  2224. SERIAL_ECHOLNPGM("X vector length:");
  2225. MYSERIAL.println(l);
  2226. // Length of the vec_y
  2227. l = sqrt(vec_y[0] * vec_y[0] + vec_y[1] * vec_y[1]);
  2228. SERIAL_ECHOLNPGM("Y vector length:");
  2229. MYSERIAL.println(l);
  2230. // Zero point correction
  2231. l = sqrt(cntr[0] * cntr[0] + cntr[1] * cntr[1]);
  2232. SERIAL_ECHOLNPGM("Zero point correction:");
  2233. MYSERIAL.println(l);
  2234. // vec_x and vec_y shall be nearly perpendicular.
  2235. l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
  2236. SERIAL_ECHOLNPGM("Perpendicularity");
  2237. MYSERIAL.println(fabs(l));
  2238. SERIAL_ECHOLNPGM("Saving bed calibration vectors to EEPROM");
  2239. }
  2240. #endif // SUPPORT_VERBOSITY
  2241. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2242. world2machine_update_current();
  2243. #ifdef SUPPORT_VERBOSITY
  2244. if (verbosity_level >= 20) {
  2245. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2246. delay_keep_alive(3000);
  2247. for (int8_t mesh_point = 0; mesh_point < 9; ++mesh_point) {
  2248. // Don't let the manage_inactivity() function remove power from the motors.
  2249. refresh_cmd_timeout();
  2250. // Go to the measurement point.
  2251. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2252. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  2253. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2254. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix;
  2255. current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2256. current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2257. go_to_current(homing_feedrate[X_AXIS] / 60);
  2258. delay_keep_alive(3000);
  2259. }
  2260. }
  2261. #endif // SUPPORT_VERBOSITY
  2262. return result;
  2263. }
  2264. 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
  2265. iteration++;
  2266. }
  2267. return result;
  2268. }
  2269. #ifndef NEW_XYZCAL
  2270. BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask)
  2271. {
  2272. // Don't let the manage_inactivity() function remove power from the motors.
  2273. refresh_cmd_timeout();
  2274. // Mask of the first three points. Are they too far?
  2275. too_far_mask = 0;
  2276. // Reusing the z_values memory for the measurement cache.
  2277. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2278. float *pts = &mbl.z_values[0][0];
  2279. float *vec_x = pts + 2 * 9;
  2280. float *vec_y = vec_x + 2;
  2281. float *cntr = vec_y + 2;
  2282. memset(pts, 0, sizeof(float) * 7 * 7);
  2283. #ifdef SUPPORT_VERBOSITY
  2284. if (verbosity_level >= 10) SERIAL_ECHOLNPGM("Improving bed offset and skew");
  2285. #endif // SUPPORT_VERBOSITY
  2286. // Cache the current correction matrix.
  2287. world2machine_initialize();
  2288. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2289. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2290. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2291. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2292. cntr[0] = world2machine_shift[0];
  2293. cntr[1] = world2machine_shift[1];
  2294. // and reset the correction matrix, so the planner will not do anything.
  2295. world2machine_reset();
  2296. bool endstops_enabled = enable_endstops(false);
  2297. bool endstop_z_enabled = enable_z_endstop(false);
  2298. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2299. uint8_t next_line;
  2300. lcd_display_message_fullscreen_P(_i("Improving bed calibration point"), next_line);////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE1 c=60 r=0
  2301. if (next_line > 3)
  2302. next_line = 3;
  2303. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2304. // Collect a matrix of 9x9 points.
  2305. BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
  2306. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2307. // Don't let the manage_inactivity() function remove power from the motors.
  2308. refresh_cmd_timeout();
  2309. // Print the decrasing ID of the measurement point.
  2310. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2311. lcd_set_cursor(0, next_line);
  2312. lcd_print(mesh_point+1);
  2313. lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2));////MSG_IMPROVE_BED_OFFSET_AND_SKEW_LINE2 c=14 r=0
  2314. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2315. // Move up.
  2316. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2317. enable_endstops(false);
  2318. enable_z_endstop(false);
  2319. go_to_current(homing_feedrate[Z_AXIS]/60);
  2320. #ifdef SUPPORT_VERBOSITY
  2321. if (verbosity_level >= 20) {
  2322. // Go to Y0, wait, then go to Y-4.
  2323. current_position[Y_AXIS] = 0.f;
  2324. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2325. SERIAL_ECHOLNPGM("At Y0");
  2326. delay_keep_alive(5000);
  2327. current_position[Y_AXIS] = Y_MIN_POS;
  2328. go_to_current(homing_feedrate[X_AXIS] / 60.f);
  2329. SERIAL_ECHOLNPGM("At Y_MIN_POS");
  2330. delay_keep_alive(5000);
  2331. }
  2332. #endif // SUPPORT_VERBOSITY
  2333. // Go to the measurement point.
  2334. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2335. 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];
  2336. 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];
  2337. // The calibration points are very close to the min Y.
  2338. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION){
  2339. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2340. #ifdef SUPPORT_VERBOSITY
  2341. if (verbosity_level >= 20) {
  2342. SERIAL_ECHOPGM("Calibration point ");
  2343. SERIAL_ECHO(mesh_point);
  2344. SERIAL_ECHOPGM("lower than Ymin. Y coordinate clamping was used.");
  2345. SERIAL_ECHOLNPGM("");
  2346. }
  2347. #endif // SUPPORT_VERBOSITY
  2348. }
  2349. go_to_current(homing_feedrate[X_AXIS]/60);
  2350. // Find its Z position by running the normal vertical search.
  2351. #ifdef SUPPORT_VERBOSITY
  2352. if (verbosity_level >= 10)
  2353. delay_keep_alive(3000);
  2354. #endif // SUPPORT_VERBOSITY
  2355. find_bed_induction_sensor_point_z();
  2356. #ifdef SUPPORT_VERBOSITY
  2357. if (verbosity_level >= 10)
  2358. delay_keep_alive(3000);
  2359. #endif // SUPPORT_VERBOSITY
  2360. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2361. current_position[Z_AXIS] -= 0.025f;
  2362. // Improve the point position by searching its center in a current plane.
  2363. int8_t n_errors = 3;
  2364. for (int8_t iter = 0; iter < 8; ) {
  2365. #ifdef SUPPORT_VERBOSITY
  2366. if (verbosity_level > 20) {
  2367. SERIAL_ECHOPGM("Improving bed point ");
  2368. SERIAL_ECHO(mesh_point);
  2369. SERIAL_ECHOPGM(", iteration ");
  2370. SERIAL_ECHO(iter);
  2371. SERIAL_ECHOPGM(", z");
  2372. MYSERIAL.print(current_position[Z_AXIS], 5);
  2373. SERIAL_ECHOLNPGM("");
  2374. }
  2375. #endif // SUPPORT_VERBOSITY
  2376. bool found = false;
  2377. if (mesh_point < 2) {
  2378. // Because the sensor cannot move in front of the first row
  2379. // of the sensor points, the y position cannot be measured
  2380. // by a cross center method.
  2381. // Use a zig-zag search for the first row of the points.
  2382. found = improve_bed_induction_sensor_point3(verbosity_level);
  2383. } else {
  2384. switch (method) {
  2385. case 0: found = improve_bed_induction_sensor_point(); break;
  2386. case 1: found = improve_bed_induction_sensor_point2(mesh_point < 2, verbosity_level); break;
  2387. default: break;
  2388. }
  2389. }
  2390. if (found) {
  2391. if (iter > 3) {
  2392. // Average the last 4 measurements.
  2393. pts[mesh_point*2 ] += current_position[X_AXIS];
  2394. pts[mesh_point*2+1] += current_position[Y_AXIS];
  2395. }
  2396. if (current_position[Y_AXIS] < Y_MIN_POS)
  2397. current_position[Y_AXIS] = Y_MIN_POS;
  2398. ++ iter;
  2399. } else if (n_errors -- == 0) {
  2400. // Give up.
  2401. result = BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND;
  2402. goto canceled;
  2403. } else {
  2404. // Try to move the Z axis down a bit to increase a chance of the sensor to trigger.
  2405. current_position[Z_AXIS] -= 0.05f;
  2406. enable_endstops(false);
  2407. enable_z_endstop(false);
  2408. go_to_current(homing_feedrate[Z_AXIS]);
  2409. #ifdef SUPPORT_VERBOSITY
  2410. if (verbosity_level >= 5) {
  2411. SERIAL_ECHOPGM("Improving bed point ");
  2412. SERIAL_ECHO(mesh_point);
  2413. SERIAL_ECHOPGM(", iteration ");
  2414. SERIAL_ECHO(iter);
  2415. SERIAL_ECHOPGM(" failed. Lowering z to ");
  2416. MYSERIAL.print(current_position[Z_AXIS], 5);
  2417. SERIAL_ECHOLNPGM("");
  2418. }
  2419. #endif // SUPPORT_VERBOSITY
  2420. }
  2421. }
  2422. #ifdef SUPPORT_VERBOSITY
  2423. if (verbosity_level >= 10)
  2424. delay_keep_alive(3000);
  2425. #endif // SUPPORT_VERBOSITY
  2426. }
  2427. // Don't let the manage_inactivity() function remove power from the motors.
  2428. refresh_cmd_timeout();
  2429. // Average the last 4 measurements.
  2430. for (int8_t i = 0; i < 8; ++ i)
  2431. pts[i] *= (1.f/4.f);
  2432. enable_endstops(false);
  2433. enable_z_endstop(false);
  2434. #ifdef SUPPORT_VERBOSITY
  2435. if (verbosity_level >= 5) {
  2436. // Test the positions. Are the positions reproducible?
  2437. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2438. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2439. // Don't let the manage_inactivity() function remove power from the motors.
  2440. refresh_cmd_timeout();
  2441. // Go to the measurement point.
  2442. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2443. current_position[X_AXIS] = pts[mesh_point*2];
  2444. current_position[Y_AXIS] = pts[mesh_point*2+1];
  2445. if (verbosity_level >= 10) {
  2446. go_to_current(homing_feedrate[X_AXIS]/60);
  2447. delay_keep_alive(3000);
  2448. }
  2449. SERIAL_ECHOPGM("Final measured bed point ");
  2450. SERIAL_ECHO(mesh_point);
  2451. SERIAL_ECHOPGM(": ");
  2452. MYSERIAL.print(current_position[X_AXIS], 5);
  2453. SERIAL_ECHOPGM(", ");
  2454. MYSERIAL.print(current_position[Y_AXIS], 5);
  2455. SERIAL_ECHOLNPGM("");
  2456. }
  2457. }
  2458. #endif // SUPPORT_VERBOSITY
  2459. {
  2460. // First fill in the too_far_mask from the measured points.
  2461. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point)
  2462. if (pts[mesh_point * 2 + 1] < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2463. too_far_mask |= 1 << mesh_point;
  2464. result = calculate_machine_skew_and_offset_LS(pts, 4, bed_ref_points_4, vec_x, vec_y, cntr, verbosity_level);
  2465. if (result < 0) {
  2466. SERIAL_ECHOLNPGM("Calculation of the machine skew and offset failed.");
  2467. goto canceled;
  2468. }
  2469. // In case of success, update the too_far_mask from the calculated points.
  2470. for (uint8_t mesh_point = 0; mesh_point < 2; ++ mesh_point) {
  2471. 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];
  2472. #ifdef SUPPORT_VERBOSITY
  2473. if (verbosity_level >= 20) {
  2474. SERIAL_ECHOLNPGM("");
  2475. SERIAL_ECHOPGM("Distance from min:");
  2476. MYSERIAL.print(y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2477. SERIAL_ECHOLNPGM("");
  2478. SERIAL_ECHOPGM("y:");
  2479. MYSERIAL.print(y);
  2480. SERIAL_ECHOLNPGM("");
  2481. }
  2482. #endif // SUPPORT_VERBOSITY
  2483. if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH)
  2484. too_far_mask |= 1 << mesh_point;
  2485. }
  2486. }
  2487. world2machine_update(vec_x, vec_y, cntr);
  2488. #if 1
  2489. // Fearlessly store the calibration values into the eeprom.
  2490. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+0), cntr [0]);
  2491. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_CENTER+4), cntr [1]);
  2492. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +0), vec_x[0]);
  2493. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_X +4), vec_x[1]);
  2494. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +0), vec_y[0]);
  2495. eeprom_update_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y +4), vec_y[1]);
  2496. #endif
  2497. // Correct the current_position to match the transformed coordinate system after world2machine_rotation_and_skew and world2machine_shift were set.
  2498. world2machine_update_current();
  2499. enable_endstops(false);
  2500. enable_z_endstop(false);
  2501. #ifdef SUPPORT_VERBOSITY
  2502. if (verbosity_level >= 5) {
  2503. // Test the positions. Are the positions reproducible? Now the calibration is active in the planner.
  2504. delay_keep_alive(3000);
  2505. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2506. for (int8_t mesh_point = 0; mesh_point < 4; ++ mesh_point) {
  2507. // Don't let the manage_inactivity() function remove power from the motors.
  2508. refresh_cmd_timeout();
  2509. // Go to the measurement point.
  2510. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2511. current_position[X_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2);
  2512. current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4+mesh_point*2+1);
  2513. if (verbosity_level >= 10) {
  2514. go_to_current(homing_feedrate[X_AXIS]/60);
  2515. delay_keep_alive(3000);
  2516. }
  2517. {
  2518. float x, y;
  2519. world2machine(current_position[X_AXIS], current_position[Y_AXIS], x, y);
  2520. SERIAL_ECHOPGM("Final calculated bed point ");
  2521. SERIAL_ECHO(mesh_point);
  2522. SERIAL_ECHOPGM(": ");
  2523. MYSERIAL.print(x, 5);
  2524. SERIAL_ECHOPGM(", ");
  2525. MYSERIAL.print(y, 5);
  2526. SERIAL_ECHOLNPGM("");
  2527. }
  2528. }
  2529. }
  2530. #endif // SUPPORT_VERBOSITY
  2531. if(!sample_z())
  2532. goto canceled;
  2533. enable_endstops(endstops_enabled);
  2534. enable_z_endstop(endstop_z_enabled);
  2535. // Don't let the manage_inactivity() function remove power from the motors.
  2536. refresh_cmd_timeout();
  2537. return result;
  2538. canceled:
  2539. // Don't let the manage_inactivity() function remove power from the motors.
  2540. refresh_cmd_timeout();
  2541. // Print head up.
  2542. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2543. go_to_current(homing_feedrate[Z_AXIS]/60);
  2544. // Store the identity matrix to EEPROM.
  2545. reset_bed_offset_and_skew();
  2546. enable_endstops(endstops_enabled);
  2547. enable_z_endstop(endstop_z_enabled);
  2548. return result;
  2549. }
  2550. #endif //NEW_XYZCAL
  2551. bool sample_z() {
  2552. bool sampled = true;
  2553. //make space
  2554. current_position[Z_AXIS] += 150;
  2555. go_to_current(homing_feedrate[Z_AXIS] / 60);
  2556. //plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate, active_extruder););
  2557. lcd_show_fullscreen_message_and_wait_P(_T(MSG_PLACE_STEEL_SHEET));
  2558. // Sample Z heights for the mesh bed leveling.
  2559. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2560. if (!sample_mesh_and_store_reference()) sampled = false;
  2561. return sampled;
  2562. }
  2563. void go_home_with_z_lift()
  2564. {
  2565. // Don't let the manage_inactivity() function remove power from the motors.
  2566. refresh_cmd_timeout();
  2567. // Go home.
  2568. // First move up to a safe height.
  2569. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2570. go_to_current(homing_feedrate[Z_AXIS]/60);
  2571. // Second move to XY [0, 0].
  2572. current_position[X_AXIS] = X_MIN_POS+0.2;
  2573. current_position[Y_AXIS] = Y_MIN_POS+0.2;
  2574. // Clamp to the physical coordinates.
  2575. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2576. go_to_current(homing_feedrate[X_AXIS]/20);
  2577. // Third move up to a safe height.
  2578. current_position[Z_AXIS] = Z_MIN_POS;
  2579. go_to_current(homing_feedrate[Z_AXIS]/60);
  2580. }
  2581. // Sample the 9 points of the bed and store them into the EEPROM as a reference.
  2582. // When calling this function, the X, Y, Z axes should be already homed,
  2583. // and the world2machine correction matrix should be active.
  2584. // Returns false if the reference values are more than 3mm far away.
  2585. bool sample_mesh_and_store_reference()
  2586. {
  2587. bool endstops_enabled = enable_endstops(false);
  2588. bool endstop_z_enabled = enable_z_endstop(false);
  2589. // Don't let the manage_inactivity() function remove power from the motors.
  2590. refresh_cmd_timeout();
  2591. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2592. uint8_t next_line;
  2593. lcd_display_message_fullscreen_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1), next_line);
  2594. if (next_line > 3)
  2595. next_line = 3;
  2596. // display "point xx of yy"
  2597. lcd_set_cursor(0, next_line);
  2598. lcd_print(1);
  2599. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2600. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2601. // Sample Z heights for the mesh bed leveling.
  2602. // In addition, store the results into an eeprom, to be used later for verification of the bed leveling process.
  2603. {
  2604. // The first point defines the reference.
  2605. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2606. go_to_current(homing_feedrate[Z_AXIS]/60);
  2607. current_position[X_AXIS] = BED_X0;
  2608. current_position[Y_AXIS] = BED_Y0;
  2609. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2610. go_to_current(homing_feedrate[X_AXIS]/60);
  2611. memcpy(destination, current_position, sizeof(destination));
  2612. enable_endstops(true);
  2613. homeaxis(Z_AXIS);
  2614. #ifdef TMC2130
  2615. if (!axis_known_position[Z_AXIS] && (READ(Z_TMC2130_DIAG) != 0)) //Z crash
  2616. {
  2617. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2618. return false;
  2619. }
  2620. #endif //TMC2130
  2621. enable_endstops(false);
  2622. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2623. {
  2624. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2625. return false;
  2626. }
  2627. mbl.set_z(0, 0, current_position[Z_AXIS]);
  2628. }
  2629. for (int8_t mesh_point = 1; mesh_point != MESH_MEAS_NUM_X_POINTS * MESH_MEAS_NUM_Y_POINTS; ++ mesh_point) {
  2630. // Don't let the manage_inactivity() function remove power from the motors.
  2631. refresh_cmd_timeout();
  2632. // Print the decrasing ID of the measurement point.
  2633. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2634. go_to_current(homing_feedrate[Z_AXIS]/60);
  2635. int8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS;
  2636. int8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2637. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag
  2638. current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2639. current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2640. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  2641. go_to_current(homing_feedrate[X_AXIS]/60);
  2642. #ifdef MESH_BED_CALIBRATION_SHOW_LCD
  2643. // display "point xx of yy"
  2644. lcd_set_cursor(0, next_line);
  2645. lcd_print(mesh_point+1);
  2646. lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2));
  2647. #endif /* MESH_BED_CALIBRATION_SHOW_LCD */
  2648. if (!find_bed_induction_sensor_point_z()) //Z crash or deviation > 50um
  2649. {
  2650. kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
  2651. return false;
  2652. }
  2653. // Get cords of measuring point
  2654. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2655. }
  2656. {
  2657. // Verify the span of the Z values.
  2658. float zmin = mbl.z_values[0][0];
  2659. float zmax = zmin;
  2660. for (int8_t j = 0; j < 3; ++ j)
  2661. for (int8_t i = 0; i < 3; ++ i) {
  2662. zmin = min(zmin, mbl.z_values[j][i]);
  2663. zmax = max(zmax, mbl.z_values[j][i]);
  2664. }
  2665. if (zmax - zmin > 3.f) {
  2666. // The span of the Z offsets is extreme. Give up.
  2667. // Homing failed on some of the points.
  2668. SERIAL_PROTOCOLLNPGM("Exreme span of the Z values!");
  2669. return false;
  2670. }
  2671. }
  2672. // Store the correction values to EEPROM.
  2673. // Offsets of the Z heiths of the calibration points from the first point.
  2674. // The offsets are saved as 16bit signed int, scaled to tenths of microns.
  2675. {
  2676. uint16_t addr = EEPROM_BED_CALIBRATION_Z_JITTER;
  2677. for (int8_t j = 0; j < 3; ++ j)
  2678. for (int8_t i = 0; i < 3; ++ i) {
  2679. if (i == 0 && j == 0)
  2680. continue;
  2681. float dif = mbl.z_values[j][i] - mbl.z_values[0][0];
  2682. int16_t dif_quantized = int16_t(floor(dif * 100.f + 0.5f));
  2683. eeprom_update_word((uint16_t*)addr, *reinterpret_cast<uint16_t*>(&dif_quantized));
  2684. #if 0
  2685. {
  2686. uint16_t z_offset_u = eeprom_read_word((uint16_t*)addr);
  2687. float dif2 = *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
  2688. SERIAL_ECHOPGM("Bed point ");
  2689. SERIAL_ECHO(i);
  2690. SERIAL_ECHOPGM(",");
  2691. SERIAL_ECHO(j);
  2692. SERIAL_ECHOPGM(", differences: written ");
  2693. MYSERIAL.print(dif, 5);
  2694. SERIAL_ECHOPGM(", read: ");
  2695. MYSERIAL.print(dif2, 5);
  2696. SERIAL_ECHOLNPGM("");
  2697. }
  2698. #endif
  2699. addr += 2;
  2700. }
  2701. }
  2702. mbl.upsample_3x3();
  2703. mbl.active = true;
  2704. go_home_with_z_lift();
  2705. enable_endstops(endstops_enabled);
  2706. enable_z_endstop(endstop_z_enabled);
  2707. return true;
  2708. }
  2709. #ifndef NEW_XYZCAL
  2710. bool scan_bed_induction_points(int8_t verbosity_level)
  2711. {
  2712. // Don't let the manage_inactivity() function remove power from the motors.
  2713. refresh_cmd_timeout();
  2714. // Reusing the z_values memory for the measurement cache.
  2715. // 7x7=49 floats, good for 16 (x,y,z) vectors.
  2716. float *pts = &mbl.z_values[0][0];
  2717. float *vec_x = pts + 2 * 9;
  2718. float *vec_y = vec_x + 2;
  2719. float *cntr = vec_y + 2;
  2720. memset(pts, 0, sizeof(float) * 7 * 7);
  2721. // Cache the current correction matrix.
  2722. world2machine_initialize();
  2723. vec_x[0] = world2machine_rotation_and_skew[0][0];
  2724. vec_x[1] = world2machine_rotation_and_skew[1][0];
  2725. vec_y[0] = world2machine_rotation_and_skew[0][1];
  2726. vec_y[1] = world2machine_rotation_and_skew[1][1];
  2727. cntr[0] = world2machine_shift[0];
  2728. cntr[1] = world2machine_shift[1];
  2729. // and reset the correction matrix, so the planner will not do anything.
  2730. world2machine_reset();
  2731. bool endstops_enabled = enable_endstops(false);
  2732. bool endstop_z_enabled = enable_z_endstop(false);
  2733. // Collect a matrix of 9x9 points.
  2734. for (int8_t mesh_point = 0; mesh_point < 9; ++ mesh_point) {
  2735. // Don't let the manage_inactivity() function remove power from the motors.
  2736. refresh_cmd_timeout();
  2737. // Move up.
  2738. current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
  2739. enable_endstops(false);
  2740. enable_z_endstop(false);
  2741. go_to_current(homing_feedrate[Z_AXIS]/60);
  2742. // Go to the measurement point.
  2743. // Use the coorrected coordinate, which is a result of find_bed_offset_and_skew().
  2744. uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1
  2745. uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS;
  2746. if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix;
  2747. float bedX = BED_X(ix, MESH_MEAS_NUM_X_POINTS);
  2748. float bedY = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS);
  2749. current_position[X_AXIS] = vec_x[0] * bedX + vec_y[0] * bedY + cntr[0];
  2750. current_position[Y_AXIS] = vec_x[1] * bedX + vec_y[1] * bedY + cntr[1];
  2751. // The calibration points are very close to the min Y.
  2752. if (current_position[Y_AXIS] < Y_MIN_POS_FOR_BED_CALIBRATION)
  2753. current_position[Y_AXIS] = Y_MIN_POS_FOR_BED_CALIBRATION;
  2754. go_to_current(homing_feedrate[X_AXIS]/60);
  2755. find_bed_induction_sensor_point_z();
  2756. scan_bed_induction_sensor_point();
  2757. }
  2758. // Don't let the manage_inactivity() function remove power from the motors.
  2759. refresh_cmd_timeout();
  2760. enable_endstops(false);
  2761. enable_z_endstop(false);
  2762. // Don't let the manage_inactivity() function remove power from the motors.
  2763. refresh_cmd_timeout();
  2764. enable_endstops(endstops_enabled);
  2765. enable_z_endstop(endstop_z_enabled);
  2766. return true;
  2767. }
  2768. #endif //NEW_XYZCAL
  2769. // Shift a Z axis by a given delta.
  2770. // To replace loading of the babystep correction.
  2771. static void shift_z(float delta)
  2772. {
  2773. 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);
  2774. st_synchronize();
  2775. plan_set_z_position(current_position[Z_AXIS]);
  2776. }
  2777. #define BABYSTEP_LOADZ_BY_PLANNER
  2778. // Number of baby steps applied
  2779. static int babystepLoadZ = 0;
  2780. void babystep_load()
  2781. {
  2782. babystepLoadZ = 0;
  2783. // Apply Z height correction aka baby stepping before mesh bed leveling gets activated.
  2784. if (calibration_status() < CALIBRATION_STATUS_LIVE_ADJUST)
  2785. {
  2786. check_babystep(); //checking if babystep is in allowed range, otherwise setting babystep to 0
  2787. // End of G80: Apply the baby stepping value.
  2788. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepLoadZ);
  2789. #if 0
  2790. SERIAL_ECHO("Z baby step: ");
  2791. SERIAL_ECHO(babystepLoadZ);
  2792. SERIAL_ECHO(", current Z: ");
  2793. SERIAL_ECHO(current_position[Z_AXIS]);
  2794. SERIAL_ECHO("correction: ");
  2795. SERIAL_ECHO(float(babystepLoadZ) / float(axis_steps_per_unit[Z_AXIS]));
  2796. SERIAL_ECHOLN("");
  2797. #endif
  2798. }
  2799. }
  2800. void babystep_apply()
  2801. {
  2802. babystep_load();
  2803. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2804. shift_z(- float(babystepLoadZ) / float(cs.axis_steps_per_unit[Z_AXIS]));
  2805. #else
  2806. babystepsTodoZadd(babystepLoadZ);
  2807. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2808. }
  2809. void babystep_undo()
  2810. {
  2811. #ifdef BABYSTEP_LOADZ_BY_PLANNER
  2812. shift_z(float(babystepLoadZ) / float(cs.axis_steps_per_unit[Z_AXIS]));
  2813. #else
  2814. babystepsTodoZsubtract(babystepLoadZ);
  2815. #endif /* BABYSTEP_LOADZ_BY_PLANNER */
  2816. babystepLoadZ = 0;
  2817. }
  2818. void babystep_reset()
  2819. {
  2820. babystepLoadZ = 0;
  2821. }
  2822. void count_xyz_details(float (&distanceMin)[2]) {
  2823. float cntr[2] = {
  2824. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 0)),
  2825. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_CENTER + 4))
  2826. };
  2827. float vec_x[2] = {
  2828. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 0)),
  2829. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_X + 4))
  2830. };
  2831. float vec_y[2] = {
  2832. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 0)),
  2833. eeprom_read_float((float*)(EEPROM_BED_CALIBRATION_VEC_Y + 4))
  2834. };
  2835. #if 0
  2836. a2 = -1 * asin(vec_y[0] / MACHINE_AXIS_SCALE_Y);
  2837. a1 = asin(vec_x[1] / MACHINE_AXIS_SCALE_X);
  2838. angleDiff = fabs(a2 - a1);
  2839. #endif
  2840. for (uint8_t mesh_point = 0; mesh_point < 2; ++mesh_point) {
  2841. 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];
  2842. distanceMin[mesh_point] = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
  2843. }
  2844. }
  2845. /*
  2846. e_MBL_TYPE e_mbl_type = e_MBL_OPTIMAL;
  2847. void mbl_mode_set() {
  2848. switch (e_mbl_type) {
  2849. case e_MBL_OPTIMAL: e_mbl_type = e_MBL_PREC; break;
  2850. case e_MBL_PREC: e_mbl_type = e_MBL_FAST; break;
  2851. case e_MBL_FAST: e_mbl_type = e_MBL_OPTIMAL; break;
  2852. default: e_mbl_type = e_MBL_OPTIMAL; break;
  2853. }
  2854. eeprom_update_byte((uint8_t*)EEPROM_MBL_TYPE,(uint8_t)e_mbl_type);
  2855. }
  2856. void mbl_mode_init() {
  2857. uint8_t mbl_type = eeprom_read_byte((uint8_t*)EEPROM_MBL_TYPE);
  2858. if (mbl_type == 0xFF) e_mbl_type = e_MBL_OPTIMAL;
  2859. else e_mbl_type = mbl_type;
  2860. }
  2861. */
  2862. void mbl_settings_init() {
  2863. //3x3 mesh; 3 Z-probes on each point, magnet elimination on
  2864. //magnet elimination: use aaproximate Z-coordinate instead of measured values for points which are near magnets
  2865. if (eeprom_read_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION) == 0xFF) {
  2866. eeprom_update_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION, 1);
  2867. }
  2868. if (eeprom_read_byte((uint8_t*)EEPROM_MBL_PROBE_NR) == 0xFF) {
  2869. eeprom_update_byte((uint8_t*)EEPROM_MBL_PROBE_NR, 3);
  2870. }
  2871. mbl_z_probe_nr = eeprom_read_byte((uint8_t*)EEPROM_MBL_POINTS_NR);
  2872. if (mbl_z_probe_nr == 0xFF) {
  2873. mbl_z_probe_nr = 4;
  2874. eeprom_update_byte((uint8_t*)EEPROM_MBL_POINTS_NR, mbl_z_probe_nr);
  2875. }
  2876. }
  2877. bool mbl_point_measurement_valid(uint8_t ix, uint8_t iy, uint8_t meas_points, bool zigzag) {
  2878. //"human readable" heatbed plan
  2879. //magnet proximity influence Z coordinate measurements significantly (40 - 100 um)
  2880. //0 - measurement point is above magnet and Z coordinate can be influenced negatively
  2881. //1 - we should be in safe distance from magnets, measurement should be accurate
  2882. if ((ix >= meas_points) || (iy >= meas_points)) return false;
  2883. uint8_t valid_points_mask[7] = {
  2884. //[X_MAX,Y_MAX]
  2885. 0b1111101,
  2886. 0b1110111,
  2887. 0b1111111,
  2888. 0b0111011,
  2889. 0b1110111,
  2890. 0b1111111,
  2891. 0b1110111,
  2892. //[0,0]
  2893. };
  2894. if (meas_points == 3) {
  2895. ix *= 3;
  2896. iy *= 3;
  2897. }
  2898. if((iy%2) == 0) return (valid_points_mask[6 - iy] & (1 << (6 - ix)));
  2899. else return (valid_points_mask[6 - iy] & (1 << ix));
  2900. }
  2901. void mbl_single_point_interpolation(uint8_t x, uint8_t y, uint8_t meas_points) {
  2902. uint8_t count = 0;
  2903. float z = 0;
  2904. if(mbl_point_measurement_valid(x, y+1, meas_points, false)) { z += mbl.z_values[x][y+1]; count++; }
  2905. if(mbl_point_measurement_valid(x, y-1, meas_points, false)) { z += mbl.z_values[x][y-1]; count++; }
  2906. if(mbl_point_measurement_valid(x+1, y, meas_points, false)) { z += mbl.z_values[x+1][y]; count++; }
  2907. if(mbl_point_measurement_valid(x-1, y, meas_points, false)) { z += mbl.z_values[x+1][y]; count++; }
  2908. if(count != 0) mbl.z_values[x][y] = z / count; //if we have at least one valid point in surrounding area use average value, otherwise use inaccurately measured Z-coordinate
  2909. }
  2910. void mbl_interpolation(uint8_t meas_points) {
  2911. for (uint8_t x = 0; x < meas_points; x++) {
  2912. for (uint8_t y = 0; y < meas_points; y++) {
  2913. if (!mbl_point_measurement_valid(x, y, meas_points, false)) {
  2914. mbl_single_point_interpolation(x, y, meas_points);
  2915. }
  2916. }
  2917. }
  2918. }