mesh_bed_calibration.cpp 114 KB

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