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

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