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

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