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.

probe.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. /**
  23. * probe.cpp
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if HAS_BED_PROBE
  27. #include "probe.h"
  28. #include "../libs/buzzer.h"
  29. #include "motion.h"
  30. #include "temperature.h"
  31. #include "endstops.h"
  32. #include "../gcode/gcode.h"
  33. #include "../lcd/ultralcd.h"
  34. #include "../MarlinCore.h" // for stop(), disable_e_steppers, wait_for_user
  35. #if HAS_LEVELING
  36. #include "../feature/bedlevel/bedlevel.h"
  37. #endif
  38. #if ENABLED(DELTA)
  39. #include "delta.h"
  40. #endif
  41. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  42. #include "planner.h"
  43. #endif
  44. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  45. #include "../feature/backlash.h"
  46. #endif
  47. #if ENABLED(BLTOUCH)
  48. #include "../feature/bltouch.h"
  49. #endif
  50. #if ENABLED(HOST_PROMPT_SUPPORT)
  51. #include "../feature/host_actions.h" // for PROMPT_USER_CONTINUE
  52. #endif
  53. #if HAS_Z_SERVO_PROBE
  54. #include "servo.h"
  55. #endif
  56. #if ENABLED(SENSORLESS_PROBING)
  57. #include "stepper.h"
  58. #include "../feature/tmc_util.h"
  59. #endif
  60. #if QUIET_PROBING
  61. #include "stepper/indirection.h"
  62. #endif
  63. #if ENABLED(EXTENSIBLE_UI)
  64. #include "../lcd/extensible_ui/ui_api.h"
  65. #endif
  66. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  67. #include "../core/debug_out.h"
  68. xyz_pos_t probe_offset; // Initialized by settings.load()
  69. #if HAS_PROBE_XY_OFFSET
  70. xyz_pos_t &probe_offset_xy = probe_offset;
  71. #endif
  72. #if ENABLED(Z_PROBE_SLED)
  73. #ifndef SLED_DOCKING_OFFSET
  74. #define SLED_DOCKING_OFFSET 0
  75. #endif
  76. /**
  77. * Method to dock/undock a sled designed by Charles Bell.
  78. *
  79. * stow[in] If false, move to MAX_X and engage the solenoid
  80. * If true, move to MAX_X and release the solenoid
  81. */
  82. static void dock_sled(bool stow) {
  83. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("dock_sled(", stow, ")");
  84. // Dock sled a bit closer to ensure proper capturing
  85. do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET - ((stow) ? 1 : 0));
  86. #if HAS_SOLENOID_1 && DISABLED(EXT_SOLENOID)
  87. WRITE(SOL1_PIN, !stow); // switch solenoid
  88. #endif
  89. }
  90. #elif ENABLED(TOUCH_MI_PROBE)
  91. // Move to the magnet to unlock the probe
  92. void run_deploy_moves_script() {
  93. #ifndef TOUCH_MI_DEPLOY_XPOS
  94. #define TOUCH_MI_DEPLOY_XPOS X_MIN_POS
  95. #elif TOUCH_MI_DEPLOY_XPOS > X_MAX_BED
  96. TemporaryGlobalEndstopsState unlock_x(false);
  97. #endif
  98. #if TOUCH_MI_DEPLOY_YPOS > Y_MAX_BED
  99. TemporaryGlobalEndstopsState unlock_y(false);
  100. #endif
  101. #if ENABLED(TOUCH_MI_MANUAL_DEPLOY)
  102. const screenFunc_t prev_screen = ui.currentScreen;
  103. LCD_MESSAGEPGM(MSG_MANUAL_DEPLOY_TOUCHMI);
  104. ui.return_to_status();
  105. KEEPALIVE_STATE(PAUSED_FOR_USER);
  106. wait_for_user = true; // LCD click or M108 will clear this
  107. #if ENABLED(HOST_PROMPT_SUPPORT)
  108. host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Deploy TouchMI probe."), CONTINUE_STR);
  109. #endif
  110. while (wait_for_user) idle();
  111. ui.reset_status();
  112. ui.goto_screen(prev_screen);
  113. #elif defined(TOUCH_MI_DEPLOY_XPOS) && defined(TOUCH_MI_DEPLOY_YPOS)
  114. do_blocking_move_to_xy(TOUCH_MI_DEPLOY_XPOS, TOUCH_MI_DEPLOY_YPOS);
  115. #elif defined(TOUCH_MI_DEPLOY_XPOS)
  116. do_blocking_move_to_x(TOUCH_MI_DEPLOY_XPOS);
  117. #elif defined(TOUCH_MI_DEPLOY_YPOS)
  118. do_blocking_move_to_y(TOUCH_MI_DEPLOY_YPOS);
  119. #endif
  120. }
  121. // Move down to the bed to stow the probe
  122. void run_stow_moves_script() {
  123. const xyz_pos_t oldpos = current_position;
  124. endstops.enable_z_probe(false);
  125. do_blocking_move_to_z(TOUCH_MI_RETRACT_Z, MMM_TO_MMS(HOMING_FEEDRATE_Z));
  126. do_blocking_move_to(oldpos, MMM_TO_MMS(HOMING_FEEDRATE_Z));
  127. }
  128. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  129. void run_deploy_moves_script() {
  130. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_1
  131. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE
  132. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE 0.0
  133. #endif
  134. constexpr xyz_pos_t deploy_1 = Z_PROBE_ALLEN_KEY_DEPLOY_1;
  135. do_blocking_move_to(deploy_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE));
  136. #endif
  137. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_2
  138. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE
  139. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE 0.0
  140. #endif
  141. constexpr xyz_pos_t deploy_2 = Z_PROBE_ALLEN_KEY_DEPLOY_2;
  142. do_blocking_move_to(deploy_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE));
  143. #endif
  144. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_3
  145. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE
  146. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE 0.0
  147. #endif
  148. constexpr xyz_pos_t deploy_3 = Z_PROBE_ALLEN_KEY_DEPLOY_3;
  149. do_blocking_move_to(deploy_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE));
  150. #endif
  151. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_4
  152. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE
  153. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE 0.0
  154. #endif
  155. constexpr xyz_pos_t deploy_4 = Z_PROBE_ALLEN_KEY_DEPLOY_4;
  156. do_blocking_move_to(deploy_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE));
  157. #endif
  158. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_5
  159. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE
  160. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE 0.0
  161. #endif
  162. constexpr xyz_pos_t deploy_5 = Z_PROBE_ALLEN_KEY_DEPLOY_5;
  163. do_blocking_move_to(deploy_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE));
  164. #endif
  165. }
  166. void run_stow_moves_script() {
  167. #ifdef Z_PROBE_ALLEN_KEY_STOW_1
  168. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE
  169. #define Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE 0.0
  170. #endif
  171. constexpr xyz_pos_t stow_1 = Z_PROBE_ALLEN_KEY_STOW_1;
  172. do_blocking_move_to(stow_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE));
  173. #endif
  174. #ifdef Z_PROBE_ALLEN_KEY_STOW_2
  175. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE
  176. #define Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE 0.0
  177. #endif
  178. constexpr xyz_pos_t stow_2 = Z_PROBE_ALLEN_KEY_STOW_2;
  179. do_blocking_move_to(stow_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE));
  180. #endif
  181. #ifdef Z_PROBE_ALLEN_KEY_STOW_3
  182. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE
  183. #define Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE 0.0
  184. #endif
  185. constexpr xyz_pos_t stow_3 = Z_PROBE_ALLEN_KEY_STOW_3;
  186. do_blocking_move_to(stow_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE));
  187. #endif
  188. #ifdef Z_PROBE_ALLEN_KEY_STOW_4
  189. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE
  190. #define Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE 0.0
  191. #endif
  192. constexpr xyz_pos_t stow_4 = Z_PROBE_ALLEN_KEY_STOW_4;
  193. do_blocking_move_to(stow_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE));
  194. #endif
  195. #ifdef Z_PROBE_ALLEN_KEY_STOW_5
  196. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE
  197. #define Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE 0.0
  198. #endif
  199. constexpr xyz_pos_t stow_5 = Z_PROBE_ALLEN_KEY_STOW_5;
  200. do_blocking_move_to(stow_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE));
  201. #endif
  202. }
  203. #endif // Z_PROBE_ALLEN_KEY
  204. #if QUIET_PROBING
  205. void probing_pause(const bool p) {
  206. #if ENABLED(PROBING_HEATERS_OFF)
  207. thermalManager.pause(p);
  208. #endif
  209. #if ENABLED(PROBING_FANS_OFF)
  210. thermalManager.set_fans_paused(p);
  211. #endif
  212. #if ENABLED(PROBING_STEPPERS_OFF)
  213. disable_e_steppers();
  214. #if NONE(DELTA, HOME_AFTER_DEACTIVATE)
  215. disable_X(); disable_Y();
  216. #endif
  217. #endif
  218. if (p) safe_delay(
  219. #if DELAY_BEFORE_PROBING > 25
  220. DELAY_BEFORE_PROBING
  221. #else
  222. 25
  223. #endif
  224. );
  225. }
  226. #endif // QUIET_PROBING
  227. /**
  228. * Raise Z to a minimum height to make room for a probe to move
  229. */
  230. inline void do_probe_raise(const float z_raise) {
  231. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("do_probe_raise(", z_raise, ")");
  232. float z_dest = z_raise;
  233. if (probe_offset.z < 0) z_dest -= probe_offset.z;
  234. NOMORE(z_dest, Z_MAX_POS);
  235. if (z_dest > current_position.z)
  236. do_blocking_move_to_z(z_dest);
  237. }
  238. FORCE_INLINE void probe_specific_action(const bool deploy) {
  239. #if ENABLED(PAUSE_BEFORE_DEPLOY_STOW)
  240. do {
  241. #if ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED)
  242. if (deploy == (READ(Z_MIN_PROBE_PIN) == Z_MIN_PROBE_ENDSTOP_INVERTING)) break;
  243. #endif
  244. BUZZ(100, 659);
  245. BUZZ(100, 698);
  246. PGM_P const ds_str = deploy ? GET_TEXT(MSG_MANUAL_DEPLOY) : GET_TEXT(MSG_MANUAL_STOW);
  247. ui.return_to_status(); // To display the new status message
  248. ui.set_status_P(ds_str, 99);
  249. serialprintPGM(ds_str);
  250. SERIAL_EOL();
  251. KEEPALIVE_STATE(PAUSED_FOR_USER);
  252. wait_for_user = true;
  253. #if ENABLED(HOST_PROMPT_SUPPORT)
  254. host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Stow Probe"), CONTINUE_STR);
  255. #endif
  256. #if ENABLED(EXTENSIBLE_UI)
  257. ExtUI::onUserConfirmRequired_P(PSTR("Stow Probe"));
  258. #endif
  259. while (wait_for_user) idle();
  260. ui.reset_status();
  261. } while(
  262. #if ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED)
  263. true
  264. #else
  265. false
  266. #endif
  267. );
  268. #endif // PAUSE_BEFORE_DEPLOY_STOW
  269. #if ENABLED(SOLENOID_PROBE)
  270. #if HAS_SOLENOID_1
  271. WRITE(SOL1_PIN, deploy);
  272. #endif
  273. #elif ENABLED(Z_PROBE_SLED)
  274. dock_sled(!deploy);
  275. #elif HAS_Z_SERVO_PROBE
  276. #if DISABLED(BLTOUCH)
  277. MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][deploy ? 0 : 1]);
  278. #elif ENABLED(BLTOUCH_HS_MODE)
  279. // In HIGH SPEED MODE, use the normal retractable probe logic in this code
  280. // i.e. no intermediate STOWs and DEPLOYs in between individual probe actions
  281. if (deploy) bltouch.deploy(); else bltouch.stow();
  282. #endif
  283. #elif EITHER(TOUCH_MI_PROBE, Z_PROBE_ALLEN_KEY)
  284. deploy ? run_deploy_moves_script() : run_stow_moves_script();
  285. #elif ENABLED(RACK_AND_PINION_PROBE)
  286. do_blocking_move_to_x(deploy ? Z_PROBE_DEPLOY_X : Z_PROBE_RETRACT_X);
  287. #elif DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  288. UNUSED(deploy);
  289. #endif
  290. }
  291. // returns false for ok and true for failure
  292. bool set_probe_deployed(const bool deploy) {
  293. if (DEBUGGING(LEVELING)) {
  294. DEBUG_POS("set_probe_deployed", current_position);
  295. DEBUG_ECHOLNPAIR("deploy: ", deploy);
  296. }
  297. if (endstops.z_probe_enabled == deploy) return false;
  298. // Make room for probe to deploy (or stow)
  299. // Fix-mounted probe should only raise for deploy
  300. // unless PAUSE_BEFORE_DEPLOY_STOW is enabled
  301. #if EITHER(FIX_MOUNTED_PROBE, NOZZLE_AS_PROBE) && DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  302. const bool deploy_stow_condition = deploy;
  303. #else
  304. constexpr bool deploy_stow_condition = true;
  305. #endif
  306. // For beds that fall when Z is powered off only raise for trusted Z
  307. #if ENABLED(UNKNOWN_Z_NO_RAISE)
  308. const bool unknown_condition = TEST(axis_known_position, Z_AXIS);
  309. #else
  310. constexpr float unknown_condition = true;
  311. #endif
  312. if (deploy_stow_condition && unknown_condition)
  313. do_probe_raise(_MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE));
  314. #if EITHER(Z_PROBE_SLED, Z_PROBE_ALLEN_KEY)
  315. if (axis_unhomed_error(
  316. #if ENABLED(Z_PROBE_SLED)
  317. _BV(X_AXIS)
  318. #endif
  319. )) {
  320. SERIAL_ERROR_MSG(MSG_STOP_UNHOMED);
  321. stop();
  322. return true;
  323. }
  324. #endif
  325. const xy_pos_t old_xy = current_position;
  326. #if ENABLED(PROBE_TRIGGERED_WHEN_STOWED_TEST)
  327. #if HAS_CUSTOM_PROBE_PIN
  328. #define PROBE_STOWED() (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
  329. #else
  330. #define PROBE_STOWED() (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
  331. #endif
  332. #endif
  333. #ifdef PROBE_STOWED
  334. // Only deploy/stow if needed
  335. if (PROBE_STOWED() == deploy) {
  336. if (!deploy) endstops.enable_z_probe(false); // Switch off triggered when stowed probes early
  337. // otherwise an Allen-Key probe can't be stowed.
  338. probe_specific_action(deploy);
  339. }
  340. if (PROBE_STOWED() == deploy) { // Unchanged after deploy/stow action?
  341. if (IsRunning()) {
  342. SERIAL_ERROR_MSG("Z-Probe failed");
  343. LCD_ALERTMESSAGEPGM_P(PSTR("Err: ZPROBE"));
  344. }
  345. stop();
  346. return true;
  347. }
  348. #else
  349. probe_specific_action(deploy);
  350. #endif
  351. do_blocking_move_to(old_xy);
  352. endstops.enable_z_probe(deploy);
  353. return false;
  354. }
  355. #ifdef Z_AFTER_PROBING
  356. // After probing move to a preferred Z position
  357. void move_z_after_probing() {
  358. if (current_position.z != Z_AFTER_PROBING) {
  359. do_blocking_move_to_z(Z_AFTER_PROBING);
  360. current_position.z = Z_AFTER_PROBING;
  361. }
  362. }
  363. #endif
  364. /**
  365. * @brief Used by run_z_probe to do a single Z probe move.
  366. *
  367. * @param z Z destination
  368. * @param fr_mm_s Feedrate in mm/s
  369. * @return true to indicate an error
  370. */
  371. #if HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  372. const char msg_wait_for_bed_heating[25] PROGMEM = "Wait for bed heating...\n";
  373. #endif
  374. static bool do_probe_move(const float z, const feedRate_t fr_mm_s) {
  375. if (DEBUGGING(LEVELING)) DEBUG_POS(">>> do_probe_move", current_position);
  376. #if HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  377. // Wait for bed to heat back up between probing points
  378. if (thermalManager.isHeatingBed()) {
  379. serialprintPGM(msg_wait_for_bed_heating);
  380. LCD_MESSAGEPGM(MSG_BED_HEATING);
  381. thermalManager.wait_for_bed();
  382. ui.reset_status();
  383. }
  384. #endif
  385. #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  386. if (bltouch.deploy()) return true; // DEPLOY in LOW SPEED MODE on every probe action
  387. #endif
  388. // Disable stealthChop if used. Enable diag1 pin on driver.
  389. #if ENABLED(SENSORLESS_PROBING)
  390. sensorless_t stealth_states { false };
  391. #if ENABLED(DELTA)
  392. stealth_states.x = tmc_enable_stallguard(stepperX);
  393. stealth_states.y = tmc_enable_stallguard(stepperY);
  394. #endif
  395. stealth_states.z = tmc_enable_stallguard(stepperZ);
  396. endstops.enable(true);
  397. #endif
  398. #if QUIET_PROBING
  399. probing_pause(true);
  400. #endif
  401. // Move down until the probe is triggered
  402. do_blocking_move_to_z(z, fr_mm_s);
  403. // Check to see if the probe was triggered
  404. const bool probe_triggered =
  405. #if BOTH(DELTA, SENSORLESS_PROBING)
  406. endstops.trigger_state() & (_BV(X_MIN) | _BV(Y_MIN) | _BV(Z_MIN))
  407. #else
  408. TEST(endstops.trigger_state(),
  409. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  410. Z_MIN
  411. #else
  412. Z_MIN_PROBE
  413. #endif
  414. )
  415. #endif
  416. ;
  417. #if QUIET_PROBING
  418. probing_pause(false);
  419. #endif
  420. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  421. #if ENABLED(SENSORLESS_PROBING)
  422. endstops.not_homing();
  423. #if ENABLED(DELTA)
  424. tmc_disable_stallguard(stepperX, stealth_states.x);
  425. tmc_disable_stallguard(stepperY, stealth_states.y);
  426. #endif
  427. tmc_disable_stallguard(stepperZ, stealth_states.z);
  428. #endif
  429. #if ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  430. if (probe_triggered && bltouch.stow()) return true; // STOW in LOW SPEED MODE on trigger on every probe action
  431. #endif
  432. // Clear endstop flags
  433. endstops.hit_on_purpose();
  434. // Get Z where the steppers were interrupted
  435. set_current_from_steppers_for_axis(Z_AXIS);
  436. // Tell the planner where we actually are
  437. sync_plan_position();
  438. if (DEBUGGING(LEVELING)) DEBUG_POS("<<< do_probe_move", current_position);
  439. return !probe_triggered;
  440. }
  441. /**
  442. * @brief Probe at the current XY (possibly more than once) to find the bed Z.
  443. *
  444. * @details Used by probe_at_point to get the bed Z height at the current XY.
  445. * Leaves current_position.z at the height where the probe triggered.
  446. *
  447. * @return The Z position of the bed at the current XY or NAN on error.
  448. */
  449. static float run_z_probe() {
  450. if (DEBUGGING(LEVELING)) DEBUG_POS(">>> run_z_probe", current_position);
  451. // Stop the probe before it goes too low to prevent damage.
  452. // If Z isn't known then probe to -10mm.
  453. const float z_probe_low_point = TEST(axis_known_position, Z_AXIS) ? -probe_offset.z + Z_PROBE_LOW_POINT : -10.0;
  454. // Double-probing does a fast probe followed by a slow probe
  455. #if TOTAL_PROBING == 2
  456. // Do a first probe at the fast speed
  457. if (do_probe_move(z_probe_low_point, MMM_TO_MMS(Z_PROBE_SPEED_FAST))) {
  458. if (DEBUGGING(LEVELING)) {
  459. DEBUG_ECHOLNPGM("FAST Probe fail!");
  460. DEBUG_POS("<<< run_z_probe", current_position);
  461. }
  462. return NAN;
  463. }
  464. const float first_probe_z = current_position.z;
  465. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("1st Probe Z:", first_probe_z);
  466. // Raise to give the probe clearance
  467. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_MULTI_PROBE, MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  468. #elif Z_PROBE_SPEED_FAST != Z_PROBE_SPEED_SLOW
  469. // If the nozzle is well over the travel height then
  470. // move down quickly before doing the slow probe
  471. const float z = Z_CLEARANCE_DEPLOY_PROBE + 5.0 + (probe_offset.z < 0 ? -probe_offset.z : 0);
  472. if (current_position.z > z) {
  473. // Probe down fast. If the probe never triggered, raise for probe clearance
  474. if (!do_probe_move(z, MMM_TO_MMS(Z_PROBE_SPEED_FAST)))
  475. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_BETWEEN_PROBES, MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  476. }
  477. #endif
  478. #ifdef EXTRA_PROBING
  479. float probes[TOTAL_PROBING];
  480. #endif
  481. #if TOTAL_PROBING > 2
  482. float probes_total = 0;
  483. for (
  484. #if EXTRA_PROBING
  485. uint8_t p = 0; p < TOTAL_PROBING; p++
  486. #else
  487. uint8_t p = TOTAL_PROBING; p--;
  488. #endif
  489. )
  490. #endif
  491. {
  492. // Probe downward slowly to find the bed
  493. if (do_probe_move(z_probe_low_point, MMM_TO_MMS(Z_PROBE_SPEED_SLOW))) {
  494. if (DEBUGGING(LEVELING)) {
  495. DEBUG_ECHOLNPGM("SLOW Probe fail!");
  496. DEBUG_POS("<<< run_z_probe", current_position);
  497. }
  498. return NAN;
  499. }
  500. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  501. backlash.measure_with_probe();
  502. #endif
  503. const float z = current_position.z;
  504. #if EXTRA_PROBING
  505. // Insert Z measurement into probes[]. Keep it sorted ascending.
  506. for (uint8_t i = 0; i <= p; i++) { // Iterate the saved Zs to insert the new Z
  507. if (i == p || probes[i] > z) { // Last index or new Z is smaller than this Z
  508. for (int8_t m = p; --m >= i;) probes[m + 1] = probes[m]; // Shift items down after the insertion point
  509. probes[i] = z; // Insert the new Z measurement
  510. break; // Only one to insert. Done!
  511. }
  512. }
  513. #elif TOTAL_PROBING > 2
  514. probes_total += z;
  515. #else
  516. UNUSED(z);
  517. #endif
  518. #if TOTAL_PROBING > 2
  519. // Small Z raise after all but the last probe
  520. if (p
  521. #if EXTRA_PROBING
  522. < TOTAL_PROBING - 1
  523. #endif
  524. ) do_blocking_move_to_z(z + Z_CLEARANCE_MULTI_PROBE, MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  525. #endif
  526. }
  527. #if TOTAL_PROBING > 2
  528. #if EXTRA_PROBING
  529. // Take the center value (or average the two middle values) as the median
  530. static constexpr int PHALF = (TOTAL_PROBING - 1) / 2;
  531. const float middle = probes[PHALF],
  532. median = ((TOTAL_PROBING) & 1) ? middle : (middle + probes[PHALF + 1]) * 0.5f;
  533. // Remove values farthest from the median
  534. uint8_t min_avg_idx = 0, max_avg_idx = TOTAL_PROBING - 1;
  535. for (uint8_t i = EXTRA_PROBING; i--;)
  536. if (ABS(probes[max_avg_idx] - median) > ABS(probes[min_avg_idx] - median))
  537. max_avg_idx--; else min_avg_idx++;
  538. // Return the average value of all remaining probes.
  539. for (uint8_t i = min_avg_idx; i <= max_avg_idx; i++)
  540. probes_total += probes[i];
  541. #endif
  542. const float measured_z = probes_total * RECIPROCAL(MULTIPLE_PROBING);
  543. #elif TOTAL_PROBING == 2
  544. const float z2 = current_position.z;
  545. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("2nd Probe Z:", z2, " Discrepancy:", first_probe_z - z2);
  546. // Return a weighted average of the fast and slow probes
  547. const float measured_z = (z2 * 3.0 + first_probe_z * 2.0) * 0.2;
  548. #else
  549. // Return the single probe result
  550. const float measured_z = current_position.z;
  551. #endif
  552. if (DEBUGGING(LEVELING)) DEBUG_POS("<<< run_z_probe", current_position);
  553. return measured_z;
  554. }
  555. /**
  556. * - Move to the given XY
  557. * - Deploy the probe, if not already deployed
  558. * - Probe the bed, get the Z position
  559. * - Depending on the 'stow' flag
  560. * - Stow the probe, or
  561. * - Raise to the BETWEEN height
  562. * - Return the probed Z position
  563. */
  564. float probe_at_point(const float &rx, const float &ry, const ProbePtRaise raise_after/*=PROBE_PT_NONE*/, const uint8_t verbose_level/*=0*/, const bool probe_relative/*=true*/) {
  565. if (DEBUGGING(LEVELING)) {
  566. DEBUG_ECHOLNPAIR(
  567. ">>> probe_at_point(", LOGICAL_X_POSITION(rx), ", ", LOGICAL_Y_POSITION(ry),
  568. ", ", raise_after == PROBE_PT_RAISE ? "raise" : raise_after == PROBE_PT_STOW ? "stow" : "none",
  569. ", ", int(verbose_level),
  570. ", ", probe_relative ? "probe" : "nozzle", "_relative)"
  571. );
  572. DEBUG_POS("", current_position);
  573. }
  574. // TODO: Adapt for SCARA, where the offset rotates
  575. xyz_pos_t npos = { rx, ry };
  576. if (probe_relative) {
  577. if (!position_is_reachable_by_probe(npos)) return NAN; // The given position is in terms of the probe
  578. npos -= probe_offset_xy; // Get the nozzle position
  579. }
  580. else if (!position_is_reachable(npos)) return NAN; // The given position is in terms of the nozzle
  581. npos.z =
  582. #if ENABLED(DELTA)
  583. // Move below clip height or xy move will be aborted by do_blocking_move_to
  584. _MIN(current_position.z, delta_clip_start_height)
  585. #else
  586. current_position.z
  587. #endif
  588. ;
  589. const float old_feedrate_mm_s = feedrate_mm_s;
  590. feedrate_mm_s = XY_PROBE_FEEDRATE_MM_S;
  591. // Move the probe to the starting XYZ
  592. do_blocking_move_to(npos);
  593. float measured_z = NAN;
  594. if (!DEPLOY_PROBE()) {
  595. measured_z = run_z_probe() + probe_offset.z;
  596. const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
  597. if (big_raise || raise_after == PROBE_PT_RAISE)
  598. do_blocking_move_to_z(current_position.z + (big_raise ? 25 : Z_CLEARANCE_BETWEEN_PROBES), MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  599. else if (raise_after == PROBE_PT_STOW)
  600. if (STOW_PROBE()) measured_z = NAN;
  601. }
  602. if (verbose_level > 2) {
  603. SERIAL_ECHOPAIR_F("Bed X: ", LOGICAL_X_POSITION(rx), 3);
  604. SERIAL_ECHOPAIR_F(" Y: ", LOGICAL_Y_POSITION(ry), 3);
  605. SERIAL_ECHOLNPAIR_F(" Z: ", measured_z, 3);
  606. }
  607. feedrate_mm_s = old_feedrate_mm_s;
  608. if (isnan(measured_z)) {
  609. STOW_PROBE();
  610. LCD_MESSAGEPGM(MSG_LCD_PROBING_FAILED);
  611. SERIAL_ERROR_MSG(MSG_ERR_PROBING_FAILED);
  612. }
  613. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< probe_at_point");
  614. return measured_z;
  615. }
  616. #if HAS_Z_SERVO_PROBE
  617. void servo_probe_init() {
  618. /**
  619. * Set position of Z Servo Endstop
  620. *
  621. * The servo might be deployed and positioned too low to stow
  622. * when starting up the machine or rebooting the board.
  623. * There's no way to know where the nozzle is positioned until
  624. * homing has been done - no homing with z-probe without init!
  625. *
  626. */
  627. STOW_Z_SERVO();
  628. }
  629. #endif // HAS_Z_SERVO_PROBE
  630. #endif // HAS_BED_PROBE