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

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