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.

G425.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 "../../Marlin.h"
  23. #if ENABLED(CALIBRATION_GCODE)
  24. #include "../gcode.h"
  25. #include "../../lcd/ultralcd.h"
  26. #include "../../module/motion.h"
  27. #include "../../module/planner.h"
  28. #include "../../module/tool_change.h"
  29. #include "../../module/endstops.h"
  30. #include "../../feature/bedlevel/bedlevel.h"
  31. #include "../../feature/backlash.h"
  32. /**
  33. * G425 backs away from the calibration object by various distances
  34. * depending on the confidence level:
  35. *
  36. * UNKNOWN - No real notion on where the calibration object is on the bed
  37. * UNCERTAIN - Measurement may be uncertain due to backlash
  38. * CERTAIN - Measurement obtained with backlash compensation
  39. */
  40. #ifndef CALIBRATION_MEASUREMENT_UNKNOWN
  41. #define CALIBRATION_MEASUREMENT_UNKNOWN 5.0 // mm
  42. #endif
  43. #ifndef CALIBRATION_MEASUREMENT_UNCERTAIN
  44. #define CALIBRATION_MEASUREMENT_UNCERTAIN 1.0 // mm
  45. #endif
  46. #ifndef CALIBRATION_MEASUREMENT_CERTAIN
  47. #define CALIBRATION_MEASUREMENT_CERTAIN 0.5 // mm
  48. #endif
  49. #define HAS_X_CENTER BOTH(CALIBRATION_MEASURE_LEFT, CALIBRATION_MEASURE_RIGHT)
  50. #define HAS_Y_CENTER BOTH(CALIBRATION_MEASURE_FRONT, CALIBRATION_MEASURE_BACK)
  51. enum side_t : uint8_t { TOP, RIGHT, FRONT, LEFT, BACK, NUM_SIDES };
  52. struct measurements_t {
  53. static constexpr float dimensions[XYZ] = CALIBRATION_OBJECT_DIMENSIONS;
  54. static constexpr float true_center[XYZ] = CALIBRATION_OBJECT_CENTER;
  55. float obj_center[XYZ] = CALIBRATION_OBJECT_CENTER;
  56. float obj_side[NUM_SIDES];
  57. float backlash[NUM_SIDES];
  58. float pos_error[XYZ];
  59. float nozzle_outer_dimension[2] = {CALIBRATION_NOZZLE_OUTER_DIAMETER, CALIBRATION_NOZZLE_OUTER_DIAMETER};
  60. };
  61. #define TEMPORARY_BED_LEVELING_STATE(enable) TemporaryBedLevelingState tbls(enable)
  62. #define TEMPORARY_SOFT_ENDSTOP_STATE(enable) REMEMBER(tes, soft_endstops_enabled, enable);
  63. #if ENABLED(BACKLASH_GCODE)
  64. #define TEMPORARY_BACKLASH_CORRECTION(value) REMEMBER(tbst, backlash.correction, value)
  65. #else
  66. #define TEMPORARY_BACKLASH_CORRECTION(value)
  67. #endif
  68. #if ENABLED(BACKLASH_GCODE) && defined(BACKLASH_SMOOTHING_MM)
  69. #define TEMPORARY_BACKLASH_SMOOTHING(value) REMEMBER(tbsm, backlash.smoothing_mm, value)
  70. #else
  71. #define TEMPORARY_BACKLASH_SMOOTHING(value)
  72. #endif
  73. /**
  74. * A class to save and change the bed leveling state,
  75. * then restore it when it goes out of scope.
  76. */
  77. class TemporaryBedLevelingState {
  78. bool saved;
  79. public:
  80. TemporaryBedLevelingState(const bool enable) : saved(planner.leveling_active) {
  81. set_bed_leveling_enabled(enable);
  82. }
  83. ~TemporaryBedLevelingState() { set_bed_leveling_enabled(saved); }
  84. };
  85. /**
  86. * Move to a particular location. Up to three individual axes
  87. * and their destinations can be specified, in any order.
  88. */
  89. inline void move_to(
  90. const AxisEnum a1 = NO_AXIS, const float p1 = 0,
  91. const AxisEnum a2 = NO_AXIS, const float p2 = 0,
  92. const AxisEnum a3 = NO_AXIS, const float p3 = 0
  93. ) {
  94. set_destination_from_current();
  95. // Note: The order of p1, p2, p3 may not correspond to X, Y, Z
  96. if (a1 != NO_AXIS) destination[a1] = p1;
  97. if (a2 != NO_AXIS) destination[a2] = p2;
  98. if (a3 != NO_AXIS) destination[a3] = p3;
  99. // Make sure coordinates are within bounds
  100. destination[X_AXIS] = MAX(MIN(destination[X_AXIS], X_MAX_POS), X_MIN_POS);
  101. destination[Y_AXIS] = MAX(MIN(destination[Y_AXIS], Y_MAX_POS), Y_MIN_POS);
  102. destination[Z_AXIS] = MAX(MIN(destination[Z_AXIS], Z_MAX_POS), Z_MIN_POS);
  103. // Move to position
  104. do_blocking_move_to(destination, MMM_TO_MMS(CALIBRATION_FEEDRATE_TRAVEL));
  105. }
  106. /**
  107. * Move to the exact center above the calibration object
  108. *
  109. * m in - Measurement record
  110. * uncertainty in - How far away from the object top to park
  111. */
  112. inline void park_above_object(measurements_t &m, const float uncertainty) {
  113. // Move to safe distance above calibration object
  114. move_to(Z_AXIS, m.obj_center[Z_AXIS] + m.dimensions[Z_AXIS] / 2 + uncertainty);
  115. // Move to center of calibration object in XY
  116. move_to(X_AXIS, m.obj_center[X_AXIS], Y_AXIS, m.obj_center[Y_AXIS]);
  117. }
  118. #if HOTENDS > 1
  119. inline void set_nozzle(measurements_t &m, const uint8_t extruder) {
  120. if (extruder != active_extruder) {
  121. park_above_object(m, CALIBRATION_MEASUREMENT_UNKNOWN);
  122. tool_change(extruder);
  123. }
  124. }
  125. #endif
  126. #if HAS_HOTEND_OFFSET
  127. inline void normalize_hotend_offsets() {
  128. for (uint8_t e = 1; e < HOTENDS; e++) {
  129. hotend_offset[X_AXIS][e] -= hotend_offset[X_AXIS][0];
  130. hotend_offset[Y_AXIS][e] -= hotend_offset[Y_AXIS][0];
  131. hotend_offset[Z_AXIS][e] -= hotend_offset[Z_AXIS][0];
  132. }
  133. hotend_offset[X_AXIS][0] = 0;
  134. hotend_offset[Y_AXIS][0] = 0;
  135. hotend_offset[Z_AXIS][0] = 0;
  136. }
  137. #endif
  138. inline bool read_calibration_pin() {
  139. #if HAS_CALIBRATION_PIN
  140. return (READ(CALIBRATION_PIN) != CALIBRATION_PIN_INVERTING);
  141. #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  142. return (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  143. #else
  144. return (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING);
  145. #endif
  146. }
  147. /**
  148. * Move along axis in the specified dir until the probe value becomes stop_state,
  149. * then return the axis value.
  150. *
  151. * axis in - Axis along which the measurement will take place
  152. * dir in - Direction along that axis (-1 or 1)
  153. * stop_state in - Move until probe pin becomes this value
  154. * fast in - Fast vs. precise measurement
  155. */
  156. float measuring_movement(const AxisEnum axis, const int dir, const bool stop_state, const bool fast) {
  157. const float step = fast ? 0.25 : CALIBRATION_MEASUREMENT_RESOLUTION;
  158. const float mms = MMM_TO_MMS(fast ? CALIBRATION_FEEDRATE_FAST : CALIBRATION_FEEDRATE_SLOW);
  159. const float limit = fast ? 50 : 5;
  160. set_destination_from_current();
  161. for (float travel = 0; travel < limit; travel += step) {
  162. destination[axis] += dir * step;
  163. do_blocking_move_to(destination, mms);
  164. planner.synchronize();
  165. if (read_calibration_pin() == stop_state)
  166. break;
  167. }
  168. return destination[axis];
  169. }
  170. /**
  171. * Move along axis until the probe is triggered. Move toolhead to its starting
  172. * point and return the measured value.
  173. *
  174. * axis in - Axis along which the measurement will take place
  175. * dir in - Direction along that axis (-1 or 1)
  176. * stop_state in - Move until probe pin becomes this value
  177. * backlash_ptr in/out - When not nullptr, measure and record axis backlash
  178. * uncertainty in - If uncertainty is CALIBRATION_MEASUREMENT_UNKNOWN, do a fast probe.
  179. */
  180. inline float measure(const AxisEnum axis, const int dir, const bool stop_state, float * const backlash_ptr, const float uncertainty) {
  181. const bool fast = uncertainty == CALIBRATION_MEASUREMENT_UNKNOWN;
  182. // Save position
  183. set_destination_from_current();
  184. const float start_pos = destination[axis];
  185. const float measured_pos = measuring_movement(axis, dir, stop_state, fast);
  186. // Measure backlash
  187. if (backlash_ptr && !fast) {
  188. const float release_pos = measuring_movement(axis, -dir, !stop_state, fast);
  189. *backlash_ptr = ABS(release_pos - measured_pos);
  190. }
  191. // Return to starting position
  192. destination[axis] = start_pos;
  193. do_blocking_move_to(destination, MMM_TO_MMS(CALIBRATION_FEEDRATE_TRAVEL));
  194. return measured_pos;
  195. }
  196. /**
  197. * Probe one side of the calibration object
  198. *
  199. * m in/out - Measurement record, m.obj_center and m.obj_side will be updated.
  200. * uncertainty in - How far away from the calibration object to begin probing
  201. * side in - Side of probe where probe will occur
  202. * probe_top_at_edge in - When probing sides, probe top of calibration object nearest edge
  203. * to find out height of edge
  204. */
  205. inline void probe_side(measurements_t &m, const float uncertainty, const side_t side, const bool probe_top_at_edge=false) {
  206. const float dimensions[] = CALIBRATION_OBJECT_DIMENSIONS;
  207. AxisEnum axis;
  208. float dir;
  209. park_above_object(m, uncertainty);
  210. switch (side) {
  211. case TOP: {
  212. const float measurement = measure(Z_AXIS, -1, true, &m.backlash[TOP], uncertainty);
  213. m.obj_center[Z_AXIS] = measurement - dimensions[Z_AXIS] / 2;
  214. m.obj_side[TOP] = measurement;
  215. return;
  216. }
  217. case RIGHT: axis = X_AXIS; dir = -1; break;
  218. case FRONT: axis = Y_AXIS; dir = 1; break;
  219. case LEFT: axis = X_AXIS; dir = 1; break;
  220. case BACK: axis = Y_AXIS; dir = -1; break;
  221. default:
  222. return;
  223. }
  224. if (probe_top_at_edge) {
  225. // Probe top nearest the side we are probing
  226. move_to(axis, m.obj_center[axis] + (-dir) * (dimensions[axis] / 2 - m.nozzle_outer_dimension[axis]));
  227. m.obj_side[TOP] = measure(Z_AXIS, -1, true, &m.backlash[TOP], uncertainty);
  228. m.obj_center[Z_AXIS] = m.obj_side[TOP] - dimensions[Z_AXIS] / 2;
  229. }
  230. // Move to safe distance to the side of the calibration object
  231. move_to(axis, m.obj_center[axis] + (-dir) * (dimensions[axis] / 2 + m.nozzle_outer_dimension[axis] / 2 + uncertainty));
  232. // Plunge below the side of the calibration object and measure
  233. move_to(Z_AXIS, m.obj_side[TOP] - CALIBRATION_NOZZLE_TIP_HEIGHT * 0.7);
  234. const float measurement = measure(axis, dir, true, &m.backlash[side], uncertainty);
  235. m.obj_center[axis] = measurement + dir * (dimensions[axis] / 2 + m.nozzle_outer_dimension[axis] / 2);
  236. m.obj_side[side] = measurement;
  237. }
  238. /**
  239. * Probe all sides of the calibration calibration object
  240. *
  241. * m in/out - Measurement record: center, backlash and error values be updated.
  242. * uncertainty in - How far away from the calibration object to begin probing
  243. */
  244. inline void probe_sides(measurements_t &m, const float uncertainty) {
  245. #ifdef CALIBRATION_MEASURE_AT_TOP_EDGES
  246. constexpr bool probe_top_at_edge = true;
  247. #else
  248. // Probing at the exact center only works if the center is flat. Probing on a washer
  249. // or bolt will require probing the top near the side edges, away from the center.
  250. constexpr bool probe_top_at_edge = false;
  251. probe_side(m, uncertainty, TOP);
  252. #endif
  253. #ifdef CALIBRATION_MEASURE_RIGHT
  254. probe_side(m, uncertainty, RIGHT, probe_top_at_edge);
  255. #endif
  256. #ifdef CALIBRATION_MEASURE_FRONT
  257. probe_side(m, uncertainty, FRONT, probe_top_at_edge);
  258. #endif
  259. #ifdef CALIBRATION_MEASURE_LEFT
  260. probe_side(m, uncertainty, LEFT, probe_top_at_edge);
  261. #endif
  262. #ifdef CALIBRATION_MEASURE_BACK
  263. probe_side(m, uncertainty, BACK, probe_top_at_edge);
  264. #endif
  265. // Compute the measured center of the calibration object.
  266. #if HAS_X_CENTER
  267. m.obj_center[X_AXIS] = (m.obj_side[LEFT] + m.obj_side[RIGHT]) / 2;
  268. #endif
  269. #if HAS_Y_CENTER
  270. m.obj_center[Y_AXIS] = (m.obj_side[FRONT] + m.obj_side[BACK]) / 2;
  271. #endif
  272. // Compute the outside diameter of the nozzle at the height
  273. // at which it makes contact with the calibration object
  274. #if HAS_X_CENTER
  275. m.nozzle_outer_dimension[X_AXIS] = m.obj_side[RIGHT] - m.obj_side[LEFT] - m.dimensions[X_AXIS];
  276. #endif
  277. #if HAS_Y_CENTER
  278. m.nozzle_outer_dimension[Y_AXIS] = m.obj_side[BACK] - m.obj_side[FRONT] - m.dimensions[Y_AXIS];
  279. #endif
  280. park_above_object(m, uncertainty);
  281. // The difference between the known and the measured location
  282. // of the calibration object is the positional error
  283. m.pos_error[X_AXIS] =
  284. #if HAS_X_CENTER
  285. m.true_center[X_AXIS] - m.obj_center[X_AXIS];
  286. #else
  287. 0;
  288. #endif
  289. m.pos_error[Y_AXIS] =
  290. #if HAS_Y_CENTER
  291. m.true_center[Y_AXIS] - m.obj_center[Y_AXIS];
  292. #else
  293. 0;
  294. #endif
  295. m.pos_error[Z_AXIS] = m.true_center[Z_AXIS] - m.obj_center[Z_AXIS];
  296. }
  297. #if ENABLED(CALIBRATION_REPORTING)
  298. inline void report_measured_faces(const measurements_t &m) {
  299. SERIAL_ECHOLNPGM("Sides:");
  300. SERIAL_ECHOLNPAIR(" Top: ", m.obj_side[TOP]);
  301. #if ENABLED(CALIBRATION_MEASURE_LEFT)
  302. SERIAL_ECHOLNPAIR(" Left: ", m.obj_side[LEFT]);
  303. #endif
  304. #if ENABLED(CALIBRATION_MEASURE_RIGHT)
  305. SERIAL_ECHOLNPAIR(" Right: ", m.obj_side[RIGHT]);
  306. #endif
  307. #if ENABLED(CALIBRATION_MEASURE_FRONT)
  308. SERIAL_ECHOLNPAIR(" Front: ", m.obj_side[FRONT]);
  309. #endif
  310. #if ENABLED(CALIBRATION_MEASURE_BACK)
  311. SERIAL_ECHOLNPAIR(" Back: ", m.obj_side[BACK]);
  312. #endif
  313. SERIAL_EOL();
  314. }
  315. inline void report_measured_center(const measurements_t &m) {
  316. SERIAL_ECHOLNPGM("Center:");
  317. #if HAS_X_CENTER
  318. SERIAL_ECHOLNPAIR(" X", m.obj_center[X_AXIS]);
  319. #endif
  320. #if HAS_Y_CENTER
  321. SERIAL_ECHOLNPAIR(" Y", m.obj_center[Y_AXIS]);
  322. #endif
  323. SERIAL_ECHOLNPAIR(" Z", m.obj_center[Z_AXIS]);
  324. SERIAL_EOL();
  325. }
  326. inline void report_measured_backlash(const measurements_t &m) {
  327. SERIAL_ECHOLNPGM("Backlash:");
  328. #if ENABLED(CALIBRATION_MEASURE_LEFT)
  329. SERIAL_ECHOLNPAIR(" Left: ", m.backlash[LEFT]);
  330. #endif
  331. #if ENABLED(CALIBRATION_MEASURE_RIGHT)
  332. SERIAL_ECHOLNPAIR(" Right: ", m.backlash[RIGHT]);
  333. #endif
  334. #if ENABLED(CALIBRATION_MEASURE_FRONT)
  335. SERIAL_ECHOLNPAIR(" Front: ", m.backlash[FRONT]);
  336. #endif
  337. #if ENABLED(CALIBRATION_MEASURE_BACK)
  338. SERIAL_ECHOLNPAIR(" Back: ", m.backlash[BACK]);
  339. #endif
  340. SERIAL_ECHOLNPAIR(" Top: ", m.backlash[TOP]);
  341. SERIAL_EOL();
  342. }
  343. inline void report_measured_positional_error(const measurements_t &m) {
  344. SERIAL_CHAR('T');
  345. SERIAL_ECHO(int(active_extruder));
  346. SERIAL_ECHOLNPGM(" Positional Error:");
  347. #if HAS_X_CENTER
  348. SERIAL_ECHOLNPAIR(" X", m.pos_error[X_AXIS]);
  349. #endif
  350. #if HAS_Y_CENTER
  351. SERIAL_ECHOLNPAIR(" Y", m.pos_error[Y_AXIS]);
  352. #endif
  353. SERIAL_ECHOLNPAIR(" Z", m.pos_error[Z_AXIS]);
  354. SERIAL_EOL();
  355. }
  356. inline void report_measured_nozzle_dimensions(const measurements_t &m) {
  357. SERIAL_ECHOLNPGM("Nozzle Tip Outer Dimensions:");
  358. #if HAS_X_CENTER
  359. SERIAL_ECHOLNPAIR(" X", m.nozzle_outer_dimension[X_AXIS]);
  360. #endif
  361. #if HAS_Y_CENTER
  362. SERIAL_ECHOLNPAIR(" Y", m.nozzle_outer_dimension[Y_AXIS]);
  363. #endif
  364. SERIAL_EOL();
  365. }
  366. #if HAS_HOTEND_OFFSET
  367. //
  368. // This function requires normalize_hotend_offsets() to be called
  369. //
  370. inline void report_hotend_offsets() {
  371. for (uint8_t e = 1; e < HOTENDS; e++) {
  372. SERIAL_ECHOPAIR("T", int(e));
  373. SERIAL_ECHOLNPGM(" Hotend Offset:");
  374. SERIAL_ECHOLNPAIR(" X: ", hotend_offset[X_AXIS][e]);
  375. SERIAL_ECHOLNPAIR(" Y: ", hotend_offset[Y_AXIS][e]);
  376. SERIAL_ECHOLNPAIR(" Z: ", hotend_offset[Z_AXIS][e]);
  377. SERIAL_EOL();
  378. }
  379. }
  380. #endif
  381. #endif // CALIBRATION_REPORTING
  382. /**
  383. * Probe around the calibration object to measure backlash
  384. *
  385. * m in/out - Measurement record, updated with new readings
  386. * uncertainty in - How far away from the object to begin probing
  387. */
  388. inline void calibrate_backlash(measurements_t &m, const float uncertainty) {
  389. // Backlash compensation should be off while measuring backlash
  390. {
  391. // New scope for TEMPORARY_BACKLASH_CORRECTION
  392. TEMPORARY_BACKLASH_CORRECTION(all_off);
  393. TEMPORARY_BACKLASH_SMOOTHING(0.0f);
  394. probe_sides(m, uncertainty);
  395. #if ENABLED(BACKLASH_GCODE)
  396. #if HAS_X_CENTER
  397. backlash.distance_mm[X_AXIS] = (m.backlash[LEFT] + m.backlash[RIGHT]) / 2;
  398. #elif ENABLED(CALIBRATION_MEASURE_LEFT)
  399. backlash.distance_mm[X_AXIS] = m.backlash[LEFT];
  400. #elif ENABLED(CALIBRATION_MEASURE_RIGHT)
  401. backlash.distance_mm[X_AXIS] = m.backlash[RIGHT];
  402. #endif
  403. #if HAS_Y_CENTER
  404. backlash.distance_mm[Y_AXIS] = (m.backlash[FRONT] + m.backlash[BACK]) / 2;
  405. #elif ENABLED(CALIBRATION_MEASURE_FRONT)
  406. backlash.distance_mm[Y_AXIS] = m.backlash[FRONT];
  407. #elif ENABLED(CALIBRATION_MEASURE_BACK)
  408. backlash.distance_mm[Y_AXIS] = m.backlash[BACK];
  409. #endif
  410. backlash.distance_mm[Z_AXIS] = m.backlash[TOP];
  411. #endif
  412. }
  413. #if ENABLED(BACKLASH_GCODE)
  414. // Turn on backlash compensation and move in all
  415. // directions to take up any backlash
  416. {
  417. // New scope for TEMPORARY_BACKLASH_CORRECTION
  418. TEMPORARY_BACKLASH_CORRECTION(all_on);
  419. TEMPORARY_BACKLASH_SMOOTHING(0.0f);
  420. move_to(
  421. X_AXIS, current_position[X_AXIS] + 3,
  422. Y_AXIS, current_position[Y_AXIS] + 3,
  423. Z_AXIS, current_position[Z_AXIS] + 3
  424. );
  425. move_to(
  426. X_AXIS, current_position[X_AXIS] - 3,
  427. Y_AXIS, current_position[Y_AXIS] - 3,
  428. Z_AXIS, current_position[Z_AXIS] - 3
  429. );
  430. }
  431. #endif
  432. }
  433. inline void update_measurements(measurements_t &m, const AxisEnum axis) {
  434. const float true_center[XYZ] = CALIBRATION_OBJECT_CENTER;
  435. current_position[axis] += m.pos_error[axis];
  436. m.obj_center[axis] = true_center[axis];
  437. m.pos_error[axis] = 0;
  438. }
  439. /**
  440. * Probe around the calibration object. Adjust the position and toolhead offset
  441. * using the deviation from the known position of the calibration object.
  442. *
  443. * m in/out - Measurement record, updated with new readings
  444. * uncertainty in - How far away from the object to begin probing
  445. * extruder in - What extruder to probe
  446. *
  447. * Prerequisites:
  448. * - Call calibrate_backlash() beforehand for best accuracy
  449. */
  450. inline void calibrate_toolhead(measurements_t &m, const float uncertainty, const uint8_t extruder) {
  451. TEMPORARY_BACKLASH_CORRECTION(all_on);
  452. TEMPORARY_BACKLASH_SMOOTHING(0.0f);
  453. #if HOTENDS > 1
  454. set_nozzle(m, extruder);
  455. #endif
  456. probe_sides(m, uncertainty);
  457. // Adjust the hotend offset
  458. #if HAS_HOTEND_OFFSET
  459. #if HAS_X_CENTER
  460. hotend_offset[X_AXIS][extruder] += m.pos_error[X_AXIS];
  461. #endif
  462. #if HAS_Y_CENTER
  463. hotend_offset[Y_AXIS][extruder] += m.pos_error[Y_AXIS];
  464. #endif
  465. hotend_offset[Z_AXIS][extruder] += m.pos_error[Z_AXIS];
  466. normalize_hotend_offsets();
  467. #endif
  468. // Correct for positional error, so the object
  469. // is at the known actual spot
  470. planner.synchronize();
  471. #if HAS_X_CENTER
  472. update_measurements(m, X_AXIS);
  473. #endif
  474. #if HAS_Y_CENTER
  475. update_measurements(m, Y_AXIS);
  476. #endif
  477. update_measurements(m, Z_AXIS);
  478. sync_plan_position();
  479. }
  480. /**
  481. * Probe around the calibration object for all toolheads, adjusting the coordinate
  482. * system for the first nozzle and the nozzle offset for subsequent nozzles.
  483. *
  484. * m in/out - Measurement record, updated with new readings
  485. * uncertainty in - How far away from the object to begin probing
  486. */
  487. inline void calibrate_all_toolheads(measurements_t &m, const float uncertainty) {
  488. TEMPORARY_BACKLASH_CORRECTION(all_on);
  489. TEMPORARY_BACKLASH_SMOOTHING(0.0f);
  490. HOTEND_LOOP() calibrate_toolhead(m, uncertainty, e);
  491. #if HAS_HOTEND_OFFSET
  492. normalize_hotend_offsets();
  493. #endif
  494. #if HOTENDS > 1
  495. set_nozzle(m, 0);
  496. #endif
  497. }
  498. /**
  499. * Perform a full auto-calibration routine:
  500. *
  501. * 1) For each nozzle, touch top and sides of object to determine object position and
  502. * nozzle offsets. Do a fast but rough search over a wider area.
  503. * 2) With the first nozzle, touch top and sides of object to determine backlash values
  504. * for all axis (if BACKLASH_GCODE is enabled)
  505. * 3) For each nozzle, touch top and sides of object slowly to determine precise
  506. * position of object. Adjust coordinate system and nozzle offsets so probed object
  507. * location corresponds to known object location with a high degree of precision.
  508. */
  509. inline void calibrate_all() {
  510. measurements_t m;
  511. #if HAS_HOTEND_OFFSET
  512. reset_hotend_offsets();
  513. #endif
  514. TEMPORARY_BACKLASH_CORRECTION(all_on);
  515. TEMPORARY_BACKLASH_SMOOTHING(0.0f);
  516. // Do a fast and rough calibration of the toolheads
  517. calibrate_all_toolheads(m, CALIBRATION_MEASUREMENT_UNKNOWN);
  518. #if ENABLED(BACKLASH_GCODE)
  519. calibrate_backlash(m, CALIBRATION_MEASUREMENT_UNCERTAIN);
  520. #endif
  521. // Cycle the toolheads so the servos settle into their "natural" positions
  522. #if HOTENDS > 1
  523. HOTEND_LOOP() set_nozzle(m, e);
  524. #endif
  525. // Do a slow and precise calibration of the toolheads
  526. calibrate_all_toolheads(m, CALIBRATION_MEASUREMENT_UNCERTAIN);
  527. move_to(X_AXIS, 150); // Park nozzle away from calibration object
  528. }
  529. /**
  530. * G425: Perform calibration with calibration object.
  531. *
  532. * B - Perform calibration of backlash only.
  533. * T<extruder> - Perform calibration of toolhead only.
  534. * V - Probe object and print position, error, backlash and hotend offset.
  535. * U - Uncertainty, how far to start probe away from the object (mm)
  536. *
  537. * no args - Perform entire calibration sequence (backlash + position on all toolheads)
  538. */
  539. void GcodeSuite::G425() {
  540. TEMPORARY_SOFT_ENDSTOP_STATE(false);
  541. TEMPORARY_BED_LEVELING_STATE(false);
  542. if (axis_unhomed_error()) return;
  543. measurements_t m;
  544. float uncertainty = parser.seenval('U') ? parser.value_float() : CALIBRATION_MEASUREMENT_UNCERTAIN;
  545. if (parser.seen('B'))
  546. calibrate_backlash(m, uncertainty);
  547. else if (parser.seen('T'))
  548. calibrate_toolhead(m, uncertainty, parser.has_value() ? parser.value_int() : active_extruder);
  549. #if ENABLED(CALIBRATION_REPORTING)
  550. else if (parser.seen('V')) {
  551. probe_sides(m, uncertainty);
  552. SERIAL_EOL();
  553. report_measured_faces(m);
  554. report_measured_center(m);
  555. report_measured_backlash(m);
  556. report_measured_nozzle_dimensions(m);
  557. report_measured_positional_error(m);
  558. #if HAS_HOTEND_OFFSET
  559. normalize_hotend_offsets();
  560. report_hotend_offsets();
  561. #endif
  562. }
  563. #endif
  564. else
  565. calibrate_all();
  566. }
  567. #endif // CALIBRATION_GCODE