mesh_bed_calibration.cpp 101 KB

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