My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

G28.cpp 15KB

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