mesh_bed_calibration.cpp 108 KB

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