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

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