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 44KB

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