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

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