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

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