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

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