mesh_bed_calibration.cpp 123 KB

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