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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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/probe.h"
  27. #include "../../module/motion.h"
  28. #include "../../module/stepper.h"
  29. #include "../../module/endstops.h"
  30. #include "../../lcd/ultralcd.h"
  31. #if HOTENDS > 1
  32. #include "../../module/tool_change.h"
  33. #endif
  34. #if HAS_LEVELING
  35. #include "../../feature/bedlevel/bedlevel.h"
  36. #endif
  37. constexpr uint8_t _7P_STEP = 1, // 7-point step - to change number of calibration points
  38. _4P_STEP = _7P_STEP * 2, // 4-point step
  39. NPP = _7P_STEP * 6; // number of calibration points on the radius
  40. enum CalEnum { // the 7 main calibration points - add definitions if needed
  41. CEN = 0,
  42. __A = 1,
  43. _AB = __A + _7P_STEP,
  44. __B = _AB + _7P_STEP,
  45. _BC = __B + _7P_STEP,
  46. __C = _BC + _7P_STEP,
  47. _CA = __C + _7P_STEP,
  48. };
  49. #define LOOP_CAL_PT(VAR, S, N) for (uint8_t VAR=S; VAR<=NPP; VAR+=N)
  50. #define F_LOOP_CAL_PT(VAR, S, N) for (float VAR=S; VAR<NPP+0.9999; VAR+=N)
  51. #define I_LOOP_CAL_PT(VAR, S, N) for (float VAR=S; VAR>CEN+0.9999; VAR-=N)
  52. #define LOOP_CAL_ALL(VAR) LOOP_CAL_PT(VAR, CEN, 1)
  53. #define LOOP_CAL_RAD(VAR) LOOP_CAL_PT(VAR, __A, _7P_STEP)
  54. #define LOOP_CAL_ACT(VAR, _4P, _OP) LOOP_CAL_PT(VAR, _OP ? _AB : __A, _4P ? _4P_STEP : _7P_STEP)
  55. static void print_signed_float(const char * const prefix, const float &f) {
  56. SERIAL_PROTOCOLPGM(" ");
  57. serialprintPGM(prefix);
  58. SERIAL_PROTOCOLCHAR(':');
  59. if (f >= 0) SERIAL_CHAR('+');
  60. SERIAL_PROTOCOL_F(f, 2);
  61. }
  62. static void print_G33_settings(const bool end_stops, const bool tower_angles) {
  63. SERIAL_PROTOCOLPAIR(".Height:", delta_height);
  64. if (end_stops) {
  65. print_signed_float(PSTR("Ex"), delta_endstop_adj[A_AXIS]);
  66. print_signed_float(PSTR("Ey"), delta_endstop_adj[B_AXIS]);
  67. print_signed_float(PSTR("Ez"), delta_endstop_adj[C_AXIS]);
  68. }
  69. if (end_stops && tower_angles) {
  70. SERIAL_PROTOCOLPAIR(" Radius:", delta_radius);
  71. SERIAL_EOL();
  72. SERIAL_CHAR('.');
  73. SERIAL_PROTOCOL_SP(13);
  74. }
  75. if (tower_angles) {
  76. print_signed_float(PSTR("Tx"), delta_tower_angle_trim[A_AXIS]);
  77. print_signed_float(PSTR("Ty"), delta_tower_angle_trim[B_AXIS]);
  78. print_signed_float(PSTR("Tz"), delta_tower_angle_trim[C_AXIS]);
  79. }
  80. if ((!end_stops && tower_angles) || (end_stops && !tower_angles)) { // XOR
  81. SERIAL_PROTOCOLPAIR(" Radius:", delta_radius);
  82. }
  83. SERIAL_EOL();
  84. }
  85. static void print_G33_results(const float z_at_pt[NPP + 1], const bool tower_points, const bool opposite_points) {
  86. SERIAL_PROTOCOLPGM(". ");
  87. print_signed_float(PSTR("c"), z_at_pt[CEN]);
  88. if (tower_points) {
  89. print_signed_float(PSTR(" x"), z_at_pt[__A]);
  90. print_signed_float(PSTR(" y"), z_at_pt[__B]);
  91. print_signed_float(PSTR(" z"), z_at_pt[__C]);
  92. }
  93. if (tower_points && opposite_points) {
  94. SERIAL_EOL();
  95. SERIAL_CHAR('.');
  96. SERIAL_PROTOCOL_SP(13);
  97. }
  98. if (opposite_points) {
  99. print_signed_float(PSTR("yz"), z_at_pt[_BC]);
  100. print_signed_float(PSTR("zx"), z_at_pt[_CA]);
  101. print_signed_float(PSTR("xy"), z_at_pt[_AB]);
  102. }
  103. SERIAL_EOL();
  104. }
  105. /**
  106. * After G33:
  107. * - Move to the print ceiling (DELTA_HOME_TO_SAFE_ZONE only)
  108. * - Stow the probe
  109. * - Restore endstops state
  110. * - Select the old tool, if needed
  111. */
  112. static void G33_cleanup(
  113. #if HOTENDS > 1
  114. const uint8_t old_tool_index
  115. #endif
  116. ) {
  117. #if ENABLED(DELTA_HOME_TO_SAFE_ZONE)
  118. do_blocking_move_to_z(delta_clip_start_height);
  119. #endif
  120. STOW_PROBE();
  121. clean_up_after_endstop_or_probe_move();
  122. #if HOTENDS > 1
  123. tool_change(old_tool_index, 0, true);
  124. #endif
  125. }
  126. static float probe_G33_points(float z_at_pt[NPP + 1], const int8_t probe_points, const bool towers_set, const bool stow_after_each) {
  127. const bool _0p_calibration = probe_points == 0,
  128. _1p_calibration = probe_points == 1,
  129. _4p_calibration = probe_points == 2,
  130. _4p_opposite_points = _4p_calibration && !towers_set,
  131. _7p_calibration = probe_points >= 3 || probe_points == 0,
  132. _7p_no_intermediates = probe_points == 3,
  133. _7p_1_intermediates = probe_points == 4,
  134. _7p_2_intermediates = probe_points == 5,
  135. _7p_4_intermediates = probe_points == 6,
  136. _7p_6_intermediates = probe_points == 7,
  137. _7p_8_intermediates = probe_points == 8,
  138. _7p_11_intermediates = probe_points == 9,
  139. _7p_14_intermediates = probe_points == 10,
  140. _7p_intermed_points = probe_points >= 4,
  141. _7p_6_centre = probe_points >= 5 && probe_points <= 7,
  142. _7p_9_centre = probe_points >= 8;
  143. #if HAS_BED_PROBE
  144. const float dx = (X_PROBE_OFFSET_FROM_EXTRUDER),
  145. dy = (Y_PROBE_OFFSET_FROM_EXTRUDER);
  146. #endif
  147. LOOP_CAL_ALL(axis) z_at_pt[axis] = 0.0;
  148. if (!_0p_calibration) {
  149. if (!_7p_no_intermediates && !_7p_4_intermediates && !_7p_11_intermediates) { // probe the center
  150. z_at_pt[CEN] +=
  151. #if HAS_BED_PROBE
  152. probe_pt(dx, dy, stow_after_each, 1, false)
  153. #else
  154. lcd_probe_pt(0, 0)
  155. #endif
  156. ;
  157. }
  158. if (_7p_calibration) { // probe extra center points
  159. const float start = _7p_9_centre ? _CA + _7P_STEP / 3.0 : _7p_6_centre ? _CA : __C,
  160. steps = _7p_9_centre ? _4P_STEP / 3.0 : _7p_6_centre ? _7P_STEP : _4P_STEP;
  161. I_LOOP_CAL_PT(axis, start, steps) {
  162. const float a = RADIANS(210 + (360 / NPP) * (axis - 1)),
  163. r = delta_calibration_radius * 0.1;
  164. z_at_pt[CEN] +=
  165. #if HAS_BED_PROBE
  166. probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1, false)
  167. #else
  168. lcd_probe_pt(cos(a) * r, sin(a) * r)
  169. #endif
  170. ;
  171. }
  172. z_at_pt[CEN] /= float(_7p_2_intermediates ? 7 : probe_points);
  173. }
  174. if (!_1p_calibration) { // probe the radius
  175. const CalEnum start = _4p_opposite_points ? _AB : __A;
  176. const float steps = _7p_14_intermediates ? _7P_STEP / 15.0 : // 15r * 6 + 10c = 100
  177. _7p_11_intermediates ? _7P_STEP / 12.0 : // 12r * 6 + 9c = 81
  178. _7p_8_intermediates ? _7P_STEP / 9.0 : // 9r * 6 + 10c = 64
  179. _7p_6_intermediates ? _7P_STEP / 7.0 : // 7r * 6 + 7c = 49
  180. _7p_4_intermediates ? _7P_STEP / 5.0 : // 5r * 6 + 6c = 36
  181. _7p_2_intermediates ? _7P_STEP / 3.0 : // 3r * 6 + 7c = 25
  182. _7p_1_intermediates ? _7P_STEP / 2.0 : // 2r * 6 + 4c = 16
  183. _7p_no_intermediates ? _7P_STEP : // 1r * 6 + 3c = 9
  184. _4P_STEP; // .5r * 6 + 1c = 4
  185. bool zig_zag = true;
  186. F_LOOP_CAL_PT(axis, start, _7p_9_centre ? steps * 3 : steps) {
  187. const int8_t offset = _7p_9_centre ? 1 : 0;
  188. for (int8_t circle = -offset; circle <= offset; circle++) {
  189. const float a = RADIANS(210 + (360 / NPP) * (axis - 1)),
  190. r = delta_calibration_radius * (1 + 0.1 * (zig_zag ? circle : - circle)),
  191. interpol = FMOD(axis, 1);
  192. const float z_temp =
  193. #if HAS_BED_PROBE
  194. probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1, false)
  195. #else
  196. lcd_probe_pt(cos(a) * r, sin(a) * r)
  197. #endif
  198. ;
  199. // split probe point to neighbouring calibration points
  200. z_at_pt[uint8_t(round(axis - interpol + NPP - 1)) % NPP + 1] += z_temp * sq(cos(RADIANS(interpol * 90)));
  201. z_at_pt[uint8_t(round(axis - interpol)) % NPP + 1] += z_temp * sq(sin(RADIANS(interpol * 90)));
  202. }
  203. zig_zag = !zig_zag;
  204. }
  205. if (_7p_intermed_points)
  206. LOOP_CAL_RAD(axis)
  207. z_at_pt[axis] /= _7P_STEP / steps;
  208. }
  209. float S1 = z_at_pt[CEN],
  210. S2 = sq(z_at_pt[CEN]);
  211. int16_t N = 1;
  212. if (!_1p_calibration) { // std dev from zero plane
  213. LOOP_CAL_ACT(axis, _4p_calibration, _4p_opposite_points) {
  214. S1 += z_at_pt[axis];
  215. S2 += sq(z_at_pt[axis]);
  216. N++;
  217. }
  218. return round(SQRT(S2 / N) * 1000.0) / 1000.0 + 0.00001;
  219. }
  220. }
  221. return 0.00001;
  222. }
  223. #if HAS_BED_PROBE
  224. static void G33_auto_tune() {
  225. float z_at_pt[NPP + 1] = { 0.0 },
  226. z_at_pt_base[NPP + 1] = { 0.0 },
  227. z_temp, h_fac = 0.0, r_fac = 0.0, a_fac = 0.0, norm = 0.8;
  228. #define ZP(N,I) ((N) * z_at_pt[I])
  229. #define Z06(I) ZP(6, I)
  230. #define Z03(I) ZP(3, I)
  231. #define Z02(I) ZP(2, I)
  232. #define Z01(I) ZP(1, I)
  233. #define Z32(I) ZP(3/2, I)
  234. SERIAL_PROTOCOLPGM("AUTO TUNE baseline");
  235. SERIAL_EOL();
  236. probe_G33_points(z_at_pt_base, 3, true, false);
  237. print_G33_results(z_at_pt_base, true, true);
  238. LOOP_XYZ(axis) {
  239. delta_endstop_adj[axis] -= 1.0;
  240. recalc_delta_settings();
  241. endstops.enable(true);
  242. if (!home_delta()) return;
  243. endstops.not_homing();
  244. SERIAL_PROTOCOLPGM("Tuning E");
  245. SERIAL_CHAR(tolower(axis_codes[axis]));
  246. SERIAL_EOL();
  247. probe_G33_points(z_at_pt, 3, true, false);
  248. LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis];
  249. print_G33_results(z_at_pt, true, true);
  250. delta_endstop_adj[axis] += 1.0;
  251. recalc_delta_settings();
  252. switch (axis) {
  253. case A_AXIS :
  254. h_fac += 4.0 / (Z03(CEN) +Z01(__A) +Z32(_CA) +Z32(_AB)); // Offset by X-tower end-stop
  255. break;
  256. case B_AXIS :
  257. h_fac += 4.0 / (Z03(CEN) +Z01(__B) +Z32(_BC) +Z32(_AB)); // Offset by Y-tower end-stop
  258. break;
  259. case C_AXIS :
  260. h_fac += 4.0 / (Z03(CEN) +Z01(__C) +Z32(_BC) +Z32(_CA) ); // Offset by Z-tower end-stop
  261. break;
  262. }
  263. }
  264. h_fac /= 3.0;
  265. h_fac *= norm; // Normalize to 1.02 for Kossel mini
  266. for (int8_t zig_zag = -1; zig_zag < 2; zig_zag += 2) {
  267. delta_radius += 1.0 * zig_zag;
  268. recalc_delta_settings();
  269. endstops.enable(true);
  270. if (!home_delta()) return;
  271. endstops.not_homing();
  272. SERIAL_PROTOCOLPGM("Tuning R");
  273. SERIAL_PROTOCOL(zig_zag == -1 ? "-" : "+");
  274. SERIAL_EOL();
  275. probe_G33_points(z_at_pt, 3, true, false);
  276. LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis];
  277. print_G33_results(z_at_pt, true, true);
  278. delta_radius -= 1.0 * zig_zag;
  279. recalc_delta_settings();
  280. r_fac -= zig_zag * 6.0 / (Z03(__A) +Z03(__B) +Z03(__C) +Z03(_BC) +Z03(_CA) +Z03(_AB)); // Offset by delta radius
  281. }
  282. r_fac /= 2.0;
  283. r_fac *= 3 * norm; // Normalize to 2.25 for Kossel mini
  284. LOOP_XYZ(axis) {
  285. delta_tower_angle_trim[axis] += 1.0;
  286. delta_endstop_adj[(axis + 1) % 3] -= 1.0 / 4.5;
  287. delta_endstop_adj[(axis + 2) % 3] += 1.0 / 4.5;
  288. z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
  289. delta_height -= z_temp;
  290. LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
  291. recalc_delta_settings();
  292. endstops.enable(true);
  293. if (!home_delta()) return;
  294. endstops.not_homing();
  295. SERIAL_PROTOCOLPGM("Tuning T");
  296. SERIAL_CHAR(tolower(axis_codes[axis]));
  297. SERIAL_EOL();
  298. probe_G33_points(z_at_pt, 3, true, false);
  299. LOOP_CAL_ALL(axis) z_at_pt[axis] -= z_at_pt_base[axis];
  300. print_G33_results(z_at_pt, true, true);
  301. delta_tower_angle_trim[axis] -= 1.0;
  302. delta_endstop_adj[(axis+1) % 3] += 1.0/4.5;
  303. delta_endstop_adj[(axis+2) % 3] -= 1.0/4.5;
  304. z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
  305. delta_height -= z_temp;
  306. LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
  307. recalc_delta_settings();
  308. switch (axis) {
  309. case A_AXIS :
  310. a_fac += 4.0 / ( Z06(__B) -Z06(__C) +Z06(_CA) -Z06(_AB)); // Offset by alpha tower angle
  311. break;
  312. case B_AXIS :
  313. a_fac += 4.0 / (-Z06(__A) +Z06(__C) -Z06(_BC) +Z06(_AB)); // Offset by beta tower angle
  314. break;
  315. case C_AXIS :
  316. a_fac += 4.0 / (Z06(__A) -Z06(__B) +Z06(_BC) -Z06(_CA) ); // Offset by gamma tower angle
  317. break;
  318. }
  319. }
  320. a_fac /= 3.0;
  321. a_fac *= norm; // Normalize to 0.83 for Kossel mini
  322. endstops.enable(true);
  323. if (!home_delta()) return;
  324. endstops.not_homing();
  325. print_signed_float(PSTR( "H_FACTOR: "), h_fac);
  326. print_signed_float(PSTR(" R_FACTOR: "), r_fac);
  327. print_signed_float(PSTR(" A_FACTOR: "), a_fac);
  328. SERIAL_EOL();
  329. SERIAL_PROTOCOLPGM("Copy these values to Configuration.h");
  330. SERIAL_EOL();
  331. }
  332. #endif // HAS_BED_PROBE
  333. /**
  334. * G33 - Delta '1-4-7-point' Auto-Calibration
  335. * Calibrate height, endstops, delta radius, and tower angles.
  336. *
  337. * Parameters:
  338. *
  339. * Pn Number of probe points:
  340. * P0 No probe. Normalize only.
  341. * P1 Probe center and set height only.
  342. * P2 Probe center and towers. Set height, endstops and delta radius.
  343. * P3 Probe all positions: center, towers and opposite towers. Set all.
  344. * P4-P10 Probe all positions + at different itermediate locations and average them.
  345. *
  346. * T Don't calibrate tower angle corrections
  347. *
  348. * Cn.nn Calibration precision; when omitted calibrates to maximum precision
  349. *
  350. * Fn Force to run at least n iterations and takes the best result
  351. *
  352. * A Auto tune calibartion factors (set in Configuration.h)
  353. *
  354. * Vn Verbose level:
  355. * V0 Dry-run mode. Report settings and probe results. No calibration.
  356. * V1 Report settings
  357. * V2 Report settings and probe results
  358. *
  359. * E Engage the probe for each point
  360. */
  361. void GcodeSuite::G33() {
  362. const int8_t probe_points = parser.intval('P', DELTA_CALIBRATION_DEFAULT_POINTS);
  363. if (!WITHIN(probe_points, 0, 10)) {
  364. SERIAL_PROTOCOLLNPGM("?(P)oints is implausible (0-10).");
  365. return;
  366. }
  367. const int8_t verbose_level = parser.byteval('V', 1);
  368. if (!WITHIN(verbose_level, 0, 2)) {
  369. SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0-2).");
  370. return;
  371. }
  372. const float calibration_precision = parser.floatval('C');
  373. if (calibration_precision < 0) {
  374. SERIAL_PROTOCOLLNPGM("?(C)alibration precision is implausible (>=0).");
  375. return;
  376. }
  377. const int8_t force_iterations = parser.intval('F', 0);
  378. if (!WITHIN(force_iterations, 0, 30)) {
  379. SERIAL_PROTOCOLLNPGM("?(F)orce iteration is implausible (0-30).");
  380. return;
  381. }
  382. const bool towers_set = !parser.boolval('T'),
  383. auto_tune = parser.boolval('A'),
  384. stow_after_each = parser.boolval('E'),
  385. _0p_calibration = probe_points == 0,
  386. _1p_calibration = probe_points == 1,
  387. _4p_calibration = probe_points == 2,
  388. _7p_9_centre = probe_points >= 8,
  389. _tower_results = (_4p_calibration && towers_set)
  390. || probe_points >= 3 || probe_points == 0,
  391. _opposite_results = (_4p_calibration && !towers_set)
  392. || probe_points >= 3 || probe_points == 0,
  393. _endstop_results = probe_points != 1,
  394. _angle_results = (probe_points >= 3 || probe_points == 0) && towers_set;
  395. const static char save_message[] PROGMEM = "Save with M500 and/or copy to Configuration.h";
  396. int8_t iterations = 0;
  397. float test_precision,
  398. zero_std_dev = (verbose_level ? 999.0 : 0.0), // 0.0 in dry-run mode : forced end
  399. zero_std_dev_min = zero_std_dev,
  400. e_old[ABC] = {
  401. delta_endstop_adj[A_AXIS],
  402. delta_endstop_adj[B_AXIS],
  403. delta_endstop_adj[C_AXIS]
  404. },
  405. dr_old = delta_radius,
  406. zh_old = delta_height,
  407. ta_old[ABC] = {
  408. delta_tower_angle_trim[A_AXIS],
  409. delta_tower_angle_trim[B_AXIS],
  410. delta_tower_angle_trim[C_AXIS]
  411. };
  412. SERIAL_PROTOCOLLNPGM("G33 Auto Calibrate");
  413. if (!_1p_calibration && !_0p_calibration) { // test if the outer radius is reachable
  414. LOOP_CAL_RAD(axis) {
  415. const float a = RADIANS(210 + (360 / NPP) * (axis - 1)),
  416. r = delta_calibration_radius * (1 + (_7p_9_centre ? 0.1 : 0.0));
  417. if (!position_is_reachable(cos(a) * r, sin(a) * r)) {
  418. SERIAL_PROTOCOLLNPGM("?(M665 B)ed radius is implausible.");
  419. return;
  420. }
  421. }
  422. }
  423. stepper.synchronize();
  424. #if HAS_LEVELING
  425. reset_bed_level(); // After calibration bed-level data is no longer valid
  426. #endif
  427. #if HOTENDS > 1
  428. const uint8_t old_tool_index = active_extruder;
  429. tool_change(0, 0, true);
  430. #define G33_CLEANUP() G33_cleanup(old_tool_index)
  431. #else
  432. #define G33_CLEANUP() G33_cleanup()
  433. #endif
  434. setup_for_endstop_or_probe_move();
  435. endstops.enable(true);
  436. if (!_0p_calibration) {
  437. if (!home_delta())
  438. return;
  439. endstops.not_homing();
  440. }
  441. if (auto_tune) {
  442. #if HAS_BED_PROBE
  443. G33_auto_tune();
  444. #else
  445. SERIAL_PROTOCOLLNPGM("A probe is needed for auto-tune");
  446. #endif
  447. G33_CLEANUP();
  448. return;
  449. }
  450. // Report settings
  451. const char *checkingac = PSTR("Checking... AC"); // TODO: Make translatable string
  452. serialprintPGM(checkingac);
  453. if (verbose_level == 0) SERIAL_PROTOCOLPGM(" (DRY-RUN)");
  454. SERIAL_EOL();
  455. lcd_setstatusPGM(checkingac);
  456. print_G33_settings(_endstop_results, _angle_results);
  457. do {
  458. float z_at_pt[NPP + 1] = { 0.0 };
  459. test_precision = zero_std_dev;
  460. iterations++;
  461. // Probe the points
  462. zero_std_dev = probe_G33_points(z_at_pt, probe_points, towers_set, stow_after_each);
  463. // Solve matrices
  464. if ((zero_std_dev < test_precision || iterations <= force_iterations) && zero_std_dev > calibration_precision) {
  465. if (zero_std_dev < zero_std_dev_min) {
  466. COPY(e_old, delta_endstop_adj);
  467. dr_old = delta_radius;
  468. zh_old = delta_height;
  469. COPY(ta_old, delta_tower_angle_trim);
  470. }
  471. float e_delta[ABC] = { 0.0 }, r_delta = 0.0, t_delta[ABC] = { 0.0 };
  472. const float r_diff = delta_radius - delta_calibration_radius,
  473. h_factor = 1 / 6.0 *
  474. #ifdef H_FACTOR
  475. (H_FACTOR), // Set in Configuration.h
  476. #else
  477. (1.00 + r_diff * 0.001), // 1.02 for r_diff = 20mm
  478. #endif
  479. r_factor = 1 / 6.0 *
  480. #ifdef R_FACTOR
  481. -(R_FACTOR), // Set in Configuration.h
  482. #else
  483. -(1.75 + 0.005 * r_diff + 0.001 * sq(r_diff)), // 2.25 for r_diff = 20mm
  484. #endif
  485. a_factor = 1 / 6.0 *
  486. #ifdef A_FACTOR
  487. (A_FACTOR); // Set in Configuration.h
  488. #else
  489. (66.66 / delta_calibration_radius); // 0.83 for cal_rd = 80mm
  490. #endif
  491. #define ZP(N,I) ((N) * z_at_pt[I])
  492. #define Z6(I) ZP(6, I)
  493. #define Z4(I) ZP(4, I)
  494. #define Z2(I) ZP(2, I)
  495. #define Z1(I) ZP(1, I)
  496. #if !HAS_BED_PROBE
  497. test_precision = 0.00; // forced end
  498. #endif
  499. switch (probe_points) {
  500. case 0:
  501. test_precision = 0.00; // forced end
  502. break;
  503. case 1:
  504. test_precision = 0.00; // forced end
  505. LOOP_XYZ(axis) e_delta[axis] = Z1(CEN);
  506. break;
  507. case 2:
  508. if (towers_set) {
  509. e_delta[A_AXIS] = (Z6(CEN) +Z4(__A) -Z2(__B) -Z2(__C)) * h_factor;
  510. e_delta[B_AXIS] = (Z6(CEN) -Z2(__A) +Z4(__B) -Z2(__C)) * h_factor;
  511. e_delta[C_AXIS] = (Z6(CEN) -Z2(__A) -Z2(__B) +Z4(__C)) * h_factor;
  512. r_delta = (Z6(CEN) -Z2(__A) -Z2(__B) -Z2(__C)) * r_factor;
  513. }
  514. else {
  515. e_delta[A_AXIS] = (Z6(CEN) -Z4(_BC) +Z2(_CA) +Z2(_AB)) * h_factor;
  516. e_delta[B_AXIS] = (Z6(CEN) +Z2(_BC) -Z4(_CA) +Z2(_AB)) * h_factor;
  517. e_delta[C_AXIS] = (Z6(CEN) +Z2(_BC) +Z2(_CA) -Z4(_AB)) * h_factor;
  518. r_delta = (Z6(CEN) -Z2(_BC) -Z2(_CA) -Z2(_AB)) * r_factor;
  519. }
  520. break;
  521. default:
  522. e_delta[A_AXIS] = (Z6(CEN) +Z2(__A) -Z1(__B) -Z1(__C) -Z2(_BC) +Z1(_CA) +Z1(_AB)) * h_factor;
  523. e_delta[B_AXIS] = (Z6(CEN) -Z1(__A) +Z2(__B) -Z1(__C) +Z1(_BC) -Z2(_CA) +Z1(_AB)) * h_factor;
  524. e_delta[C_AXIS] = (Z6(CEN) -Z1(__A) -Z1(__B) +Z2(__C) +Z1(_BC) +Z1(_CA) -Z2(_AB)) * h_factor;
  525. r_delta = (Z6(CEN) -Z1(__A) -Z1(__B) -Z1(__C) -Z1(_BC) -Z1(_CA) -Z1(_AB)) * r_factor;
  526. if (towers_set) {
  527. t_delta[A_AXIS] = ( -Z4(__B) +Z4(__C) -Z4(_CA) +Z4(_AB)) * a_factor;
  528. t_delta[B_AXIS] = ( Z4(__A) -Z4(__C) +Z4(_BC) -Z4(_AB)) * a_factor;
  529. t_delta[C_AXIS] = (-Z4(__A) +Z4(__B) -Z4(_BC) +Z4(_CA) ) * a_factor;
  530. e_delta[A_AXIS] += (t_delta[B_AXIS] - t_delta[C_AXIS]) / 4.5;
  531. e_delta[B_AXIS] += (t_delta[C_AXIS] - t_delta[A_AXIS]) / 4.5;
  532. e_delta[C_AXIS] += (t_delta[A_AXIS] - t_delta[B_AXIS]) / 4.5;
  533. }
  534. break;
  535. }
  536. LOOP_XYZ(axis) delta_endstop_adj[axis] += e_delta[axis];
  537. delta_radius += r_delta;
  538. LOOP_XYZ(axis) delta_tower_angle_trim[axis] += t_delta[axis];
  539. }
  540. else if (zero_std_dev >= test_precision) { // step one back
  541. COPY(delta_endstop_adj, e_old);
  542. delta_radius = dr_old;
  543. delta_height = zh_old;
  544. COPY(delta_tower_angle_trim, ta_old);
  545. }
  546. if (verbose_level != 0) { // !dry run
  547. // normalise angles to least squares
  548. if (_angle_results) {
  549. float a_sum = 0.0;
  550. LOOP_XYZ(axis) a_sum += delta_tower_angle_trim[axis];
  551. LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0;
  552. }
  553. // adjust delta_height and endstops by the max amount
  554. const float z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
  555. delta_height -= z_temp;
  556. LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
  557. }
  558. recalc_delta_settings();
  559. NOMORE(zero_std_dev_min, zero_std_dev);
  560. // print report
  561. if (verbose_level != 1)
  562. print_G33_results(z_at_pt, _tower_results, _opposite_results);
  563. if (verbose_level != 0) { // !dry run
  564. if ((zero_std_dev >= test_precision && iterations > force_iterations) || zero_std_dev <= calibration_precision) { // end iterations
  565. SERIAL_PROTOCOLPGM("Calibration OK");
  566. SERIAL_PROTOCOL_SP(32);
  567. #if HAS_BED_PROBE
  568. if (zero_std_dev >= test_precision && !_1p_calibration)
  569. SERIAL_PROTOCOLPGM("rolling back.");
  570. else
  571. #endif
  572. {
  573. SERIAL_PROTOCOLPGM("std dev:");
  574. SERIAL_PROTOCOL_F(zero_std_dev_min, 3);
  575. }
  576. SERIAL_EOL();
  577. char mess[21];
  578. strcpy_P(mess, PSTR("Calibration sd:"));
  579. if (zero_std_dev_min < 1)
  580. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev_min * 1000.0));
  581. else
  582. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev_min));
  583. lcd_setstatus(mess);
  584. print_G33_settings(_endstop_results, _angle_results);
  585. serialprintPGM(save_message);
  586. SERIAL_EOL();
  587. }
  588. else { // !end iterations
  589. char mess[15];
  590. if (iterations < 31)
  591. sprintf_P(mess, PSTR("Iteration : %02i"), (int)iterations);
  592. else
  593. strcpy_P(mess, PSTR("No convergence"));
  594. SERIAL_PROTOCOL(mess);
  595. SERIAL_PROTOCOL_SP(32);
  596. SERIAL_PROTOCOLPGM("std dev:");
  597. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  598. SERIAL_EOL();
  599. lcd_setstatus(mess);
  600. print_G33_settings(_endstop_results, _angle_results);
  601. }
  602. }
  603. else { // dry run
  604. const char *enddryrun = PSTR("End DRY-RUN");
  605. serialprintPGM(enddryrun);
  606. SERIAL_PROTOCOL_SP(35);
  607. SERIAL_PROTOCOLPGM("std dev:");
  608. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  609. SERIAL_EOL();
  610. char mess[21];
  611. strcpy_P(mess, enddryrun);
  612. strcpy_P(&mess[11], PSTR(" sd:"));
  613. if (zero_std_dev < 1)
  614. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev * 1000.0));
  615. else
  616. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev));
  617. lcd_setstatus(mess);
  618. }
  619. endstops.enable(true);
  620. if (!home_delta())
  621. return;
  622. endstops.not_homing();
  623. }
  624. while (((zero_std_dev < test_precision && iterations < 31) || iterations <= force_iterations) && zero_std_dev > calibration_precision);
  625. G33_CLEANUP();
  626. }
  627. #endif // DELTA_AUTO_CALIBRATION