My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tool_change.cpp 44KB

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