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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * module/probe.cpp
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if HAS_BED_PROBE
  27. #include "probe.h"
  28. #include "../libs/buzzer.h"
  29. #include "motion.h"
  30. #include "temperature.h"
  31. #include "endstops.h"
  32. #include "../gcode/gcode.h"
  33. #include "../lcd/marlinui.h"
  34. #include "../MarlinCore.h" // for stop(), disable_e_steppers(), wait_for_user_response()
  35. #if HAS_LEVELING
  36. #include "../feature/bedlevel/bedlevel.h"
  37. #endif
  38. #if ENABLED(DELTA)
  39. #include "delta.h"
  40. #endif
  41. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  42. #include "planner.h"
  43. #endif
  44. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  45. #include "../feature/backlash.h"
  46. #endif
  47. #if ENABLED(BLTOUCH)
  48. #include "../feature/bltouch.h"
  49. #endif
  50. #if ENABLED(HOST_PROMPT_SUPPORT)
  51. #include "../feature/host_actions.h" // for PROMPT_USER_CONTINUE
  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 HAS_QUIET_PROBING
  61. #include "stepper/indirection.h"
  62. #endif
  63. #if ENABLED(EXTENSIBLE_UI)
  64. #include "../lcd/extui/ui_api.h"
  65. #endif
  66. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  67. #include "../core/debug_out.h"
  68. Probe probe;
  69. xyz_pos_t Probe::offset; // Initialized by settings.load()
  70. #if HAS_PROBE_XY_OFFSET
  71. const xy_pos_t &Probe::offset_xy = Probe::offset;
  72. #endif
  73. #if ENABLED(Z_PROBE_SLED)
  74. #ifndef SLED_DOCKING_OFFSET
  75. #define SLED_DOCKING_OFFSET 0
  76. #endif
  77. /**
  78. * Method to dock/undock a sled designed by Charles Bell.
  79. *
  80. * stow[in] If false, move to MAX_X and engage the solenoid
  81. * If true, move to MAX_X and release the solenoid
  82. */
  83. static void dock_sled(const bool stow) {
  84. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("dock_sled(", stow, ")");
  85. // Dock sled a bit closer to ensure proper capturing
  86. do_blocking_move_to_x(X_MAX_POS + SLED_DOCKING_OFFSET - ((stow) ? 1 : 0));
  87. #if HAS_SOLENOID_1 && DISABLED(EXT_SOLENOID)
  88. WRITE(SOL1_PIN, !stow); // switch solenoid
  89. #endif
  90. }
  91. #elif ENABLED(TOUCH_MI_PROBE)
  92. // Move to the magnet to unlock the probe
  93. inline void run_deploy_moves_script() {
  94. #ifndef TOUCH_MI_DEPLOY_XPOS
  95. #define TOUCH_MI_DEPLOY_XPOS X_MIN_POS
  96. #elif TOUCH_MI_DEPLOY_XPOS > X_MAX_BED
  97. TemporaryGlobalEndstopsState unlock_x(false);
  98. #endif
  99. #if TOUCH_MI_DEPLOY_YPOS > Y_MAX_BED
  100. TemporaryGlobalEndstopsState unlock_y(false);
  101. #endif
  102. #if ENABLED(TOUCH_MI_MANUAL_DEPLOY)
  103. const screenFunc_t prev_screen = ui.currentScreen;
  104. LCD_MESSAGEPGM(MSG_MANUAL_DEPLOY_TOUCHMI);
  105. ui.return_to_status();
  106. TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Deploy TouchMI"), CONTINUE_STR));
  107. wait_for_user_response();
  108. ui.reset_status();
  109. ui.goto_screen(prev_screen);
  110. #elif defined(TOUCH_MI_DEPLOY_XPOS) && defined(TOUCH_MI_DEPLOY_YPOS)
  111. do_blocking_move_to_xy(TOUCH_MI_DEPLOY_XPOS, TOUCH_MI_DEPLOY_YPOS);
  112. #elif defined(TOUCH_MI_DEPLOY_XPOS)
  113. do_blocking_move_to_x(TOUCH_MI_DEPLOY_XPOS);
  114. #elif defined(TOUCH_MI_DEPLOY_YPOS)
  115. do_blocking_move_to_y(TOUCH_MI_DEPLOY_YPOS);
  116. #endif
  117. }
  118. // Move down to the bed to stow the probe
  119. inline void run_stow_moves_script() {
  120. const xyz_pos_t oldpos = current_position;
  121. endstops.enable_z_probe(false);
  122. do_blocking_move_to_z(TOUCH_MI_RETRACT_Z, homing_feedrate(Z_AXIS));
  123. do_blocking_move_to(oldpos, homing_feedrate(Z_AXIS));
  124. }
  125. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  126. inline void run_deploy_moves_script() {
  127. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_1
  128. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE
  129. #define Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE 0.0
  130. #endif
  131. constexpr xyz_pos_t deploy_1 = Z_PROBE_ALLEN_KEY_DEPLOY_1;
  132. do_blocking_move_to(deploy_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_1_FEEDRATE));
  133. #endif
  134. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_2
  135. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE
  136. #define Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE 0.0
  137. #endif
  138. constexpr xyz_pos_t deploy_2 = Z_PROBE_ALLEN_KEY_DEPLOY_2;
  139. do_blocking_move_to(deploy_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE));
  140. #endif
  141. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_3
  142. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE
  143. #define Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE 0.0
  144. #endif
  145. constexpr xyz_pos_t deploy_3 = Z_PROBE_ALLEN_KEY_DEPLOY_3;
  146. do_blocking_move_to(deploy_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE));
  147. #endif
  148. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_4
  149. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE
  150. #define Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE 0.0
  151. #endif
  152. constexpr xyz_pos_t deploy_4 = Z_PROBE_ALLEN_KEY_DEPLOY_4;
  153. do_blocking_move_to(deploy_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_4_FEEDRATE));
  154. #endif
  155. #ifdef Z_PROBE_ALLEN_KEY_DEPLOY_5
  156. #ifndef Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE
  157. #define Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE 0.0
  158. #endif
  159. constexpr xyz_pos_t deploy_5 = Z_PROBE_ALLEN_KEY_DEPLOY_5;
  160. do_blocking_move_to(deploy_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_DEPLOY_5_FEEDRATE));
  161. #endif
  162. }
  163. inline void run_stow_moves_script() {
  164. #ifdef Z_PROBE_ALLEN_KEY_STOW_1
  165. #ifndef Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE
  166. #define Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE 0.0
  167. #endif
  168. constexpr xyz_pos_t stow_1 = Z_PROBE_ALLEN_KEY_STOW_1;
  169. do_blocking_move_to(stow_1, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_1_FEEDRATE));
  170. #endif
  171. #ifdef Z_PROBE_ALLEN_KEY_STOW_2
  172. #ifndef Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE
  173. #define Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE 0.0
  174. #endif
  175. constexpr xyz_pos_t stow_2 = Z_PROBE_ALLEN_KEY_STOW_2;
  176. do_blocking_move_to(stow_2, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_2_FEEDRATE));
  177. #endif
  178. #ifdef Z_PROBE_ALLEN_KEY_STOW_3
  179. #ifndef Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE
  180. #define Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE 0.0
  181. #endif
  182. constexpr xyz_pos_t stow_3 = Z_PROBE_ALLEN_KEY_STOW_3;
  183. do_blocking_move_to(stow_3, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_3_FEEDRATE));
  184. #endif
  185. #ifdef Z_PROBE_ALLEN_KEY_STOW_4
  186. #ifndef Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE
  187. #define Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE 0.0
  188. #endif
  189. constexpr xyz_pos_t stow_4 = Z_PROBE_ALLEN_KEY_STOW_4;
  190. do_blocking_move_to(stow_4, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_4_FEEDRATE));
  191. #endif
  192. #ifdef Z_PROBE_ALLEN_KEY_STOW_5
  193. #ifndef Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE
  194. #define Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE 0.0
  195. #endif
  196. constexpr xyz_pos_t stow_5 = Z_PROBE_ALLEN_KEY_STOW_5;
  197. do_blocking_move_to(stow_5, MMM_TO_MMS(Z_PROBE_ALLEN_KEY_STOW_5_FEEDRATE));
  198. #endif
  199. }
  200. #endif // Z_PROBE_ALLEN_KEY
  201. #if HAS_QUIET_PROBING
  202. void Probe::set_probing_paused(const bool p) {
  203. TERN_(PROBING_HEATERS_OFF, thermalManager.pause(p));
  204. TERN_(PROBING_FANS_OFF, thermalManager.set_fans_paused(p));
  205. #if ENABLED(PROBING_STEPPERS_OFF)
  206. disable_e_steppers();
  207. #if NONE(DELTA, HOME_AFTER_DEACTIVATE)
  208. DISABLE_AXIS_X(); DISABLE_AXIS_Y();
  209. #endif
  210. #endif
  211. if (p) safe_delay(25
  212. #if DELAY_BEFORE_PROBING > 25
  213. - 25 + DELAY_BEFORE_PROBING
  214. #endif
  215. );
  216. }
  217. #endif // HAS_QUIET_PROBING
  218. /**
  219. * Raise Z to a minimum height to make room for a probe to move
  220. */
  221. void Probe::do_z_raise(const float z_raise) {
  222. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probe::do_z_raise(", z_raise, ")");
  223. float z_dest = z_raise;
  224. if (offset.z < 0) z_dest -= offset.z;
  225. do_z_clearance(z_dest);
  226. }
  227. FORCE_INLINE void probe_specific_action(const bool deploy) {
  228. #if ENABLED(PAUSE_BEFORE_DEPLOY_STOW)
  229. do {
  230. #if ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED)
  231. if (deploy == PROBE_TRIGGERED()) break;
  232. #endif
  233. BUZZ(100, 659);
  234. BUZZ(100, 698);
  235. PGM_P const ds_str = deploy ? GET_TEXT(MSG_MANUAL_DEPLOY) : GET_TEXT(MSG_MANUAL_STOW);
  236. ui.return_to_status(); // To display the new status message
  237. ui.set_status_P(ds_str, 99);
  238. serialprintPGM(ds_str);
  239. SERIAL_EOL();
  240. TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Stow Probe"), CONTINUE_STR));
  241. TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired_P(PSTR("Stow Probe")));
  242. wait_for_user_response();
  243. ui.reset_status();
  244. } while (ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED));
  245. #endif // PAUSE_BEFORE_DEPLOY_STOW
  246. #if ENABLED(SOLENOID_PROBE)
  247. #if HAS_SOLENOID_1
  248. WRITE(SOL1_PIN, deploy);
  249. #endif
  250. #elif ENABLED(Z_PROBE_SLED)
  251. dock_sled(!deploy);
  252. #elif ENABLED(BLTOUCH)
  253. deploy ? bltouch.deploy() : bltouch.stow();
  254. #elif HAS_Z_SERVO_PROBE
  255. MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][deploy ? 0 : 1]);
  256. #elif EITHER(TOUCH_MI_PROBE, Z_PROBE_ALLEN_KEY)
  257. deploy ? run_deploy_moves_script() : run_stow_moves_script();
  258. #elif ENABLED(RACK_AND_PINION_PROBE)
  259. do_blocking_move_to_x(deploy ? Z_PROBE_DEPLOY_X : Z_PROBE_RETRACT_X);
  260. #elif DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  261. UNUSED(deploy);
  262. #endif
  263. }
  264. #if EITHER(PREHEAT_BEFORE_PROBING, PREHEAT_BEFORE_LEVELING)
  265. /**
  266. * Do preheating as required before leveling or probing
  267. */
  268. void Probe::preheat_for_probing(const uint16_t hotend_temp, const uint16_t bed_temp) {
  269. #if PROBING_NOZZLE_TEMP || LEVELING_NOZZLE_TEMP
  270. #define WAIT_FOR_NOZZLE_HEAT
  271. #endif
  272. #if PROBING_BED_TEMP || LEVELING_BED_TEMP
  273. #define WAIT_FOR_BED_HEAT
  274. #endif
  275. const uint16_t hotendPreheat = TERN0(WAIT_FOR_NOZZLE_HEAT, thermalManager.degHotend(0) < hotend_temp) ? hotend_temp : 0,
  276. bedPreheat = TERN0(WAIT_FOR_BED_HEAT, thermalManager.degBed() < bed_temp) ? bed_temp : 0;
  277. DEBUG_ECHOPGM("Preheating ");
  278. if (hotendPreheat) {
  279. DEBUG_ECHOPAIR("hotend (", hotendPreheat, ") ");
  280. if (bedPreheat) DEBUG_ECHOPGM("and ");
  281. }
  282. if (bedPreheat) DEBUG_ECHOPAIR("bed (", bedPreheat, ") ");
  283. DEBUG_EOL();
  284. TERN_(WAIT_FOR_NOZZLE_HEAT, if (hotendPreheat) thermalManager.setTargetHotend(hotendPreheat, 0));
  285. TERN_(WAIT_FOR_BED_HEAT, if (bedPreheat) thermalManager.setTargetBed(bedPreheat));
  286. TERN_(WAIT_FOR_NOZZLE_HEAT, if (hotendPreheat) thermalManager.wait_for_hotend(0));
  287. TERN_(WAIT_FOR_BED_HEAT, if (bedPreheat) thermalManager.wait_for_bed_heating());
  288. }
  289. #endif
  290. /**
  291. * Attempt to deploy or stow the probe
  292. *
  293. * Return TRUE if the probe could not be deployed/stowed
  294. */
  295. bool Probe::set_deployed(const bool deploy) {
  296. if (DEBUGGING(LEVELING)) {
  297. DEBUG_POS("Probe::set_deployed", current_position);
  298. DEBUG_ECHOLNPAIR("deploy: ", deploy);
  299. }
  300. if (endstops.z_probe_enabled == deploy) return false;
  301. // Make room for probe to deploy (or stow)
  302. // Fix-mounted probe should only raise for deploy
  303. // unless PAUSE_BEFORE_DEPLOY_STOW is enabled
  304. #if EITHER(FIX_MOUNTED_PROBE, NOZZLE_AS_PROBE) && DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  305. const bool z_raise_wanted = deploy;
  306. #else
  307. constexpr bool z_raise_wanted = true;
  308. #endif
  309. // For beds that fall when Z is powered off only raise for trusted Z
  310. #if ENABLED(UNKNOWN_Z_NO_RAISE)
  311. const bool z_is_trusted = axis_is_trusted(Z_AXIS);
  312. #else
  313. constexpr float z_is_trusted = true;
  314. #endif
  315. if (z_is_trusted && z_raise_wanted)
  316. do_z_raise(_MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE));
  317. #if EITHER(Z_PROBE_SLED, Z_PROBE_ALLEN_KEY)
  318. if (homing_needed_error(TERN_(Z_PROBE_SLED, _BV(X_AXIS)))) {
  319. SERIAL_ERROR_MSG(STR_STOP_UNHOMED);
  320. stop();
  321. return true;
  322. }
  323. #endif
  324. const xy_pos_t old_xy = current_position;
  325. #if ENABLED(PROBE_TRIGGERED_WHEN_STOWED_TEST)
  326. // Only deploy/stow if needed
  327. if (PROBE_TRIGGERED() == deploy) {
  328. if (!deploy) endstops.enable_z_probe(false); // Switch off triggered when stowed probes early
  329. // otherwise an Allen-Key probe can't be stowed.
  330. probe_specific_action(deploy);
  331. }
  332. if (PROBE_TRIGGERED() == deploy) { // Unchanged after deploy/stow action?
  333. if (IsRunning()) {
  334. SERIAL_ERROR_MSG("Z-Probe failed");
  335. LCD_ALERTMESSAGEPGM_P(PSTR("Err: ZPROBE"));
  336. }
  337. stop();
  338. return true;
  339. }
  340. #else
  341. probe_specific_action(deploy);
  342. #endif
  343. // If preheating is required before any probing...
  344. TERN_(PREHEAT_BEFORE_PROBING, if (deploy) preheat_for_probing(PROBING_NOZZLE_TEMP, PROBING_BED_TEMP));
  345. do_blocking_move_to(old_xy);
  346. endstops.enable_z_probe(deploy);
  347. return false;
  348. }
  349. /**
  350. * @brief Used by run_z_probe to do a single Z probe move.
  351. *
  352. * @param z Z destination
  353. * @param fr_mm_s Feedrate in mm/s
  354. * @return true to indicate an error
  355. */
  356. /**
  357. * @brief Move down until the probe triggers or the low limit is reached
  358. *
  359. * @details Used by run_z_probe to get each bed Z height measurement.
  360. * Sets current_position.z to the height where the probe triggered
  361. * (according to the Z stepper count). The float Z is propagated
  362. * back to the planner.position to preempt any rounding error.
  363. *
  364. * @return TRUE if the probe failed to trigger.
  365. */
  366. bool Probe::probe_down_to_z(const float z, const feedRate_t fr_mm_s) {
  367. DEBUG_SECTION(log_probe, "Probe::probe_down_to_z", DEBUGGING(LEVELING));
  368. #if BOTH(HAS_HEATED_BED, WAIT_FOR_BED_HEATER)
  369. thermalManager.wait_for_bed_heating();
  370. #endif
  371. if (TERN0(BLTOUCH_SLOW_MODE, bltouch.deploy())) return true; // Deploy in LOW SPEED MODE on every probe action
  372. // Disable stealthChop if used. Enable diag1 pin on driver.
  373. #if ENABLED(SENSORLESS_PROBING)
  374. sensorless_t stealth_states { false };
  375. #if ENABLED(DELTA)
  376. stealth_states.x = tmc_enable_stallguard(stepperX);
  377. stealth_states.y = tmc_enable_stallguard(stepperY);
  378. #endif
  379. stealth_states.z = tmc_enable_stallguard(stepperZ);
  380. endstops.enable(true);
  381. #endif
  382. TERN_(HAS_QUIET_PROBING, set_probing_paused(true));
  383. // Move down until the probe is triggered
  384. do_blocking_move_to_z(z, fr_mm_s);
  385. // Check to see if the probe was triggered
  386. const bool probe_triggered =
  387. #if BOTH(DELTA, SENSORLESS_PROBING)
  388. endstops.trigger_state() & (_BV(X_MIN) | _BV(Y_MIN) | _BV(Z_MIN))
  389. #else
  390. TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE))
  391. #endif
  392. ;
  393. TERN_(HAS_QUIET_PROBING, set_probing_paused(false));
  394. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  395. #if ENABLED(SENSORLESS_PROBING)
  396. endstops.not_homing();
  397. #if ENABLED(DELTA)
  398. tmc_disable_stallguard(stepperX, stealth_states.x);
  399. tmc_disable_stallguard(stepperY, stealth_states.y);
  400. #endif
  401. tmc_disable_stallguard(stepperZ, stealth_states.z);
  402. #endif
  403. if (probe_triggered && TERN0(BLTOUCH_SLOW_MODE, bltouch.stow())) // Stow in LOW SPEED MODE on every trigger
  404. return true;
  405. // Clear endstop flags
  406. endstops.hit_on_purpose();
  407. // Get Z where the steppers were interrupted
  408. set_current_from_steppers_for_axis(Z_AXIS);
  409. // Tell the planner where we actually are
  410. sync_plan_position();
  411. return !probe_triggered;
  412. }
  413. #if ENABLED(PROBE_TARE)
  414. /**
  415. * @brief Init the tare pin
  416. *
  417. * @details Init tare pin to ON state for a strain gauge, otherwise OFF
  418. */
  419. void Probe::tare_init() {
  420. OUT_WRITE(PROBE_TARE_PIN, !PROBE_TARE_STATE);
  421. }
  422. /**
  423. * @brief Tare the Z probe
  424. *
  425. * @details Signal to the probe to tare itself
  426. *
  427. * @return TRUE if the tare cold not be completed
  428. */
  429. bool Probe::tare() {
  430. #if BOTH(PROBE_ACTIVATION_SWITCH, PROBE_TARE_ONLY_WHILE_INACTIVE)
  431. if (endstops.probe_switch_activated()) {
  432. SERIAL_ECHOLNPGM("Cannot tare an active probe");
  433. return true;
  434. }
  435. #endif
  436. SERIAL_ECHOLNPGM("Taring probe");
  437. WRITE(PROBE_TARE_PIN, PROBE_TARE_STATE);
  438. delay(PROBE_TARE_TIME);
  439. WRITE(PROBE_TARE_PIN, !PROBE_TARE_STATE);
  440. delay(PROBE_TARE_DELAY);
  441. endstops.hit_on_purpose();
  442. return false;
  443. }
  444. #endif
  445. /**
  446. * @brief Probe at the current XY (possibly more than once) to find the bed Z.
  447. *
  448. * @details Used by probe_at_point to get the bed Z height at the current XY.
  449. * Leaves current_position.z at the height where the probe triggered.
  450. *
  451. * @return The Z position of the bed at the current XY or NAN on error.
  452. */
  453. float Probe::run_z_probe(const bool sanity_check/*=true*/) {
  454. DEBUG_SECTION(log_probe, "Probe::run_z_probe", DEBUGGING(LEVELING));
  455. auto try_to_probe = [&](PGM_P const plbl, const float &z_probe_low_point, const feedRate_t fr_mm_s, const bool scheck, const float clearance) -> bool {
  456. // Tare the probe, if supported
  457. if (TERN0(PROBE_TARE, tare())) return true;
  458. // Do a first probe at the fast speed
  459. const bool probe_fail = probe_down_to_z(z_probe_low_point, fr_mm_s), // No probe trigger?
  460. early_fail = (scheck && current_position.z > -offset.z + clearance); // Probe triggered too high?
  461. #if ENABLED(DEBUG_LEVELING_FEATURE)
  462. if (DEBUGGING(LEVELING) && (probe_fail || early_fail)) {
  463. DEBUG_PRINT_P(plbl);
  464. DEBUG_ECHOPGM(" Probe fail! -");
  465. if (probe_fail) DEBUG_ECHOPGM(" No trigger.");
  466. if (early_fail) DEBUG_ECHOPGM(" Triggered early.");
  467. DEBUG_EOL();
  468. }
  469. #else
  470. UNUSED(plbl);
  471. #endif
  472. return probe_fail || early_fail;
  473. };
  474. // Stop the probe before it goes too low to prevent damage.
  475. // If Z isn't known then probe to -10mm.
  476. const float z_probe_low_point = axis_is_trusted(Z_AXIS) ? -offset.z + Z_PROBE_LOW_POINT : -10.0;
  477. // Double-probing does a fast probe followed by a slow probe
  478. #if TOTAL_PROBING == 2
  479. // Attempt to tare the probe
  480. if (TERN0(PROBE_TARE, tare())) return NAN;
  481. // Do a first probe at the fast speed
  482. if (try_to_probe(PSTR("FAST"), z_probe_low_point, z_probe_fast_mm_s,
  483. sanity_check, Z_CLEARANCE_BETWEEN_PROBES) ) return NAN;
  484. const float first_probe_z = current_position.z;
  485. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("1st Probe Z:", first_probe_z);
  486. // Raise to give the probe clearance
  487. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_MULTI_PROBE, z_probe_fast_mm_s);
  488. #elif Z_PROBE_SPEED_FAST != Z_PROBE_SPEED_SLOW
  489. // If the nozzle is well over the travel height then
  490. // move down quickly before doing the slow probe
  491. const float z = Z_CLEARANCE_DEPLOY_PROBE + 5.0 + (offset.z < 0 ? -offset.z : 0);
  492. if (current_position.z > z) {
  493. // Probe down fast. If the probe never triggered, raise for probe clearance
  494. if (!probe_down_to_z(z, z_probe_fast_mm_s))
  495. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_BETWEEN_PROBES, z_probe_fast_mm_s);
  496. }
  497. #endif
  498. #if EXTRA_PROBING > 0
  499. float probes[TOTAL_PROBING];
  500. #endif
  501. #if TOTAL_PROBING > 2
  502. float probes_z_sum = 0;
  503. for (
  504. #if EXTRA_PROBING > 0
  505. uint8_t p = 0; p < TOTAL_PROBING; p++
  506. #else
  507. uint8_t p = TOTAL_PROBING; p--;
  508. #endif
  509. )
  510. #endif
  511. {
  512. // If the probe won't tare, return
  513. if (TERN0(PROBE_TARE, tare())) return true;
  514. // Probe downward slowly to find the bed
  515. if (try_to_probe(PSTR("SLOW"), z_probe_low_point, MMM_TO_MMS(Z_PROBE_SPEED_SLOW),
  516. sanity_check, Z_CLEARANCE_MULTI_PROBE) ) return NAN;
  517. TERN_(MEASURE_BACKLASH_WHEN_PROBING, backlash.measure_with_probe());
  518. const float z = current_position.z;
  519. #if EXTRA_PROBING > 0
  520. // Insert Z measurement into probes[]. Keep it sorted ascending.
  521. LOOP_LE_N(i, p) { // Iterate the saved Zs to insert the new Z
  522. if (i == p || probes[i] > z) { // Last index or new Z is smaller than this Z
  523. for (int8_t m = p; --m >= i;) probes[m + 1] = probes[m]; // Shift items down after the insertion point
  524. probes[i] = z; // Insert the new Z measurement
  525. break; // Only one to insert. Done!
  526. }
  527. }
  528. #elif TOTAL_PROBING > 2
  529. probes_z_sum += z;
  530. #else
  531. UNUSED(z);
  532. #endif
  533. #if TOTAL_PROBING > 2
  534. // Small Z raise after all but the last probe
  535. if (p
  536. #if EXTRA_PROBING > 0
  537. < TOTAL_PROBING - 1
  538. #endif
  539. ) do_blocking_move_to_z(z + Z_CLEARANCE_MULTI_PROBE, z_probe_fast_mm_s);
  540. #endif
  541. }
  542. #if TOTAL_PROBING > 2
  543. #if EXTRA_PROBING > 0
  544. // Take the center value (or average the two middle values) as the median
  545. static constexpr int PHALF = (TOTAL_PROBING - 1) / 2;
  546. const float middle = probes[PHALF],
  547. median = ((TOTAL_PROBING) & 1) ? middle : (middle + probes[PHALF + 1]) * 0.5f;
  548. // Remove values farthest from the median
  549. uint8_t min_avg_idx = 0, max_avg_idx = TOTAL_PROBING - 1;
  550. for (uint8_t i = EXTRA_PROBING; i--;)
  551. if (ABS(probes[max_avg_idx] - median) > ABS(probes[min_avg_idx] - median))
  552. max_avg_idx--; else min_avg_idx++;
  553. // Return the average value of all remaining probes.
  554. LOOP_S_LE_N(i, min_avg_idx, max_avg_idx)
  555. probes_z_sum += probes[i];
  556. #endif
  557. const float measured_z = probes_z_sum * RECIPROCAL(MULTIPLE_PROBING);
  558. #elif TOTAL_PROBING == 2
  559. const float z2 = current_position.z;
  560. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("2nd Probe Z:", z2, " Discrepancy:", first_probe_z - z2);
  561. // Return a weighted average of the fast and slow probes
  562. const float measured_z = (z2 * 3.0 + first_probe_z * 2.0) * 0.2;
  563. #else
  564. // Return the single probe result
  565. const float measured_z = current_position.z;
  566. #endif
  567. return measured_z;
  568. }
  569. /**
  570. * - Move to the given XY
  571. * - Deploy the probe, if not already deployed
  572. * - Probe the bed, get the Z position
  573. * - Depending on the 'stow' flag
  574. * - Stow the probe, or
  575. * - Raise to the BETWEEN height
  576. * - Return the probed Z position
  577. */
  578. float Probe::probe_at_point(const float &rx, const float &ry, const ProbePtRaise raise_after/*=PROBE_PT_NONE*/, const uint8_t verbose_level/*=0*/, const bool probe_relative/*=true*/, const bool sanity_check/*=true*/) {
  579. DEBUG_SECTION(log_probe, "Probe::probe_at_point", DEBUGGING(LEVELING));
  580. if (DEBUGGING(LEVELING)) {
  581. DEBUG_ECHOLNPAIR(
  582. "...(", LOGICAL_X_POSITION(rx), ", ", LOGICAL_Y_POSITION(ry),
  583. ", ", raise_after == PROBE_PT_RAISE ? "raise" : raise_after == PROBE_PT_STOW ? "stow" : "none",
  584. ", ", verbose_level,
  585. ", ", probe_relative ? "probe" : "nozzle", "_relative)"
  586. );
  587. DEBUG_POS("", current_position);
  588. }
  589. #if BOTH(BLTOUCH, BLTOUCH_HS_MODE)
  590. if (bltouch.triggered()) bltouch._reset();
  591. #endif
  592. // On delta keep Z below clip height or do_blocking_move_to will abort
  593. xyz_pos_t npos = { rx, ry, _MIN(TERN(DELTA, delta_clip_start_height, current_position.z), current_position.z) };
  594. if (probe_relative) { // The given position is in terms of the probe
  595. if (!can_reach(npos)) {
  596. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Position Not Reachable");
  597. return NAN;
  598. }
  599. npos -= offset_xy; // Get the nozzle position
  600. }
  601. else if (!position_is_reachable(npos)) return NAN; // The given position is in terms of the nozzle
  602. // Move the probe to the starting XYZ
  603. do_blocking_move_to(npos, feedRate_t(XY_PROBE_FEEDRATE_MM_S));
  604. float measured_z = NAN;
  605. if (!deploy()) measured_z = run_z_probe(sanity_check) + offset.z;
  606. if (!isnan(measured_z)) {
  607. const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
  608. if (big_raise || raise_after == PROBE_PT_RAISE)
  609. do_blocking_move_to_z(current_position.z + (big_raise ? 25 : Z_CLEARANCE_BETWEEN_PROBES), z_probe_fast_mm_s);
  610. else if (raise_after == PROBE_PT_STOW)
  611. if (stow()) measured_z = NAN; // Error on stow?
  612. if (verbose_level > 2)
  613. SERIAL_ECHOLNPAIR("Bed X: ", LOGICAL_X_POSITION(rx), " Y: ", LOGICAL_Y_POSITION(ry), " Z: ", measured_z);
  614. }
  615. if (isnan(measured_z)) {
  616. stow();
  617. LCD_MESSAGEPGM(MSG_LCD_PROBING_FAILED);
  618. #if DISABLED(G29_RETRY_AND_RECOVER)
  619. SERIAL_ERROR_MSG(STR_ERR_PROBING_FAILED);
  620. #endif
  621. }
  622. return measured_z;
  623. }
  624. #if HAS_Z_SERVO_PROBE
  625. void Probe::servo_probe_init() {
  626. /**
  627. * Set position of Z Servo Endstop
  628. *
  629. * The servo might be deployed and positioned too low to stow
  630. * when starting up the machine or rebooting the board.
  631. * There's no way to know where the nozzle is positioned until
  632. * homing has been done - no homing with z-probe without init!
  633. */
  634. STOW_Z_SERVO();
  635. }
  636. #endif // HAS_Z_SERVO_PROBE
  637. #endif // HAS_BED_PROBE