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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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"
  35. #if HAS_LEVELING
  36. #include "../feature/bedlevel/bedlevel.h"
  37. #endif
  38. #if ENABLED(DELTA)
  39. #include "../module/delta.h"
  40. #endif
  41. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  42. #include "planner.h"
  43. #endif
  44. float zprobe_zoffset; // Initialized by settings.load()
  45. #if HAS_Z_SERVO_PROBE
  46. #include "../module/servo.h"
  47. #endif
  48. #if ENABLED(SENSORLESS_PROBING)
  49. #include "stepper.h"
  50. #include "../feature/tmc_util.h"
  51. #endif
  52. #if ENABLED(Z_PROBE_SLED)
  53. #ifndef SLED_DOCKING_OFFSET
  54. #define SLED_DOCKING_OFFSET 0
  55. #endif
  56. /**
  57. * Method to dock/undock a sled designed by Charles Bell.
  58. *
  59. * stow[in] If false, move to MAX_X and engage the solenoid
  60. * If true, move to MAX_X and release the solenoid
  61. */
  62. static void dock_sled(bool stow) {
  63. #if ENABLED(DEBUG_LEVELING_FEATURE)
  64. if (DEBUGGING(LEVELING)) {
  65. SERIAL_ECHOPAIR("dock_sled(", stow);
  66. SERIAL_CHAR(')');
  67. SERIAL_EOL();
  68. }
  69. #endif
  70. // Dock sled a bit closer to ensure proper capturing
  71. do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET - ((stow) ? 1 : 0));
  72. #if HAS_SOLENOID_1 && DISABLED(EXT_SOLENOID)
  73. WRITE(SOL1_PIN, !stow); // switch solenoid
  74. #endif
  75. }
  76. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  77. void run_deploy_moves_script() {
  78. #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)
  79. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_X
  80. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_X current_position[X_AXIS]
  81. #endif
  82. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_Y
  83. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Y current_position[Y_AXIS]
  84. #endif
  85. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_Z
  86. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_Z current_position[Z_AXIS]
  87. #endif
  88. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE
  89. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE 0.0
  90. #endif
  91. 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 };
  92. do_blocking_move_to(deploy_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE));
  93. #endif
  94. #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)
  95. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_X
  96. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_X current_position[X_AXIS]
  97. #endif
  98. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_Y
  99. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Y current_position[Y_AXIS]
  100. #endif
  101. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_Z
  102. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_Z current_position[Z_AXIS]
  103. #endif
  104. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE
  105. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE 0.0
  106. #endif
  107. 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 };
  108. do_blocking_move_to(deploy_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE));
  109. #endif
  110. #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)
  111. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_X
  112. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_X current_position[X_AXIS]
  113. #endif
  114. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_Y
  115. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Y current_position[Y_AXIS]
  116. #endif
  117. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_Z
  118. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_Z current_position[Z_AXIS]
  119. #endif
  120. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE
  121. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE 0.0
  122. #endif
  123. 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 };
  124. do_blocking_move_to(deploy_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE));
  125. #endif
  126. #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)
  127. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_X
  128. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_X current_position[X_AXIS]
  129. #endif
  130. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_Y
  131. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_Y current_position[Y_AXIS]
  132. #endif
  133. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_Z
  134. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_Z current_position[Z_AXIS]
  135. #endif
  136. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE
  137. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE 0.0
  138. #endif
  139. 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 };
  140. do_blocking_move_to(deploy_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE));
  141. #endif
  142. #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)
  143. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_X
  144. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_X current_position[X_AXIS]
  145. #endif
  146. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_Y
  147. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_Y current_position[Y_AXIS]
  148. #endif
  149. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_Z
  150. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_Z current_position[Z_AXIS]
  151. #endif
  152. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE
  153. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE 0.0
  154. #endif
  155. 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 };
  156. do_blocking_move_to(deploy_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE));
  157. #endif
  158. }
  159. void run_stow_moves_script() {
  160. #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)
  161. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_X
  162. #define Z_PROBE_ALLEN_KEY_STOW_1_X current_position[X_AXIS]
  163. #endif
  164. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_Y
  165. #define Z_PROBE_ALLEN_KEY_STOW_1_Y current_position[Y_AXIS]
  166. #endif
  167. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_Z
  168. #define Z_PROBE_ALLEN_KEY_STOW_1_Z current_position[Z_AXIS]
  169. #endif
  170. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE
  171. #define Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE 0.0
  172. #endif
  173. 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 };
  174. do_blocking_move_to(stow_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE));
  175. #endif
  176. #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)
  177. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_X
  178. #define Z_PROBE_ALLEN_KEY_STOW_2_X current_position[X_AXIS]
  179. #endif
  180. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_Y
  181. #define Z_PROBE_ALLEN_KEY_STOW_2_Y current_position[Y_AXIS]
  182. #endif
  183. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_Z
  184. #define Z_PROBE_ALLEN_KEY_STOW_2_Z current_position[Z_AXIS]
  185. #endif
  186. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE
  187. #define Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE 0.0
  188. #endif
  189. 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 };
  190. do_blocking_move_to(stow_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE));
  191. #endif
  192. #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)
  193. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_X
  194. #define Z_PROBE_ALLEN_KEY_STOW_3_X current_position[X_AXIS]
  195. #endif
  196. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_Y
  197. #define Z_PROBE_ALLEN_KEY_STOW_3_Y current_position[Y_AXIS]
  198. #endif
  199. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_Z
  200. #define Z_PROBE_ALLEN_KEY_STOW_3_Z current_position[Z_AXIS]
  201. #endif
  202. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE
  203. #define Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE 0.0
  204. #endif
  205. 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 };
  206. do_blocking_move_to(stow_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE));
  207. #endif
  208. #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)
  209. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_X
  210. #define Z_PROBE_ALLEN_KEY_STOW_4_X current_position[X_AXIS]
  211. #endif
  212. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_Y
  213. #define Z_PROBE_ALLEN_KEY_STOW_4_Y current_position[Y_AXIS]
  214. #endif
  215. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_Z
  216. #define Z_PROBE_ALLEN_KEY_STOW_4_Z current_position[Z_AXIS]
  217. #endif
  218. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE
  219. #define Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE 0.0
  220. #endif
  221. 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 };
  222. do_blocking_move_to(stow_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE));
  223. #endif
  224. #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)
  225. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_X
  226. #define Z_PROBE_ALLEN_KEY_STOW_5_X current_position[X_AXIS]
  227. #endif
  228. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_Y
  229. #define Z_PROBE_ALLEN_KEY_STOW_5_Y current_position[Y_AXIS]
  230. #endif
  231. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_Z
  232. #define Z_PROBE_ALLEN_KEY_STOW_5_Z current_position[Z_AXIS]
  233. #endif
  234. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE
  235. #define Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE 0.0
  236. #endif
  237. 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 };
  238. do_blocking_move_to(stow_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE));
  239. #endif
  240. }
  241. #endif // Z_PROBE_ALLEN_KEY
  242. #if ENABLED(PROBING_FANS_OFF)
  243. void fans_pause(const bool p) {
  244. if (p != fans_paused) {
  245. fans_paused = p;
  246. if (p)
  247. for (uint8_t x = 0; x < FAN_COUNT; x++) {
  248. paused_fan_speed[x] = fan_speed[x];
  249. fan_speed[x] = 0;
  250. }
  251. else
  252. for (uint8_t x = 0; x < FAN_COUNT; x++)
  253. fan_speed[x] = paused_fan_speed[x];
  254. }
  255. }
  256. #endif // PROBING_FANS_OFF
  257. #if QUIET_PROBING
  258. void probing_pause(const bool p) {
  259. #if ENABLED(PROBING_HEATERS_OFF)
  260. thermalManager.pause(p);
  261. #endif
  262. #if ENABLED(PROBING_FANS_OFF)
  263. fans_pause(p);
  264. #endif
  265. #if ENABLED(PROBING_STEPPERS_OFF)
  266. disable_e_steppers();
  267. #if DISABLED(DELTA)
  268. disable_X(); disable_Y();
  269. #endif
  270. #endif
  271. if (p) safe_delay(
  272. #if DELAY_BEFORE_PROBING > 25
  273. DELAY_BEFORE_PROBING
  274. #else
  275. 25
  276. #endif
  277. );
  278. }
  279. #endif // QUIET_PROBING
  280. #if ENABLED(BLTOUCH)
  281. void bltouch_command(const int angle) {
  282. MOVE_SERVO(Z_PROBE_SERVO_NR, angle); // Give the BL-Touch the command and wait
  283. safe_delay(BLTOUCH_DELAY);
  284. }
  285. bool set_bltouch_deployed(const bool deploy) {
  286. if (deploy && TEST_BLTOUCH()) { // If BL-Touch says it's triggered
  287. bltouch_command(BLTOUCH_RESET); // try to reset it.
  288. bltouch_command(BLTOUCH_DEPLOY); // Also needs to deploy and stow to
  289. bltouch_command(BLTOUCH_STOW); // clear the triggered condition.
  290. safe_delay(1500); // Wait for internal self-test to complete.
  291. // (Measured completion time was 0.65 seconds
  292. // after reset, deploy, and stow sequence)
  293. if (TEST_BLTOUCH()) { // If it still claims to be triggered...
  294. SERIAL_ERROR_START();
  295. SERIAL_ERRORLNPGM(MSG_STOP_BLTOUCH);
  296. stop(); // punt!
  297. return true;
  298. }
  299. }
  300. bltouch_command(deploy ? BLTOUCH_DEPLOY : BLTOUCH_STOW);
  301. #if ENABLED(DEBUG_LEVELING_FEATURE)
  302. if (DEBUGGING(LEVELING)) {
  303. SERIAL_ECHOPAIR("set_bltouch_deployed(", deploy);
  304. SERIAL_CHAR(')');
  305. SERIAL_EOL();
  306. }
  307. #endif
  308. return false;
  309. }
  310. #endif // BLTOUCH
  311. /**
  312. * Raise Z to a minimum height to make room for a probe to move
  313. */
  314. inline void do_probe_raise(const float z_raise) {
  315. #if ENABLED(DEBUG_LEVELING_FEATURE)
  316. if (DEBUGGING(LEVELING)) {
  317. SERIAL_ECHOPAIR("do_probe_raise(", z_raise);
  318. SERIAL_CHAR(')');
  319. SERIAL_EOL();
  320. }
  321. #endif
  322. float z_dest = z_raise;
  323. if (zprobe_zoffset < 0) z_dest -= zprobe_zoffset;
  324. NOMORE(z_dest, Z_MAX_POS);
  325. if (z_dest > current_position[Z_AXIS])
  326. do_blocking_move_to_z(z_dest);
  327. }
  328. FORCE_INLINE void probe_specific_action(const bool deploy) {
  329. #if ENABLED(PAUSE_BEFORE_DEPLOY_STOW)
  330. BUZZ(100, 659);
  331. BUZZ(100, 698);
  332. PGM_P const ds_str = deploy ? PSTR(MSG_MANUAL_DEPLOY) : PSTR(MSG_MANUAL_STOW);
  333. ui.return_to_status(); // To display the new status message
  334. ui.set_status_P(ds_str, 99);
  335. serialprintPGM(ds_str);
  336. SERIAL_EOL();
  337. KEEPALIVE_STATE(PAUSED_FOR_USER);
  338. wait_for_user = true;
  339. while (wait_for_user) idle();
  340. ui.reset_status();
  341. KEEPALIVE_STATE(IN_HANDLER);
  342. #endif // PAUSE_BEFORE_DEPLOY_STOW
  343. #if ENABLED(SOLENOID_PROBE)
  344. #if HAS_SOLENOID_1
  345. WRITE(SOL1_PIN, deploy);
  346. #endif
  347. #elif ENABLED(Z_PROBE_SLED)
  348. dock_sled(!deploy);
  349. #elif HAS_Z_SERVO_PROBE && DISABLED(BLTOUCH)
  350. MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][deploy ? 0 : 1]);
  351. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  352. deploy ? run_deploy_moves_script() : run_stow_moves_script();
  353. #elif ENABLED(RACK_AND_PINION_PROBE)
  354. do_blocking_move_to_x(deploy ? Z_PROBE_DEPLOY_X : Z_PROBE_RETRACT_X);
  355. #endif
  356. }
  357. // returns false for ok and true for failure
  358. bool set_probe_deployed(const bool deploy) {
  359. #if ENABLED(DEBUG_LEVELING_FEATURE)
  360. if (DEBUGGING(LEVELING)) {
  361. DEBUG_POS("set_probe_deployed", current_position);
  362. SERIAL_ECHOLNPAIR("deploy: ", deploy);
  363. }
  364. #endif
  365. if (endstops.z_probe_enabled == deploy) return false;
  366. // Make room for probe to deploy (or stow)
  367. // Fix-mounted probe should only raise for deploy
  368. // unless PAUSE_BEFORE_DEPLOY_STOW is enabled
  369. #if ENABLED(FIX_MOUNTED_PROBE) && DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  370. const bool deploy_stow_condition = deploy;
  371. #else
  372. constexpr bool deploy_stow_condition = true;
  373. #endif
  374. // For beds that fall when Z is powered off only raise for trusted Z
  375. #if ENABLED(UNKNOWN_Z_NO_RAISE)
  376. const bool unknown_condition = TEST(axis_known_position, Z_AXIS);
  377. #else
  378. constexpr float unknown_condition = true;
  379. #endif
  380. if (deploy_stow_condition && unknown_condition)
  381. do_probe_raise(MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE));
  382. #if ENABLED(Z_PROBE_SLED) || ENABLED(Z_PROBE_ALLEN_KEY)
  383. #if ENABLED(Z_PROBE_SLED)
  384. #define _AUE_ARGS true, false, false
  385. #else
  386. #define _AUE_ARGS
  387. #endif
  388. if (axis_unhomed_error(_AUE_ARGS)) {
  389. SERIAL_ERROR_START();
  390. SERIAL_ERRORLNPGM(MSG_STOP_UNHOMED);
  391. stop();
  392. return true;
  393. }
  394. #endif
  395. const float oldXpos = current_position[X_AXIS],
  396. oldYpos = current_position[Y_AXIS];
  397. #if ENABLED(PROBE_TRIGGERED_WHEN_STOWED_TEST)
  398. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  399. #define PROBE_STOWED() (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
  400. #else
  401. #define PROBE_STOWED() (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
  402. #endif
  403. #endif
  404. #ifdef PROBE_STOWED
  405. // Only deploy/stow if needed
  406. if (PROBE_STOWED() == deploy) {
  407. if (!deploy) endstops.enable_z_probe(false); // Switch off triggered when stowed probes early
  408. // otherwise an Allen-Key probe can't be stowed.
  409. probe_specific_action(deploy);
  410. }
  411. if (PROBE_STOWED() == deploy) { // Unchanged after deploy/stow action?
  412. if (IsRunning()) {
  413. SERIAL_ERROR_START();
  414. SERIAL_ERRORLNPGM("Z-Probe failed");
  415. LCD_ALERTMESSAGEPGM("Err: ZPROBE");
  416. }
  417. stop();
  418. return true;
  419. }
  420. #else
  421. probe_specific_action(deploy);
  422. #endif
  423. do_blocking_move_to(oldXpos, oldYpos, current_position[Z_AXIS]); // return to position before deploy
  424. endstops.enable_z_probe(deploy);
  425. return false;
  426. }
  427. #ifdef Z_AFTER_PROBING
  428. // After probing move to a preferred Z position
  429. void move_z_after_probing() {
  430. if (current_position[Z_AXIS] != Z_AFTER_PROBING) {
  431. do_blocking_move_to_z(Z_AFTER_PROBING);
  432. current_position[Z_AXIS] = Z_AFTER_PROBING;
  433. }
  434. }
  435. #endif
  436. /**
  437. * @brief Used by run_z_probe to do a single Z probe move.
  438. *
  439. * @param z Z destination
  440. * @param fr_mm_s Feedrate in mm/s
  441. * @return true to indicate an error
  442. */
  443. #if HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  444. const char msg_wait_for_bed_heating[25] PROGMEM = "Wait for bed heating...\n";
  445. #endif
  446. static bool do_probe_move(const float z, const float fr_mm_s) {
  447. #if ENABLED(DEBUG_LEVELING_FEATURE)
  448. if (DEBUGGING(LEVELING)) DEBUG_POS(">>> do_probe_move", current_position);
  449. #endif
  450. #if HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  451. // Wait for bed to heat back up between probing points
  452. if (thermalManager.isHeatingBed()) {
  453. serialprintPGM(msg_wait_for_bed_heating);
  454. LCD_MESSAGEPGM(MSG_BED_HEATING);
  455. while (thermalManager.isHeatingBed()) safe_delay(200);
  456. ui.reset_status();
  457. }
  458. #endif
  459. // Deploy BLTouch at the start of any probe
  460. #if ENABLED(BLTOUCH)
  461. if (set_bltouch_deployed(true)) return true;
  462. #endif
  463. // Disable stealthChop if used. Enable diag1 pin on driver.
  464. #if ENABLED(SENSORLESS_PROBING)
  465. #if ENABLED(DELTA)
  466. tmc_stallguard(stepperX);
  467. tmc_stallguard(stepperY);
  468. #endif
  469. tmc_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_stallguard(stepperX, false);
  499. tmc_stallguard(stepperY, false);
  500. #endif
  501. tmc_stallguard(stepperZ, false);
  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 MULTIPLE_PROBING > 2
  574. probes_total += current_position[Z_AXIS];
  575. if (p > 1) do_blocking_move_to_z(current_position[Z_AXIS] + Z_CLEARANCE_MULTI_PROBE, MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  576. }
  577. #endif
  578. #if MULTIPLE_PROBING > 2
  579. // Return the average value of all probes
  580. const float measured_z = probes_total * (1.0f / (MULTIPLE_PROBING));
  581. #elif MULTIPLE_PROBING == 2
  582. const float z2 = current_position[Z_AXIS];
  583. #if ENABLED(DEBUG_LEVELING_FEATURE)
  584. if (DEBUGGING(LEVELING)) {
  585. SERIAL_ECHOPAIR("2nd Probe Z:", z2);
  586. SERIAL_ECHOLNPAIR(" Discrepancy:", first_probe_z - z2);
  587. }
  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_ECHOPAIR(">>> probe_pt(", LOGICAL_X_POSITION(rx));
  613. SERIAL_ECHOPAIR(", ", LOGICAL_Y_POSITION(ry));
  614. SERIAL_ECHOPAIR(", ", raise_after == PROBE_PT_RAISE ? "raise" : raise_after == PROBE_PT_STOW ? "stow" : "none");
  615. SERIAL_ECHOPAIR(", ", int(verbose_level));
  616. SERIAL_ECHOPAIR(", ", probe_relative ? "probe" : "nozzle");
  617. SERIAL_ECHOLNPGM("_relative)");
  618. DEBUG_POS("", current_position);
  619. }
  620. #endif
  621. // TODO: Adapt for SCARA, where the offset rotates
  622. float nx = rx, ny = ry;
  623. if (probe_relative) {
  624. if (!position_is_reachable_by_probe(rx, ry)) return NAN; // The given position is in terms of the probe
  625. nx -= (X_PROBE_OFFSET_FROM_EXTRUDER); // Get the nozzle position
  626. ny -= (Y_PROBE_OFFSET_FROM_EXTRUDER);
  627. }
  628. else if (!position_is_reachable(nx, ny)) return NAN; // The given position is in terms of the nozzle
  629. const float nz =
  630. #if ENABLED(DELTA)
  631. // Move below clip height or xy move will be aborted by do_blocking_move_to
  632. MIN(current_position[Z_AXIS], delta_clip_start_height)
  633. #else
  634. current_position[Z_AXIS]
  635. #endif
  636. ;
  637. const float old_feedrate_mm_s = feedrate_mm_s;
  638. feedrate_mm_s = XY_PROBE_FEEDRATE_MM_S;
  639. // Move the probe to the starting XYZ
  640. do_blocking_move_to(nx, ny, nz);
  641. float measured_z = NAN;
  642. if (!DEPLOY_PROBE()) {
  643. measured_z = run_z_probe() + zprobe_zoffset;
  644. const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
  645. if (big_raise || raise_after == PROBE_PT_RAISE)
  646. do_blocking_move_to_z(current_position[Z_AXIS] + (big_raise ? 25 : Z_CLEARANCE_BETWEEN_PROBES), MMM_TO_MMS(Z_PROBE_SPEED_FAST));
  647. else if (raise_after == PROBE_PT_STOW)
  648. if (STOW_PROBE()) measured_z = NAN;
  649. }
  650. if (verbose_level > 2) {
  651. SERIAL_PROTOCOLPGM("Bed X: ");
  652. SERIAL_PROTOCOL_F(LOGICAL_X_POSITION(rx), 3);
  653. SERIAL_PROTOCOLPGM(" Y: ");
  654. SERIAL_PROTOCOL_F(LOGICAL_Y_POSITION(ry), 3);
  655. SERIAL_PROTOCOLPGM(" Z: ");
  656. SERIAL_PROTOCOL_F(measured_z, 3);
  657. SERIAL_EOL();
  658. }
  659. feedrate_mm_s = old_feedrate_mm_s;
  660. if (isnan(measured_z)) {
  661. STOW_PROBE();
  662. LCD_MESSAGEPGM(MSG_ERR_PROBING_FAILED);
  663. SERIAL_ERROR_START();
  664. SERIAL_ERRORLNPGM(MSG_ERR_PROBING_FAILED);
  665. }
  666. #if ENABLED(DEBUG_LEVELING_FEATURE)
  667. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< probe_pt");
  668. #endif
  669. return measured_z;
  670. }
  671. #if HAS_Z_SERVO_PROBE
  672. void servo_probe_init() {
  673. /**
  674. * Set position of Z Servo Endstop
  675. *
  676. * The servo might be deployed and positioned too low to stow
  677. * when starting up the machine or rebooting the board.
  678. * There's no way to know where the nozzle is positioned until
  679. * homing has been done - no homing with z-probe without init!
  680. *
  681. */
  682. STOW_Z_SERVO();
  683. }
  684. #endif // HAS_Z_SERVO_PROBE
  685. #endif // HAS_BED_PROBE