My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

G28.cpp 13KB

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