mesh_bed_calibration.cpp 105 KB

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