My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

G33.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(DELTA_AUTO_CALIBRATION)
  24. #include "../gcode.h"
  25. #include "../../module/delta.h"
  26. #include "../../module/motion.h"
  27. #include "../../module/planner.h"
  28. #include "../../module/endstops.h"
  29. #include "../../lcd/marlinui.h"
  30. #if HAS_BED_PROBE
  31. #include "../../module/probe.h"
  32. #endif
  33. #if HAS_MULTI_HOTEND
  34. #include "../../module/tool_change.h"
  35. #endif
  36. #if HAS_LEVELING
  37. #include "../../feature/bedlevel/bedlevel.h"
  38. #endif
  39. constexpr uint8_t _7P_STEP = 1, // 7-point step - to change number of calibration points
  40. _4P_STEP = _7P_STEP * 2, // 4-point step
  41. NPP = _7P_STEP * 6; // number of calibration points on the radius
  42. enum CalEnum : char { // the 7 main calibration points - add definitions if needed
  43. CEN = 0,
  44. __A = 1,
  45. _AB = __A + _7P_STEP,
  46. __B = _AB + _7P_STEP,
  47. _BC = __B + _7P_STEP,
  48. __C = _BC + _7P_STEP,
  49. _CA = __C + _7P_STEP,
  50. };
  51. #define LOOP_CAL_PT(VAR, S, N) for (uint8_t VAR=S; VAR<=NPP; VAR+=N)
  52. #define F_LOOP_CAL_PT(VAR, S, N) for (float VAR=S; VAR<NPP+0.9999; VAR+=N)
  53. #define I_LOOP_CAL_PT(VAR, S, N) for (float VAR=S; VAR>CEN+0.9999; VAR-=N)
  54. #define LOOP_CAL_ALL(VAR) LOOP_CAL_PT(VAR, CEN, 1)
  55. #define LOOP_CAL_RAD(VAR) LOOP_CAL_PT(VAR, __A, _7P_STEP)
  56. #define LOOP_CAL_ACT(VAR, _4P, _OP) LOOP_CAL_PT(VAR, _OP ? _AB : __A, _4P ? _4P_STEP : _7P_STEP)
  57. #if HAS_MULTI_HOTEND
  58. const uint8_t old_tool_index = active_extruder;
  59. #endif
  60. float lcd_probe_pt(const xy_pos_t &xy);
  61. void ac_home() {
  62. endstops.enable(true);
  63. TERN_(SENSORLESS_HOMING, endstops.set_homing_current(true));
  64. home_delta();
  65. TERN_(SENSORLESS_HOMING, endstops.set_homing_current(false));
  66. endstops.not_homing();
  67. }
  68. void ac_setup(const bool reset_bed) {
  69. TERN_(HAS_MULTI_HOTEND, tool_change(0, true));
  70. planner.synchronize();
  71. remember_feedrate_scaling_off();
  72. #if HAS_LEVELING
  73. if (reset_bed) reset_bed_level(); // After full calibration bed-level data is no longer valid
  74. #endif
  75. }
  76. void ac_cleanup(TERN_(HAS_MULTI_HOTEND, const uint8_t old_tool_index)) {
  77. TERN_(DELTA_HOME_TO_SAFE_ZONE, do_blocking_move_to_z(delta_clip_start_height));
  78. TERN_(HAS_BED_PROBE, probe.stow());
  79. restore_feedrate_and_scaling();
  80. TERN_(HAS_MULTI_HOTEND, tool_change(old_tool_index, true));
  81. }
  82. void print_signed_float(FSTR_P const prefix, const_float_t f) {
  83. SERIAL_ECHOPGM(" ");
  84. SERIAL_ECHOF(prefix, AS_CHAR(':'));
  85. serial_offset(f);
  86. }
  87. /**
  88. * - Print the delta settings
  89. */
  90. static void print_calibration_settings(const bool end_stops, const bool tower_angles) {
  91. SERIAL_ECHOPGM(".Height:", delta_height);
  92. if (end_stops) {
  93. print_signed_float(F("Ex"), delta_endstop_adj.a);
  94. print_signed_float(F("Ey"), delta_endstop_adj.b);
  95. print_signed_float(F("Ez"), delta_endstop_adj.c);
  96. }
  97. if (end_stops && tower_angles) {
  98. SERIAL_ECHOLNPGM(" Radius:", delta_radius);
  99. SERIAL_CHAR('.');
  100. SERIAL_ECHO_SP(13);
  101. }
  102. if (tower_angles) {
  103. print_signed_float(F("Tx"), delta_tower_angle_trim.a);
  104. print_signed_float(F("Ty"), delta_tower_angle_trim.b);
  105. print_signed_float(F("Tz"), delta_tower_angle_trim.c);
  106. }
  107. if (end_stops != tower_angles)
  108. SERIAL_ECHOPGM(" Radius:", delta_radius);
  109. SERIAL_EOL();
  110. }
  111. /**
  112. * - Print the probe results
  113. */
  114. static void print_calibration_results(const float z_pt[NPP + 1], const bool tower_points, const bool opposite_points) {
  115. SERIAL_ECHOPGM(". ");
  116. print_signed_float(F("c"), z_pt[CEN]);
  117. if (tower_points) {
  118. print_signed_float(F(" x"), z_pt[__A]);
  119. print_signed_float(F(" y"), z_pt[__B]);
  120. print_signed_float(F(" z"), z_pt[__C]);
  121. }
  122. if (tower_points && opposite_points) {
  123. SERIAL_EOL();
  124. SERIAL_CHAR('.');
  125. SERIAL_ECHO_SP(13);
  126. }
  127. if (opposite_points) {
  128. print_signed_float(F("yz"), z_pt[_BC]);
  129. print_signed_float(F("zx"), z_pt[_CA]);
  130. print_signed_float(F("xy"), z_pt[_AB]);
  131. }
  132. SERIAL_EOL();
  133. }
  134. /**
  135. * - Calculate the standard deviation from the zero plane
  136. */
  137. static float std_dev_points(float z_pt[NPP + 1], const bool _0p_cal, const bool _1p_cal, const bool _4p_cal, const bool _4p_opp) {
  138. if (!_0p_cal) {
  139. float S2 = sq(z_pt[CEN]);
  140. int16_t N = 1;
  141. if (!_1p_cal) { // std dev from zero plane
  142. LOOP_CAL_ACT(rad, _4p_cal, _4p_opp) {
  143. S2 += sq(z_pt[rad]);
  144. N++;
  145. }
  146. return LROUND(SQRT(S2 / N) * 1000.0f) / 1000.0f + 0.00001f;
  147. }
  148. }
  149. return 0.00001f;
  150. }
  151. /**
  152. * - Probe a point
  153. */
  154. static float calibration_probe(const xy_pos_t &xy, const bool stow, const bool probe_at_offset) {
  155. #if HAS_BED_PROBE
  156. return probe.probe_at_point(xy, stow ? PROBE_PT_STOW : PROBE_PT_RAISE, 0, probe_at_offset, false);
  157. #else
  158. UNUSED(stow);
  159. return lcd_probe_pt(xy);
  160. #endif
  161. }
  162. /**
  163. * - Probe a grid
  164. */
  165. static bool probe_calibration_points(float z_pt[NPP + 1], const int8_t probe_points, const float dcr, const bool towers_set, const bool stow_after_each, const bool probe_at_offset) {
  166. const bool _0p_calibration = probe_points == 0,
  167. _1p_calibration = probe_points == 1 || probe_points == -1,
  168. _4p_calibration = probe_points == 2,
  169. _4p_opposite_points = _4p_calibration && !towers_set,
  170. _7p_calibration = probe_points >= 3,
  171. _7p_no_intermediates = probe_points == 3,
  172. _7p_1_intermediates = probe_points == 4,
  173. _7p_2_intermediates = probe_points == 5,
  174. _7p_4_intermediates = probe_points == 6,
  175. _7p_6_intermediates = probe_points == 7,
  176. _7p_8_intermediates = probe_points == 8,
  177. _7p_11_intermediates = probe_points == 9,
  178. _7p_14_intermediates = probe_points == 10,
  179. _7p_intermed_points = probe_points >= 4,
  180. _7p_6_center = probe_points >= 5 && probe_points <= 7,
  181. _7p_9_center = probe_points >= 8;
  182. LOOP_CAL_ALL(rad) z_pt[rad] = 0.0f;
  183. if (!_0p_calibration) {
  184. if (!_7p_no_intermediates && !_7p_4_intermediates && !_7p_11_intermediates) { // probe the center
  185. const xy_pos_t center{0};
  186. z_pt[CEN] += calibration_probe(center, stow_after_each, probe_at_offset);
  187. if (isnan(z_pt[CEN])) return false;
  188. }
  189. if (_7p_calibration) { // probe extra center points
  190. const float start = _7p_9_center ? float(_CA) + _7P_STEP / 3.0f : _7p_6_center ? float(_CA) : float(__C),
  191. steps = _7p_9_center ? _4P_STEP / 3.0f : _7p_6_center ? _7P_STEP : _4P_STEP;
  192. I_LOOP_CAL_PT(rad, start, steps) {
  193. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  194. r = dcr * 0.1;
  195. const xy_pos_t vec = { cos(a), sin(a) };
  196. z_pt[CEN] += calibration_probe(vec * r, stow_after_each, probe_at_offset);
  197. if (isnan(z_pt[CEN])) return false;
  198. }
  199. z_pt[CEN] /= float(_7p_2_intermediates ? 7 : probe_points);
  200. }
  201. if (!_1p_calibration) { // probe the radius
  202. const CalEnum start = _4p_opposite_points ? _AB : __A;
  203. const float steps = _7p_14_intermediates ? _7P_STEP / 15.0f : // 15r * 6 + 10c = 100
  204. _7p_11_intermediates ? _7P_STEP / 12.0f : // 12r * 6 + 9c = 81
  205. _7p_8_intermediates ? _7P_STEP / 9.0f : // 9r * 6 + 10c = 64
  206. _7p_6_intermediates ? _7P_STEP / 7.0f : // 7r * 6 + 7c = 49
  207. _7p_4_intermediates ? _7P_STEP / 5.0f : // 5r * 6 + 6c = 36
  208. _7p_2_intermediates ? _7P_STEP / 3.0f : // 3r * 6 + 7c = 25
  209. _7p_1_intermediates ? _7P_STEP / 2.0f : // 2r * 6 + 4c = 16
  210. _7p_no_intermediates ? _7P_STEP : // 1r * 6 + 3c = 9
  211. _4P_STEP; // .5r * 6 + 1c = 4
  212. bool zig_zag = true;
  213. F_LOOP_CAL_PT(rad, start, _7p_9_center ? steps * 3 : steps) {
  214. const int8_t offset = _7p_9_center ? 2 : 0;
  215. for (int8_t circle = 0; circle <= offset; circle++) {
  216. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  217. r = dcr * (1 - 0.1 * (zig_zag ? offset - circle : circle)),
  218. interpol = FMOD(rad, 1);
  219. const xy_pos_t vec = { cos(a), sin(a) };
  220. const float z_temp = calibration_probe(vec * r, stow_after_each, probe_at_offset);
  221. if (isnan(z_temp)) return false;
  222. // split probe point to neighbouring calibration points
  223. z_pt[uint8_t(LROUND(rad - interpol + NPP - 1)) % NPP + 1] += z_temp * sq(cos(RADIANS(interpol * 90)));
  224. z_pt[uint8_t(LROUND(rad - interpol)) % NPP + 1] += z_temp * sq(sin(RADIANS(interpol * 90)));
  225. }
  226. zig_zag = !zig_zag;
  227. }
  228. if (_7p_intermed_points)
  229. LOOP_CAL_RAD(rad)
  230. z_pt[rad] /= _7P_STEP / steps;
  231. do_blocking_move_to_xy(0.0f, 0.0f);
  232. }
  233. }
  234. return true;
  235. }
  236. /**
  237. * kinematics routines and auto tune matrix scaling parameters:
  238. * see https://github.com/LVD-AC/Marlin-AC/tree/1.1.x-AC/documentation for
  239. * - formulae for approximative forward kinematics in the end-stop displacement matrix
  240. * - definition of the matrix scaling parameters
  241. */
  242. static void reverse_kinematics_probe_points(float z_pt[NPP + 1], abc_float_t mm_at_pt_axis[NPP + 1], const float dcr) {
  243. xyz_pos_t pos{0};
  244. LOOP_CAL_ALL(rad) {
  245. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  246. r = (rad == CEN ? 0.0f : dcr);
  247. pos.set(cos(a) * r, sin(a) * r, z_pt[rad]);
  248. inverse_kinematics(pos);
  249. mm_at_pt_axis[rad] = delta;
  250. }
  251. }
  252. static void forward_kinematics_probe_points(abc_float_t mm_at_pt_axis[NPP + 1], float z_pt[NPP + 1], const float dcr) {
  253. const float r_quot = dcr / delta_radius;
  254. #define ZPP(N,I,A) (((1.0f + r_quot * (N)) / 3.0f) * mm_at_pt_axis[I].A)
  255. #define Z00(I, A) ZPP( 0, I, A)
  256. #define Zp1(I, A) ZPP(+1, I, A)
  257. #define Zm1(I, A) ZPP(-1, I, A)
  258. #define Zp2(I, A) ZPP(+2, I, A)
  259. #define Zm2(I, A) ZPP(-2, I, A)
  260. z_pt[CEN] = Z00(CEN, a) + Z00(CEN, b) + Z00(CEN, c);
  261. z_pt[__A] = Zp2(__A, a) + Zm1(__A, b) + Zm1(__A, c);
  262. z_pt[__B] = Zm1(__B, a) + Zp2(__B, b) + Zm1(__B, c);
  263. z_pt[__C] = Zm1(__C, a) + Zm1(__C, b) + Zp2(__C, c);
  264. z_pt[_BC] = Zm2(_BC, a) + Zp1(_BC, b) + Zp1(_BC, c);
  265. z_pt[_CA] = Zp1(_CA, a) + Zm2(_CA, b) + Zp1(_CA, c);
  266. z_pt[_AB] = Zp1(_AB, a) + Zp1(_AB, b) + Zm2(_AB, c);
  267. }
  268. static void calc_kinematics_diff_probe_points(float z_pt[NPP + 1], const float dcr, abc_float_t delta_e, const float delta_r, abc_float_t delta_t) {
  269. const float z_center = z_pt[CEN];
  270. abc_float_t diff_mm_at_pt_axis[NPP + 1], new_mm_at_pt_axis[NPP + 1];
  271. reverse_kinematics_probe_points(z_pt, diff_mm_at_pt_axis, dcr);
  272. delta_radius += delta_r;
  273. delta_tower_angle_trim += delta_t;
  274. recalc_delta_settings();
  275. reverse_kinematics_probe_points(z_pt, new_mm_at_pt_axis, dcr);
  276. LOOP_CAL_ALL(rad) diff_mm_at_pt_axis[rad] -= new_mm_at_pt_axis[rad] + delta_e;
  277. forward_kinematics_probe_points(diff_mm_at_pt_axis, z_pt, dcr);
  278. LOOP_CAL_RAD(rad) z_pt[rad] -= z_pt[CEN] - z_center;
  279. z_pt[CEN] = z_center;
  280. delta_radius -= delta_r;
  281. delta_tower_angle_trim -= delta_t;
  282. recalc_delta_settings();
  283. }
  284. static float auto_tune_h(const float dcr) {
  285. const float r_quot = dcr / delta_radius;
  286. return RECIPROCAL(r_quot / (2.0f / 3.0f)); // (2/3)/CR
  287. }
  288. static float auto_tune_r(const float dcr) {
  289. constexpr float diff = 0.01f, delta_r = diff;
  290. float r_fac = 0.0f, z_pt[NPP + 1] = { 0.0f };
  291. abc_float_t delta_e = { 0.0f }, delta_t = { 0.0f };
  292. calc_kinematics_diff_probe_points(z_pt, dcr, delta_e, delta_r, delta_t);
  293. r_fac = -(z_pt[__A] + z_pt[__B] + z_pt[__C] + z_pt[_BC] + z_pt[_CA] + z_pt[_AB]) / 6.0f;
  294. r_fac = diff / r_fac / 3.0f; // 1/(3*delta_Z)
  295. return r_fac;
  296. }
  297. static float auto_tune_a(const float dcr) {
  298. constexpr float diff = 0.01f, delta_r = 0.0f;
  299. float a_fac = 0.0f, z_pt[NPP + 1] = { 0.0f };
  300. abc_float_t delta_e = { 0.0f }, delta_t = { 0.0f };
  301. delta_t.reset();
  302. LOOP_NUM_AXES(axis) {
  303. delta_t[axis] = diff;
  304. calc_kinematics_diff_probe_points(z_pt, dcr, delta_e, delta_r, delta_t);
  305. delta_t[axis] = 0;
  306. a_fac += z_pt[uint8_t((axis * _4P_STEP) - _7P_STEP + NPP) % NPP + 1] / 6.0f;
  307. a_fac -= z_pt[uint8_t((axis * _4P_STEP) + 1 + _7P_STEP)] / 6.0f;
  308. }
  309. a_fac = diff / a_fac / 3.0f; // 1/(3*delta_Z)
  310. return a_fac;
  311. }
  312. /**
  313. * G33 - Delta '1-4-7-point' Auto-Calibration
  314. * Calibrate height, z_offset, endstops, delta radius, and tower angles.
  315. *
  316. * Parameters:
  317. *
  318. * Pn Number of probe points:
  319. * P0 Normalizes calibration.
  320. * P1 Calibrates height only with center probe.
  321. * P2 Probe center and towers. Calibrate height, endstops and delta radius.
  322. * P3 Probe all positions: center, towers and opposite towers. Calibrate all.
  323. * P4-P10 Probe all positions at different intermediate locations and average them.
  324. *
  325. * Rn.nn Temporary reduce the probe grid by the specified amount (mm)
  326. *
  327. * T Don't calibrate tower angle corrections
  328. *
  329. * Cn.nn Calibration precision; when omitted calibrates to maximum precision
  330. *
  331. * Fn Force to run at least n iterations and take the best result
  332. *
  333. * Vn Verbose level:
  334. * V0 Dry-run mode. Report settings and probe results. No calibration.
  335. * V1 Report start and end settings only
  336. * V2 Report settings at each iteration
  337. * V3 Report settings and probe results
  338. *
  339. * E Engage the probe for each point
  340. *
  341. * O Probe at offsetted probe positions (this is wrong but it seems to work)
  342. *
  343. * With SENSORLESS_PROBING:
  344. * Use these flags to calibrate stall sensitivity: (e.g., `G33 P1 Y Z` to calibrate X only.)
  345. * X Don't activate stallguard on X.
  346. * Y Don't activate stallguard on Y.
  347. * Z Don't activate stallguard on Z.
  348. *
  349. * S Save offset_sensorless_adj
  350. */
  351. void GcodeSuite::G33() {
  352. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_PROBE));
  353. const int8_t probe_points = parser.intval('P', DELTA_CALIBRATION_DEFAULT_POINTS);
  354. if (!WITHIN(probe_points, 0, 10)) {
  355. SERIAL_ECHOLNPGM("?(P)oints implausible (0-10).");
  356. return;
  357. }
  358. const bool probe_at_offset = TERN0(HAS_PROBE_XY_OFFSET, parser.seen_test('O')),
  359. towers_set = !parser.seen_test('T');
  360. // The calibration radius is set to a calculated value
  361. float dcr = probe_at_offset ? DELTA_PRINTABLE_RADIUS : DELTA_PRINTABLE_RADIUS - PROBING_MARGIN;
  362. #if HAS_PROBE_XY_OFFSET
  363. const float total_offset = HYPOT(probe.offset_xy.x, probe.offset_xy.y);
  364. dcr -= probe_at_offset ? _MAX(total_offset, PROBING_MARGIN) : total_offset;
  365. #endif
  366. NOMORE(dcr, DELTA_PRINTABLE_RADIUS);
  367. if (parser.seenval('R')) dcr -= _MAX(parser.value_float(), 0.0f);
  368. TERN_(HAS_DELTA_SENSORLESS_PROBING, dcr *= sensorless_radius_factor);
  369. const float calibration_precision = parser.floatval('C', 0.0f);
  370. if (calibration_precision < 0) {
  371. SERIAL_ECHOLNPGM("?(C)alibration precision implausible (>=0).");
  372. return;
  373. }
  374. const int8_t force_iterations = parser.intval('F', 0);
  375. if (!WITHIN(force_iterations, 0, 30)) {
  376. SERIAL_ECHOLNPGM("?(F)orce iteration implausible (0-30).");
  377. return;
  378. }
  379. const int8_t verbose_level = parser.byteval('V', 1);
  380. if (!WITHIN(verbose_level, 0, 3)) {
  381. SERIAL_ECHOLNPGM("?(V)erbose level implausible (0-3).");
  382. return;
  383. }
  384. const bool stow_after_each = parser.seen_test('E');
  385. #if HAS_DELTA_SENSORLESS_PROBING
  386. probe.test_sensitivity = { !parser.seen_test('X'), !parser.seen_test('Y'), !parser.seen_test('Z') };
  387. const bool do_save_offset_adj = parser.seen_test('S');
  388. #endif
  389. const bool _0p_calibration = probe_points == 0,
  390. _1p_calibration = probe_points == 1 || probe_points == -1,
  391. _4p_calibration = probe_points == 2,
  392. _4p_opposite_points = _4p_calibration && !towers_set,
  393. _7p_9_center = probe_points >= 8,
  394. _tower_results = (_4p_calibration && towers_set) || probe_points >= 3,
  395. _opposite_results = (_4p_calibration && !towers_set) || probe_points >= 3,
  396. _endstop_results = probe_points != 1 && probe_points != -1 && probe_points != 0,
  397. _angle_results = probe_points >= 3 && towers_set;
  398. int8_t iterations = 0;
  399. float test_precision,
  400. zero_std_dev = (verbose_level ? 999.0f : 0.0f), // 0.0 in dry-run mode : forced end
  401. zero_std_dev_min = zero_std_dev,
  402. zero_std_dev_old = zero_std_dev,
  403. h_factor, r_factor, a_factor,
  404. r_old = delta_radius,
  405. h_old = delta_height;
  406. abc_pos_t e_old = delta_endstop_adj, a_old = delta_tower_angle_trim;
  407. SERIAL_ECHOLNPGM("G33 Auto Calibrate");
  408. // Report settings
  409. PGM_P const checkingac = PSTR("Checking... AC");
  410. SERIAL_ECHOPGM_P(checkingac);
  411. SERIAL_ECHOPGM(" at radius:", dcr);
  412. if (verbose_level == 0) SERIAL_ECHOPGM(" (DRY-RUN)");
  413. SERIAL_EOL();
  414. ui.set_status(checkingac);
  415. print_calibration_settings(_endstop_results, _angle_results);
  416. ac_setup(!_0p_calibration && !_1p_calibration);
  417. if (!_0p_calibration) ac_home();
  418. #if HAS_DELTA_SENSORLESS_PROBING
  419. if (verbose_level > 0 && do_save_offset_adj) {
  420. offset_sensorless_adj.reset();
  421. auto caltower = [&](Probe::sense_bool_t s){
  422. float z_at_pt[NPP + 1];
  423. LOOP_CAL_ALL(rad) z_at_pt[rad] = 0.0f;
  424. probe.test_sensitivity = s;
  425. if (probe_calibration_points(z_at_pt, 1, dcr, false, false, probe_at_offset))
  426. probe.set_offset_sensorless_adj(z_at_pt[CEN]);
  427. };
  428. caltower({ true, false, false }); // A
  429. caltower({ false, true, false }); // B
  430. caltower({ false, false, true }); // C
  431. probe.test_sensitivity = { true, true, true }; // reset to all
  432. }
  433. #endif
  434. do { // start iterations
  435. float z_at_pt[NPP + 1] = { 0.0f };
  436. test_precision = zero_std_dev_old != 999.0f ? (zero_std_dev + zero_std_dev_old) / 2.0f : zero_std_dev;
  437. iterations++;
  438. // Probe the points
  439. zero_std_dev_old = zero_std_dev;
  440. if (!probe_calibration_points(z_at_pt, probe_points, dcr, towers_set, stow_after_each, probe_at_offset)) {
  441. SERIAL_ECHOLNPGM("Correct delta settings with M665 and M666");
  442. return ac_cleanup(TERN_(HAS_MULTI_HOTEND, old_tool_index));
  443. }
  444. zero_std_dev = std_dev_points(z_at_pt, _0p_calibration, _1p_calibration, _4p_calibration, _4p_opposite_points);
  445. // Solve matrices
  446. if ((zero_std_dev < test_precision || iterations <= force_iterations) && zero_std_dev > calibration_precision) {
  447. #if !HAS_BED_PROBE
  448. test_precision = 0.0f; // forced end
  449. #endif
  450. if (zero_std_dev < zero_std_dev_min) {
  451. // set roll-back point
  452. e_old = delta_endstop_adj;
  453. r_old = delta_radius;
  454. h_old = delta_height;
  455. a_old = delta_tower_angle_trim;
  456. }
  457. abc_float_t e_delta = { 0.0f }, t_delta = { 0.0f };
  458. float r_delta = 0.0f;
  459. /**
  460. * convergence matrices:
  461. * see https://github.com/LVD-AC/Marlin-AC/tree/1.1.x-AC/documentation for
  462. * - definition of the matrix scaling parameters
  463. * - matrices for 4 and 7 point calibration
  464. */
  465. #define ZP(N,I) ((N) * z_at_pt[I] / 4.0f) // 4.0 = divider to normalize to integers
  466. #define Z12(I) ZP(12, I)
  467. #define Z4(I) ZP(4, I)
  468. #define Z2(I) ZP(2, I)
  469. #define Z1(I) ZP(1, I)
  470. #define Z0(I) ZP(0, I)
  471. // calculate factors
  472. if (_7p_9_center) dcr *= 0.9f;
  473. h_factor = auto_tune_h(dcr);
  474. r_factor = auto_tune_r(dcr);
  475. a_factor = auto_tune_a(dcr);
  476. if (_7p_9_center) dcr /= 0.9f;
  477. switch (probe_points) {
  478. case 0:
  479. test_precision = 0.0f; // forced end
  480. break;
  481. case 1:
  482. test_precision = 0.0f; // forced end
  483. LOOP_NUM_AXES(axis) e_delta[axis] = +Z4(CEN);
  484. break;
  485. case 2:
  486. if (towers_set) { // see 4 point calibration (towers) matrix
  487. e_delta.set((+Z4(__A) -Z2(__B) -Z2(__C)) * h_factor +Z4(CEN),
  488. (-Z2(__A) +Z4(__B) -Z2(__C)) * h_factor +Z4(CEN),
  489. (-Z2(__A) -Z2(__B) +Z4(__C)) * h_factor +Z4(CEN));
  490. r_delta = (+Z4(__A) +Z4(__B) +Z4(__C) -Z12(CEN)) * r_factor;
  491. }
  492. else { // see 4 point calibration (opposites) matrix
  493. e_delta.set((-Z4(_BC) +Z2(_CA) +Z2(_AB)) * h_factor +Z4(CEN),
  494. (+Z2(_BC) -Z4(_CA) +Z2(_AB)) * h_factor +Z4(CEN),
  495. (+Z2(_BC) +Z2(_CA) -Z4(_AB)) * h_factor +Z4(CEN));
  496. r_delta = (+Z4(_BC) +Z4(_CA) +Z4(_AB) -Z12(CEN)) * r_factor;
  497. }
  498. break;
  499. default: // see 7 point calibration (towers & opposites) matrix
  500. e_delta.set((+Z2(__A) -Z1(__B) -Z1(__C) -Z2(_BC) +Z1(_CA) +Z1(_AB)) * h_factor +Z4(CEN),
  501. (-Z1(__A) +Z2(__B) -Z1(__C) +Z1(_BC) -Z2(_CA) +Z1(_AB)) * h_factor +Z4(CEN),
  502. (-Z1(__A) -Z1(__B) +Z2(__C) +Z1(_BC) +Z1(_CA) -Z2(_AB)) * h_factor +Z4(CEN));
  503. r_delta = (+Z2(__A) +Z2(__B) +Z2(__C) +Z2(_BC) +Z2(_CA) +Z2(_AB) -Z12(CEN)) * r_factor;
  504. if (towers_set) { // see 7 point tower angle calibration (towers & opposites) matrix
  505. t_delta.set((+Z0(__A) -Z4(__B) +Z4(__C) +Z0(_BC) -Z4(_CA) +Z4(_AB) +Z0(CEN)) * a_factor,
  506. (+Z4(__A) +Z0(__B) -Z4(__C) +Z4(_BC) +Z0(_CA) -Z4(_AB) +Z0(CEN)) * a_factor,
  507. (-Z4(__A) +Z4(__B) +Z0(__C) -Z4(_BC) +Z4(_CA) +Z0(_AB) +Z0(CEN)) * a_factor);
  508. }
  509. break;
  510. }
  511. delta_endstop_adj += e_delta;
  512. delta_radius += r_delta;
  513. delta_tower_angle_trim += t_delta;
  514. }
  515. else if (zero_std_dev >= test_precision) {
  516. // roll back
  517. delta_endstop_adj = e_old;
  518. delta_radius = r_old;
  519. delta_height = h_old;
  520. delta_tower_angle_trim = a_old;
  521. }
  522. if (verbose_level != 0) { // !dry run
  523. // Normalize angles to least-squares
  524. if (_angle_results) {
  525. float a_sum = 0.0f;
  526. LOOP_NUM_AXES(axis) a_sum += delta_tower_angle_trim[axis];
  527. LOOP_NUM_AXES(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0f;
  528. }
  529. // adjust delta_height and endstops by the max amount
  530. const float z_temp = _MAX(delta_endstop_adj.a, delta_endstop_adj.b, delta_endstop_adj.c);
  531. delta_height -= z_temp;
  532. LOOP_NUM_AXES(axis) delta_endstop_adj[axis] -= z_temp;
  533. }
  534. recalc_delta_settings();
  535. NOMORE(zero_std_dev_min, zero_std_dev);
  536. // print report
  537. if (verbose_level == 3 || verbose_level == 0) {
  538. print_calibration_results(z_at_pt, _tower_results, _opposite_results);
  539. #if HAS_DELTA_SENSORLESS_PROBING
  540. if (verbose_level == 0 && probe_points == 1) {
  541. if (do_save_offset_adj)
  542. probe.set_offset_sensorless_adj(z_at_pt[CEN]);
  543. else
  544. probe.refresh_largest_sensorless_adj();
  545. }
  546. #endif
  547. }
  548. if (verbose_level != 0) { // !dry run
  549. if ((zero_std_dev >= test_precision && iterations > force_iterations) || zero_std_dev <= calibration_precision) { // end iterations
  550. SERIAL_ECHOPGM("Calibration OK");
  551. SERIAL_ECHO_SP(32);
  552. #if HAS_BED_PROBE
  553. if (zero_std_dev >= test_precision && !_1p_calibration && !_0p_calibration)
  554. SERIAL_ECHOPGM("rolling back.");
  555. else
  556. #endif
  557. {
  558. SERIAL_ECHOPAIR_F("std dev:", zero_std_dev_min, 3);
  559. }
  560. SERIAL_EOL();
  561. char mess[21];
  562. strcpy_P(mess, PSTR("Calibration sd:"));
  563. if (zero_std_dev_min < 1)
  564. sprintf_P(&mess[15], PSTR("0.%03i"), (int)LROUND(zero_std_dev_min * 1000.0f));
  565. else
  566. sprintf_P(&mess[15], PSTR("%03i.x"), (int)LROUND(zero_std_dev_min));
  567. ui.set_status(mess);
  568. print_calibration_settings(_endstop_results, _angle_results);
  569. SERIAL_ECHOLNPGM("Save with M500 and/or copy to Configuration.h");
  570. }
  571. else { // !end iterations
  572. char mess[15];
  573. if (iterations < 31)
  574. sprintf_P(mess, PSTR("Iteration : %02i"), (unsigned int)iterations);
  575. else
  576. strcpy_P(mess, PSTR("No convergence"));
  577. SERIAL_ECHO(mess);
  578. SERIAL_ECHO_SP(32);
  579. SERIAL_ECHOLNPAIR_F("std dev:", zero_std_dev, 3);
  580. ui.set_status(mess);
  581. if (verbose_level > 1)
  582. print_calibration_settings(_endstop_results, _angle_results);
  583. }
  584. }
  585. else { // dry run
  586. FSTR_P const enddryrun = F("End DRY-RUN");
  587. SERIAL_ECHOF(enddryrun);
  588. SERIAL_ECHO_SP(35);
  589. SERIAL_ECHOLNPAIR_F("std dev:", zero_std_dev, 3);
  590. char mess[21];
  591. strcpy_P(mess, FTOP(enddryrun));
  592. strcpy_P(&mess[11], PSTR(" sd:"));
  593. if (zero_std_dev < 1)
  594. sprintf_P(&mess[15], PSTR("0.%03i"), (int)LROUND(zero_std_dev * 1000.0f));
  595. else
  596. sprintf_P(&mess[15], PSTR("%03i.x"), (int)LROUND(zero_std_dev));
  597. ui.set_status(mess);
  598. }
  599. ac_home();
  600. }
  601. while (((zero_std_dev < test_precision && iterations < 31) || iterations <= force_iterations) && zero_std_dev > calibration_precision);
  602. ac_cleanup(TERN_(HAS_MULTI_HOTEND, old_tool_index));
  603. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE));
  604. #if HAS_DELTA_SENSORLESS_PROBING
  605. probe.test_sensitivity = { true, true, true };
  606. #endif
  607. }
  608. #endif // DELTA_AUTO_CALIBRATION