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

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