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

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