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

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