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.

tool_change.cpp 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #include "tool_change.h"
  24. #include "probe.h"
  25. #include "motion.h"
  26. #include "planner.h"
  27. #include "temperature.h"
  28. #include "../MarlinCore.h"
  29. //#define DEBUG_TOOL_CHANGE
  30. #define DEBUG_OUT ENABLED(DEBUG_TOOL_CHANGE)
  31. #include "../core/debug_out.h"
  32. #if HAS_MULTI_EXTRUDER
  33. toolchange_settings_t toolchange_settings; // Initialized by settings.load()
  34. #endif
  35. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  36. migration_settings_t migration = migration_defaults;
  37. bool enable_first_prime;
  38. #endif
  39. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  40. bool toolchange_extruder_ready[EXTRUDERS];
  41. #endif
  42. #if EITHER(MAGNETIC_PARKING_EXTRUDER, TOOL_SENSOR) \
  43. || defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_AFTER_TOOLCHANGE) \
  44. || (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0)
  45. #include "../gcode/gcode.h"
  46. #endif
  47. #if ENABLED(TOOL_SENSOR)
  48. #include "../lcd/marlinui.h"
  49. #endif
  50. #if ENABLED(DUAL_X_CARRIAGE)
  51. #include "stepper.h"
  52. #endif
  53. #if ANY(SWITCHING_EXTRUDER, SWITCHING_NOZZLE, SWITCHING_TOOLHEAD)
  54. #include "servo.h"
  55. #endif
  56. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  57. #include "../feature/solenoid.h"
  58. #endif
  59. #if ENABLED(MIXING_EXTRUDER)
  60. #include "../feature/mixing.h"
  61. #endif
  62. #if HAS_LEVELING
  63. #include "../feature/bedlevel/bedlevel.h"
  64. #endif
  65. #if HAS_FANMUX
  66. #include "../feature/fanmux.h"
  67. #endif
  68. #if HAS_PRUSA_MMU1
  69. #include "../feature/mmu/mmu.h"
  70. #elif HAS_PRUSA_MMU2
  71. #include "../feature/mmu/mmu2.h"
  72. #endif
  73. #if HAS_MARLINUI_MENU
  74. #include "../lcd/marlinui.h"
  75. #endif
  76. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  77. #include "../feature/pause.h"
  78. #endif
  79. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  80. #include "../gcode/gcode.h"
  81. #if TOOLCHANGE_FS_WIPE_RETRACT <= 0
  82. #undef TOOLCHANGE_FS_WIPE_RETRACT
  83. #define TOOLCHANGE_FS_WIPE_RETRACT 0
  84. #endif
  85. #endif
  86. #if DO_SWITCH_EXTRUDER
  87. #if EXTRUDERS > 3
  88. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  89. #else
  90. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  91. #endif
  92. void move_extruder_servo(const uint8_t e) {
  93. planner.synchronize();
  94. if ((EXTRUDERS & 1) && e < EXTRUDERS - 1) {
  95. MOVE_SERVO(_SERVO_NR(e), servo_angles[_SERVO_NR(e)][e & 1]);
  96. safe_delay(500);
  97. }
  98. }
  99. #endif // DO_SWITCH_EXTRUDER
  100. #if ENABLED(SWITCHING_NOZZLE)
  101. #if SWITCHING_NOZZLE_TWO_SERVOS
  102. inline void _move_nozzle_servo(const uint8_t e, const uint8_t angle_index) {
  103. constexpr int8_t sns_index[2] = { SWITCHING_NOZZLE_SERVO_NR, SWITCHING_NOZZLE_E1_SERVO_NR };
  104. constexpr int16_t sns_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
  105. planner.synchronize();
  106. MOVE_SERVO(sns_index[e], sns_angles[angle_index]);
  107. safe_delay(500);
  108. }
  109. void lower_nozzle(const uint8_t e) { _move_nozzle_servo(e, 0); }
  110. void raise_nozzle(const uint8_t e) { _move_nozzle_servo(e, 1); }
  111. #else
  112. void move_nozzle_servo(const uint8_t angle_index) {
  113. planner.synchronize();
  114. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  115. safe_delay(500);
  116. }
  117. #endif
  118. #endif // SWITCHING_NOZZLE
  119. void _line_to_current(const AxisEnum fr_axis, const float fscale=1) {
  120. line_to_current_position(planner.settings.max_feedrate_mm_s[fr_axis] * fscale);
  121. }
  122. void slow_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.2f); }
  123. void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.5f); }
  124. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  125. float parkingposx[2], // M951 R L
  126. parkinggrabdistance, // M951 I
  127. parkingslowspeed, // M951 J
  128. parkinghighspeed, // M951 H
  129. parkingtraveldistance, // M951 D
  130. compensationmultiplier;
  131. inline void magnetic_parking_extruder_tool_change(const uint8_t new_tool) {
  132. const float oldx = current_position.x,
  133. grabpos = mpe_settings.parking_xpos[new_tool] + (new_tool ? mpe_settings.grab_distance : -mpe_settings.grab_distance),
  134. offsetcompensation = TERN0(HAS_HOTEND_OFFSET, hotend_offset[active_extruder].x * mpe_settings.compensation_factor);
  135. if (homing_needed_error(_BV(X_AXIS))) return;
  136. /**
  137. * Z Lift and Nozzle Offset shift ar defined in caller method to work equal with any Multi Hotend realization
  138. *
  139. * Steps:
  140. * 1. Move high speed to park position of new extruder
  141. * 2. Move to couple position of new extruder (this also discouple the old extruder)
  142. * 3. Move to park position of new extruder
  143. * 4. Move high speed to approach park position of old extruder
  144. * 5. Move to park position of old extruder
  145. * 6. Move to starting position
  146. */
  147. // STEP 1
  148. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  149. DEBUG_ECHOPGM("(1) Move extruder ", new_tool);
  150. DEBUG_POS(" to new extruder ParkPos", current_position);
  151. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  152. planner.synchronize();
  153. // STEP 2
  154. current_position.x = grabpos + offsetcompensation;
  155. DEBUG_ECHOPGM("(2) Couple extruder ", new_tool);
  156. DEBUG_POS(" to new extruder GrabPos", current_position);
  157. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  158. planner.synchronize();
  159. // Delay before moving tool, to allow magnetic coupling
  160. gcode.dwell(150);
  161. // STEP 3
  162. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  163. DEBUG_ECHOPGM("(3) Move extruder ", new_tool);
  164. DEBUG_POS(" back to new extruder ParkPos", current_position);
  165. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  166. planner.synchronize();
  167. // STEP 4
  168. current_position.x = mpe_settings.parking_xpos[active_extruder] + (active_extruder == 0 ? MPE_TRAVEL_DISTANCE : -MPE_TRAVEL_DISTANCE) + offsetcompensation;
  169. DEBUG_ECHOPGM("(4) Move extruder ", new_tool);
  170. DEBUG_POS(" close to old extruder ParkPos", current_position);
  171. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  172. planner.synchronize();
  173. // STEP 5
  174. current_position.x = mpe_settings.parking_xpos[active_extruder] + offsetcompensation;
  175. DEBUG_ECHOPGM("(5) Park extruder ", new_tool);
  176. DEBUG_POS(" at old extruder ParkPos", current_position);
  177. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  178. planner.synchronize();
  179. // STEP 6
  180. current_position.x = oldx;
  181. DEBUG_ECHOPGM("(6) Move extruder ", new_tool);
  182. DEBUG_POS(" to starting position", current_position);
  183. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  184. planner.synchronize();
  185. DEBUG_ECHOLNPGM("Autopark done.");
  186. }
  187. #elif ENABLED(PARKING_EXTRUDER)
  188. void pe_solenoid_init() {
  189. LOOP_LE_N(n, 1) pe_solenoid_set_pin_state(n, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  190. }
  191. void pe_solenoid_set_pin_state(const uint8_t extruder_num, const uint8_t state) {
  192. switch (extruder_num) {
  193. case 1: OUT_WRITE(SOL1_PIN, state); break;
  194. default: OUT_WRITE(SOL0_PIN, state); break;
  195. }
  196. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  197. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  198. #endif
  199. }
  200. bool extruder_parked = true, do_solenoid_activation = true;
  201. // Modifies tool_change() behavior based on homing side
  202. bool parking_extruder_unpark_after_homing(const uint8_t final_tool, bool homed_towards_final_tool) {
  203. do_solenoid_activation = false; // Tell parking_extruder_tool_change to skip solenoid activation
  204. if (!extruder_parked) return false; // nothing to do
  205. if (homed_towards_final_tool) {
  206. pe_solenoid_magnet_off(1 - final_tool);
  207. DEBUG_ECHOLNPGM("Disengage magnet", 1 - final_tool);
  208. pe_solenoid_magnet_on(final_tool);
  209. DEBUG_ECHOLNPGM("Engage magnet", final_tool);
  210. parking_extruder_set_parked(false);
  211. return false;
  212. }
  213. return true;
  214. }
  215. inline void parking_extruder_tool_change(const uint8_t new_tool, bool no_move) {
  216. if (!no_move) {
  217. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  218. #if HAS_HOTEND_OFFSET
  219. const float x_offset = hotend_offset[active_extruder].x;
  220. #else
  221. constexpr float x_offset = 0;
  222. #endif
  223. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5f + x_offset,
  224. grabpos = parkingposx[new_tool] + (new_tool ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  225. /**
  226. * 1. Move to park position of old extruder
  227. * 2. Disengage magnetic field, wait for delay
  228. * 3. Move near new extruder
  229. * 4. Engage magnetic field for new extruder
  230. * 5. Move to parking incl. offset of new extruder
  231. * 6. Lower Z-Axis
  232. */
  233. // STEP 1
  234. DEBUG_POS("Start PE Tool-Change", current_position);
  235. // Don't park the active_extruder unless unparked
  236. if (!extruder_parked) {
  237. current_position.x = parkingposx[active_extruder] + x_offset;
  238. DEBUG_ECHOLNPGM("(1) Park extruder ", active_extruder);
  239. DEBUG_POS("Moving ParkPos", current_position);
  240. fast_line_to_current(X_AXIS);
  241. // STEP 2
  242. planner.synchronize();
  243. DEBUG_ECHOLNPGM("(2) Disengage magnet");
  244. pe_solenoid_magnet_off(active_extruder);
  245. // STEP 3
  246. current_position.x += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  247. DEBUG_ECHOLNPGM("(3) Move near new extruder");
  248. DEBUG_POS("Move away from parked extruder", current_position);
  249. fast_line_to_current(X_AXIS);
  250. }
  251. // STEP 4
  252. planner.synchronize();
  253. DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  254. // Just save power for inverted magnets
  255. TERN_(PARKING_EXTRUDER_SOLENOIDS_INVERT, pe_solenoid_magnet_on(active_extruder));
  256. pe_solenoid_magnet_on(new_tool);
  257. // STEP 5
  258. current_position.x = grabpos + (new_tool ? -10 : 10);
  259. fast_line_to_current(X_AXIS);
  260. current_position.x = grabpos;
  261. DEBUG_SYNCHRONIZE();
  262. DEBUG_POS("(5) Unpark extruder", current_position);
  263. slow_line_to_current(X_AXIS);
  264. // STEP 6
  265. current_position.x = DIFF_TERN(HAS_HOTEND_OFFSET, midpos, hotend_offset[new_tool].x);
  266. DEBUG_SYNCHRONIZE();
  267. DEBUG_POS("(6) Move midway between hotends", current_position);
  268. fast_line_to_current(X_AXIS);
  269. planner.synchronize(); // Always sync the final move
  270. DEBUG_POS("PE Tool-Change done.", current_position);
  271. parking_extruder_set_parked(false);
  272. }
  273. else if (do_solenoid_activation) {
  274. // Deactivate current extruder solenoid
  275. pe_solenoid_set_pin_state(active_extruder, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  276. // Engage new extruder magnetic field
  277. pe_solenoid_set_pin_state(new_tool, PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  278. }
  279. do_solenoid_activation = true; // Activate solenoid for subsequent tool_change()
  280. }
  281. #endif // PARKING_EXTRUDER
  282. #if ENABLED(SWITCHING_TOOLHEAD)
  283. // Return a bitmask of tool sensor states
  284. inline uint8_t poll_tool_sensor_pins() {
  285. return (0
  286. #if ENABLED(TOOL_SENSOR)
  287. #if PIN_EXISTS(TOOL_SENSOR1)
  288. | (READ(TOOL_SENSOR1_PIN) << 0)
  289. #endif
  290. #if PIN_EXISTS(TOOL_SENSOR2)
  291. | (READ(TOOL_SENSOR2_PIN) << 1)
  292. #endif
  293. #if PIN_EXISTS(TOOL_SENSOR3)
  294. | (READ(TOOL_SENSOR3_PIN) << 2)
  295. #endif
  296. #if PIN_EXISTS(TOOL_SENSOR4)
  297. | (READ(TOOL_SENSOR4_PIN) << 3)
  298. #endif
  299. #if PIN_EXISTS(TOOL_SENSOR5)
  300. | (READ(TOOL_SENSOR5_PIN) << 4)
  301. #endif
  302. #if PIN_EXISTS(TOOL_SENSOR6)
  303. | (READ(TOOL_SENSOR6_PIN) << 5)
  304. #endif
  305. #if PIN_EXISTS(TOOL_SENSOR7)
  306. | (READ(TOOL_SENSOR7_PIN) << 6)
  307. #endif
  308. #if PIN_EXISTS(TOOL_SENSOR8)
  309. | (READ(TOOL_SENSOR8_PIN) << 7)
  310. #endif
  311. #endif
  312. );
  313. }
  314. #if ENABLED(TOOL_SENSOR)
  315. bool tool_sensor_disabled; // = false
  316. uint8_t check_tool_sensor_stats(const uint8_t tool_index, const bool kill_on_error/*=false*/, const bool disable/*=false*/) {
  317. static uint8_t sensor_tries; // = 0
  318. for (;;) {
  319. if (poll_tool_sensor_pins() == _BV(tool_index)) {
  320. sensor_tries = 0;
  321. return tool_index;
  322. }
  323. else if (kill_on_error && (!tool_sensor_disabled || disable)) {
  324. sensor_tries++;
  325. if (sensor_tries > 10) kill(F("Tool Sensor error"));
  326. safe_delay(5);
  327. }
  328. else {
  329. sensor_tries++;
  330. if (sensor_tries > 10) return -1;
  331. safe_delay(5);
  332. }
  333. }
  334. }
  335. #endif
  336. inline void switching_toolhead_lock(const bool locked) {
  337. #ifdef SWITCHING_TOOLHEAD_SERVO_ANGLES
  338. const uint16_t swt_angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  339. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, swt_angles[locked ? 0 : 1]);
  340. #elif PIN_EXISTS(SWT_SOLENOID)
  341. OUT_WRITE(SWT_SOLENOID_PIN, locked);
  342. gcode.dwell(10);
  343. #else
  344. #error "No toolhead locking mechanism configured."
  345. #endif
  346. }
  347. #include <bitset>
  348. void swt_init() {
  349. switching_toolhead_lock(true);
  350. #if ENABLED(TOOL_SENSOR)
  351. // Init tool sensors
  352. #if PIN_EXISTS(TOOL_SENSOR1)
  353. SET_INPUT_PULLUP(TOOL_SENSOR1_PIN);
  354. #endif
  355. #if PIN_EXISTS(TOOL_SENSOR2)
  356. SET_INPUT_PULLUP(TOOL_SENSOR2_PIN);
  357. #endif
  358. #if PIN_EXISTS(TOOL_SENSOR3)
  359. SET_INPUT_PULLUP(TOOL_SENSOR3_PIN);
  360. #endif
  361. #if PIN_EXISTS(TOOL_SENSOR4)
  362. SET_INPUT_PULLUP(TOOL_SENSOR4_PIN);
  363. #endif
  364. #if PIN_EXISTS(TOOL_SENSOR5)
  365. SET_INPUT_PULLUP(TOOL_SENSOR5_PIN);
  366. #endif
  367. #if PIN_EXISTS(TOOL_SENSOR6)
  368. SET_INPUT_PULLUP(TOOL_SENSOR6_PIN);
  369. #endif
  370. #if PIN_EXISTS(TOOL_SENSOR7)
  371. SET_INPUT_PULLUP(TOOL_SENSOR7_PIN);
  372. #endif
  373. #if PIN_EXISTS(TOOL_SENSOR8)
  374. SET_INPUT_PULLUP(TOOL_SENSOR8_PIN);
  375. #endif
  376. if (check_tool_sensor_stats(0)) {
  377. LCD_MESSAGE_F("TC error");
  378. switching_toolhead_lock(false);
  379. while (check_tool_sensor_stats(0)) { /* nada */ }
  380. switching_toolhead_lock(true);
  381. }
  382. LCD_MESSAGE_F("TC Success");
  383. #endif
  384. }
  385. inline void switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  386. if (no_move) return;
  387. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  388. const float placexpos = toolheadposx[active_extruder],
  389. grabxpos = toolheadposx[new_tool];
  390. (void)check_tool_sensor_stats(active_extruder, true);
  391. /**
  392. * 1. Move to switch position of current toolhead
  393. * 2. Unlock tool and drop it in the dock
  394. * 3. Move to the new toolhead
  395. * 4. Grab and lock the new toolhead
  396. */
  397. // 1. Move to switch position of current toolhead
  398. DEBUG_POS("Start ST Tool-Change", current_position);
  399. current_position.x = placexpos;
  400. DEBUG_ECHOLNPGM("(1) Place old tool ", active_extruder);
  401. DEBUG_POS("Move X SwitchPos", current_position);
  402. fast_line_to_current(X_AXIS);
  403. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  404. DEBUG_SYNCHRONIZE();
  405. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  406. slow_line_to_current(Y_AXIS);
  407. // 2. Unlock tool and drop it in the dock
  408. TERN_(TOOL_SENSOR, tool_sensor_disabled = true);
  409. planner.synchronize();
  410. DEBUG_ECHOLNPGM("(2) Unlock and Place Toolhead");
  411. switching_toolhead_lock(false);
  412. safe_delay(500);
  413. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  414. DEBUG_POS("Move Y SwitchPos", current_position);
  415. slow_line_to_current(Y_AXIS);
  416. // Wait for move to complete, then another 0.2s
  417. planner.synchronize();
  418. safe_delay(200);
  419. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  420. DEBUG_POS("Move back Y clear", current_position);
  421. slow_line_to_current(Y_AXIS); // move away from docked toolhead
  422. (void)check_tool_sensor_stats(active_extruder);
  423. // 3. Move to the new toolhead
  424. current_position.x = grabxpos;
  425. DEBUG_SYNCHRONIZE();
  426. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  427. DEBUG_POS("Move to new toolhead X", current_position);
  428. fast_line_to_current(X_AXIS);
  429. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  430. DEBUG_SYNCHRONIZE();
  431. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  432. slow_line_to_current(Y_AXIS);
  433. // 4. Grab and lock the new toolhead
  434. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  435. DEBUG_SYNCHRONIZE();
  436. DEBUG_ECHOLNPGM("(4) Grab and lock new toolhead");
  437. DEBUG_POS("Move Y SwitchPos", current_position);
  438. slow_line_to_current(Y_AXIS);
  439. // Wait for move to finish, pause 0.2s, move servo, pause 0.5s
  440. planner.synchronize();
  441. safe_delay(200);
  442. (void)check_tool_sensor_stats(new_tool, true, true);
  443. switching_toolhead_lock(true);
  444. safe_delay(500);
  445. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  446. DEBUG_POS("Move back Y clear", current_position);
  447. slow_line_to_current(Y_AXIS); // Move away from docked toolhead
  448. planner.synchronize(); // Always sync the final move
  449. (void)check_tool_sensor_stats(new_tool, true, true);
  450. DEBUG_POS("ST Tool-Change done.", current_position);
  451. }
  452. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  453. inline void magnetic_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  454. if (no_move) return;
  455. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  456. toolheadclearx[] = SWITCHING_TOOLHEAD_X_SECURITY;
  457. const float placexpos = toolheadposx[active_extruder],
  458. placexclear = toolheadclearx[active_extruder],
  459. grabxpos = toolheadposx[new_tool],
  460. grabxclear = toolheadclearx[new_tool];
  461. /**
  462. * 1. Move to switch position of current toolhead
  463. * 2. Release and place toolhead in the dock
  464. * 3. Move to the new toolhead
  465. * 4. Grab the new toolhead and move to security position
  466. */
  467. DEBUG_POS("Start MST Tool-Change", current_position);
  468. // 1. Move to switch position current toolhead
  469. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR;
  470. SERIAL_ECHOLNPGM("(1) Place old tool ", active_extruder);
  471. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  472. fast_line_to_current(Y_AXIS);
  473. current_position.x = placexclear;
  474. DEBUG_SYNCHRONIZE();
  475. DEBUG_POS("Move X SwitchPos + Security", current_position);
  476. fast_line_to_current(X_AXIS);
  477. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  478. DEBUG_SYNCHRONIZE();
  479. DEBUG_POS("Move Y SwitchPos", current_position);
  480. fast_line_to_current(Y_AXIS);
  481. current_position.x = placexpos;
  482. DEBUG_SYNCHRONIZE();
  483. DEBUG_POS("Move X SwitchPos", current_position);
  484. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS] * 0.25f);
  485. // 2. Release and place toolhead in the dock
  486. DEBUG_SYNCHRONIZE();
  487. DEBUG_ECHOLNPGM("(2) Release and Place Toolhead");
  488. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  489. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  490. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.1f);
  491. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_SECURITY;
  492. DEBUG_SYNCHRONIZE();
  493. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  494. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  495. // 3. Move to new toolhead position
  496. DEBUG_SYNCHRONIZE();
  497. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  498. current_position.x = grabxpos;
  499. DEBUG_POS("Move to new toolhead X", current_position);
  500. fast_line_to_current(X_AXIS);
  501. // 4. Grab the new toolhead and move to security position
  502. DEBUG_SYNCHRONIZE();
  503. DEBUG_ECHOLNPGM("(4) Grab new toolhead, move to security position");
  504. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  505. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  506. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  507. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  508. DEBUG_SYNCHRONIZE();
  509. DEBUG_POS("Move Y SwitchPos", current_position);
  510. _line_to_current(Y_AXIS, 0.2f);
  511. #if ENABLED(PRIME_BEFORE_REMOVE) && (SWITCHING_TOOLHEAD_PRIME_MM || SWITCHING_TOOLHEAD_RETRACT_MM)
  512. #if SWITCHING_TOOLHEAD_PRIME_MM
  513. current_position.e += SWITCHING_TOOLHEAD_PRIME_MM;
  514. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_PRIME_FEEDRATE), new_tool);
  515. #endif
  516. #if SWITCHING_TOOLHEAD_RETRACT_MM
  517. current_position.e -= SWITCHING_TOOLHEAD_RETRACT_MM;
  518. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_RETRACT_FEEDRATE), new_tool);
  519. #endif
  520. #else
  521. planner.synchronize();
  522. safe_delay(100); // Give switch time to settle
  523. #endif
  524. current_position.x = grabxclear;
  525. DEBUG_POS("Move to new toolhead X + Security", current_position);
  526. _line_to_current(X_AXIS, 0.1f);
  527. planner.synchronize();
  528. safe_delay(100); // Give switch time to settle
  529. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  530. DEBUG_POS("Move back Y clear", current_position);
  531. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  532. planner.synchronize(); // Always sync last tool-change move
  533. DEBUG_POS("MST Tool-Change done.", current_position);
  534. }
  535. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  536. inline void est_activate_solenoid() { OUT_WRITE(SOL0_PIN, HIGH); }
  537. inline void est_deactivate_solenoid() { OUT_WRITE(SOL0_PIN, LOW); }
  538. void est_init() { est_activate_solenoid(); }
  539. inline void em_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move) {
  540. if (no_move) return;
  541. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  542. const float placexpos = toolheadposx[active_extruder],
  543. grabxpos = toolheadposx[new_tool];
  544. const xyz_pos_t &hoffs = hotend_offset[active_extruder];
  545. /**
  546. * 1. Raise Z-Axis to give enough clearance
  547. * 2. Move to position near active extruder parking
  548. * 3. Move gently to park position of active extruder
  549. * 4. Disengage magnetic field, wait for delay
  550. * 5. Leave extruder and move to position near new extruder parking
  551. * 6. Move gently to park position of new extruder
  552. * 7. Engage magnetic field for new extruder parking
  553. * 8. Unpark extruder
  554. * 9. Apply Z hotend offset to current position
  555. */
  556. DEBUG_POS("Start EMST Tool-Change", current_position);
  557. // 1. Raise Z-Axis to give enough clearance
  558. current_position.z += SWITCHING_TOOLHEAD_Z_HOP;
  559. DEBUG_POS("(1) Raise Z-Axis ", current_position);
  560. fast_line_to_current(Z_AXIS);
  561. // 2. Move to position near active extruder parking
  562. DEBUG_SYNCHRONIZE();
  563. DEBUG_ECHOLNPGM("(2) Move near active extruder parking", active_extruder);
  564. DEBUG_POS("Moving ParkPos", current_position);
  565. current_position.set(hoffs.x + placexpos,
  566. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  567. fast_line_to_current(X_AXIS);
  568. // 3. Move gently to park position of active extruder
  569. DEBUG_SYNCHRONIZE();
  570. SERIAL_ECHOLNPGM("(3) Move gently to park position of active extruder", active_extruder);
  571. DEBUG_POS("Moving ParkPos", current_position);
  572. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  573. slow_line_to_current(Y_AXIS);
  574. // 4. Disengage magnetic field, wait for delay
  575. planner.synchronize();
  576. DEBUG_ECHOLNPGM("(4) Disengage magnet");
  577. est_deactivate_solenoid();
  578. // 5. Leave extruder and move to position near new extruder parking
  579. DEBUG_ECHOLNPGM("(5) Move near new extruder parking");
  580. DEBUG_POS("Moving ParkPos", current_position);
  581. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  582. slow_line_to_current(Y_AXIS);
  583. current_position.set(hoffs.x + grabxpos,
  584. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  585. fast_line_to_current(X_AXIS);
  586. // 6. Move gently to park position of new extruder
  587. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  588. if (DEBUGGING(LEVELING)) {
  589. planner.synchronize();
  590. DEBUG_ECHOLNPGM("(6) Move near new extruder");
  591. }
  592. slow_line_to_current(Y_AXIS);
  593. // 7. Engage magnetic field for new extruder parking
  594. DEBUG_SYNCHRONIZE();
  595. DEBUG_ECHOLNPGM("(7) Engage magnetic field");
  596. est_activate_solenoid();
  597. // 8. Unpark extruder
  598. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  599. DEBUG_ECHOLNPGM("(8) Unpark extruder");
  600. slow_line_to_current(X_AXIS);
  601. planner.synchronize(); // Always sync the final move
  602. // 9. Apply Z hotend offset to current position
  603. DEBUG_POS("(9) Applying Z-offset", current_position);
  604. current_position.z += hoffs.z - hotend_offset[new_tool].z;
  605. DEBUG_POS("EMST Tool-Change done.", current_position);
  606. }
  607. #endif // ELECTROMAGNETIC_SWITCHING_TOOLHEAD
  608. #if HAS_EXTRUDERS
  609. inline void invalid_extruder_error(const uint8_t e) {
  610. SERIAL_ECHO_START();
  611. SERIAL_CHAR('T'); SERIAL_ECHO(e);
  612. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER);
  613. }
  614. #endif
  615. #if ENABLED(DUAL_X_CARRIAGE)
  616. /**
  617. * @brief Dual X Tool Change
  618. * @details Change tools, with extra behavior based on current mode
  619. *
  620. * @param new_tool Tool index to activate
  621. * @param no_move Flag indicating no moves should take place
  622. */
  623. inline void dualx_tool_change(const uint8_t new_tool, bool &no_move) {
  624. DEBUG_ECHOPGM("Dual X Carriage Mode ");
  625. switch (dual_x_carriage_mode) {
  626. case DXC_FULL_CONTROL_MODE: DEBUG_ECHOLNPGM("FULL_CONTROL"); break;
  627. case DXC_AUTO_PARK_MODE: DEBUG_ECHOLNPGM("AUTO_PARK"); break;
  628. case DXC_DUPLICATION_MODE: DEBUG_ECHOLNPGM("DUPLICATION"); break;
  629. case DXC_MIRRORED_MODE: DEBUG_ECHOLNPGM("MIRRORED"); break;
  630. }
  631. // Get the home position of the currently-active tool
  632. const float xhome = x_home_pos(active_extruder);
  633. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE // If Auto-Park mode is enabled
  634. && IsRunning() && !no_move // ...and movement is permitted
  635. && (delayed_move_time || current_position.x != xhome) // ...and delayed_move_time is set OR not "already parked"...
  636. ) {
  637. DEBUG_ECHOLNPGM("MoveX to ", xhome);
  638. current_position.x = xhome;
  639. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS]); // Park the current head
  640. planner.synchronize();
  641. }
  642. // Activate the new extruder ahead of calling set_axis_is_at_home!
  643. active_extruder = new_tool;
  644. // This function resets the max/min values - the current position may be overwritten below.
  645. set_axis_is_at_home(X_AXIS);
  646. DEBUG_POS("New Extruder", current_position);
  647. switch (dual_x_carriage_mode) {
  648. case DXC_FULL_CONTROL_MODE:
  649. // New current position is the position of the activated extruder
  650. current_position.x = inactive_extruder_x;
  651. // Save the inactive extruder's position (from the old current_position)
  652. inactive_extruder_x = destination.x;
  653. DEBUG_ECHOLNPGM("DXC Full Control curr.x=", current_position.x, " dest.x=", destination.x);
  654. break;
  655. case DXC_AUTO_PARK_MODE:
  656. idex_set_parked();
  657. break;
  658. default:
  659. break;
  660. }
  661. // Ensure X axis DIR pertains to the correct carriage
  662. stepper.set_directions();
  663. DEBUG_ECHOLNPGM("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  664. DEBUG_POS("New extruder (parked)", current_position);
  665. }
  666. #endif // DUAL_X_CARRIAGE
  667. /**
  668. * Prime active tool using TOOLCHANGE_FILAMENT_SWAP settings
  669. */
  670. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  671. void tool_change_prime() {
  672. if (toolchange_settings.extra_prime > 0
  673. && TERN(PREVENT_COLD_EXTRUSION, !thermalManager.targetTooColdToExtrude(active_extruder), 1)
  674. ) {
  675. destination = current_position; // Remember the old position
  676. const bool ok = TERN1(TOOLCHANGE_PARK, all_axes_homed() && toolchange_settings.enable_park);
  677. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  678. // Store and stop fan. Restored on any exit.
  679. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  680. #endif
  681. // Z raise
  682. if (ok) {
  683. // Do a small lift to avoid the workpiece in the move back (below)
  684. current_position.z += toolchange_settings.z_raise;
  685. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  686. fast_line_to_current(Z_AXIS);
  687. planner.synchronize();
  688. }
  689. // Park
  690. #if ENABLED(TOOLCHANGE_PARK)
  691. if (ok) {
  692. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  693. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  694. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), active_extruder);
  695. planner.synchronize();
  696. }
  697. #endif
  698. // Prime (All distances are added and slowed down to ensure secure priming in all circumstances)
  699. unscaled_e_move(toolchange_settings.swap_length + toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  700. // Cutting retraction
  701. #if TOOLCHANGE_FS_WIPE_RETRACT
  702. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  703. #endif
  704. // Cool down with fan
  705. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  706. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  707. gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time));
  708. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  709. #endif
  710. // Move back
  711. #if ENABLED(TOOLCHANGE_PARK)
  712. if (ok) {
  713. #if ENABLED(TOOLCHANGE_NO_RETURN)
  714. const float temp = destination.z;
  715. destination = current_position;
  716. destination.z = temp;
  717. #endif
  718. prepare_internal_move_to_destination(TERN(TOOLCHANGE_NO_RETURN, planner.settings.max_feedrate_mm_s[Z_AXIS], MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE)));
  719. }
  720. #endif
  721. // Cutting recover
  722. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  723. // Resume at the old E position
  724. current_position.e = destination.e;
  725. sync_plan_position_e();
  726. }
  727. }
  728. #endif // TOOLCHANGE_FILAMENT_SWAP
  729. /**
  730. * Perform a tool-change, which may result in moving the
  731. * previous tool out of the way and the new tool into place.
  732. */
  733. void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  734. if (TERN0(MAGNETIC_SWITCHING_TOOLHEAD, new_tool == active_extruder))
  735. return;
  736. #if ENABLED(MIXING_EXTRUDER)
  737. UNUSED(no_move);
  738. if (new_tool >= MIXING_VIRTUAL_TOOLS)
  739. return invalid_extruder_error(new_tool);
  740. #if MIXING_VIRTUAL_TOOLS > 1
  741. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  742. mixer.T(new_tool);
  743. #endif
  744. #elif HAS_PRUSA_MMU2
  745. UNUSED(no_move);
  746. mmu2.tool_change(new_tool);
  747. #elif EXTRUDERS == 0
  748. // Nothing to do
  749. UNUSED(new_tool); UNUSED(no_move);
  750. #elif EXTRUDERS < 2
  751. UNUSED(no_move);
  752. if (new_tool) invalid_extruder_error(new_tool);
  753. return;
  754. #elif HAS_MULTI_EXTRUDER
  755. planner.synchronize();
  756. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  757. if (new_tool != 0 && idex_is_duplicating())
  758. return invalid_extruder_error(new_tool);
  759. #endif
  760. if (new_tool >= EXTRUDERS)
  761. return invalid_extruder_error(new_tool);
  762. if (!no_move && homing_needed()) {
  763. no_move = true;
  764. DEBUG_ECHOLNPGM("No move (not homed)");
  765. }
  766. TERN_(HAS_MARLINUI_MENU, if (!no_move) ui.update());
  767. #if ENABLED(DUAL_X_CARRIAGE)
  768. const bool idex_full_control = dual_x_carriage_mode == DXC_FULL_CONTROL_MODE;
  769. #else
  770. constexpr bool idex_full_control = false;
  771. #endif
  772. const uint8_t old_tool = active_extruder;
  773. const bool can_move_away = !no_move && !idex_full_control;
  774. #if HAS_LEVELING
  775. // Set current position to the physical position
  776. TEMPORARY_BED_LEVELING_STATE(false);
  777. #endif
  778. // First tool priming. To prime again, reboot the machine.
  779. #if ENABLED(TOOLCHANGE_FS_PRIME_FIRST_USED)
  780. static bool first_tool_is_primed = false;
  781. if (new_tool == old_tool && !first_tool_is_primed && enable_first_prime) {
  782. tool_change_prime();
  783. first_tool_is_primed = true;
  784. TERN_(TOOLCHANGE_FS_INIT_BEFORE_SWAP, toolchange_extruder_ready[old_tool] = true); // Primed and initialized
  785. }
  786. #endif
  787. if (new_tool != old_tool || TERN0(PARKING_EXTRUDER, extruder_parked)) { // PARKING_EXTRUDER may need to attach old_tool when homing
  788. destination = current_position;
  789. #if BOTH(TOOLCHANGE_FILAMENT_SWAP, HAS_FAN) && TOOLCHANGE_FS_FAN >= 0
  790. // Store and stop fan. Restored on any exit.
  791. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  792. #endif
  793. // Z raise before retraction
  794. #if ENABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  795. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  796. // Do a small lift to avoid the workpiece in the move back (below)
  797. current_position.z += toolchange_settings.z_raise;
  798. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  799. fast_line_to_current(Z_AXIS);
  800. planner.synchronize();
  801. }
  802. #endif
  803. // Unload / Retract
  804. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  805. const bool should_swap = can_move_away && toolchange_settings.swap_length,
  806. too_cold = TERN0(PREVENT_COLD_EXTRUSION,
  807. !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(old_tool) || thermalManager.targetTooColdToExtrude(new_tool))
  808. );
  809. if (should_swap) {
  810. if (too_cold) {
  811. SERIAL_ECHO_MSG(STR_ERR_HOTEND_TOO_COLD);
  812. if (ENABLED(SINGLENOZZLE)) { active_extruder = new_tool; return; }
  813. }
  814. else {
  815. // For first new tool, change without unloading the old. 'Just prime/init the new'
  816. if (TERN1(TOOLCHANGE_FS_PRIME_FIRST_USED, first_tool_is_primed))
  817. unscaled_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  818. TERN_(TOOLCHANGE_FS_PRIME_FIRST_USED, first_tool_is_primed = true); // The first new tool will be primed by toolchanging
  819. }
  820. }
  821. #endif
  822. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, raise_nozzle(old_tool));
  823. REMEMBER(fr, feedrate_mm_s, XY_PROBE_FEEDRATE_MM_S);
  824. #if HAS_SOFTWARE_ENDSTOPS
  825. #if HAS_HOTEND_OFFSET
  826. #define _EXT_ARGS , old_tool, new_tool
  827. #else
  828. #define _EXT_ARGS
  829. #endif
  830. update_software_endstops(X_AXIS _EXT_ARGS);
  831. #if DISABLED(DUAL_X_CARRIAGE)
  832. update_software_endstops(Y_AXIS _EXT_ARGS);
  833. update_software_endstops(Z_AXIS _EXT_ARGS);
  834. #endif
  835. #endif
  836. #if DISABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  837. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  838. // Do a small lift to avoid the workpiece in the move back (below)
  839. current_position.z += toolchange_settings.z_raise;
  840. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  841. fast_line_to_current(Z_AXIS);
  842. }
  843. #endif
  844. // Toolchange park
  845. #if ENABLED(TOOLCHANGE_PARK) && DISABLED(SWITCHING_NOZZLE)
  846. if (can_move_away && toolchange_settings.enable_park) {
  847. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  848. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  849. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), old_tool);
  850. planner.synchronize();
  851. }
  852. #endif
  853. #if HAS_HOTEND_OFFSET
  854. xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
  855. TERN_(DUAL_X_CARRIAGE, diff.x = 0);
  856. #else
  857. constexpr xyz_pos_t diff{0};
  858. #endif
  859. #if ENABLED(DUAL_X_CARRIAGE)
  860. dualx_tool_change(new_tool, no_move);
  861. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  862. parking_extruder_tool_change(new_tool, no_move);
  863. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  864. magnetic_parking_extruder_tool_change(new_tool);
  865. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  866. switching_toolhead_tool_change(new_tool, no_move);
  867. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  868. magnetic_switching_toolhead_tool_change(new_tool, no_move);
  869. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching ToolChanger
  870. em_switching_toolhead_tool_change(new_tool, no_move);
  871. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS // Switching Nozzle (single servo)
  872. // Raise by a configured distance to avoid workpiece, except with
  873. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  874. if (!no_move) {
  875. const float newz = current_position.z + _MAX(-diff.z, 0.0);
  876. // Check if Z has space to compensate at least z_offset, and if not, just abort now
  877. const float maxz = _MIN(TERN(HAS_SOFTWARE_ENDSTOPS, soft_endstop.max.z, Z_MAX_POS), Z_MAX_POS);
  878. if (newz > maxz) return;
  879. current_position.z = _MIN(newz + toolchange_settings.z_raise, maxz);
  880. fast_line_to_current(Z_AXIS);
  881. }
  882. move_nozzle_servo(new_tool);
  883. #endif
  884. IF_DISABLED(DUAL_X_CARRIAGE, active_extruder = new_tool); // Set the new active extruder
  885. TERN_(TOOL_SENSOR, tool_sensor_disabled = false);
  886. (void)check_tool_sensor_stats(active_extruder, true);
  887. // The newly-selected extruder XYZ is actually at...
  888. DEBUG_ECHOLNPGM("Offset Tool XYZ by { ", diff.x, ", ", diff.y, ", ", diff.z, " }");
  889. current_position += diff;
  890. // Tell the planner the new "current position"
  891. sync_plan_position();
  892. #if ENABLED(DELTA)
  893. //LOOP_LINEAR_AXES(i) update_software_endstops(i); // or modify the constrain function
  894. const bool safe_to_move = current_position.z < delta_clip_start_height - 1;
  895. #else
  896. constexpr bool safe_to_move = true;
  897. #endif
  898. // Return to position and lower again
  899. const bool should_move = safe_to_move && !no_move && IsRunning();
  900. if (should_move) {
  901. #if EITHER(SINGLENOZZLE_STANDBY_TEMP, SINGLENOZZLE_STANDBY_FAN)
  902. thermalManager.singlenozzle_change(old_tool, new_tool);
  903. #endif
  904. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  905. if (should_swap && !too_cold) {
  906. float fr = toolchange_settings.unretract_speed;
  907. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  908. if (!toolchange_extruder_ready[new_tool]) {
  909. toolchange_extruder_ready[new_tool] = true;
  910. fr = toolchange_settings.prime_speed; // Next move is a prime
  911. unscaled_e_move(0, MMM_TO_MMS(fr)); // Init planner with 0 length move
  912. }
  913. #endif
  914. // Unretract (or Prime)
  915. unscaled_e_move(toolchange_settings.swap_length, MMM_TO_MMS(fr));
  916. // Extra Prime
  917. unscaled_e_move(toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  918. // Cutting retraction
  919. #if TOOLCHANGE_FS_WIPE_RETRACT
  920. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  921. #endif
  922. // Cool down with fan
  923. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  924. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  925. gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time));
  926. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  927. #endif
  928. }
  929. #endif
  930. // Prevent a move outside physical bounds
  931. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  932. // If the original position is within tool store area, go to X origin at once
  933. if (destination.y < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  934. current_position.x = X_MIN_POS;
  935. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], new_tool);
  936. planner.synchronize();
  937. }
  938. #else
  939. apply_motion_limits(destination);
  940. #endif
  941. // Should the nozzle move back to the old position?
  942. if (can_move_away) {
  943. #if ENABLED(TOOLCHANGE_NO_RETURN)
  944. // Just move back down
  945. DEBUG_ECHOLNPGM("Move back Z only");
  946. if (TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park))
  947. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  948. #else
  949. // Move back to the original (or adjusted) position
  950. DEBUG_POS("Move back", destination);
  951. #if ENABLED(TOOLCHANGE_PARK)
  952. if (toolchange_settings.enable_park) do_blocking_move_to_xy_z(destination, destination.z, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE));
  953. #else
  954. do_blocking_move_to_xy(destination, planner.settings.max_feedrate_mm_s[X_AXIS]);
  955. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  956. #endif
  957. #endif
  958. }
  959. else DEBUG_ECHOLNPGM("Move back skipped");
  960. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  961. if (should_swap && !too_cold) {
  962. // Cutting recover
  963. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  964. current_position.e = 0;
  965. sync_plan_position_e(); // New extruder primed and set to 0
  966. // Restart Fan
  967. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  968. RESTORE(fan);
  969. #endif
  970. }
  971. #endif
  972. TERN_(DUAL_X_CARRIAGE, idex_set_parked(false));
  973. }
  974. #if ENABLED(SWITCHING_NOZZLE)
  975. // Move back down. (Including when the new tool is higher.)
  976. if (!should_move)
  977. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  978. #endif
  979. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, lower_nozzle(new_tool));
  980. } // (new_tool != old_tool)
  981. planner.synchronize();
  982. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  983. disable_all_solenoids();
  984. enable_solenoid_on_active_extruder();
  985. #endif
  986. #if HAS_PRUSA_MMU1
  987. if (new_tool >= E_STEPPERS) return invalid_extruder_error(new_tool);
  988. select_multiplexed_stepper(new_tool);
  989. #endif
  990. #if DO_SWITCH_EXTRUDER
  991. planner.synchronize();
  992. move_extruder_servo(active_extruder);
  993. #endif
  994. TERN_(HAS_FANMUX, fanmux_switch(active_extruder));
  995. if (ENABLED(EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN) || !no_move) {
  996. #ifdef EVENT_GCODE_TOOLCHANGE_T0
  997. if (new_tool == 0)
  998. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0));
  999. #endif
  1000. #ifdef EVENT_GCODE_TOOLCHANGE_T1
  1001. if (new_tool == 1)
  1002. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1));
  1003. #endif
  1004. #ifdef EVENT_GCODE_AFTER_TOOLCHANGE
  1005. if (TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE))
  1006. gcode.process_subcommands_now(F(EVENT_GCODE_AFTER_TOOLCHANGE));
  1007. #endif
  1008. }
  1009. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, active_extruder);
  1010. #endif // HAS_MULTI_EXTRUDER
  1011. }
  1012. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  1013. #define DEBUG_OUT ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  1014. #include "../core/debug_out.h"
  1015. bool extruder_migration() {
  1016. #if ENABLED(PREVENT_COLD_EXTRUSION)
  1017. if (thermalManager.targetTooColdToExtrude(active_extruder)) {
  1018. DEBUG_ECHOLNPGM("Migration Source Too Cold");
  1019. return false;
  1020. }
  1021. #endif
  1022. // No auto-migration or specified target?
  1023. if (!migration.target && active_extruder >= migration.last) {
  1024. DEBUG_ECHO_MSG("No Migration Target");
  1025. DEBUG_ECHO_MSG("Target: ", migration.target, " Last: ", migration.last, " Active: ", active_extruder);
  1026. migration.automode = false;
  1027. return false;
  1028. }
  1029. // Migrate to a target or the next extruder
  1030. uint8_t migration_extruder = active_extruder;
  1031. if (migration.target) {
  1032. DEBUG_ECHOLNPGM("Migration using fixed target");
  1033. // Specified target ok?
  1034. const int16_t t = migration.target - 1;
  1035. if (t != active_extruder) migration_extruder = t;
  1036. }
  1037. else if (migration.automode && migration_extruder < migration.last && migration_extruder < EXTRUDERS - 1)
  1038. migration_extruder++;
  1039. if (migration_extruder == active_extruder) {
  1040. DEBUG_ECHOLNPGM("Migration source matches active");
  1041. return false;
  1042. }
  1043. // Migration begins
  1044. DEBUG_ECHOLNPGM("Beginning migration");
  1045. migration.in_progress = true; // Prevent runout script
  1046. planner.synchronize();
  1047. // Remember position before migration
  1048. const float resume_current_e = current_position.e;
  1049. // Migrate the flow
  1050. planner.set_flow(migration_extruder, planner.flow_percentage[active_extruder]);
  1051. // Migrate the retracted state
  1052. #if ENABLED(FWRETRACT)
  1053. fwretract.retracted[migration_extruder] = fwretract.retracted[active_extruder];
  1054. #endif
  1055. // Migrate the temperature to the new hotend
  1056. #if HAS_MULTI_HOTEND
  1057. thermalManager.setTargetHotend(thermalManager.degTargetHotend(active_extruder), migration_extruder);
  1058. TERN_(AUTOTEMP, planner.autotemp_update());
  1059. thermalManager.set_heating_message(0);
  1060. thermalManager.wait_for_hotend(active_extruder);
  1061. #endif
  1062. // Migrate Linear Advance K factor to the new extruder
  1063. TERN_(LIN_ADVANCE, planner.extruder_advance_K[active_extruder] = planner.extruder_advance_K[migration_extruder]);
  1064. // Perform the tool change
  1065. tool_change(migration_extruder);
  1066. // Retract if previously retracted
  1067. #if ENABLED(FWRETRACT)
  1068. if (fwretract.retracted[active_extruder])
  1069. unscaled_e_move(-fwretract.settings.retract_length, fwretract.settings.retract_feedrate_mm_s);
  1070. #endif
  1071. // If no available extruder
  1072. if (EXTRUDERS < 2 || active_extruder >= EXTRUDERS - 2 || active_extruder == migration.last)
  1073. migration.automode = false;
  1074. migration.in_progress = false;
  1075. current_position.e = resume_current_e;
  1076. planner.synchronize();
  1077. planner.set_e_position_mm(current_position.e); // New extruder primed and ready
  1078. DEBUG_ECHOLNPGM("Migration Complete");
  1079. return true;
  1080. }
  1081. #endif // TOOLCHANGE_MIGRATION_FEATURE