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

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