mesh_bed_calibration.cpp 93 KB

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