mesh_bed_calibration.cpp 101 KB

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