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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "../../module/tool_change.h"
  31. #include "../../lcd/ultralcd.h"
  32. #if HAS_LEVELING
  33. #include "../../feature/bedlevel/bedlevel.h"
  34. #endif
  35. /**
  36. * G33 - Delta '1-4-7-point' Auto-Calibration
  37. * Calibrate height, endstops, delta radius, and tower angles.
  38. *
  39. * Parameters:
  40. *
  41. * Pn Number of probe points:
  42. *
  43. * P0 No probe. Normalize only.
  44. * P1 Probe center and set height only.
  45. * P2 Probe center and towers. Set height, endstops, and delta radius.
  46. * P3 Probe all positions: center, towers and opposite towers. Set all.
  47. * P4-P7 Probe all positions at different locations and average them.
  48. *
  49. * T0 Don't calibrate tower angle corrections
  50. *
  51. * Cn.nn Calibration precision; when omitted calibrates to maximum precision
  52. *
  53. * Fn Force to run at least n iterations and takes the best result
  54. *
  55. * Vn Verbose level:
  56. *
  57. * V0 Dry-run mode. Report settings and probe results. No calibration.
  58. * V1 Report settings
  59. * V2 Report settings and probe results
  60. *
  61. * E Engage the probe for each point
  62. */
  63. static void print_signed_float(const char * const prefix, const float &f) {
  64. SERIAL_PROTOCOLPGM(" ");
  65. serialprintPGM(prefix);
  66. SERIAL_PROTOCOLCHAR(':');
  67. if (f >= 0) SERIAL_CHAR('+');
  68. SERIAL_PROTOCOL_F(f, 2);
  69. }
  70. static void print_G33_settings(const bool end_stops, const bool tower_angles) {
  71. SERIAL_PROTOCOLPAIR(".Height:", DELTA_HEIGHT + home_offset[Z_AXIS]);
  72. if (end_stops) {
  73. print_signed_float(PSTR(" Ex"), delta_endstop_adj[A_AXIS]);
  74. print_signed_float(PSTR("Ey"), delta_endstop_adj[B_AXIS]);
  75. print_signed_float(PSTR("Ez"), delta_endstop_adj[C_AXIS]);
  76. SERIAL_PROTOCOLPAIR(" Radius:", delta_radius);
  77. }
  78. SERIAL_EOL();
  79. if (tower_angles) {
  80. SERIAL_PROTOCOLPGM(".Tower angle : ");
  81. print_signed_float(PSTR("Tx"), delta_tower_angle_trim[A_AXIS]);
  82. print_signed_float(PSTR("Ty"), delta_tower_angle_trim[B_AXIS]);
  83. print_signed_float(PSTR("Tz"), delta_tower_angle_trim[C_AXIS]);
  84. SERIAL_EOL();
  85. }
  86. }
  87. static void G33_cleanup(
  88. #if HOTENDS > 1
  89. const uint8_t old_tool_index
  90. #endif
  91. ) {
  92. #if ENABLED(DELTA_HOME_TO_SAFE_ZONE)
  93. do_blocking_move_to_z(delta_clip_start_height);
  94. #endif
  95. STOW_PROBE();
  96. clean_up_after_endstop_or_probe_move();
  97. #if HOTENDS > 1
  98. tool_change(old_tool_index, 0, true);
  99. #endif
  100. }
  101. void GcodeSuite::G33() {
  102. const int8_t probe_points = parser.intval('P', DELTA_CALIBRATION_DEFAULT_POINTS);
  103. if (!WITHIN(probe_points, 0, 7)) {
  104. SERIAL_PROTOCOLLNPGM("?(P)oints is implausible (0-7).");
  105. return;
  106. }
  107. const int8_t verbose_level = parser.byteval('V', 1);
  108. if (!WITHIN(verbose_level, 0, 2)) {
  109. SERIAL_PROTOCOLLNPGM("?(V)erbose level is implausible (0-2).");
  110. return;
  111. }
  112. const float calibration_precision = parser.floatval('C');
  113. if (calibration_precision < 0) {
  114. SERIAL_PROTOCOLLNPGM("?(C)alibration precision is implausible (>=0).");
  115. return;
  116. }
  117. const int8_t force_iterations = parser.intval('F', 0);
  118. if (!WITHIN(force_iterations, 0, 30)) {
  119. SERIAL_PROTOCOLLNPGM("?(F)orce iteration is implausible (0-30).");
  120. return;
  121. }
  122. const bool towers_set = parser.boolval('T', true),
  123. _0p_calibration = probe_points == 0,
  124. _1p_calibration = probe_points == 1,
  125. _4p_calibration = probe_points == 2,
  126. _4p_towers_points = _4p_calibration && towers_set,
  127. _4p_opposite_points = _4p_calibration && !towers_set,
  128. _7p_calibration = probe_points >= 3 || _0p_calibration,
  129. _7p_half_circle = probe_points == 3,
  130. _7p_double_circle = probe_points == 5,
  131. _7p_triple_circle = probe_points == 6,
  132. _7p_quadruple_circle = probe_points == 7,
  133. _7p_multi_circle = _7p_double_circle || _7p_triple_circle || _7p_quadruple_circle,
  134. _7p_intermed_points = _7p_calibration && !_7p_half_circle;
  135. #if DISABLED(PROBE_MANUALLY)
  136. const bool stow_after_each = parser.boolval('E');
  137. const float dx = (X_PROBE_OFFSET_FROM_EXTRUDER),
  138. dy = (Y_PROBE_OFFSET_FROM_EXTRUDER);
  139. #endif
  140. const static char save_message[] PROGMEM = "Save with M500 and/or copy to Configuration.h";
  141. int8_t iterations = 0;
  142. float test_precision,
  143. zero_std_dev = (verbose_level ? 999.0 : 0.0), // 0.0 in dry-run mode : forced end
  144. zero_std_dev_old = zero_std_dev,
  145. zero_std_dev_min = zero_std_dev,
  146. e_old[ABC] = {
  147. delta_endstop_adj[A_AXIS],
  148. delta_endstop_adj[B_AXIS],
  149. delta_endstop_adj[C_AXIS]
  150. },
  151. dr_old = delta_radius,
  152. zh_old = home_offset[Z_AXIS],
  153. ta_old[ABC] = {
  154. delta_tower_angle_trim[A_AXIS],
  155. delta_tower_angle_trim[B_AXIS],
  156. delta_tower_angle_trim[C_AXIS]
  157. };
  158. if (!_1p_calibration && !_0p_calibration) { // test if the outer radius is reachable
  159. const float circles = (_7p_quadruple_circle ? 1.5 :
  160. _7p_triple_circle ? 1.0 :
  161. _7p_double_circle ? 0.5 : 0),
  162. r = (1 + circles * 0.1) * delta_calibration_radius;
  163. for (uint8_t axis = 1; axis < 13; ++axis) {
  164. const float a = RADIANS(180 + 30 * axis);
  165. if (!position_is_reachable_xy(cos(a) * r, sin(a) * r)) {
  166. SERIAL_PROTOCOLLNPGM("?(M665 B)ed radius is implausible.");
  167. return;
  168. }
  169. }
  170. }
  171. SERIAL_PROTOCOLLNPGM("G33 Auto Calibrate");
  172. stepper.synchronize();
  173. #if HAS_LEVELING
  174. reset_bed_level(); // After calibration bed-level data is no longer valid
  175. #endif
  176. #if HOTENDS > 1
  177. const uint8_t old_tool_index = active_extruder;
  178. tool_change(0, 0, true);
  179. #define G33_CLEANUP() G33_cleanup(old_tool_index)
  180. #else
  181. #define G33_CLEANUP() G33_cleanup()
  182. #endif
  183. setup_for_endstop_or_probe_move();
  184. endstops.enable(true);
  185. if (!_0p_calibration) {
  186. if (!home_delta())
  187. return;
  188. endstops.not_homing();
  189. }
  190. // print settings
  191. const char *checkingac = PSTR("Checking... AC"); // TODO: Make translatable string
  192. serialprintPGM(checkingac);
  193. if (verbose_level == 0) SERIAL_PROTOCOLPGM(" (DRY-RUN)");
  194. SERIAL_EOL();
  195. lcd_setstatusPGM(checkingac);
  196. print_G33_settings(!_1p_calibration, _7p_calibration && towers_set);
  197. #if DISABLED(PROBE_MANUALLY)
  198. if (!_0p_calibration) {
  199. const float measured_z = probe_pt(dx, dy, stow_after_each, 1, false); // 1st probe to set height
  200. if (isnan(measured_z)) return G33_CLEANUP();
  201. home_offset[Z_AXIS] -= measured_z;
  202. }
  203. #endif
  204. do {
  205. float z_at_pt[13] = { 0.0 };
  206. test_precision = _0p_calibration ? 0.00 : zero_std_dev_old != 999.0 ? (zero_std_dev + zero_std_dev_old) / 2 : zero_std_dev;
  207. iterations++;
  208. // Probe the points
  209. if (!_0p_calibration){
  210. if (!_7p_half_circle && !_7p_triple_circle) { // probe the center
  211. #if ENABLED(PROBE_MANUALLY)
  212. z_at_pt[0] += lcd_probe_pt(0, 0);
  213. #else
  214. z_at_pt[0] += probe_pt(dx, dy, stow_after_each, 1, false);
  215. if (isnan(z_at_pt[0])) return G33_CLEANUP();
  216. #endif
  217. }
  218. if (_7p_calibration) { // probe extra center points
  219. for (int8_t axis = _7p_multi_circle ? 11 : 9; axis > 0; axis -= _7p_multi_circle ? 2 : 4) {
  220. const float a = RADIANS(180 + 30 * axis), r = delta_calibration_radius * 0.1;
  221. #if ENABLED(PROBE_MANUALLY)
  222. z_at_pt[0] += lcd_probe_pt(cos(a) * r, sin(a) * r);
  223. #else
  224. z_at_pt[0] += probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1);
  225. if (isnan(z_at_pt[0])) return G33_CLEANUP();
  226. #endif
  227. }
  228. z_at_pt[0] /= float(_7p_double_circle ? 7 : probe_points);
  229. }
  230. if (!_1p_calibration) { // probe the radius
  231. bool zig_zag = true;
  232. const uint8_t start = _4p_opposite_points ? 3 : 1,
  233. step = _4p_calibration ? 4 : _7p_half_circle ? 2 : 1;
  234. for (uint8_t axis = start; axis < 13; axis += step) {
  235. const float zigadd = (zig_zag ? 0.5 : 0.0),
  236. offset_circles = _7p_quadruple_circle ? zigadd + 1.0 :
  237. _7p_triple_circle ? zigadd + 0.5 :
  238. _7p_double_circle ? zigadd : 0;
  239. for (float circles = -offset_circles ; circles <= offset_circles; circles++) {
  240. const float a = RADIANS(180 + 30 * axis),
  241. r = delta_calibration_radius * (1 + circles * (zig_zag ? 0.1 : -0.1));
  242. #if ENABLED(PROBE_MANUALLY)
  243. z_at_pt[axis] += lcd_probe_pt(cos(a) * r, sin(a) * r);
  244. #else
  245. z_at_pt[axis] += probe_pt(cos(a) * r + dx, sin(a) * r + dy, stow_after_each, 1);
  246. if (isnan(z_at_pt[axis])) return G33_CLEANUP();
  247. #endif
  248. }
  249. zig_zag = !zig_zag;
  250. z_at_pt[axis] /= (2 * offset_circles + 1);
  251. }
  252. }
  253. if (_7p_intermed_points) // average intermediates to tower and opposites
  254. for (uint8_t axis = 1; axis < 13; axis += 2)
  255. z_at_pt[axis] = (z_at_pt[axis] + (z_at_pt[axis + 1] + z_at_pt[(axis + 10) % 12 + 1]) / 2.0) / 2.0;
  256. }
  257. float S1 = z_at_pt[0],
  258. S2 = sq(z_at_pt[0]);
  259. int16_t N = 1;
  260. if (!_1p_calibration) // std dev from zero plane
  261. for (uint8_t axis = (_4p_opposite_points ? 3 : 1); axis < 13; axis += (_4p_calibration ? 4 : 2)) {
  262. S1 += z_at_pt[axis];
  263. S2 += sq(z_at_pt[axis]);
  264. N++;
  265. }
  266. zero_std_dev_old = zero_std_dev;
  267. zero_std_dev = round(SQRT(S2 / N) * 1000.0) / 1000.0 + 0.00001;
  268. // Solve matrices
  269. if ((zero_std_dev < test_precision && zero_std_dev > calibration_precision) || iterations <= force_iterations) {
  270. if (zero_std_dev < zero_std_dev_min) {
  271. COPY(e_old, delta_endstop_adj);
  272. dr_old = delta_radius;
  273. zh_old = home_offset[Z_AXIS];
  274. COPY(ta_old, delta_tower_angle_trim);
  275. }
  276. float e_delta[ABC] = { 0.0 }, r_delta = 0.0, t_delta[ABC] = { 0.0 };
  277. const float r_diff = delta_radius - delta_calibration_radius,
  278. h_factor = (1.00 + r_diff * 0.001) / 6.0, //1.02 / 6 for r_diff = 20mm
  279. r_factor = -(1.75 + 0.005 * r_diff + 0.001 * sq(r_diff)) / 6.0, //2.25 / 6 for r_diff = 20mm
  280. a_factor = 66.66 / delta_calibration_radius; //1.25 for cal_rd = 80mm
  281. #define ZP(N,I) ((N) * z_at_pt[I])
  282. #define Z6(I) ZP(6, I)
  283. #define Z4(I) ZP(4, I)
  284. #define Z2(I) ZP(2, I)
  285. #define Z1(I) ZP(1, I)
  286. #if ENABLED(PROBE_MANUALLY)
  287. test_precision = 0.00; // forced end
  288. #endif
  289. switch (probe_points) {
  290. case 1:
  291. test_precision = 0.00; // forced end
  292. LOOP_XYZ(axis) e_delta[axis] = Z1(0);
  293. break;
  294. case 2:
  295. if (towers_set) {
  296. e_delta[A_AXIS] = (Z6(0) + Z4(1) - Z2(5) - Z2(9)) * h_factor;
  297. e_delta[B_AXIS] = (Z6(0) - Z2(1) + Z4(5) - Z2(9)) * h_factor;
  298. e_delta[C_AXIS] = (Z6(0) - Z2(1) - Z2(5) + Z4(9)) * h_factor;
  299. r_delta = (Z6(0) - Z2(1) - Z2(5) - Z2(9)) * r_factor;
  300. }
  301. else {
  302. e_delta[A_AXIS] = (Z6(0) - Z4(7) + Z2(11) + Z2(3)) * h_factor;
  303. e_delta[B_AXIS] = (Z6(0) + Z2(7) - Z4(11) + Z2(3)) * h_factor;
  304. e_delta[C_AXIS] = (Z6(0) + Z2(7) + Z2(11) - Z4(3)) * h_factor;
  305. r_delta = (Z6(0) - Z2(7) - Z2(11) - Z2(3)) * r_factor;
  306. }
  307. break;
  308. default:
  309. e_delta[A_AXIS] = (Z6(0) + Z2(1) - Z1(5) - Z1(9) - Z2(7) + Z1(11) + Z1(3)) * h_factor;
  310. e_delta[B_AXIS] = (Z6(0) - Z1(1) + Z2(5) - Z1(9) + Z1(7) - Z2(11) + Z1(3)) * h_factor;
  311. e_delta[C_AXIS] = (Z6(0) - Z1(1) - Z1(5) + Z2(9) + Z1(7) + Z1(11) - Z2(3)) * h_factor;
  312. r_delta = (Z6(0) - Z1(1) - Z1(5) - Z1(9) - Z1(7) - Z1(11) - Z1(3)) * r_factor;
  313. if (towers_set) {
  314. t_delta[A_AXIS] = ( - Z2(5) + Z1(9) - Z2(11) + Z1(3)) * a_factor;
  315. t_delta[B_AXIS] = ( Z2(1) - Z1(9) + Z2(7) - Z1(3)) * a_factor;
  316. t_delta[C_AXIS] = ( -Z2(1) + Z1(5) - Z2(7) + Z1(11) ) * a_factor;
  317. }
  318. break;
  319. }
  320. LOOP_XYZ(axis) delta_endstop_adj[axis] += e_delta[axis];
  321. delta_radius += r_delta;
  322. LOOP_XYZ(axis) delta_tower_angle_trim[axis] += t_delta[axis];
  323. }
  324. else if (zero_std_dev >= test_precision) { // step one back
  325. COPY(delta_endstop_adj, e_old);
  326. delta_radius = dr_old;
  327. home_offset[Z_AXIS] = zh_old;
  328. COPY(delta_tower_angle_trim, ta_old);
  329. }
  330. if (verbose_level != 0) { // !dry run
  331. // normalise angles to least squares
  332. float a_sum = 0.0;
  333. LOOP_XYZ(axis) a_sum += delta_tower_angle_trim[axis];
  334. LOOP_XYZ(axis) delta_tower_angle_trim[axis] -= a_sum / 3.0;
  335. // adjust delta_height and endstops by the max amount
  336. const float z_temp = MAX3(delta_endstop_adj[A_AXIS], delta_endstop_adj[B_AXIS], delta_endstop_adj[C_AXIS]);
  337. home_offset[Z_AXIS] -= z_temp;
  338. LOOP_XYZ(axis) delta_endstop_adj[axis] -= z_temp;
  339. }
  340. recalc_delta_settings(delta_radius, delta_diagonal_rod, delta_tower_angle_trim);
  341. NOMORE(zero_std_dev_min, zero_std_dev);
  342. // print report
  343. if (verbose_level != 1) {
  344. SERIAL_PROTOCOLPGM(". ");
  345. print_signed_float(PSTR("c"), z_at_pt[0]);
  346. if (_4p_towers_points || _7p_calibration) {
  347. print_signed_float(PSTR(" x"), z_at_pt[1]);
  348. print_signed_float(PSTR(" y"), z_at_pt[5]);
  349. print_signed_float(PSTR(" z"), z_at_pt[9]);
  350. }
  351. if (!_4p_opposite_points) SERIAL_EOL();
  352. if ((_4p_opposite_points) || _7p_calibration) {
  353. if (_7p_calibration) {
  354. SERIAL_CHAR('.');
  355. SERIAL_PROTOCOL_SP(13);
  356. }
  357. print_signed_float(PSTR(" yz"), z_at_pt[7]);
  358. print_signed_float(PSTR("zx"), z_at_pt[11]);
  359. print_signed_float(PSTR("xy"), z_at_pt[3]);
  360. SERIAL_EOL();
  361. }
  362. }
  363. if (verbose_level != 0) { // !dry run
  364. if ((zero_std_dev >= test_precision || zero_std_dev <= calibration_precision) && iterations > force_iterations) { // end iterations
  365. SERIAL_PROTOCOLPGM("Calibration OK");
  366. SERIAL_PROTOCOL_SP(36);
  367. #if DISABLED(PROBE_MANUALLY)
  368. if (zero_std_dev >= test_precision && !_1p_calibration)
  369. SERIAL_PROTOCOLPGM("rolling back.");
  370. else
  371. #endif
  372. {
  373. SERIAL_PROTOCOLPGM("std dev:");
  374. SERIAL_PROTOCOL_F(zero_std_dev_min, 3);
  375. }
  376. SERIAL_EOL();
  377. char mess[21];
  378. sprintf_P(mess, PSTR("Calibration sd:"));
  379. if (zero_std_dev_min < 1)
  380. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev_min * 1000.0));
  381. else
  382. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev_min));
  383. lcd_setstatus(mess);
  384. print_G33_settings(!_1p_calibration, _7p_calibration && towers_set);
  385. serialprintPGM(save_message);
  386. SERIAL_EOL();
  387. }
  388. else { // !end iterations
  389. char mess[15];
  390. if (iterations < 31)
  391. sprintf_P(mess, PSTR("Iteration : %02i"), (int)iterations);
  392. else
  393. sprintf_P(mess, PSTR("No convergence"));
  394. SERIAL_PROTOCOL(mess);
  395. SERIAL_PROTOCOL_SP(36);
  396. SERIAL_PROTOCOLPGM("std dev:");
  397. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  398. SERIAL_EOL();
  399. lcd_setstatus(mess);
  400. print_G33_settings(!_1p_calibration, _7p_calibration && towers_set);
  401. }
  402. }
  403. else { // dry run
  404. const char *enddryrun = PSTR("End DRY-RUN");
  405. serialprintPGM(enddryrun);
  406. SERIAL_PROTOCOL_SP(39);
  407. SERIAL_PROTOCOLPGM("std dev:");
  408. SERIAL_PROTOCOL_F(zero_std_dev, 3);
  409. SERIAL_EOL();
  410. char mess[21];
  411. sprintf_P(mess, enddryrun);
  412. sprintf_P(&mess[11], PSTR(" sd:"));
  413. if (zero_std_dev < 1)
  414. sprintf_P(&mess[15], PSTR("0.%03i"), (int)round(zero_std_dev * 1000.0));
  415. else
  416. sprintf_P(&mess[15], PSTR("%03i.x"), (int)round(zero_std_dev));
  417. lcd_setstatus(mess);
  418. }
  419. endstops.enable(true);
  420. home_delta();
  421. endstops.not_homing();
  422. }
  423. while ((zero_std_dev < test_precision && zero_std_dev > calibration_precision && iterations < 31) || iterations <= force_iterations);
  424. G33_CLEANUP();
  425. }
  426. #endif // DELTA_AUTO_CALIBRATION