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

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