My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

G425.cpp 22KB

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