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

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