My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

probe.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. #ifndef DELAY_BEFORE_PROBING
  203. #define DELAY_BEFORE_PROBING 25
  204. #endif
  205. void Probe::set_probing_paused(const bool dopause) {
  206. TERN_(PROBING_HEATERS_OFF, thermalManager.pause(dopause));
  207. TERN_(PROBING_FANS_OFF, thermalManager.set_fans_paused(dopause));
  208. #if ENABLED(PROBING_STEPPERS_OFF)
  209. IF_DISABLED(DELTA, static uint8_t old_trusted);
  210. if (dopause) {
  211. #if DISABLED(DELTA)
  212. old_trusted = axis_trusted;
  213. DISABLE_AXIS_X();
  214. DISABLE_AXIS_Y();
  215. #endif
  216. disable_e_steppers();
  217. }
  218. else {
  219. #if DISABLED(DELTA)
  220. if (TEST(old_trusted, X_AXIS)) ENABLE_AXIS_X();
  221. if (TEST(old_trusted, Y_AXIS)) ENABLE_AXIS_Y();
  222. #endif
  223. axis_trusted = old_trusted;
  224. }
  225. #endif
  226. if (dopause) safe_delay(_MAX(DELAY_BEFORE_PROBING, 25));
  227. }
  228. #endif // HAS_QUIET_PROBING
  229. /**
  230. * Raise Z to a minimum height to make room for a probe to move
  231. */
  232. void Probe::do_z_raise(const float z_raise) {
  233. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probe::do_z_raise(", z_raise, ")");
  234. float z_dest = z_raise;
  235. if (offset.z < 0) z_dest -= offset.z;
  236. do_z_clearance(z_dest);
  237. }
  238. FORCE_INLINE void probe_specific_action(const bool deploy) {
  239. #if ENABLED(PAUSE_BEFORE_DEPLOY_STOW)
  240. do {
  241. #if ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED)
  242. if (deploy == PROBE_TRIGGERED()) break;
  243. #endif
  244. BUZZ(100, 659);
  245. BUZZ(100, 698);
  246. PGM_P const ds_str = deploy ? GET_TEXT(MSG_MANUAL_DEPLOY) : GET_TEXT(MSG_MANUAL_STOW);
  247. ui.return_to_status(); // To display the new status message
  248. ui.set_status_P(ds_str, 99);
  249. SERIAL_ECHOLNPGM_P(ds_str);
  250. TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Stow Probe"), CONTINUE_STR));
  251. TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired_P(PSTR("Stow Probe")));
  252. wait_for_user_response();
  253. ui.reset_status();
  254. } while (ENABLED(PAUSE_PROBE_DEPLOY_WHEN_TRIGGERED));
  255. #endif // PAUSE_BEFORE_DEPLOY_STOW
  256. #if ENABLED(SOLENOID_PROBE)
  257. #if HAS_SOLENOID_1
  258. WRITE(SOL1_PIN, deploy);
  259. #endif
  260. #elif ENABLED(Z_PROBE_SLED)
  261. dock_sled(!deploy);
  262. #elif ENABLED(BLTOUCH)
  263. deploy ? bltouch.deploy() : bltouch.stow();
  264. #elif HAS_Z_SERVO_PROBE
  265. MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][deploy ? 0 : 1]);
  266. #elif EITHER(TOUCH_MI_PROBE, Z_PROBE_ALLEN_KEY)
  267. deploy ? run_deploy_moves_script() : run_stow_moves_script();
  268. #elif ENABLED(RACK_AND_PINION_PROBE)
  269. do_blocking_move_to_x(deploy ? Z_PROBE_DEPLOY_X : Z_PROBE_RETRACT_X);
  270. #elif DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  271. UNUSED(deploy);
  272. #endif
  273. }
  274. #if EITHER(PREHEAT_BEFORE_PROBING, PREHEAT_BEFORE_LEVELING)
  275. #if ENABLED(PREHEAT_BEFORE_PROBING)
  276. #ifndef PROBING_NOZZLE_TEMP
  277. #define PROBING_NOZZLE_TEMP 0
  278. #endif
  279. #ifndef PROBING_BED_TEMP
  280. #define PROBING_BED_TEMP 0
  281. #endif
  282. #endif
  283. /**
  284. * Do preheating as required before leveling or probing.
  285. * - If a preheat input is higher than the current target, raise the target temperature.
  286. * - If a preheat input is higher than the current temperature, wait for stabilization.
  287. */
  288. void Probe::preheat_for_probing(const int16_t hotend_temp, const int16_t bed_temp) {
  289. #if HAS_HOTEND && (PROBING_NOZZLE_TEMP || LEVELING_NOZZLE_TEMP)
  290. #define WAIT_FOR_NOZZLE_HEAT
  291. #endif
  292. #if HAS_HEATED_BED && (PROBING_BED_TEMP || LEVELING_BED_TEMP)
  293. #define WAIT_FOR_BED_HEAT
  294. #endif
  295. DEBUG_ECHOPGM("Preheating ");
  296. #if ENABLED(WAIT_FOR_NOZZLE_HEAT)
  297. const int16_t hotendPreheat = hotend_temp > thermalManager.degTargetHotend(0) ? hotend_temp : 0;
  298. if (hotendPreheat) {
  299. DEBUG_ECHOPAIR("hotend (", hotendPreheat, ")");
  300. thermalManager.setTargetHotend(hotendPreheat, 0);
  301. }
  302. #elif ENABLED(WAIT_FOR_BED_HEAT)
  303. constexpr int16_t hotendPreheat = 0;
  304. #endif
  305. #if ENABLED(WAIT_FOR_BED_HEAT)
  306. const int16_t bedPreheat = bed_temp > thermalManager.degTargetBed() ? bed_temp : 0;
  307. if (bedPreheat) {
  308. if (hotendPreheat) DEBUG_ECHOPGM(" and ");
  309. DEBUG_ECHOPAIR("bed (", bedPreheat, ")");
  310. thermalManager.setTargetBed(bedPreheat);
  311. }
  312. #endif
  313. DEBUG_EOL();
  314. TERN_(WAIT_FOR_NOZZLE_HEAT, if (hotend_temp > thermalManager.degHotend(0) + (TEMP_WINDOW)) thermalManager.wait_for_hotend(0));
  315. TERN_(WAIT_FOR_BED_HEAT, if (bed_temp > thermalManager.degBed() + (TEMP_BED_WINDOW)) thermalManager.wait_for_bed_heating());
  316. }
  317. #endif
  318. /**
  319. * Attempt to deploy or stow the probe
  320. *
  321. * Return TRUE if the probe could not be deployed/stowed
  322. */
  323. bool Probe::set_deployed(const bool deploy) {
  324. if (DEBUGGING(LEVELING)) {
  325. DEBUG_POS("Probe::set_deployed", current_position);
  326. DEBUG_ECHOLNPAIR("deploy: ", deploy);
  327. }
  328. if (endstops.z_probe_enabled == deploy) return false;
  329. // Make room for probe to deploy (or stow)
  330. // Fix-mounted probe should only raise for deploy
  331. // unless PAUSE_BEFORE_DEPLOY_STOW is enabled
  332. #if EITHER(FIX_MOUNTED_PROBE, NOZZLE_AS_PROBE) && DISABLED(PAUSE_BEFORE_DEPLOY_STOW)
  333. const bool z_raise_wanted = deploy;
  334. #else
  335. constexpr bool z_raise_wanted = true;
  336. #endif
  337. if (z_raise_wanted)
  338. do_z_raise(_MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE));
  339. #if EITHER(Z_PROBE_SLED, Z_PROBE_ALLEN_KEY)
  340. if (homing_needed_error(TERN_(Z_PROBE_SLED, _BV(X_AXIS)))) {
  341. SERIAL_ERROR_MSG(STR_STOP_UNHOMED);
  342. stop();
  343. return true;
  344. }
  345. #endif
  346. const xy_pos_t old_xy = current_position;
  347. #if ENABLED(PROBE_TRIGGERED_WHEN_STOWED_TEST)
  348. // Only deploy/stow if needed
  349. if (PROBE_TRIGGERED() == deploy) {
  350. if (!deploy) endstops.enable_z_probe(false); // Switch off triggered when stowed probes early
  351. // otherwise an Allen-Key probe can't be stowed.
  352. probe_specific_action(deploy);
  353. }
  354. if (PROBE_TRIGGERED() == deploy) { // Unchanged after deploy/stow action?
  355. if (IsRunning()) {
  356. SERIAL_ERROR_MSG("Z-Probe failed");
  357. LCD_ALERTMESSAGEPGM_P(PSTR("Err: ZPROBE"));
  358. }
  359. stop();
  360. return true;
  361. }
  362. #else
  363. probe_specific_action(deploy);
  364. #endif
  365. // If preheating is required before any probing...
  366. TERN_(PREHEAT_BEFORE_PROBING, if (deploy) preheat_for_probing(PROBING_NOZZLE_TEMP, PROBING_BED_TEMP));
  367. do_blocking_move_to(old_xy);
  368. endstops.enable_z_probe(deploy);
  369. return false;
  370. }
  371. /**
  372. * @brief Used by run_z_probe to do a single Z probe move.
  373. *
  374. * @param z Z destination
  375. * @param fr_mm_s Feedrate in mm/s
  376. * @return true to indicate an error
  377. */
  378. /**
  379. * @brief Move down until the probe triggers or the low limit is reached
  380. *
  381. * @details Used by run_z_probe to get each bed Z height measurement.
  382. * Sets current_position.z to the height where the probe triggered
  383. * (according to the Z stepper count). The float Z is propagated
  384. * back to the planner.position to preempt any rounding error.
  385. *
  386. * @return TRUE if the probe failed to trigger.
  387. */
  388. bool Probe::probe_down_to_z(const_float_t z, const_feedRate_t fr_mm_s) {
  389. DEBUG_SECTION(log_probe, "Probe::probe_down_to_z", DEBUGGING(LEVELING));
  390. #if BOTH(HAS_HEATED_BED, WAIT_FOR_BED_HEATER)
  391. thermalManager.wait_for_bed_heating();
  392. #endif
  393. #if BOTH(HAS_TEMP_HOTEND, WAIT_FOR_HOTEND)
  394. thermalManager.wait_for_hotend_heating(active_extruder);
  395. #endif
  396. if (TERN0(BLTOUCH_SLOW_MODE, bltouch.deploy())) return true; // Deploy in LOW SPEED MODE on every probe action
  397. // Disable stealthChop if used. Enable diag1 pin on driver.
  398. #if ENABLED(SENSORLESS_PROBING)
  399. sensorless_t stealth_states { false };
  400. #if ENABLED(DELTA)
  401. stealth_states.x = tmc_enable_stallguard(stepperX);
  402. stealth_states.y = tmc_enable_stallguard(stepperY);
  403. #endif
  404. stealth_states.z = tmc_enable_stallguard(stepperZ);
  405. endstops.enable(true);
  406. #endif
  407. TERN_(HAS_QUIET_PROBING, set_probing_paused(true));
  408. // Move down until the probe is triggered
  409. do_blocking_move_to_z(z, fr_mm_s);
  410. // Check to see if the probe was triggered
  411. const bool probe_triggered =
  412. #if BOTH(DELTA, SENSORLESS_PROBING)
  413. endstops.trigger_state() & (_BV(X_MIN) | _BV(Y_MIN) | _BV(Z_MIN))
  414. #else
  415. TEST(endstops.trigger_state(), TERN(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, Z_MIN, Z_MIN_PROBE))
  416. #endif
  417. ;
  418. TERN_(HAS_QUIET_PROBING, set_probing_paused(false));
  419. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  420. #if ENABLED(SENSORLESS_PROBING)
  421. endstops.not_homing();
  422. #if ENABLED(DELTA)
  423. tmc_disable_stallguard(stepperX, stealth_states.x);
  424. tmc_disable_stallguard(stepperY, stealth_states.y);
  425. #endif
  426. tmc_disable_stallguard(stepperZ, stealth_states.z);
  427. #endif
  428. if (probe_triggered && TERN0(BLTOUCH_SLOW_MODE, bltouch.stow())) // Stow in LOW SPEED MODE on every trigger
  429. return true;
  430. // Clear endstop flags
  431. endstops.hit_on_purpose();
  432. // Get Z where the steppers were interrupted
  433. set_current_from_steppers_for_axis(Z_AXIS);
  434. // Tell the planner where we actually are
  435. sync_plan_position();
  436. return !probe_triggered;
  437. }
  438. #if ENABLED(PROBE_TARE)
  439. /**
  440. * @brief Init the tare pin
  441. *
  442. * @details Init tare pin to ON state for a strain gauge, otherwise OFF
  443. */
  444. void Probe::tare_init() {
  445. OUT_WRITE(PROBE_TARE_PIN, !PROBE_TARE_STATE);
  446. }
  447. /**
  448. * @brief Tare the Z probe
  449. *
  450. * @details Signal to the probe to tare itself
  451. *
  452. * @return TRUE if the tare cold not be completed
  453. */
  454. bool Probe::tare() {
  455. #if BOTH(PROBE_ACTIVATION_SWITCH, PROBE_TARE_ONLY_WHILE_INACTIVE)
  456. if (endstops.probe_switch_activated()) {
  457. SERIAL_ECHOLNPGM("Cannot tare an active probe");
  458. return true;
  459. }
  460. #endif
  461. SERIAL_ECHOLNPGM("Taring probe");
  462. WRITE(PROBE_TARE_PIN, PROBE_TARE_STATE);
  463. delay(PROBE_TARE_TIME);
  464. WRITE(PROBE_TARE_PIN, !PROBE_TARE_STATE);
  465. delay(PROBE_TARE_DELAY);
  466. endstops.hit_on_purpose();
  467. return false;
  468. }
  469. #endif
  470. /**
  471. * @brief Probe at the current XY (possibly more than once) to find the bed Z.
  472. *
  473. * @details Used by probe_at_point to get the bed Z height at the current XY.
  474. * Leaves current_position.z at the height where the probe triggered.
  475. *
  476. * @return The Z position of the bed at the current XY or NAN on error.
  477. */
  478. float Probe::run_z_probe(const bool sanity_check/*=true*/) {
  479. DEBUG_SECTION(log_probe, "Probe::run_z_probe", DEBUGGING(LEVELING));
  480. auto try_to_probe = [&](PGM_P const plbl, const_float_t z_probe_low_point, const feedRate_t fr_mm_s, const bool scheck, const float clearance) -> bool {
  481. // Tare the probe, if supported
  482. if (TERN0(PROBE_TARE, tare())) return true;
  483. // Do a first probe at the fast speed
  484. const bool probe_fail = probe_down_to_z(z_probe_low_point, fr_mm_s), // No probe trigger?
  485. early_fail = (scheck && current_position.z > -offset.z + clearance); // Probe triggered too high?
  486. #if ENABLED(DEBUG_LEVELING_FEATURE)
  487. if (DEBUGGING(LEVELING) && (probe_fail || early_fail)) {
  488. DEBUG_ECHOPGM_P(plbl);
  489. DEBUG_ECHOPGM(" Probe fail! -");
  490. if (probe_fail) DEBUG_ECHOPGM(" No trigger.");
  491. if (early_fail) DEBUG_ECHOPGM(" Triggered early.");
  492. DEBUG_EOL();
  493. }
  494. #else
  495. UNUSED(plbl);
  496. #endif
  497. return probe_fail || early_fail;
  498. };
  499. // Stop the probe before it goes too low to prevent damage.
  500. // If Z isn't known then probe to -10mm.
  501. const float z_probe_low_point = axis_is_trusted(Z_AXIS) ? -offset.z + Z_PROBE_LOW_POINT : -10.0;
  502. // Double-probing does a fast probe followed by a slow probe
  503. #if TOTAL_PROBING == 2
  504. // Attempt to tare the probe
  505. if (TERN0(PROBE_TARE, tare())) return NAN;
  506. // Do a first probe at the fast speed
  507. if (try_to_probe(PSTR("FAST"), z_probe_low_point, z_probe_fast_mm_s,
  508. sanity_check, Z_CLEARANCE_BETWEEN_PROBES) ) return NAN;
  509. const float first_probe_z = current_position.z;
  510. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("1st Probe Z:", first_probe_z);
  511. // Raise to give the probe clearance
  512. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_MULTI_PROBE, z_probe_fast_mm_s);
  513. #elif Z_PROBE_FEEDRATE_FAST != Z_PROBE_FEEDRATE_SLOW
  514. // If the nozzle is well over the travel height then
  515. // move down quickly before doing the slow probe
  516. const float z = Z_CLEARANCE_DEPLOY_PROBE + 5.0 + (offset.z < 0 ? -offset.z : 0);
  517. if (current_position.z > z) {
  518. // Probe down fast. If the probe never triggered, raise for probe clearance
  519. if (!probe_down_to_z(z, z_probe_fast_mm_s))
  520. do_blocking_move_to_z(current_position.z + Z_CLEARANCE_BETWEEN_PROBES, z_probe_fast_mm_s);
  521. }
  522. #endif
  523. #if EXTRA_PROBING > 0
  524. float probes[TOTAL_PROBING];
  525. #endif
  526. #if TOTAL_PROBING > 2
  527. float probes_z_sum = 0;
  528. for (
  529. #if EXTRA_PROBING > 0
  530. uint8_t p = 0; p < TOTAL_PROBING; p++
  531. #else
  532. uint8_t p = TOTAL_PROBING; p--;
  533. #endif
  534. )
  535. #endif
  536. {
  537. // If the probe won't tare, return
  538. if (TERN0(PROBE_TARE, tare())) return true;
  539. // Probe downward slowly to find the bed
  540. if (try_to_probe(PSTR("SLOW"), z_probe_low_point, MMM_TO_MMS(Z_PROBE_FEEDRATE_SLOW),
  541. sanity_check, Z_CLEARANCE_MULTI_PROBE) ) return NAN;
  542. TERN_(MEASURE_BACKLASH_WHEN_PROBING, backlash.measure_with_probe());
  543. const float z = current_position.z;
  544. #if EXTRA_PROBING > 0
  545. // Insert Z measurement into probes[]. Keep it sorted ascending.
  546. LOOP_LE_N(i, p) { // Iterate the saved Zs to insert the new Z
  547. if (i == p || probes[i] > z) { // Last index or new Z is smaller than this Z
  548. for (int8_t m = p; --m >= i;) probes[m + 1] = probes[m]; // Shift items down after the insertion point
  549. probes[i] = z; // Insert the new Z measurement
  550. break; // Only one to insert. Done!
  551. }
  552. }
  553. #elif TOTAL_PROBING > 2
  554. probes_z_sum += z;
  555. #else
  556. UNUSED(z);
  557. #endif
  558. #if TOTAL_PROBING > 2
  559. // Small Z raise after all but the last probe
  560. if (p
  561. #if EXTRA_PROBING > 0
  562. < TOTAL_PROBING - 1
  563. #endif
  564. ) do_blocking_move_to_z(z + Z_CLEARANCE_MULTI_PROBE, z_probe_fast_mm_s);
  565. #endif
  566. }
  567. #if TOTAL_PROBING > 2
  568. #if EXTRA_PROBING > 0
  569. // Take the center value (or average the two middle values) as the median
  570. static constexpr int PHALF = (TOTAL_PROBING - 1) / 2;
  571. const float middle = probes[PHALF],
  572. median = ((TOTAL_PROBING) & 1) ? middle : (middle + probes[PHALF + 1]) * 0.5f;
  573. // Remove values farthest from the median
  574. uint8_t min_avg_idx = 0, max_avg_idx = TOTAL_PROBING - 1;
  575. for (uint8_t i = EXTRA_PROBING; i--;)
  576. if (ABS(probes[max_avg_idx] - median) > ABS(probes[min_avg_idx] - median))
  577. max_avg_idx--; else min_avg_idx++;
  578. // Return the average value of all remaining probes.
  579. LOOP_S_LE_N(i, min_avg_idx, max_avg_idx)
  580. probes_z_sum += probes[i];
  581. #endif
  582. const float measured_z = probes_z_sum * RECIPROCAL(MULTIPLE_PROBING);
  583. #elif TOTAL_PROBING == 2
  584. const float z2 = current_position.z;
  585. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("2nd Probe Z:", z2, " Discrepancy:", first_probe_z - z2);
  586. // Return a weighted average of the fast and slow probes
  587. const float measured_z = (z2 * 3.0 + first_probe_z * 2.0) * 0.2;
  588. #else
  589. // Return the single probe result
  590. const float measured_z = current_position.z;
  591. #endif
  592. return measured_z;
  593. }
  594. /**
  595. * - Move to the given XY
  596. * - Deploy the probe, if not already deployed
  597. * - Probe the bed, get the Z position
  598. * - Depending on the 'stow' flag
  599. * - Stow the probe, or
  600. * - Raise to the BETWEEN height
  601. * - Return the probed Z position
  602. */
  603. float Probe::probe_at_point(const_float_t rx, const_float_t ry, const ProbePtRaise raise_after/*=PROBE_PT_NONE*/, const uint8_t verbose_level/*=0*/, const bool probe_relative/*=true*/, const bool sanity_check/*=true*/) {
  604. DEBUG_SECTION(log_probe, "Probe::probe_at_point", DEBUGGING(LEVELING));
  605. if (DEBUGGING(LEVELING)) {
  606. DEBUG_ECHOLNPAIR(
  607. "...(", LOGICAL_X_POSITION(rx), ", ", LOGICAL_Y_POSITION(ry),
  608. ", ", raise_after == PROBE_PT_RAISE ? "raise" : raise_after == PROBE_PT_STOW ? "stow" : "none",
  609. ", ", verbose_level,
  610. ", ", probe_relative ? "probe" : "nozzle", "_relative)"
  611. );
  612. DEBUG_POS("", current_position);
  613. }
  614. #if BOTH(BLTOUCH, BLTOUCH_HS_MODE)
  615. if (bltouch.triggered()) bltouch._reset();
  616. #endif
  617. // On delta keep Z below clip height or do_blocking_move_to will abort
  618. xyz_pos_t npos = { rx, ry, _MIN(TERN(DELTA, delta_clip_start_height, current_position.z), current_position.z) };
  619. if (probe_relative) { // The given position is in terms of the probe
  620. if (!can_reach(npos)) {
  621. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Position Not Reachable");
  622. return NAN;
  623. }
  624. npos -= offset_xy; // Get the nozzle position
  625. }
  626. else if (!position_is_reachable(npos)) return NAN; // The given position is in terms of the nozzle
  627. // Move the probe to the starting XYZ
  628. do_blocking_move_to(npos, feedRate_t(XY_PROBE_FEEDRATE_MM_S));
  629. float measured_z = NAN;
  630. if (!deploy()) measured_z = run_z_probe(sanity_check) + offset.z;
  631. if (!isnan(measured_z)) {
  632. const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
  633. if (big_raise || raise_after == PROBE_PT_RAISE)
  634. do_blocking_move_to_z(current_position.z + (big_raise ? 25 : Z_CLEARANCE_BETWEEN_PROBES), z_probe_fast_mm_s);
  635. else if (raise_after == PROBE_PT_STOW)
  636. if (stow()) measured_z = NAN; // Error on stow?
  637. if (verbose_level > 2)
  638. SERIAL_ECHOLNPAIR("Bed X: ", LOGICAL_X_POSITION(rx), " Y: ", LOGICAL_Y_POSITION(ry), " Z: ", measured_z);
  639. }
  640. if (isnan(measured_z)) {
  641. stow();
  642. LCD_MESSAGEPGM(MSG_LCD_PROBING_FAILED);
  643. #if DISABLED(G29_RETRY_AND_RECOVER)
  644. SERIAL_ERROR_MSG(STR_ERR_PROBING_FAILED);
  645. #endif
  646. }
  647. return measured_z;
  648. }
  649. #if HAS_Z_SERVO_PROBE
  650. void Probe::servo_probe_init() {
  651. /**
  652. * Set position of Z Servo Endstop
  653. *
  654. * The servo might be deployed and positioned too low to stow
  655. * when starting up the machine or rebooting the board.
  656. * There's no way to know where the nozzle is positioned until
  657. * homing has been done - no homing with z-probe without init!
  658. */
  659. STOW_Z_SERVO();
  660. }
  661. #endif // HAS_Z_SERVO_PROBE
  662. #endif // HAS_BED_PROBE