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

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