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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://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/stepper.h"
  28. #include "../../module/endstops.h"
  29. #include "../../lcd/ultralcd.h"
  30. #if HAS_BED_PROBE
  31. #include "../../module/probe.h"
  32. #endif
  33. #if HOTENDS > 1
  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 HOTENDS > 1
  58. const uint8_t old_tool_index = active_extruder;
  59. #define AC_CLEANUP() ac_cleanup(old_tool_index)
  60. #else
  61. #define AC_CLEANUP() ac_cleanup()
  62. #endif
  63. float lcd_probe_pt(const float &rx, const float &ry);
  64. bool ac_home() {
  65. endstops.enable(true);
  66. if (!home_delta())
  67. return false;
  68. endstops.not_homing();
  69. return true;
  70. }
  71. void ac_setup(const bool reset_bed) {
  72. #if HOTENDS > 1
  73. tool_change(0, 0, true);
  74. #endif
  75. planner.synchronize();
  76. setup_for_endstop_or_probe_move();
  77. #if HAS_LEVELING
  78. if (reset_bed) reset_bed_level(); // After full calibration bed-level data is no longer valid
  79. #endif
  80. }
  81. void ac_cleanup(
  82. #if HOTENDS > 1
  83. const uint8_t old_tool_index
  84. #endif
  85. ) {
  86. #if ENABLED(DELTA_HOME_TO_SAFE_ZONE)
  87. do_blocking_move_to_z(delta_clip_start_height);
  88. #endif
  89. #if HAS_BED_PROBE
  90. STOW_PROBE();
  91. #endif
  92. clean_up_after_endstop_or_probe_move();
  93. #if HOTENDS > 1
  94. tool_change(old_tool_index, 0, true);
  95. #endif
  96. }
  97. void print_signed_float(const char * const prefix, const float &f) {
  98. SERIAL_PROTOCOLPGM(" ");
  99. serialprintPGM(prefix);
  100. SERIAL_PROTOCOLCHAR(':');
  101. if (f >= 0) SERIAL_CHAR('+');
  102. SERIAL_PROTOCOL_F(f, 2);
  103. }
  104. /**
  105. * - Print the delta settings
  106. */
  107. static void print_calibration_settings(const bool end_stops, const bool tower_angles) {
  108. SERIAL_PROTOCOLPAIR(".Height:", delta_height);
  109. if (end_stops) {
  110. print_signed_float(PSTR("Ex"), delta_endstop_adj[A_AXIS]);
  111. print_signed_float(PSTR("Ey"), delta_endstop_adj[B_AXIS]);
  112. print_signed_float(PSTR("Ez"), delta_endstop_adj[C_AXIS]);
  113. }
  114. if (end_stops && tower_angles) {
  115. SERIAL_PROTOCOLPAIR(" Radius:", delta_radius);
  116. SERIAL_EOL();
  117. SERIAL_CHAR('.');
  118. SERIAL_PROTOCOL_SP(13);
  119. }
  120. if (tower_angles) {
  121. print_signed_float(PSTR("Tx"), delta_tower_angle_trim[A_AXIS]);
  122. print_signed_float(PSTR("Ty"), delta_tower_angle_trim[B_AXIS]);
  123. print_signed_float(PSTR("Tz"), delta_tower_angle_trim[C_AXIS]);
  124. }
  125. if ((!end_stops && tower_angles) || (end_stops && !tower_angles)) { // XOR
  126. SERIAL_PROTOCOLPAIR(" Radius:", delta_radius);
  127. }
  128. #if HAS_BED_PROBE
  129. if (!end_stops && !tower_angles) {
  130. SERIAL_PROTOCOL_SP(30);
  131. print_signed_float(PSTR("Offset"), zprobe_zoffset);
  132. }
  133. #endif
  134. SERIAL_EOL();
  135. }
  136. /**
  137. * - Print the probe results
  138. */
  139. static void print_calibration_results(const float z_pt[NPP + 1], const bool tower_points, const bool opposite_points) {
  140. SERIAL_PROTOCOLPGM(". ");
  141. print_signed_float(PSTR("c"), z_pt[CEN]);
  142. if (tower_points) {
  143. print_signed_float(PSTR(" x"), z_pt[__A]);
  144. print_signed_float(PSTR(" y"), z_pt[__B]);
  145. print_signed_float(PSTR(" z"), z_pt[__C]);
  146. }
  147. if (tower_points && opposite_points) {
  148. SERIAL_EOL();
  149. SERIAL_CHAR('.');
  150. SERIAL_PROTOCOL_SP(13);
  151. }
  152. if (opposite_points) {
  153. print_signed_float(PSTR("yz"), z_pt[_BC]);
  154. print_signed_float(PSTR("zx"), z_pt[_CA]);
  155. print_signed_float(PSTR("xy"), z_pt[_AB]);
  156. }
  157. SERIAL_EOL();
  158. }
  159. /**
  160. * - Calculate the standard deviation from the zero plane
  161. */
  162. 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) {
  163. if (!_0p_cal) {
  164. float S2 = sq(z_pt[CEN]);
  165. int16_t N = 1;
  166. if (!_1p_cal) { // std dev from zero plane
  167. LOOP_CAL_ACT(rad, _4p_cal, _4p_opp) {
  168. S2 += sq(z_pt[rad]);
  169. N++;
  170. }
  171. return round(SQRT(S2 / N) * 1000.0) / 1000.0 + 0.00001;
  172. }
  173. }
  174. return 0.00001;
  175. }
  176. /**
  177. * - Probe a point
  178. */
  179. static float calibration_probe(const float &nx, const float &ny, const bool stow, const bool set_up) {
  180. #if HAS_BED_PROBE
  181. return probe_pt(nx, ny, set_up ? PROBE_PT_BIG_RAISE : stow ? PROBE_PT_STOW : PROBE_PT_RAISE, 0, false);
  182. #else
  183. UNUSED(stow);
  184. UNUSED(set_up);
  185. return lcd_probe_pt(nx, ny);
  186. #endif
  187. }
  188. #if HAS_BED_PROBE
  189. static float probe_z_shift(const float center) {
  190. STOW_PROBE();
  191. endstops.enable_z_probe(false);
  192. float z_shift = lcd_probe_pt(0, 0) - center;
  193. endstops.enable_z_probe(true);
  194. return z_shift;
  195. }
  196. #endif
  197. /**
  198. * - Probe a grid
  199. */
  200. static bool probe_calibration_points(float z_pt[NPP + 1], const int8_t probe_points, const bool towers_set, const bool stow_after_each, const bool set_up) {
  201. const bool _0p_calibration = probe_points == 0,
  202. _1p_calibration = probe_points == 1 || probe_points == -1,
  203. _4p_calibration = probe_points == 2,
  204. _4p_opposite_points = _4p_calibration && !towers_set,
  205. _7p_calibration = probe_points >= 3,
  206. _7p_no_intermediates = probe_points == 3,
  207. _7p_1_intermediates = probe_points == 4,
  208. _7p_2_intermediates = probe_points == 5,
  209. _7p_4_intermediates = probe_points == 6,
  210. _7p_6_intermediates = probe_points == 7,
  211. _7p_8_intermediates = probe_points == 8,
  212. _7p_11_intermediates = probe_points == 9,
  213. _7p_14_intermediates = probe_points == 10,
  214. _7p_intermed_points = probe_points >= 4,
  215. _7p_6_center = probe_points >= 5 && probe_points <= 7,
  216. _7p_9_center = probe_points >= 8;
  217. LOOP_CAL_ALL(rad) z_pt[rad] = 0.0;
  218. if (!_0p_calibration) {
  219. if (!_7p_no_intermediates && !_7p_4_intermediates && !_7p_11_intermediates) { // probe the center
  220. z_pt[CEN] += calibration_probe(0, 0, stow_after_each, set_up);
  221. if (isnan(z_pt[CEN])) return false;
  222. }
  223. if (_7p_calibration) { // probe extra center points
  224. const float start = _7p_9_center ? _CA + _7P_STEP / 3.0 : _7p_6_center ? _CA : __C,
  225. steps = _7p_9_center ? _4P_STEP / 3.0 : _7p_6_center ? _7P_STEP : _4P_STEP;
  226. I_LOOP_CAL_PT(rad, start, steps) {
  227. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  228. r = delta_calibration_radius * 0.1;
  229. z_pt[CEN] += calibration_probe(cos(a) * r, sin(a) * r, stow_after_each, set_up);
  230. if (isnan(z_pt[CEN])) return false;
  231. }
  232. z_pt[CEN] /= float(_7p_2_intermediates ? 7 : probe_points);
  233. }
  234. if (!_1p_calibration) { // probe the radius
  235. const CalEnum start = _4p_opposite_points ? _AB : __A;
  236. const float steps = _7p_14_intermediates ? _7P_STEP / 15.0 : // 15r * 6 + 10c = 100
  237. _7p_11_intermediates ? _7P_STEP / 12.0 : // 12r * 6 + 9c = 81
  238. _7p_8_intermediates ? _7P_STEP / 9.0 : // 9r * 6 + 10c = 64
  239. _7p_6_intermediates ? _7P_STEP / 7.0 : // 7r * 6 + 7c = 49
  240. _7p_4_intermediates ? _7P_STEP / 5.0 : // 5r * 6 + 6c = 36
  241. _7p_2_intermediates ? _7P_STEP / 3.0 : // 3r * 6 + 7c = 25
  242. _7p_1_intermediates ? _7P_STEP / 2.0 : // 2r * 6 + 4c = 16
  243. _7p_no_intermediates ? _7P_STEP : // 1r * 6 + 3c = 9
  244. _4P_STEP; // .5r * 6 + 1c = 4
  245. bool zig_zag = true;
  246. F_LOOP_CAL_PT(rad, start, _7p_9_center ? steps * 3 : steps) {
  247. const int8_t offset = _7p_9_center ? 2 : 0;
  248. for (int8_t circle = 0; circle <= offset; circle++) {
  249. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  250. r = delta_calibration_radius * (1 - 0.1 * (zig_zag ? offset - circle : circle)),
  251. interpol = fmod(rad, 1);
  252. const float z_temp = calibration_probe(cos(a) * r, sin(a) * r, stow_after_each, set_up);
  253. if (isnan(z_temp)) return false;
  254. // split probe point to neighbouring calibration points
  255. z_pt[uint8_t(round(rad - interpol + NPP - 1)) % NPP + 1] += z_temp * sq(cos(RADIANS(interpol * 90)));
  256. z_pt[uint8_t(round(rad - interpol)) % NPP + 1] += z_temp * sq(sin(RADIANS(interpol * 90)));
  257. }
  258. zig_zag = !zig_zag;
  259. }
  260. if (_7p_intermed_points)
  261. LOOP_CAL_RAD(rad)
  262. z_pt[rad] /= _7P_STEP / steps;
  263. do_blocking_move_to_xy(0.0, 0.0);
  264. }
  265. }
  266. return true;
  267. }
  268. /**
  269. * kinematics routines and auto tune matrix scaling parameters:
  270. * see https://github.com/LVD-AC/Marlin-AC/tree/1.1.x-AC/documentation for
  271. * - formulae for approximative forward kinematics in the end-stop displacement matrix
  272. * - definition of the matrix scaling parameters
  273. */
  274. static void reverse_kinematics_probe_points(float z_pt[NPP + 1], float mm_at_pt_axis[NPP + 1][ABC]) {
  275. float pos[XYZ] = { 0.0 };
  276. LOOP_CAL_ALL(rad) {
  277. const float a = RADIANS(210 + (360 / NPP) * (rad - 1)),
  278. r = (rad == CEN ? 0.0 : delta_calibration_radius);
  279. pos[X_AXIS] = cos(a) * r;
  280. pos[Y_AXIS] = sin(a) * r;
  281. pos[Z_AXIS] = z_pt[rad];
  282. inverse_kinematics(pos);
  283. LOOP_XYZ(axis) mm_at_pt_axis[rad][axis] = delta[axis];
  284. }
  285. }
  286. static void forward_kinematics_probe_points(float mm_at_pt_axis[NPP + 1][ABC], float z_pt[NPP + 1]) {
  287. const float r_quot = delta_calibration_radius / delta_radius;
  288. #define ZPP(N,I,A) ((1 / 3.0 + r_quot * (N) / 3.0 ) * mm_at_pt_axis[I][A])
  289. #define Z00(I, A) ZPP( 0, I, A)
  290. #define Zp1(I, A) ZPP(+1, I, A)
  291. #define Zm1(I, A) ZPP(-1, I, A)
  292. #define Zp2(I, A) ZPP(+2, I, A)
  293. #define Zm2(I, A) ZPP(-2, I, A)
  294. z_pt[CEN] = Z00(CEN, A_AXIS) + Z00(CEN, B_AXIS) + Z00(CEN, C_AXIS);
  295. z_pt[__A] = Zp2(__A, A_AXIS) + Zm1(__A, B_AXIS) + Zm1(__A, C_AXIS);
  296. z_pt[__B] = Zm1(__B, A_AXIS) + Zp2(__B, B_AXIS) + Zm1(__B, C_AXIS);
  297. z_pt[__C] = Zm1(__C, A_AXIS) + Zm1(__C, B_AXIS) + Zp2(__C, C_AXIS);
  298. z_pt[_BC] = Zm2(_BC, A_AXIS) + Zp1(_BC, B_AXIS) + Zp1(_BC, C_AXIS);
  299. z_pt[_CA] = Zp1(_CA, A_AXIS) + Zm2(_CA, B_AXIS) + Zp1(_CA, C_AXIS);
  300. z_pt[_AB] = Zp1(_AB, A_AXIS) + Zp1(_AB, B_AXIS) + Zm2(_AB, C_AXIS);
  301. }
  302. static void calc_kinematics_diff_probe_points(float z_pt[NPP + 1], float delta_e[ABC], float delta_r, float delta_t[ABC]) {
  303. const float z_center = z_pt[CEN];
  304. float diff_mm_at_pt_axis[NPP + 1][ABC],
  305. new_mm_at_pt_axis[NPP + 1][ABC];
  306. reverse_kinematics_probe_points(z_pt, diff_mm_at_pt_axis);
  307. delta_radius += delta_r;
  308. LOOP_XYZ(axis) delta_tower_angle_trim[axis] += delta_t[axis];
  309. recalc_delta_settings();
  310. reverse_kinematics_probe_points(z_pt, new_mm_at_pt_axis);
  311. LOOP_XYZ(axis) LOOP_CAL_ALL(rad) diff_mm_at_pt_axis[rad][axis] -= new_mm_at_pt_axis[rad][axis] + delta_e[axis];
  312. forward_kinematics_probe_points(diff_mm_at_pt_axis, z_pt);
  313. LOOP_CAL_RAD(rad) z_pt[rad] -= z_pt[CEN] - z_center;
  314. z_pt[CEN] = z_center;
  315. delta_radius -= delta_r;
  316. LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= delta_t[axis];
  317. recalc_delta_settings();
  318. }
  319. static float auto_tune_h() {
  320. const float r_quot = delta_calibration_radius / delta_radius;
  321. float h_fac = 0.0;
  322. h_fac = r_quot / (2.0 / 3.0);
  323. h_fac = 1.0 / h_fac; // (2/3)/CR
  324. return h_fac;
  325. }
  326. static float auto_tune_r() {
  327. const float diff = 0.01;
  328. float r_fac = 0.0,
  329. z_pt[NPP + 1] = { 0.0 },
  330. delta_e[ABC] = {0.0},
  331. delta_r = {0.0},
  332. delta_t[ABC] = {0.0};
  333. delta_r = diff;
  334. calc_kinematics_diff_probe_points(z_pt, delta_e, delta_r, delta_t);
  335. r_fac = -(z_pt[__A] + z_pt[__B] + z_pt[__C] + z_pt[_BC] + z_pt[_CA] + z_pt[_AB]) / 6.0;
  336. r_fac = diff / r_fac / 3.0; // 1/(3*delta_Z)
  337. return r_fac;
  338. }
  339. static float auto_tune_a() {
  340. const float diff = 0.01;
  341. float a_fac = 0.0,
  342. z_pt[NPP + 1] = { 0.0 },
  343. delta_e[ABC] = {0.0},
  344. delta_r = {0.0},
  345. delta_t[ABC] = {0.0};
  346. LOOP_XYZ(axis) {
  347. LOOP_XYZ(axis_2) delta_t[axis_2] = 0.0;
  348. delta_t[axis] = diff;
  349. calc_kinematics_diff_probe_points(z_pt, delta_e, delta_r, delta_t);
  350. a_fac += z_pt[uint8_t((axis * _4P_STEP) - _7P_STEP + NPP) % NPP + 1] / 6.0;
  351. a_fac -= z_pt[uint8_t((axis * _4P_STEP) + 1 + _7P_STEP)] / 6.0;
  352. }
  353. a_fac = diff / a_fac / 3.0; // 1/(3*delta_Z)
  354. return a_fac;
  355. }
  356. /**
  357. * G33 - Delta '1-4-7-point' Auto-Calibration
  358. * Calibrate height, z_offset, endstops, delta radius, and tower angles.
  359. *
  360. * Parameters:
  361. *
  362. * S Setup mode; disables probe protection
  363. *
  364. * Pn Number of probe points:
  365. * P-1 Checks the z_offset with a center probe and paper test.
  366. * P0 Normalizes calibration.
  367. * P1 Calibrates height only with center probe.
  368. * P2 Probe center and towers. Calibrate height, endstops and delta radius.
  369. * P3 Probe all positions: center, towers and opposite towers. Calibrate all.
  370. * P4-P10 Probe all positions at different intermediate locations and average them.
  371. *
  372. * T Don't calibrate tower angle corrections
  373. *
  374. * Cn.nn Calibration precision; when omitted calibrates to maximum precision
  375. *
  376. * Fn Force to run at least n iterations and take the best result
  377. *
  378. * Vn Verbose level:
  379. * V0 Dry-run mode. Report settings and probe results. No calibration.
  380. * V1 Report start and end settings only
  381. * V2 Report settings at each iteration
  382. * V3 Report settings and probe results
  383. *
  384. * E Engage the probe for each point
  385. */
  386. void GcodeSuite::G33() {
  387. const bool set_up =
  388. #if HAS_BED_PROBE
  389. parser.seen('S');
  390. #else
  391. false;
  392. #endif
  393. const int8_t probe_points = set_up ? 2 : parser.intval('P', DELTA_CALIBRATION_DEFAULT_POINTS);
  394. if (!WITHIN(probe_points, -1, 10)) {
  395. SERIAL_PROTOCOLLNPGM("?(P)oints is implausible (-1 - 10).");
  396. return;
  397. }
  398. const bool towers_set = !parser.seen('T');
  399. const float calibration_precision = set_up ? Z_CLEARANCE_BETWEEN_PROBES / 5.0 : parser.floatval('C', 0.0);
  400. if (calibration_precision < 0) {
  401. SERIAL_PROTOCOLLNPGM("?(C)alibration precision is implausible (>=0).");
  402. return;
  403. }
  404. const int8_t force_iterations = parser.intval('F', 0);
  405. if (!WITHIN(force_iterations, 0, 30)) {
  406. SERIAL_PROTOCOLLNPGM("?(F)orce iteration is implausible (0 - 30).");
  407. return;
  408. }
  409. const int8_t verbose_level = parser.byteval('V', 1);
  410. if (!WITHIN(verbose_level, 0, 3)) {
  411. SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0 - 3).");
  412. return;
  413. }
  414. const bool stow_after_each = parser.seen('E');
  415. if (set_up) {
  416. delta_height = 999.99;
  417. delta_radius = DELTA_PRINTABLE_RADIUS;
  418. ZERO(delta_endstop_adj);
  419. ZERO(delta_tower_angle_trim);
  420. recalc_delta_settings();
  421. }
  422. const bool _0p_calibration = probe_points == 0,
  423. _1p_calibration = probe_points == 1 || probe_points == -1,
  424. _4p_calibration = probe_points == 2,
  425. _4p_opposite_points = _4p_calibration && !towers_set,
  426. _7p_9_center = probe_points >= 8,
  427. _tower_results = (_4p_calibration && towers_set) || probe_points >= 3,
  428. _opposite_results = (_4p_calibration && !towers_set) || probe_points >= 3,
  429. _endstop_results = probe_points != 1 && probe_points != -1 && probe_points != 0,
  430. _angle_results = probe_points >= 3 && towers_set;
  431. static const char save_message[] PROGMEM = "Save with M500 and/or copy to Configuration.h";
  432. int8_t iterations = 0;
  433. float test_precision,
  434. zero_std_dev = (verbose_level ? 999.0 : 0.0), // 0.0 in dry-run mode : forced end
  435. zero_std_dev_min = zero_std_dev,
  436. zero_std_dev_old = zero_std_dev,
  437. h_factor,
  438. r_factor,
  439. a_factor,
  440. e_old[ABC] = {
  441. delta_endstop_adj[A_AXIS],
  442. delta_endstop_adj[B_AXIS],
  443. delta_endstop_adj[C_AXIS]
  444. },
  445. r_old = delta_radius,
  446. h_old = delta_height,
  447. a_old[ABC] = {
  448. delta_tower_angle_trim[A_AXIS],
  449. delta_tower_angle_trim[B_AXIS],
  450. delta_tower_angle_trim[C_AXIS]
  451. };
  452. SERIAL_PROTOCOLLNPGM("G33 Auto Calibrate");
  453. if (!_1p_calibration && !_0p_calibration) { // test if the outer radius is reachable
  454. LOOP_CAL_RAD(axis) {
  455. const float a = RADIANS(210 + (360 / NPP) * (axis - 1)),
  456. r = delta_calibration_radius;
  457. if (!position_is_reachable(cos(a) * r, sin(a) * r)) {
  458. SERIAL_PROTOCOLLNPGM("?(M665 B)ed radius is implausible.");
  459. return;
  460. }
  461. }
  462. }
  463. // Report settings
  464. const char* checkingac = PSTR("Checking... AC");
  465. serialprintPGM(checkingac);
  466. if (verbose_level == 0) SERIAL_PROTOCOLPGM(" (DRY-RUN)");
  467. if (set_up) SERIAL_PROTOCOLPGM(" (SET-UP)");
  468. SERIAL_EOL();
  469. lcd_setstatusPGM(checkingac);
  470. print_calibration_settings(_endstop_results, _angle_results);
  471. ac_setup(!_0p_calibration && !_1p_calibration);
  472. if (!_0p_calibration)
  473. if (!ac_home()) return;
  474. do { // start iterations
  475. float z_at_pt[NPP + 1] = { 0.0 };
  476. test_precision = zero_std_dev_old != 999.0 ? (zero_std_dev + zero_std_dev_old) / 2 : zero_std_dev;
  477. iterations++;
  478. // Probe the points
  479. zero_std_dev_old = zero_std_dev;
  480. if (!probe_calibration_points(z_at_pt, probe_points, towers_set, stow_after_each, set_up)) {
  481. SERIAL_PROTOCOLLNPGM("Correct delta settings with M665 and M666");
  482. return AC_CLEANUP();
  483. }
  484. zero_std_dev = std_dev_points(z_at_pt, _0p_calibration, _1p_calibration, _4p_calibration, _4p_opposite_points);
  485. // Solve matrices
  486. if ((zero_std_dev < test_precision || iterations <= force_iterations) && zero_std_dev > calibration_precision) {
  487. #if !HAS_BED_PROBE
  488. test_precision = 0.00; // forced end
  489. #endif
  490. if (zero_std_dev < zero_std_dev_min) {
  491. // set roll-back point
  492. COPY(e_old, delta_endstop_adj);
  493. r_old = delta_radius;
  494. h_old = delta_height;
  495. COPY(a_old, delta_tower_angle_trim);
  496. }
  497. float e_delta[ABC] = { 0.0 },
  498. r_delta = 0.0,
  499. t_delta[ABC] = { 0.0 };
  500. /**
  501. * convergence matrices:
  502. * see https://github.com/LVD-AC/Marlin-AC/tree/1.1.x-AC/documentation for
  503. * - definition of the matrix scaling parameters
  504. * - matrices for 4 and 7 point calibration
  505. */
  506. #define ZP(N,I) ((N) * z_at_pt[I] / 4.0) // 4.0 = divider to normalize to integers
  507. #define Z12(I) ZP(12, I)
  508. #define Z4(I) ZP(4, I)
  509. #define Z2(I) ZP(2, I)
  510. #define Z1(I) ZP(1, I)
  511. #define Z0(I) ZP(0, I)
  512. // calculate factors
  513. const float cr_old = delta_calibration_radius;
  514. if (_7p_9_center) delta_calibration_radius *= 0.9;
  515. h_factor = auto_tune_h();
  516. r_factor = auto_tune_r();
  517. a_factor = auto_tune_a();
  518. delta_calibration_radius = cr_old;
  519. switch (probe_points) {
  520. case -1:
  521. #if HAS_BED_PROBE
  522. zprobe_zoffset += probe_z_shift(z_at_pt[CEN]);
  523. #endif
  524. case 0:
  525. test_precision = 0.00; // forced end
  526. break;
  527. case 1:
  528. test_precision = 0.00; // forced end
  529. LOOP_XYZ(axis) e_delta[axis] = +Z4(CEN);
  530. break;
  531. case 2:
  532. if (towers_set) { // see 4 point calibration (towers) matrix
  533. e_delta[A_AXIS] = (+Z4(__A) -Z2(__B) -Z2(__C)) * h_factor +Z4(CEN);
  534. e_delta[B_AXIS] = (-Z2(__A) +Z4(__B) -Z2(__C)) * h_factor +Z4(CEN);
  535. e_delta[C_AXIS] = (-Z2(__A) -Z2(__B) +Z4(__C)) * h_factor +Z4(CEN);
  536. r_delta = (+Z4(__A) +Z4(__B) +Z4(__C) -Z12(CEN)) * r_factor;
  537. }
  538. else { // see 4 point calibration (opposites) matrix
  539. e_delta[A_AXIS] = (-Z4(_BC) +Z2(_CA) +Z2(_AB)) * h_factor +Z4(CEN);
  540. e_delta[B_AXIS] = (+Z2(_BC) -Z4(_CA) +Z2(_AB)) * h_factor +Z4(CEN);
  541. e_delta[C_AXIS] = (+Z2(_BC) +Z2(_CA) -Z4(_AB)) * h_factor +Z4(CEN);
  542. r_delta = (+Z4(_BC) +Z4(_CA) +Z4(_AB) -Z12(CEN)) * r_factor;
  543. }
  544. break;
  545. default: // see 7 point calibration (towers & opposites) matrix
  546. e_delta[A_AXIS] = (+Z2(__A) -Z1(__B) -Z1(__C) -Z2(_BC) +Z1(_CA) +Z1(_AB)) * h_factor +Z4(CEN);
  547. e_delta[B_AXIS] = (-Z1(__A) +Z2(__B) -Z1(__C) +Z1(_BC) -Z2(_CA) +Z1(_AB)) * h_factor +Z4(CEN);
  548. e_delta[C_AXIS] = (-Z1(__A) -Z1(__B) +Z2(__C) +Z1(_BC) +Z1(_CA) -Z2(_AB)) * h_factor +Z4(CEN);
  549. r_delta = (+Z2(__A) +Z2(__B) +Z2(__C) +Z2(_BC) +Z2(_CA) +Z2(_AB) -Z12(CEN)) * r_factor;
  550. if (towers_set) { // see 7 point tower angle calibration (towers & opposites) matrix
  551. t_delta[A_AXIS] = (+Z0(__A) -Z4(__B) +Z4(__C) +Z0(_BC) -Z4(_CA) +Z4(_AB) +Z0(CEN)) * a_factor;
  552. t_delta[B_AXIS] = (+Z4(__A) +Z0(__B) -Z4(__C) +Z4(_BC) +Z0(_CA) -Z4(_AB) +Z0(CEN)) * a_factor;
  553. t_delta[C_AXIS] = (-Z4(__A) +Z4(__B) +Z0(__C) -Z4(_BC) +Z4(_CA) +Z0(_AB) +Z0(CEN)) * a_factor;
  554. }
  555. break;
  556. }
  557. LOOP_XYZ(axis) delta_endstop_adj[axis] += e_delta[axis];
  558. delta_radius += r_delta;
  559. LOOP_XYZ(axis) delta_tower_angle_trim[axis] += t_delta[axis];
  560. }
  561. else if (zero_std_dev >= test_precision) {
  562. // roll back
  563. COPY(delta_endstop_adj, e_old);
  564. delta_radius = r_old;
  565. delta_height = h_old;
  566. COPY(delta_tower_angle_trim, a_old);
  567. }
  568. if (verbose_level != 0) { // !dry run
  569. // normalise angles to least squares
  570. if (_angle_results) {
  571. float a_sum = 0.0;
  572. LOOP_XYZ(axis) a_sum += delta_tower_angle_trim[axis];
  573. LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0;
  574. }
  575. // adjust delta_height and endstops by the max amount
  576. const float z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
  577. delta_height -= z_temp;
  578. LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
  579. }
  580. recalc_delta_settings();
  581. NOMORE(zero_std_dev_min, zero_std_dev);
  582. // print report
  583. if (verbose_level == 3)
  584. print_calibration_results(z_at_pt, _tower_results, _opposite_results);
  585. if (verbose_level != 0) { // !dry run
  586. if ((zero_std_dev >= test_precision && iterations > force_iterations) || zero_std_dev <= calibration_precision) { // end iterations
  587. SERIAL_PROTOCOLPGM("Calibration OK");
  588. SERIAL_PROTOCOL_SP(32);
  589. #if HAS_BED_PROBE
  590. if (zero_std_dev >= test_precision && !_1p_calibration && !_0p_calibration)
  591. SERIAL_PROTOCOLPGM("rolling back.");
  592. else
  593. #endif
  594. {
  595. SERIAL_PROTOCOLPGM("std dev:");
  596. SERIAL_PROTOCOL_F(zero_std_dev_min, 3);
  597. }
  598. SERIAL_EOL();
  599. char mess[21];
  600. strcpy_P(mess, PSTR("Calibration sd:"));
  601. if (zero_std_dev_min < 1)
  602. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev_min * 1000.0));
  603. else
  604. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev_min));
  605. lcd_setstatus(mess);
  606. print_calibration_settings(_endstop_results, _angle_results);
  607. serialprintPGM(save_message);
  608. SERIAL_EOL();
  609. }
  610. else { // !end iterations
  611. char mess[15];
  612. if (iterations < 31)
  613. sprintf_P(mess, PSTR("Iteration : %02i"), (int)iterations);
  614. else
  615. strcpy_P(mess, PSTR("No convergence"));
  616. SERIAL_PROTOCOL(mess);
  617. SERIAL_PROTOCOL_SP(32);
  618. SERIAL_PROTOCOLPGM("std dev:");
  619. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  620. SERIAL_EOL();
  621. lcd_setstatus(mess);
  622. if (verbose_level > 1)
  623. print_calibration_settings(_endstop_results, _angle_results);
  624. }
  625. }
  626. else { // dry run
  627. const char *enddryrun = PSTR("End DRY-RUN");
  628. serialprintPGM(enddryrun);
  629. SERIAL_PROTOCOL_SP(35);
  630. SERIAL_PROTOCOLPGM("std dev:");
  631. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  632. SERIAL_EOL();
  633. char mess[21];
  634. strcpy_P(mess, enddryrun);
  635. strcpy_P(&mess[11], PSTR(" sd:"));
  636. if (zero_std_dev < 1)
  637. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev * 1000.0));
  638. else
  639. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev));
  640. lcd_setstatus(mess);
  641. }
  642. if (!ac_home()) return;
  643. }
  644. while (((zero_std_dev < test_precision && iterations < 31) || iterations <= force_iterations) && zero_std_dev > calibration_precision);
  645. AC_CLEANUP();
  646. }
  647. #endif // DELTA_AUTO_CALIBRATION