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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 <http://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 HOTENDS > 1
  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. #if HOMING_Z_WITH_PROBE || ENABLED(BLTOUCH)
  36. #include "../../module/probe.h"
  37. #endif
  38. #include "../../lcd/ultralcd.h"
  39. #if HAS_DRIVER(L6470) // set L6470 absolute position registers to counts
  40. #include "../../libs/L6470/L6470_Marlin.h"
  41. #endif
  42. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  43. #include "../../core/debug_out.h"
  44. #if ENABLED(QUICK_HOME)
  45. static void quick_home_xy() {
  46. // Pretend the current position is 0,0
  47. current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;
  48. sync_plan_position();
  49. const int x_axis_home_dir =
  50. #if ENABLED(DUAL_X_CARRIAGE)
  51. x_home_dir(active_extruder)
  52. #else
  53. home_dir(X_AXIS)
  54. #endif
  55. ;
  56. const float mlx = max_length(X_AXIS),
  57. mly = max_length(Y_AXIS),
  58. mlratio = mlx > mly ? mly / mlx : mlx / mly,
  59. fr_mm_s = MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0);
  60. #if ENABLED(SENSORLESS_HOMING)
  61. sensorless_t stealth_states { false, false, false, false, false, false, false };
  62. stealth_states.x = tmc_enable_stallguard(stepperX);
  63. stealth_states.y = tmc_enable_stallguard(stepperY);
  64. #if AXIS_HAS_STALLGUARD(X2)
  65. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  66. #endif
  67. #if AXIS_HAS_STALLGUARD(Y2)
  68. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  69. #endif
  70. #endif
  71. do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * home_dir(Y_AXIS), fr_mm_s);
  72. endstops.validate_homing_move();
  73. current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;
  74. #if ENABLED(SENSORLESS_HOMING)
  75. tmc_disable_stallguard(stepperX, stealth_states.x);
  76. tmc_disable_stallguard(stepperY, stealth_states.y);
  77. #if AXIS_HAS_STALLGUARD(X2)
  78. tmc_disable_stallguard(stepperX2, stealth_states.x2);
  79. #endif
  80. #if AXIS_HAS_STALLGUARD(Y2)
  81. tmc_disable_stallguard(stepperY2, stealth_states.y2);
  82. #endif
  83. #endif
  84. }
  85. #endif // QUICK_HOME
  86. #if ENABLED(Z_SAFE_HOMING)
  87. inline void home_z_safely() {
  88. // Disallow Z homing if X or Y are unknown
  89. if (!TEST(axis_known_position, X_AXIS) || !TEST(axis_known_position, Y_AXIS)) {
  90. LCD_MESSAGEPGM(MSG_ERR_Z_HOMING);
  91. SERIAL_ECHO_MSG(MSG_ERR_Z_HOMING);
  92. return;
  93. }
  94. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z_SAFE_HOMING >>>");
  95. sync_plan_position();
  96. /**
  97. * Move the Z probe (or just the nozzle) to the safe homing point
  98. */
  99. destination[X_AXIS] = Z_SAFE_HOMING_X_POINT;
  100. destination[Y_AXIS] = Z_SAFE_HOMING_Y_POINT;
  101. destination[Z_AXIS] = current_position[Z_AXIS]; // Z is already at the right height
  102. #if HOMING_Z_WITH_PROBE
  103. destination[X_AXIS] -= X_PROBE_OFFSET_FROM_EXTRUDER;
  104. destination[Y_AXIS] -= Y_PROBE_OFFSET_FROM_EXTRUDER;
  105. #endif
  106. if (position_is_reachable(destination[X_AXIS], destination[Y_AXIS])) {
  107. if (DEBUGGING(LEVELING)) DEBUG_POS("Z_SAFE_HOMING", destination);
  108. // This causes the carriage on Dual X to unpark
  109. #if ENABLED(DUAL_X_CARRIAGE)
  110. active_extruder_parked = false;
  111. #endif
  112. #if ENABLED(SENSORLESS_HOMING)
  113. safe_delay(500); // Short delay needed to settle
  114. #endif
  115. do_blocking_move_to_xy(destination[X_AXIS], destination[Y_AXIS]);
  116. homeaxis(Z_AXIS);
  117. }
  118. else {
  119. LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
  120. SERIAL_ECHO_MSG(MSG_ZPROBE_OUT);
  121. }
  122. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< Z_SAFE_HOMING");
  123. }
  124. #endif // Z_SAFE_HOMING
  125. /**
  126. * G28: Home all axes according to settings
  127. *
  128. * Parameters
  129. *
  130. * None Home to all axes with no parameters.
  131. * With QUICK_HOME enabled XY will home together, then Z.
  132. *
  133. * O Home only if position is unknown
  134. *
  135. * Rn Raise by n mm/inches before homing
  136. *
  137. * Cartesian/SCARA parameters
  138. *
  139. * X Home to the X endstop
  140. * Y Home to the Y endstop
  141. * Z Home to the Z endstop
  142. *
  143. */
  144. void GcodeSuite::G28(const bool always_home_all) {
  145. if (DEBUGGING(LEVELING)) {
  146. DEBUG_ECHOLNPGM(">>> G28");
  147. log_machine_info();
  148. }
  149. #if ENABLED(DUAL_X_CARRIAGE)
  150. bool IDEX_saved_duplication_state = extruder_duplication_enabled;
  151. DualXMode IDEX_saved_mode = dual_x_carriage_mode;
  152. #endif
  153. #if ENABLED(MARLIN_DEV_MODE)
  154. if (parser.seen('S')) {
  155. LOOP_XYZ(a) set_axis_is_at_home((AxisEnum)a);
  156. sync_plan_position();
  157. SERIAL_ECHOLNPGM("Simulated Homing");
  158. report_current_position();
  159. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G28");
  160. return;
  161. }
  162. #endif
  163. if (parser.boolval('O')) {
  164. if (
  165. #if ENABLED(HOME_AFTER_DEACTIVATE)
  166. all_axes_known() // homing needed anytime steppers deactivate
  167. #else
  168. all_axes_homed() // homing needed only if never homed
  169. #endif
  170. ) {
  171. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> homing not needed, skip\n<<< G28");
  172. return;
  173. }
  174. }
  175. // Wait for planner moves to finish!
  176. planner.synchronize();
  177. // Disable the leveling matrix before homing
  178. #if HAS_LEVELING
  179. // Cancel the active G29 session
  180. #if ENABLED(PROBE_MANUALLY)
  181. g29_in_progress = false;
  182. #endif
  183. #if ENABLED(RESTORE_LEVELING_AFTER_G28)
  184. const bool leveling_was_active = planner.leveling_active;
  185. #endif
  186. set_bed_leveling_enabled(false);
  187. #endif
  188. #if ENABLED(CNC_WORKSPACE_PLANES)
  189. workspace_plane = PLANE_XY;
  190. #endif
  191. // Always home with tool 0 active
  192. #if HOTENDS > 1
  193. #if DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE)
  194. const uint8_t old_tool_index = active_extruder;
  195. #endif
  196. tool_change(0, true);
  197. #endif
  198. #if HAS_DUPLICATION_MODE
  199. extruder_duplication_enabled = false;
  200. #endif
  201. setup_for_endstop_or_probe_move();
  202. endstops.enable(true); // Enable endstops for next homing move
  203. #if ENABLED(DELTA)
  204. home_delta();
  205. UNUSED(always_home_all);
  206. #else // NOT DELTA
  207. const bool homeX = parser.seen('X'), homeY = parser.seen('Y'), homeZ = parser.seen('Z'),
  208. home_all = always_home_all || (homeX == homeY && homeX == homeZ),
  209. doX = home_all || homeX, doY = home_all || homeY, doZ = home_all || homeZ;
  210. set_destination_from_current();
  211. #if HAS_BED_PROBE
  212. STOW_PROBE();
  213. #endif
  214. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  215. if (doZ) homeaxis(Z_AXIS);
  216. #endif
  217. const float z_homing_height = (
  218. #if ENABLED(UNKNOWN_Z_NO_RAISE)
  219. !TEST(axis_known_position, Z_AXIS) ? 0 :
  220. #endif
  221. (parser.seenval('R') ? parser.value_linear_units() : Z_HOMING_HEIGHT)
  222. );
  223. if (z_homing_height && (doX || doY)) {
  224. // Raise Z before homing any other axes and z is not already high enough (never lower z)
  225. destination[Z_AXIS] = z_homing_height;
  226. if (destination[Z_AXIS] > current_position[Z_AXIS]) {
  227. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Raise Z (before homing) to ", destination[Z_AXIS]);
  228. do_blocking_move_to_z(destination[Z_AXIS]);
  229. }
  230. }
  231. #if ENABLED(QUICK_HOME)
  232. if (doX && doY) quick_home_xy();
  233. #endif
  234. // Home Y (before X)
  235. #if ENABLED(HOME_Y_BEFORE_X)
  236. if (doY
  237. #if ENABLED(CODEPENDENT_XY_HOMING)
  238. || doX
  239. #endif
  240. ) homeaxis(Y_AXIS);
  241. #endif
  242. // Home X
  243. if (doX
  244. #if ENABLED(CODEPENDENT_XY_HOMING) && DISABLED(HOME_Y_BEFORE_X)
  245. || doY
  246. #endif
  247. ) {
  248. #if ENABLED(DUAL_X_CARRIAGE)
  249. // Always home the 2nd (right) extruder first
  250. active_extruder = 1;
  251. homeaxis(X_AXIS);
  252. // Remember this extruder's position for later tool change
  253. inactive_extruder_x_pos = current_position[X_AXIS];
  254. // Home the 1st (left) extruder
  255. active_extruder = 0;
  256. homeaxis(X_AXIS);
  257. // Consider the active extruder to be parked
  258. COPY(raised_parked_position, current_position);
  259. delayed_move_time = 0;
  260. active_extruder_parked = true;
  261. #else
  262. homeaxis(X_AXIS);
  263. #endif
  264. }
  265. // Home Y (after X)
  266. #if DISABLED(HOME_Y_BEFORE_X)
  267. if (doY) homeaxis(Y_AXIS);
  268. #endif
  269. // Home Z last if homing towards the bed
  270. #if Z_HOME_DIR < 0
  271. if (doZ) {
  272. #if ENABLED(Z_SAFE_HOMING)
  273. home_z_safely();
  274. #else
  275. homeaxis(Z_AXIS);
  276. #endif
  277. #if HOMING_Z_WITH_PROBE && defined(Z_AFTER_PROBING)
  278. move_z_after_probing();
  279. #endif
  280. } // doZ
  281. #endif // Z_HOME_DIR < 0
  282. sync_plan_position();
  283. #endif // !DELTA (G28)
  284. /**
  285. * Preserve DXC mode across a G28 for IDEX printers in DXC_DUPLICATION_MODE.
  286. * This is important because it lets a user use the LCD Panel to set an IDEX Duplication mode, and
  287. * then print a standard GCode file that contains a single print that does a G28 and has no other
  288. * IDEX specific commands in it.
  289. */
  290. #if ENABLED(DUAL_X_CARRIAGE)
  291. if (dxc_is_duplicating()) {
  292. // Always home the 2nd (right) extruder first
  293. active_extruder = 1;
  294. homeaxis(X_AXIS);
  295. // Remember this extruder's position for later tool change
  296. inactive_extruder_x_pos = current_position[X_AXIS];
  297. // Home the 1st (left) extruder
  298. active_extruder = 0;
  299. homeaxis(X_AXIS);
  300. // Consider the active extruder to be parked
  301. COPY(raised_parked_position, current_position);
  302. delayed_move_time = 0;
  303. active_extruder_parked = true;
  304. extruder_duplication_enabled = IDEX_saved_duplication_state;
  305. extruder_duplication_enabled = false;
  306. dual_x_carriage_mode = IDEX_saved_mode;
  307. stepper.set_directions();
  308. }
  309. #endif // DUAL_X_CARRIAGE
  310. #ifdef HOMING_BACKOFF_MM
  311. endstops.enable(false);
  312. constexpr float endstop_backoff[XYZ] = HOMING_BACKOFF_MM;
  313. const float backoff_x = doX ? ABS(endstop_backoff[X_AXIS]) * (X_HOME_DIR) : 0,
  314. backoff_y = doY ? ABS(endstop_backoff[Y_AXIS]) * (Y_HOME_DIR) : 0,
  315. backoff_z = doZ ? ABS(endstop_backoff[Z_AXIS]) * (Z_HOME_DIR) : 0;
  316. if (backoff_z) do_blocking_move_to_z(current_position[Z_AXIS] - backoff_z);
  317. if (backoff_x || backoff_y) do_blocking_move_to_xy(current_position[X_AXIS] - backoff_x, current_position[Y_AXIS] - backoff_y);
  318. #endif
  319. endstops.not_homing();
  320. #if BOTH(DELTA, DELTA_HOME_TO_SAFE_ZONE)
  321. // move to a height where we can use the full xy-area
  322. do_blocking_move_to_z(delta_clip_start_height);
  323. #endif
  324. #if HAS_LEVELING && ENABLED(RESTORE_LEVELING_AFTER_G28)
  325. set_bed_leveling_enabled(leveling_was_active);
  326. #endif
  327. clean_up_after_endstop_or_probe_move();
  328. // Restore the active tool after homing
  329. #if HOTENDS > 1 && (DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE))
  330. #if EITHER(PARKING_EXTRUDER, DUAL_X_CARRIAGE)
  331. #define NO_FETCH false // fetch the previous toolhead
  332. #else
  333. #define NO_FETCH true
  334. #endif
  335. tool_change(old_tool_index, NO_FETCH);
  336. #endif
  337. ui.refresh();
  338. report_current_position();
  339. #if ENABLED(NANODLP_Z_SYNC)
  340. #if ENABLED(NANODLP_ALL_AXIS)
  341. #define _HOME_SYNC true // For any axis, output sync text.
  342. #else
  343. #define _HOME_SYNC doZ // Only for Z-axis
  344. #endif
  345. if (_HOME_SYNC)
  346. SERIAL_ECHOLNPGM(MSG_Z_MOVE_COMP);
  347. #endif
  348. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< G28");
  349. #if HAS_DRIVER(L6470)
  350. // Set L6470 absolute position registers to counts
  351. for (uint8_t j = 1; j <= L6470::chain[0]; j++) {
  352. const uint8_t cv = L6470::chain[j];
  353. L6470.set_param(cv, L6470_ABS_POS, stepper.position((AxisEnum)L6470.axis_xref[cv]));
  354. }
  355. #endif
  356. }