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

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