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.

G28.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #include "../../inc/MarlinConfig.h"
  23. #include "../gcode.h"
  24. #include "../../module/stepper.h"
  25. #include "../../module/endstops.h"
  26. #if HAS_MULTI_HOTEND
  27. #include "../../module/tool_change.h"
  28. #endif
  29. #if HAS_LEVELING
  30. #include "../../feature/bedlevel/bedlevel.h"
  31. #endif
  32. #if ENABLED(SENSORLESS_HOMING)
  33. #include "../../feature/tmc_util.h"
  34. #endif
  35. #include "../../module/probe.h"
  36. #if ENABLED(BLTOUCH)
  37. #include "../../feature/bltouch.h"
  38. #endif
  39. #include "../../lcd/marlinui.h"
  40. #if ENABLED(DWIN_CREALITY_LCD)
  41. #include "../../lcd/dwin/e3v2/dwin.h"
  42. #endif
  43. #if ENABLED(EXTENSIBLE_UI)
  44. #include "../../lcd/extui/ui_api.h"
  45. #endif
  46. #if HAS_L64XX // set L6470 absolute position registers to counts
  47. #include "../../libs/L64XX/L64XX_Marlin.h"
  48. #endif
  49. #if ENABLED(LASER_MOVE_G28_OFF)
  50. #include "../../feature/spindle_laser.h"
  51. #endif
  52. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  53. #include "../../core/debug_out.h"
  54. #if ENABLED(QUICK_HOME)
  55. static void quick_home_xy() {
  56. // Pretend the current position is 0,0
  57. current_position.set(0.0, 0.0);
  58. sync_plan_position();
  59. const int x_axis_home_dir = x_home_dir(active_extruder);
  60. const float mlx = max_length(X_AXIS),
  61. mly = max_length(Y_AXIS),
  62. mlratio = mlx > mly ? mly / mlx : mlx / mly,
  63. fr_mm_s = _MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0);
  64. #if ENABLED(SENSORLESS_HOMING)
  65. sensorless_t stealth_states {
  66. tmc_enable_stallguard(stepperX)
  67. , tmc_enable_stallguard(stepperY)
  68. , false
  69. , false
  70. #if AXIS_HAS_STALLGUARD(X2)
  71. || tmc_enable_stallguard(stepperX2)
  72. #endif
  73. , false
  74. #if AXIS_HAS_STALLGUARD(Y2)
  75. || tmc_enable_stallguard(stepperY2)
  76. #endif
  77. };
  78. #endif
  79. do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * Y_HOME_DIR, fr_mm_s);
  80. endstops.validate_homing_move();
  81. current_position.set(0.0, 0.0);
  82. #if ENABLED(SENSORLESS_HOMING)
  83. tmc_disable_stallguard(stepperX, stealth_states.x);
  84. tmc_disable_stallguard(stepperY, stealth_states.y);
  85. #if AXIS_HAS_STALLGUARD(X2)
  86. tmc_disable_stallguard(stepperX2, stealth_states.x2);
  87. #endif
  88. #if AXIS_HAS_STALLGUARD(Y2)
  89. tmc_disable_stallguard(stepperY2, stealth_states.y2);
  90. #endif
  91. #endif
  92. }
  93. #endif // QUICK_HOME
  94. #if ENABLED(Z_SAFE_HOMING)
  95. inline void home_z_safely() {
  96. DEBUG_SECTION(log_G28, "home_z_safely", DEBUGGING(LEVELING));
  97. // Disallow Z homing if X or Y homing is needed
  98. if (homing_needed_error(_BV(X_AXIS) | _BV(Y_AXIS))) return;
  99. sync_plan_position();
  100. /**
  101. * Move the Z probe (or just the nozzle) to the safe homing point
  102. * (Z is already at the right height)
  103. */
  104. constexpr xy_float_t safe_homing_xy = { Z_SAFE_HOMING_X_POINT, Z_SAFE_HOMING_Y_POINT };
  105. #if HAS_HOME_OFFSET
  106. xy_float_t okay_homing_xy = safe_homing_xy;
  107. okay_homing_xy -= home_offset;
  108. #else
  109. constexpr xy_float_t okay_homing_xy = safe_homing_xy;
  110. #endif
  111. destination.set(okay_homing_xy, current_position.z);
  112. TERN_(HOMING_Z_WITH_PROBE, destination -= probe.offset_xy);
  113. if (position_is_reachable(destination)) {
  114. if (DEBUGGING(LEVELING)) DEBUG_POS("home_z_safely", destination);
  115. // Free the active extruder for movement
  116. TERN_(DUAL_X_CARRIAGE, idex_set_parked(false));
  117. TERN_(SENSORLESS_HOMING, safe_delay(500)); // Short delay needed to settle
  118. do_blocking_move_to_xy(destination);
  119. homeaxis(Z_AXIS);
  120. }
  121. else {
  122. LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
  123. SERIAL_ECHO_MSG(STR_ZPROBE_OUT_SER);
  124. }
  125. }
  126. #endif // Z_SAFE_HOMING
  127. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  128. slow_homing_t begin_slow_homing() {
  129. slow_homing_t slow_homing{0};
  130. slow_homing.acceleration.set(planner.settings.max_acceleration_mm_per_s2[X_AXIS],
  131. planner.settings.max_acceleration_mm_per_s2[Y_AXIS]);
  132. planner.settings.max_acceleration_mm_per_s2[X_AXIS] = 100;
  133. planner.settings.max_acceleration_mm_per_s2[Y_AXIS] = 100;
  134. #if HAS_CLASSIC_JERK
  135. slow_homing.jerk_xy = planner.max_jerk;
  136. planner.max_jerk.set(0, 0);
  137. #endif
  138. planner.reset_acceleration_rates();
  139. return slow_homing;
  140. }
  141. void end_slow_homing(const slow_homing_t &slow_homing) {
  142. planner.settings.max_acceleration_mm_per_s2[X_AXIS] = slow_homing.acceleration.x;
  143. planner.settings.max_acceleration_mm_per_s2[Y_AXIS] = slow_homing.acceleration.y;
  144. TERN_(HAS_CLASSIC_JERK, planner.max_jerk = slow_homing.jerk_xy);
  145. planner.reset_acceleration_rates();
  146. }
  147. #endif // IMPROVE_HOMING_RELIABILITY
  148. /**
  149. * G28: Home all axes according to settings
  150. *
  151. * Parameters
  152. *
  153. * None Home to all axes with no parameters.
  154. * With QUICK_HOME enabled XY will home together, then Z.
  155. *
  156. * O Home only if position is unknown
  157. *
  158. * Rn Raise by n mm/inches before homing
  159. *
  160. * Cartesian/SCARA parameters
  161. *
  162. * X Home to the X endstop
  163. * Y Home to the Y endstop
  164. * Z Home to the Z endstop
  165. */
  166. void GcodeSuite::G28() {
  167. DEBUG_SECTION(log_G28, "G28", DEBUGGING(LEVELING));
  168. if (DEBUGGING(LEVELING)) log_machine_info();
  169. TERN_(LASER_MOVE_G28_OFF, cutter.set_inline_enabled(false)); // turn off laser
  170. #if ENABLED(DUAL_X_CARRIAGE)
  171. bool IDEX_saved_duplication_state = extruder_duplication_enabled;
  172. DualXMode IDEX_saved_mode = dual_x_carriage_mode;
  173. #endif
  174. #if ENABLED(MARLIN_DEV_MODE)
  175. if (parser.seen('S')) {
  176. LOOP_XYZ(a) set_axis_is_at_home((AxisEnum)a);
  177. sync_plan_position();
  178. SERIAL_ECHOLNPGM("Simulated Homing");
  179. report_current_position();
  180. return;
  181. }
  182. #endif
  183. // Home (O)nly if position is unknown
  184. if (!axes_should_home() && parser.boolval('O')) {
  185. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> homing not needed, skip");
  186. return;
  187. }
  188. TERN_(DWIN_CREALITY_LCD, DWIN_StartHoming());
  189. TERN_(EXTENSIBLE_UI, ExtUI::onHomingStart());
  190. planner.synchronize(); // Wait for planner moves to finish!
  191. SET_SOFT_ENDSTOP_LOOSE(false); // Reset a leftover 'loose' motion state
  192. // Disable the leveling matrix before homing
  193. #if HAS_LEVELING
  194. const bool leveling_restore_state = parser.boolval('L', TERN(RESTORE_LEVELING_AFTER_G28, planner.leveling_active, ENABLED(ENABLE_LEVELING_AFTER_G28)));
  195. IF_ENABLED(PROBE_MANUALLY, g29_in_progress = false); // Cancel the active G29 session
  196. set_bed_leveling_enabled(false);
  197. #endif
  198. // Reset to the XY plane
  199. TERN_(CNC_WORKSPACE_PLANES, workspace_plane = PLANE_XY);
  200. // Count this command as movement / activity
  201. reset_stepper_timeout();
  202. #define HAS_CURRENT_HOME(N) (defined(N##_CURRENT_HOME) && N##_CURRENT_HOME != N##_CURRENT)
  203. #if HAS_CURRENT_HOME(X) || HAS_CURRENT_HOME(X2) || HAS_CURRENT_HOME(Y) || HAS_CURRENT_HOME(Y2)
  204. #define HAS_HOMING_CURRENT 1
  205. #endif
  206. #if HAS_HOMING_CURRENT
  207. auto debug_current = [](PGM_P const s, const int16_t a, const int16_t b){
  208. serialprintPGM(s); DEBUG_ECHOLNPAIR(" current: ", a, " -> ", b);
  209. };
  210. #if HAS_CURRENT_HOME(X)
  211. const int16_t tmc_save_current_X = stepperX.getMilliamps();
  212. stepperX.rms_current(X_CURRENT_HOME);
  213. if (DEBUGGING(LEVELING)) debug_current(PSTR("X"), tmc_save_current_X, X_CURRENT_HOME);
  214. #endif
  215. #if HAS_CURRENT_HOME(X2)
  216. const int16_t tmc_save_current_X2 = stepperX2.getMilliamps();
  217. stepperX2.rms_current(X2_CURRENT_HOME);
  218. if (DEBUGGING(LEVELING)) debug_current(PSTR("X2"), tmc_save_current_X2, X2_CURRENT_HOME);
  219. #endif
  220. #if HAS_CURRENT_HOME(Y)
  221. const int16_t tmc_save_current_Y = stepperY.getMilliamps();
  222. stepperY.rms_current(Y_CURRENT_HOME);
  223. if (DEBUGGING(LEVELING)) debug_current(PSTR("Y"), tmc_save_current_Y, Y_CURRENT_HOME);
  224. #endif
  225. #if HAS_CURRENT_HOME(Y2)
  226. const int16_t tmc_save_current_Y2 = stepperY2.getMilliamps();
  227. stepperY2.rms_current(Y2_CURRENT_HOME);
  228. if (DEBUGGING(LEVELING)) debug_current(PSTR("Y2"), tmc_save_current_Y2, Y2_CURRENT_HOME);
  229. #endif
  230. #endif
  231. TERN_(IMPROVE_HOMING_RELIABILITY, slow_homing_t slow_homing = begin_slow_homing());
  232. // Always home with tool 0 active
  233. #if HAS_MULTI_HOTEND
  234. #if DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE)
  235. const uint8_t old_tool_index = active_extruder;
  236. #endif
  237. // PARKING_EXTRUDER homing requires different handling of movement / solenoid activation, depending on the side of homing
  238. #if ENABLED(PARKING_EXTRUDER)
  239. const bool pe_final_change_must_unpark = parking_extruder_unpark_after_homing(old_tool_index, X_HOME_DIR + 1 == old_tool_index * 2);
  240. #endif
  241. tool_change(0, true);
  242. #endif
  243. TERN_(HAS_DUPLICATION_MODE, set_duplication_enabled(false));
  244. remember_feedrate_scaling_off();
  245. endstops.enable(true); // Enable endstops for next homing move
  246. #if ENABLED(DELTA)
  247. constexpr bool doZ = true; // for NANODLP_Z_SYNC if your DLP is on a DELTA
  248. home_delta();
  249. TERN_(IMPROVE_HOMING_RELIABILITY, end_slow_homing(slow_homing));
  250. #else // NOT DELTA
  251. const bool homeZ = parser.seen('Z'),
  252. needX = homeZ && TERN0(Z_SAFE_HOMING, axes_should_home(_BV(X_AXIS))),
  253. needY = homeZ && TERN0(Z_SAFE_HOMING, axes_should_home(_BV(Y_AXIS))),
  254. homeX = needX || parser.seen('X'), homeY = needY || parser.seen('Y'),
  255. home_all = homeX == homeY && homeX == homeZ, // All or None
  256. doX = home_all || homeX, doY = home_all || homeY, doZ = home_all || homeZ;
  257. #if ENABLED(HOME_Z_FIRST)
  258. if (doZ) homeaxis(Z_AXIS);
  259. #endif
  260. const float z_homing_height = parser.seenval('R') ? parser.value_linear_units() : Z_HOMING_HEIGHT;
  261. if (z_homing_height && (doX || doY || TERN0(Z_SAFE_HOMING, doZ))) {
  262. // Raise Z before homing any other axes and z is not already high enough (never lower z)
  263. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Raise Z (before homing) by ", z_homing_height);
  264. do_z_clearance(z_homing_height);
  265. TERN_(BLTOUCH, bltouch.init());
  266. }
  267. #if ENABLED(QUICK_HOME)
  268. if (doX && doY) quick_home_xy();
  269. #endif
  270. // Home Y (before X)
  271. if (ENABLED(HOME_Y_BEFORE_X) && (doY || TERN0(CODEPENDENT_XY_HOMING, doX)))
  272. homeaxis(Y_AXIS);
  273. // Home X
  274. if (doX || (doY && ENABLED(CODEPENDENT_XY_HOMING) && DISABLED(HOME_Y_BEFORE_X))) {
  275. #if ENABLED(DUAL_X_CARRIAGE)
  276. // Always home the 2nd (right) extruder first
  277. active_extruder = 1;
  278. homeaxis(X_AXIS);
  279. // Remember this extruder's position for later tool change
  280. inactive_extruder_x = current_position.x;
  281. // Home the 1st (left) extruder
  282. active_extruder = 0;
  283. homeaxis(X_AXIS);
  284. // Consider the active extruder to be in its "parked" position
  285. idex_set_parked();
  286. #else
  287. homeaxis(X_AXIS);
  288. #endif
  289. }
  290. // Home Y (after X)
  291. if (DISABLED(HOME_Y_BEFORE_X) && doY)
  292. homeaxis(Y_AXIS);
  293. TERN_(IMPROVE_HOMING_RELIABILITY, end_slow_homing(slow_homing));
  294. // Home Z last if homing towards the bed
  295. #if DISABLED(HOME_Z_FIRST)
  296. if (doZ) {
  297. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  298. stepper.set_all_z_lock(false);
  299. stepper.set_separate_multi_axis(false);
  300. #endif
  301. TERN(Z_SAFE_HOMING, home_z_safely(), homeaxis(Z_AXIS));
  302. probe.move_z_after_homing();
  303. }
  304. #endif
  305. sync_plan_position();
  306. #endif // !DELTA (G28)
  307. /**
  308. * Preserve DXC mode across a G28 for IDEX printers in DXC_DUPLICATION_MODE.
  309. * This is important because it lets a user use the LCD Panel to set an IDEX Duplication mode, and
  310. * then print a standard GCode file that contains a single print that does a G28 and has no other
  311. * IDEX specific commands in it.
  312. */
  313. #if ENABLED(DUAL_X_CARRIAGE)
  314. if (idex_is_duplicating()) {
  315. TERN_(IMPROVE_HOMING_RELIABILITY, slow_homing = begin_slow_homing());
  316. // Always home the 2nd (right) extruder first
  317. active_extruder = 1;
  318. homeaxis(X_AXIS);
  319. // Remember this extruder's position for later tool change
  320. inactive_extruder_x = current_position.x;
  321. // Home the 1st (left) extruder
  322. active_extruder = 0;
  323. homeaxis(X_AXIS);
  324. // Consider the active extruder to be parked
  325. idex_set_parked();
  326. dual_x_carriage_mode = IDEX_saved_mode;
  327. set_duplication_enabled(IDEX_saved_duplication_state);
  328. TERN_(IMPROVE_HOMING_RELIABILITY, end_slow_homing(slow_homing));
  329. }
  330. #endif // DUAL_X_CARRIAGE
  331. endstops.not_homing();
  332. // Clear endstop state for polled stallGuard endstops
  333. TERN_(SPI_ENDSTOPS, endstops.clear_endstop_state());
  334. #if BOTH(DELTA, DELTA_HOME_TO_SAFE_ZONE)
  335. // move to a height where we can use the full xy-area
  336. do_blocking_move_to_z(delta_clip_start_height);
  337. #endif
  338. TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_restore_state));
  339. restore_feedrate_and_scaling();
  340. // Restore the active tool after homing
  341. #if HAS_MULTI_HOTEND && (DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE))
  342. tool_change(old_tool_index, TERN(PARKING_EXTRUDER, !pe_final_change_must_unpark, DISABLED(DUAL_X_CARRIAGE))); // Do move if one of these
  343. #endif
  344. #if HAS_HOMING_CURRENT
  345. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore driver current...");
  346. #if HAS_CURRENT_HOME(X)
  347. stepperX.rms_current(tmc_save_current_X);
  348. #endif
  349. #if HAS_CURRENT_HOME(X2)
  350. stepperX2.rms_current(tmc_save_current_X2);
  351. #endif
  352. #if HAS_CURRENT_HOME(Y)
  353. stepperY.rms_current(tmc_save_current_Y);
  354. #endif
  355. #if HAS_CURRENT_HOME(Y2)
  356. stepperY2.rms_current(tmc_save_current_Y2);
  357. #endif
  358. #endif
  359. ui.refresh();
  360. TERN_(DWIN_CREALITY_LCD, DWIN_CompletedHoming());
  361. TERN_(EXTENSIBLE_UI, ExtUI::onHomingComplete());
  362. report_current_position();
  363. if (ENABLED(NANODLP_Z_SYNC) && (doZ || ENABLED(NANODLP_ALL_AXIS)))
  364. SERIAL_ECHOLNPGM(STR_Z_MOVE_COMP);
  365. #if HAS_L64XX
  366. // Set L6470 absolute position registers to counts
  367. // constexpr *might* move this to PROGMEM.
  368. // If not, this will need a PROGMEM directive and an accessor.
  369. static constexpr AxisEnum L64XX_axis_xref[MAX_L64XX] = {
  370. X_AXIS, Y_AXIS, Z_AXIS,
  371. X_AXIS, Y_AXIS, Z_AXIS, Z_AXIS,
  372. E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS, E_AXIS
  373. };
  374. for (uint8_t j = 1; j <= L64XX::chain[0]; j++) {
  375. const uint8_t cv = L64XX::chain[j];
  376. L64xxManager.set_param((L64XX_axis_t)cv, L6470_ABS_POS, stepper.position(L64XX_axis_xref[cv]));
  377. }
  378. #endif
  379. }