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

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